use of com.vividsolutions.jts.geom.Point in project openremote by openremote.
the class AbstractProtocol method updateLinkedAttribute.
/**
* Update the value of a linked attribute. Call this to publish new sensor values. This will apply any
* {@link MessageFilter}s that have been set for the {@link Attribute} against the {@link AttributeState#value}
* before sending on the sensor queue.
*/
@SuppressWarnings("unchecked")
protected final void updateLinkedAttribute(final AttributeState finalState, long timestamp) {
withLock(getProtocolName() + "::updateLinkedAttribute", () -> {
AttributeState state = finalState;
AssetAttribute attribute = linkedAttributes.get(state.getAttributeRef());
if (attribute == null) {
LOG.severe("Update linked attribute called for un-linked attribute: " + state);
return;
}
if (state.getValue().isPresent()) {
List<MessageFilter> filters;
Value value = state.getValue().get();
filters = linkedAttributeFilters.get(state.getAttributeRef());
if (filters != null) {
LOG.fine("Applying message filters to sensor value...");
for (MessageFilter filter : filters) {
if (filter.getMessageType() != value.getType().getModelType()) {
LOG.fine("Message filter type '" + filter.getMessageType().getName() + "' is not compatible with actual message type '" + value.getType().getModelType().getName() + "': " + filter.getClass().getName());
value = null;
} else {
try {
LOG.finest("Applying message filter: " + filter.getClass().getName());
value = filter.process(value);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Message filter threw and exception during processing of message: " + filter.getClass().getName(), e);
value = null;
}
}
if (value == null) {
break;
}
}
}
// Do basic value conversion
Optional<ValueType> attributeValueType = attribute.getType().map(AttributeType::getValueType);
if (value != null && attributeValueType.isPresent()) {
if (attributeValueType.get() != value.getType()) {
LOG.fine("Converting value: " + value.getType() + " -> " + attributeValueType.get());
Optional<Value> convertedValue = Values.convert(value, attributeValueType.get());
if (!convertedValue.isPresent()) {
LOG.warning("Failed to convert value: " + value.getType() + " -> " + attributeValueType.get());
} else {
value = convertedValue.get();
}
}
}
state = new AttributeState(state.getAttributeRef(), value);
}
AttributeEvent attributeEvent = new AttributeEvent(state, timestamp);
LOG.fine("Sending on sensor queue: " + attributeEvent);
producerTemplate.sendBodyAndHeader(SENSOR_QUEUE, attributeEvent, Protocol.SENSOR_QUEUE_SOURCE_PROTOCOL, getProtocolName());
if (locationLinkedAttributes.contains(state.getAttributeRef())) {
// Check value type is compatible
Point location = state.getValue().map(value -> {
if (value.getType() != ValueType.ARRAY) {
LOG.warning("Location linked attribute type is not an array");
return null;
}
Optional<List<NumberValue>> coordinates = Values.getArrayElements((ArrayValue) value, NumberValue.class, false, false);
if (!coordinates.isPresent() || coordinates.get().size() != 2 || Math.abs(coordinates.get().get(0).getNumber()) > 180 || Math.abs(coordinates.get().get(1).getNumber()) > 90) {
LOG.warning("Location linked attribute value must contain longitude then latitude in a 2 value number array");
return null;
}
try {
return new GeometryFactory().createPoint(new Coordinate(coordinates.get().get(0).getNumber(), coordinates.get().get(1).getNumber()));
} catch (Exception e) {
return null;
}
}).orElse(null);
updateAssetLocation(state.getAttributeRef().getEntityId(), location);
}
});
}
use of com.vividsolutions.jts.geom.Point in project openremote by openremote.
the class AssetStorageService method publishModificationEvents.
protected void publishModificationEvents(PersistenceEvent<ServerAsset> persistenceEvent) {
ServerAsset asset = persistenceEvent.getEntity();
switch(persistenceEvent.getCause()) {
case INSERT:
clientEventService.publishEvent(new AssetTreeModifiedEvent(timerService.getCurrentTimeMillis(), asset.getRealmId(), asset.getId()));
if (asset.getParentId() != null) {
// Child asset created
clientEventService.publishEvent(new AssetTreeModifiedEvent(timerService.getCurrentTimeMillis(), asset.getRealmId(), asset.getParentId(), true));
} else {
// Child asset created (root asset)
clientEventService.publishEvent(new AssetTreeModifiedEvent(timerService.getCurrentTimeMillis(), asset.getRealmId(), true));
}
break;
case UPDATE:
// Did the name change?
String previousName = persistenceEvent.getPreviousState("name");
String currentName = persistenceEvent.getCurrentState("name");
if (!Objects.equals(previousName, currentName)) {
clientEventService.publishEvent(new AssetTreeModifiedEvent(timerService.getCurrentTimeMillis(), asset.getRealmId(), asset.getId()));
break;
}
// Did the parent change?
String previousParentId = persistenceEvent.getPreviousState("parentId");
String currentParentId = persistenceEvent.getCurrentState("parentId");
if (!Objects.equals(previousParentId, currentParentId)) {
clientEventService.publishEvent(new AssetTreeModifiedEvent(timerService.getCurrentTimeMillis(), asset.getRealmId(), asset.getId()));
break;
}
// Did the realm change?
String previousRealmId = persistenceEvent.getPreviousState("realmId");
String currentRealmId = persistenceEvent.getCurrentState("realmId");
if (!Objects.equals(previousRealmId, currentRealmId)) {
clientEventService.publishEvent(new AssetTreeModifiedEvent(timerService.getCurrentTimeMillis(), asset.getRealmId(), asset.getId()));
break;
}
// Did the location change?
Point oldLocation = persistenceEvent.getPreviousState("location");
Point newLocation = persistenceEvent.getCurrentState("location");
if (!(oldLocation == null && newLocation == null)) {
if (oldLocation == null || newLocation == null || !oldLocation.equalsExact(newLocation, 0)) {
clientEventService.publishEvent(new LocationEvent(asset.getId(), asset.getCoordinates(), timerService.getCurrentTimeMillis()));
}
}
break;
case DELETE:
clientEventService.publishEvent(new AssetTreeModifiedEvent(timerService.getCurrentTimeMillis(), asset.getRealmId(), asset.getId()));
break;
}
}
use of com.vividsolutions.jts.geom.Point in project elasticsearch by elastic.
the class GeoJSONShapeParserTests method testParseMultiDimensionShapes.
public void testParseMultiDimensionShapes() throws IOException {
// multi dimension point
XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).value(15.0).value(18.0).endArray().endObject();
Point expectedPt = GEOMETRY_FACTORY.createPoint(new Coordinate(100.0, 0.0));
assertGeometryEquals(new JtsPoint(expectedPt, SPATIAL_CONTEXT), pointGeoJson);
// multi dimension linestring
XContentBuilder lineGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "LineString").startArray("coordinates").startArray().value(100.0).value(0.0).value(15.0).endArray().startArray().value(101.0).value(1.0).value(18.0).value(19.0).endArray().endArray().endObject();
List<Coordinate> lineCoordinates = new ArrayList<>();
lineCoordinates.add(new Coordinate(100, 0));
lineCoordinates.add(new Coordinate(101, 1));
LineString expectedLS = GEOMETRY_FACTORY.createLineString(lineCoordinates.toArray(new Coordinate[lineCoordinates.size()]));
assertGeometryEquals(jtsGeom(expectedLS), lineGeoJson);
}
use of com.vividsolutions.jts.geom.Point in project elasticsearch by elastic.
the class GeoJSONShapeParserTests method testParseGeometryCollection.
public void testParseGeometryCollection() throws IOException {
XContentBuilder geometryCollectionGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "GeometryCollection").startArray("geometries").startObject().field("type", "LineString").startArray("coordinates").startArray().value(100.0).value(0.0).endArray().startArray().value(101.0).value(1.0).endArray().endArray().endObject().startObject().field("type", "Point").startArray("coordinates").value(102.0).value(2.0).endArray().endObject().endArray().endObject();
Shape[] expected = new Shape[2];
LineString expectedLineString = GEOMETRY_FACTORY.createLineString(new Coordinate[] { new Coordinate(100, 0), new Coordinate(101, 1) });
expected[0] = jtsGeom(expectedLineString);
Point expectedPoint = GEOMETRY_FACTORY.createPoint(new Coordinate(102.0, 2.0));
expected[1] = new JtsPoint(expectedPoint, SPATIAL_CONTEXT);
//equals returns true only if geometries are in the same order
assertGeometryEquals(shapeCollection(expected), geometryCollectionGeoJson);
}
use of com.vividsolutions.jts.geom.Point in project elasticsearch by elastic.
the class GeoJSONShapeParserTests method testParseSimplePoint.
public void testParseSimplePoint() throws IOException {
XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "Point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject();
Point expected = GEOMETRY_FACTORY.createPoint(new Coordinate(100.0, 0.0));
assertGeometryEquals(new JtsPoint(expected, SPATIAL_CONTEXT), pointGeoJson);
}
Aggregations