Search in sources :

Example 1 with GeoJsonObject

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;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Check(org.openstreetmap.atlas.checks.base.Check) GeoJsonObject(org.openstreetmap.atlas.geography.geojson.GeoJsonObject) GeoJsonBuilder(org.openstreetmap.atlas.geography.geojson.GeoJsonBuilder) LocationIterableProperties(org.openstreetmap.atlas.geography.geojson.GeoJsonBuilder.LocationIterableProperties) Set(java.util.Set) HashMap(java.util.HashMap) HashSet(java.util.HashSet) JsonElement(com.google.gson.JsonElement) HighwayTag(org.openstreetmap.atlas.tags.HighwayTag) List(java.util.List) JsonArray(com.google.gson.JsonArray) Map(java.util.Map) Optional(java.util.Optional) JsonPrimitive(com.google.gson.JsonPrimitive) CheckFlag(org.openstreetmap.atlas.checks.flag.CheckFlag) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) GeoJsonObject(org.openstreetmap.atlas.geography.geojson.GeoJsonObject) LocationIterableProperties(org.openstreetmap.atlas.geography.geojson.GeoJsonBuilder.LocationIterableProperties) HashSet(java.util.HashSet)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Check (org.openstreetmap.atlas.checks.base.Check)1 CheckFlag (org.openstreetmap.atlas.checks.flag.CheckFlag)1 GeoJsonBuilder (org.openstreetmap.atlas.geography.geojson.GeoJsonBuilder)1 LocationIterableProperties (org.openstreetmap.atlas.geography.geojson.GeoJsonBuilder.LocationIterableProperties)1 GeoJsonObject (org.openstreetmap.atlas.geography.geojson.GeoJsonObject)1 HighwayTag (org.openstreetmap.atlas.tags.HighwayTag)1