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;
}
}
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;
}
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;
}
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;
}
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();
}
Aggregations