Search in sources :

Example 21 with Point

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);
        }
    });
}
Also used : java.util(java.util) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) ValidationFailure(org.openremote.model.ValidationFailure) MessageFilter(org.openremote.agent.protocol.filter.MessageFilter) Point(com.vividsolutions.jts.geom.Point) Level(java.util.logging.Level) AgentLink(org.openremote.model.asset.agent.AgentLink) GlobalLock.withLockReturning(org.openremote.container.concurrent.GlobalLock.withLockReturning) Container(org.openremote.container.Container) ProducerTemplate(org.apache.camel.ProducerTemplate) Coordinate(com.vividsolutions.jts.geom.Coordinate) MessageBrokerService(org.openremote.container.message.MessageBrokerService) Logger(java.util.logging.Logger) MessageBrokerSetupService(org.openremote.container.message.MessageBrokerSetupService) AssetMeta(org.openremote.model.asset.AssetMeta) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) org.openremote.model.value(org.openremote.model.value) RouteBuilder(org.apache.camel.builder.RouteBuilder) TimerService(org.openremote.container.timer.TimerService) MessageBrokerContext(org.openremote.container.message.MessageBrokerContext) org.openremote.model.attribute(org.openremote.model.attribute) ProtocolDescriptor(org.openremote.model.asset.agent.ProtocolDescriptor) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) ValueHolder(org.openremote.model.ValueHolder) AssetAttribute(org.openremote.model.asset.AssetAttribute) GlobalLock(org.openremote.container.concurrent.GlobalLock) GlobalLock.withLock(org.openremote.container.concurrent.GlobalLock.withLock) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Point(com.vividsolutions.jts.geom.Point) Coordinate(com.vividsolutions.jts.geom.Coordinate) AssetAttribute(org.openremote.model.asset.AssetAttribute) MessageFilter(org.openremote.agent.protocol.filter.MessageFilter)

Example 22 with Point

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;
    }
}
Also used : Point(com.vividsolutions.jts.geom.Point)

Example 23 with Point

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);
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) ArrayList(java.util.ArrayList) Point(com.vividsolutions.jts.geom.Point) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 24 with Point

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);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Coordinate(com.vividsolutions.jts.geom.Coordinate) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(com.vividsolutions.jts.geom.Point) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 25 with Point

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);
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(com.vividsolutions.jts.geom.Point) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

Point (com.vividsolutions.jts.geom.Point)48 Coordinate (com.vividsolutions.jts.geom.Coordinate)21 Geometry (com.vividsolutions.jts.geom.Geometry)15 Test (org.junit.Test)12 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)11 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)10 Polygon (com.vividsolutions.jts.geom.Polygon)9 LineString (com.vividsolutions.jts.geom.LineString)7 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)5 Envelope (com.vividsolutions.jts.geom.Envelope)4 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)4 ParseException (com.vividsolutions.jts.io.ParseException)4 ArrayList (java.util.ArrayList)4 GeojsonPoint (org.n52.io.geojson.old.GeojsonPoint)4 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)3 StringReader (java.io.StringReader)3 SolrQuery (org.apache.solr.client.solrj.SolrQuery)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)3 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)3