use of org.geojson.Feature in project dhis2-core by dhis2.
the class GeoFeatureService method getCoordinates.
/**
* Get the {@link GeoFeature} coordinate from {@link DimensionalItemObject}
* <p>
* The coordinate value is retrieved from {@link DimensionalItemObject}'s
* geoJsonAttribute value.
*
* @param feature the {@link GeoFeature}
* @param unit the {@link DimensionalItemObject} contains the coordinate
* values.
* @param geoJsonAttribute The {@link Attribute} which has
* {@link ValueType#GEOJSON} and is assigned to
* {@link OrganisationUnit}.
* @return the given {@link GeoFeature} with updated coordinate value and
* coordinate type.
*/
private void getCoordinates(GeoFeature feature, DimensionalItemObject unit, Attribute geoJsonAttribute) {
if (geoJsonAttribute == null) {
getCoordinates(feature, unit);
return;
}
if (!unit.getClass().isAssignableFrom(OrganisationUnit.class)) {
return;
}
OrganisationUnit organisationUnit = (OrganisationUnit) unit;
Optional<AttributeValue> geoJsonAttributeValue = organisationUnit.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute().getUid().equals(geoJsonAttribute.getUid())).findFirst();
if (!geoJsonAttributeValue.isPresent() || StringUtils.isBlank(geoJsonAttributeValue.get().getValue())) {
getCoordinates(feature, unit);
return;
}
try {
GeoJsonObject geoJsonObject = new ObjectMapper().readValue(geoJsonAttributeValue.get().getValue(), GeoJsonObject.class);
GeoFeature geoJsonFeature = geoJsonObject.accept(new GeoFeatureVisitor());
if (geoJsonFeature == null) {
return;
}
feature.setTy(geoJsonFeature.getTy());
feature.setCo(geoJsonFeature.getCo());
} catch (JsonProcessingException e) {
log.error(String.format("Couldn't read GeoJson value from organisationUnit %s: ", organisationUnit), e);
getCoordinates(feature, unit);
}
}
Aggregations