Search in sources :

Example 1 with GeoJsonReader

use of org.locationtech.jts.io.geojson.GeoJsonReader in project sensorweb-server-sta by 52North.

the class JSONLocation method toEntity.

@Override
public LocationEntity toEntity(JSONBase.EntityType type) {
    GeoJsonReader reader;
    switch(type) {
        case FULL:
            parseReferencedFrom();
            Assert.notNull(name, INVALID_INLINE_ENTITY_MISSING + "name");
            Assert.notNull(description, INVALID_INLINE_ENTITY_MISSING + "description");
            Assert.notNull(encodingType, INVALID_INLINE_ENTITY_MISSING + "encodingType");
            Assert.notNull(encodingType, INVALID_ENCODINGTYPE);
            Assert.isTrue(Objects.equals(encodingType, ENCODINGTYPE_GEOJSON), INVALID_ENCODINGTYPE);
            Assert.notNull(location, INVALID_INLINE_ENTITY_MISSING + "location");
            // TODO: check what is actually allowed here.
            if (location.has(GEOMETRY)) {
                Assert.isTrue("Feature".equals(location.get(TYPE).asText()), INVALID_INLINE_ENTITY_MISSING + LOCATION_TYPE);
                Assert.notNull(location.get(GEOMETRY), INVALID_INLINE_ENTITY_MISSING + LOCATION_GEOM);
            } else {
                Assert.isTrue("Point".equals(location.get(TYPE).asText()), INVALID_INLINE_ENTITY_MISSING + LOCATION_TYPE);
                Assert.notNull(location.get(COORDINATES), INVALID_INLINE_ENTITY_MISSING + LOCATION_GEOM);
            }
            self.setIdentifier(identifier);
            self.setStaIdentifier(identifier);
            self.setName(name);
            self.setDescription(description);
            self.setLocationEncoding(new FormatEntity().setFormat(encodingType));
            reader = new GeoJsonReader(factory);
            try {
                if (location.has(GEOMETRY)) {
                    self.setGeometry(reader.read(location.get(GEOMETRY).toString()));
                } else {
                    self.setGeometry(reader.read(location.toString()));
                }
            } catch (ParseException e) {
                Assert.notNull(null, COULD_NOT_PARSE + e.getMessage());
            }
            if (Things != null) {
                self.setThings(Arrays.stream(Things).map(thing -> thing.toEntity(JSONBase.EntityType.FULL, JSONBase.EntityType.REFERENCE)).collect(Collectors.toSet()));
            }
            if (HistoricalLocations != null) {
                self.setHistoricalLocations(Arrays.stream(HistoricalLocations).map(loc -> loc.toEntity(JSONBase.EntityType.FULL, JSONBase.EntityType.REFERENCE)).collect(Collectors.toSet()));
            }
            if (backReference != null) {
                if (backReference instanceof JSONThing) {
                    if (self.getThings() != null) {
                        self.getThings().add(((JSONThing) backReference).getEntity());
                    } else {
                        self.setThings(Collections.singleton(((JSONThing) backReference).getEntity()));
                    }
                } else {
                    JSONHistoricalLocation backRef = (JSONHistoricalLocation) backReference;
                    self.addHistoricalLocation(backRef.getEntity());
                }
            }
            return self;
        case PATCH:
            parseReferencedFrom();
            self.setIdentifier(identifier);
            self.setStaIdentifier(identifier);
            self.setName(name);
            self.setDescription(description);
            self.setLocationEncoding(new FormatEntity().setFormat(encodingType));
            if (encodingType != null) {
                Assert.state(encodingType.equals(ENCODINGTYPE_GEOJSON), INVALID_ENCODINGTYPE);
            }
            if (location != null) {
                reader = new GeoJsonReader(factory);
                try {
                    if (location.has(GEOMETRY)) {
                        self.setGeometry(reader.read(location.get(GEOMETRY).toString()));
                    } else {
                        self.setGeometry(reader.read(location.toString()));
                    }
                } catch (ParseException e) {
                    Assert.notNull(null, COULD_NOT_PARSE + e.getMessage());
                }
            }
            if (Things != null) {
                self.setThings(Arrays.stream(Things).map(thing -> thing.toEntity(JSONBase.EntityType.REFERENCE)).collect(Collectors.toSet()));
            }
            if (HistoricalLocations != null) {
                self.setHistoricalLocations(Arrays.stream(HistoricalLocations).map(loc -> loc.toEntity(JSONBase.EntityType.REFERENCE)).collect(Collectors.toSet()));
            }
            return self;
        case REFERENCE:
            Assert.isNull(name, INVALID_REFERENCED_ENTITY);
            Assert.isNull(description, INVALID_REFERENCED_ENTITY);
            Assert.isNull(encodingType, INVALID_REFERENCED_ENTITY);
            Assert.isNull(location, INVALID_REFERENCED_ENTITY);
            Assert.isNull(Things, INVALID_REFERENCED_ENTITY);
            self.setIdentifier(identifier);
            self.setStaIdentifier(identifier);
            return self;
        default:
            return null;
    }
}
Also used : GeoJsonReader(org.locationtech.jts.io.geojson.GeoJsonReader) FormatEntity(org.n52.series.db.beans.FormatEntity) ParseException(org.locationtech.jts.io.ParseException)

Example 2 with GeoJsonReader

use of org.locationtech.jts.io.geojson.GeoJsonReader in project sensorweb-server-sta by 52North.

the class JSONDatastream method createPostEntity.

private DatastreamEntity createPostEntity() {
    self.setIdentifier(identifier);
    self.setStaIdentifier(identifier);
    self.setName(name);
    self.setDescription(description);
    self.setObservationType(new FormatEntity().setFormat(observationType));
    if (observedArea != null) {
        GeoJsonReader reader = new GeoJsonReader(factory);
        try {
            self.setGeometry(reader.read(observedArea.toString()));
        } catch (ParseException e) {
            Assert.notNull(null, COULD_NOT_PARSE_OBS_AREA + e.getMessage());
        }
    }
    if (unitOfMeasurement.name != null) {
        UnitEntity unit = new UnitEntity();
        unit.setLink(unitOfMeasurement.definition);
        unit.setName(unitOfMeasurement.name);
        unit.setSymbol(unitOfMeasurement.symbol);
        self.setUnit(unit);
    }
    if (resultTime != null) {
        Time time = parseTime(resultTime);
        if (time instanceof TimeInstant) {
            self.setResultTimeStart(((TimeInstant) time).getValue().toDate());
            self.setResultTimeEnd(((TimeInstant) time).getValue().toDate());
        } else if (time instanceof TimePeriod) {
            self.setResultTimeStart(((TimePeriod) time).getStart().toDate());
            self.setResultTimeEnd(((TimePeriod) time).getEnd().toDate());
        }
    }
    if (Thing != null) {
        self.setThing(Thing.toEntity(JSONBase.EntityType.FULL, JSONBase.EntityType.REFERENCE));
    } else if (backReference instanceof JSONThing) {
        self.setThing(((JSONThing) backReference).getEntity());
    } else {
        Assert.notNull(null, INVALID_INLINE_ENTITY_MISSING + "Thing");
    }
    if (Sensor != null) {
        self.setProcedure(Sensor.toEntity(JSONBase.EntityType.FULL, JSONBase.EntityType.REFERENCE));
    } else if (backReference instanceof JSONSensor) {
        self.setProcedure(((JSONSensor) backReference).getEntity());
    } else {
        Assert.notNull(null, INVALID_INLINE_ENTITY_MISSING + "Sensor");
    }
    if (ObservedProperty != null) {
        self.setObservableProperty(ObservedProperty.toEntity(JSONBase.EntityType.FULL, JSONBase.EntityType.REFERENCE));
    } else if (backReference instanceof JSONObservedProperty) {
        self.setObservableProperty(((JSONObservedProperty) backReference).getEntity());
    } else {
        Assert.notNull(null, INVALID_INLINE_ENTITY_MISSING + "ObservedProperty");
    }
    if (Observations != null) {
        self.setObservations(Arrays.stream(Observations).map(entity -> entity.toEntity(JSONBase.EntityType.FULL, JSONBase.EntityType.REFERENCE)).collect(Collectors.toSet()));
    } else if (backReference instanceof JSONObservation) {
        self.setObservations(Collections.singleton(((JSONObservation) backReference).self));
    }
    return self;
}
Also used : GeoJsonReader(org.locationtech.jts.io.geojson.GeoJsonReader) FormatEntity(org.n52.series.db.beans.FormatEntity) TimePeriod(org.n52.shetland.ogc.gml.time.TimePeriod) Time(org.n52.shetland.ogc.gml.time.Time) ParseException(org.locationtech.jts.io.ParseException) UnitEntity(org.n52.series.db.beans.UnitEntity) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant)

Example 3 with GeoJsonReader

use of org.locationtech.jts.io.geojson.GeoJsonReader in project sensorweb-server-sta by 52North.

the class JSONDatastream method createPatchEntity.

private DatastreamEntity createPatchEntity() {
    self.setIdentifier(identifier);
    self.setStaIdentifier(identifier);
    self.setName(name);
    self.setDescription(description);
    if (observationType != null) {
        self.setObservationType(new FormatEntity().setFormat(observationType));
    }
    if (observedArea != null) {
        GeoJsonReader reader = new GeoJsonReader(factory);
        try {
            self.setGeometry(reader.read(observedArea.toString()));
        } catch (ParseException e) {
            Assert.notNull(null, COULD_NOT_PARSE_OBS_AREA + e.getMessage());
        }
    }
    if (unitOfMeasurement != null) {
        if (unitOfMeasurement.name == null) {
            self.setUnit(null);
        } else {
            UnitEntity unit = new UnitEntity();
            unit.setLink(unitOfMeasurement.definition);
            unit.setName(unitOfMeasurement.name);
            unit.setSymbol(unitOfMeasurement.symbol);
            self.setUnit(unit);
        }
    }
    if (resultTime != null) {
        Time time = parseTime(resultTime);
        if (time instanceof TimeInstant) {
            self.setResultTimeStart(((TimeInstant) time).getValue().toDate());
            self.setResultTimeEnd(((TimeInstant) time).getValue().toDate());
        } else if (time instanceof TimePeriod) {
            self.setResultTimeStart(((TimePeriod) time).getStart().toDate());
            self.setResultTimeEnd(((TimePeriod) time).getEnd().toDate());
        }
    }
    // }
    if (Thing != null) {
        self.setThing(Thing.toEntity(JSONBase.EntityType.REFERENCE));
    }
    if (Sensor != null) {
        self.setProcedure(Sensor.toEntity(JSONBase.EntityType.REFERENCE));
    }
    if (ObservedProperty != null) {
        self.setObservableProperty(ObservedProperty.toEntity(JSONBase.EntityType.REFERENCE));
    }
    if (Observations != null) {
        self.setObservations(Arrays.stream(Observations).map(obs -> obs.toEntity(JSONBase.EntityType.REFERENCE)).collect(Collectors.toSet()));
    }
    return self;
}
Also used : GeoJsonReader(org.locationtech.jts.io.geojson.GeoJsonReader) FormatEntity(org.n52.series.db.beans.FormatEntity) TimePeriod(org.n52.shetland.ogc.gml.time.TimePeriod) Time(org.n52.shetland.ogc.gml.time.Time) ParseException(org.locationtech.jts.io.ParseException) UnitEntity(org.n52.series.db.beans.UnitEntity) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant)

Example 4 with GeoJsonReader

use of org.locationtech.jts.io.geojson.GeoJsonReader in project ets-ogcapi-features10 by opengeospatial.

the class JsonUtils method parseFeatureGeometry.

/**
 * Parse the geometry property as geometry.
 *
 * @param feature
 *            to parse, never <code>null</code>
 * @param crs
 *            the crs of the geometry, may be <code>null</code>
 * @return the parsed geometry, <code>null</code> if the feature has no geometry property
 * @throws ParseException
 *             if the geometry could not be parsed
 */
public static Geometry parseFeatureGeometry(Map<String, Object> feature, CoordinateSystem crs) throws ParseException {
    Map<String, Object> geometry = (Map<String, Object>) feature.get("geometry");
    if (geometry == null)
        return null;
    JSONObject jsonObject = new JSONObject(geometry);
    String geomAsString = jsonObject.toJSONString();
    GeoJsonReader geoJsonReader = new GeoJsonReader();
    Geometry parsedGeometry = geoJsonReader.read(geomAsString);
    parsedGeometry.setSRID(crs.getSrid());
    return parsedGeometry;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) JSONObject(org.json.simple.JSONObject) GeoJsonReader(org.locationtech.jts.io.geojson.GeoJsonReader) JSONObject(org.json.simple.JSONObject) Map(java.util.Map)

Example 5 with GeoJsonReader

use of org.locationtech.jts.io.geojson.GeoJsonReader in project jts by locationtech.

the class GeoJsonTest method setUp.

@Override
public void setUp() throws Exception {
    this.geoJsonWriter = new GeoJsonWriter();
    this.geoJsonReader = new GeoJsonReader();
}
Also used : GeoJsonWriter(org.locationtech.jts.io.geojson.GeoJsonWriter) GeoJsonReader(org.locationtech.jts.io.geojson.GeoJsonReader)

Aggregations

GeoJsonReader (org.locationtech.jts.io.geojson.GeoJsonReader)11 ParseException (org.locationtech.jts.io.ParseException)8 Geometry (org.locationtech.jts.geom.Geometry)5 FormatEntity (org.n52.series.db.beans.FormatEntity)3 HashSet (java.util.HashSet)2 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)2 UnitEntity (org.n52.series.db.beans.UnitEntity)2 Time (org.n52.shetland.ogc.gml.time.Time)2 TimeInstant (org.n52.shetland.ogc.gml.time.TimeInstant)2 TimePeriod (org.n52.shetland.ogc.gml.time.TimePeriod)2 FDBException (com.apple.foundationdb.FDBException)1 Bindings (com.apple.foundationdb.record.Bindings)1 EvaluationContext (com.apple.foundationdb.record.EvaluationContext)1 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)1 RecordCursor (com.apple.foundationdb.record.RecordCursor)1 Index (com.apple.foundationdb.record.metadata.Index)1 Expressions.concat (com.apple.foundationdb.record.metadata.Key.Expressions.concat)1 Expressions.concatenateFields (com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields)1 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)1 Expressions.function (com.apple.foundationdb.record.metadata.Key.Expressions.function)1