use of org.openstreetmap.atlas.geography.geojson.GeoJsonObject in project atlas-checks by osmlab.
the class CheckFlagEvent method flagToFeature.
/**
* Converts give {@link CheckFlag} to {@link GeoJsonObject} with additional key-value parameters
*
* @param flag
* {@link CheckFlag} to convert to {@link GeoJsonObject}
* @param additionalProperties
* additional key-value parameters to be added in "properties" element of the
* top-level JSON object
* @return {@link GeoJsonObject} created from {@link CheckFlag}
*/
public static JsonObject flagToFeature(final CheckFlag flag, final Map<String, String> additionalProperties) {
final JsonObject flagProperties = new JsonObject();
flagProperties.addProperty("instructions", flag.getInstructions());
// Add additional properties
additionalProperties.forEach(flagProperties::addProperty);
final JsonObject feature;
final List<LocationIterableProperties> geometries = flag.getLocationIterableProperties();
if (geometries.size() == 1) {
feature = GEOJSON_BUILDER.create(geometries.get(0));
} else {
feature = GEOJSON_BUILDER.createGeometryCollection(geometries).jsonObject();
}
final JsonArray featureProperties = new JsonArray();
final Set<JsonElement> featureOsmIds = new HashSet<>();
geometries.stream().forEach(geometry -> Optional.ofNullable(geometry.getProperties()).ifPresent(propertyMap -> {
final JsonObject properties = new JsonObject();
propertyMap.forEach(properties::addProperty);
featureProperties.add(properties);
Optional.ofNullable(properties.get("osmid")).ifPresent(featureOsmIds::add);
}));
final JsonArray uniqueFeatureOsmIds = new JsonArray();
featureOsmIds.forEach(uniqueFeatureOsmIds::add);
// Override name property if able to add a decorator to the name
CheckFlagEvent.featureDecorator(featureProperties).ifPresent(decorator -> flagProperties.addProperty("name", String.format("%s (%s)", Optional.ofNullable(flagProperties.getAsJsonPrimitive("name")).map(JsonPrimitive::getAsString).orElse("Task"), decorator)));
// Reference properties lost during GeoJson conversion
flagProperties.add("feature_properties", featureProperties);
flagProperties.add("feature_osmids", uniqueFeatureOsmIds);
flagProperties.addProperty("feature_count", geometries.size());
feature.addProperty("id", flag.getIdentifier());
feature.add("properties", flagProperties);
return feature;
}
Aggregations