/** * Converts given {@link CheckFlag} to {@link JsonObject} with additional key-value parameters * * @param flag * {@link CheckFlag} to convert to {@link JsonObject} * @param additionalProperties * additional key-value parameters to be added in "properties" element of the * top-level JSON object * @return {@link JsonObject} created from {@link CheckFlag} */ public static JsonObject flagToJson(final CheckFlag flag, final Map<String, String> additionalProperties) { final JsonObject flagJson = GEOJSON_BUILDER.create(flag.getLocationIterableProperties()) .jsonObject(); final JsonObject flagPropertiesJson = new JsonObject(); flagPropertiesJson.addProperty("id", flag.getIdentifier()); flagPropertiesJson.addProperty("instructions", flag.getInstructions()); // Add additional properties additionalProperties.forEach(flagPropertiesJson::addProperty); // Add properties to the previously generate geojson flagJson.add("properties", flagPropertiesJson); return flagJson; }
/** * Creates a Json Feature from a {@link GeometryWithProperties} * * @param geometryWithProperties * {@link GeometryWithProperties} * @return a GeoJson Feature */ public JsonObject create(final GeometryWithProperties geometryWithProperties) { final Iterable<Location> geometry = geometryWithProperties.getGeometry(); final Map<String, Object> properties = geometryWithProperties.getProperties(); if (geometry instanceof Location) { return create((Location) geometry).withNewProperties(properties).jsonObject(); } else if (geometry instanceof Polygon) { return create((Polygon) geometry).withNewProperties(properties).jsonObject(); } else if (geometry instanceof PolyLine) { return create((PolyLine) geometry).withNewProperties(properties).jsonObject(); } else { throw new CoreException("Unrecognized object type {}", geometry.getClass().getSimpleName()); } }
/** * Creates a GeoJson FeatureCollection from an iterable of GeoJsonObject * * @param geoJsonObjects * a iterable of GeoJsonObject * @return a GeoJson FeatureCollection */ public GeoJsonObject createFromGeoJson(final Iterable<GeoJsonObject> geoJsonObjects) { final JsonObject result = new JsonObject(); result.addProperty(TYPE, FEATURE_COLLECTION); final JsonArray features = new JsonArray(); int counter = 0; for (final GeoJsonObject object : geoJsonObjects) { if (this.logFrequency > 0 && ++counter % this.logFrequency == 0) { logger.info("Processed {} features.", counter); } features.add(object.jsonObject()); } result.add(FEATURES, features); return new GeoJsonObject(result); }
@Test public void testLocation() { final GeoJsonObject object = new GeoJsonBuilder().create(Location.TEST_5); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-122.031007,37.390535]}}", object.toString()); object.withNewProperty("property", "value"); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-122.031007,37.390535]},\"properties\":{\"property\":\"value\"}}", object.toString()); object.withNewProperty("property2", "value2"); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-122.031007,37.390535]},\"properties\":{\"property\":\"value\",\"property2\":\"value2\"}}", object.toString()); }
@Test public void testConsistency() { final Polygon polygon = new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6); final GeoJsonObject object = new GeoJsonBuilder().create(polygon); final Map<String, String> properties = new HashMap<>(); properties.put("property", "value"); properties.put("property2", "value2"); object.withNewProperties(properties); // The GeoJsonReader reads Feature collections only object.makeFeatureCollection(); final GeoJsonReader reader = new GeoJsonReader(new StringResource(object.toString())); final PropertiesLocated item = reader.next(); Assert.assertEquals(polygon, item.getItem()); }
@Test public void testLineString() { final GeoJsonObject object = new GeoJsonBuilder() .create(new PolyLine(Location.TEST_5, Location.TEST_2)); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[-122.031007,37.390535],[-122.028464,37.321628]]}}", object.toString()); final Map<String, String> properties = new HashMap<>(); properties.put("property", "value"); properties.put("property2", "value2"); object.withNewProperties(properties); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[-122.031007,37.390535],[-122.028464,37.321628]]},\"properties\":{\"property2\":\"value2\",\"property\":\"value\"}}", object.toString()); }
@Test public void testWrite() { final StringResource resource = new StringResource(); final JsonWriter writer = new JsonWriter(resource); final PolyLine polyLine = new PolyLine(Location.TEST_6, Location.TEST_2, Location.TEST_2); writer.write(polyLine.asGeoJson().jsonObject()); writer.close(); Assert.assertEquals(polyLine.asGeoJson().toString(), resource.writtenString()); } }
@Test public void testGeoJson() { System.out.println(this.atlas.asGeoJson().toString()); }
public GeoJsonObject withNewProperty(final String key, final Object value) { final Map<String, Object> properties = new HashMap<>(); properties.put(key, value); return this.withNewProperties(properties); } }
@Override public void accept(final ChangeSet changeSet) { toGeoJson(changeSet).save(this.resource); }
/** * Creates a GeoJson FeatureCollection containing a list of Features * * @param geometriesWithProperties * associated geometries and properties used to build each Feature * @return a GeoJson FeatureCollection */ public GeoJsonObject createFromGeometriesWithProperties( final Iterable<GeometryWithProperties> geometriesWithProperties) { final JsonObject result = new JsonObject(); result.addProperty(TYPE, FEATURE_COLLECTION); final JsonArray features = new JsonArray(); int counter = 0; for (final GeometryWithProperties geometryWithProperties : geometriesWithProperties) { if (this.logFrequency > 0 && ++counter % this.logFrequency == 0) { logger.info("Processed {} features.", counter); } features.add(create(geometryWithProperties)); } result.add(FEATURES, features); return new GeoJsonObject(result); }
@Test public void testPolygon() { final GeoJsonObject object = new GeoJsonBuilder() .create(new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6)); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544],[-122.031007,37.390535]]]}}", object.toString()); final Map<String, String> properties = new HashMap<>(); properties.put("property", "value"); properties.put("property2", "value2"); object.withNewProperties(properties); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544],[-122.031007,37.390535]]]},\"properties\":{\"property2\":\"value2\",\"property\":\"value\"}}", object.toString()); }
@Test public void testMultiPolygon() { final List<Polygon> polygons = new ArrayList<>(); polygons.add(new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6)); polygons.add(new Polygon(Location.COLOSSEUM, Location.EIFFEL_TOWER, Location.TEST_7)); final GeoJsonObject object = new GeoJsonBuilder().createMultiPolygons(polygons); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"MultiPolygon\",\"coordinates\":[[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544],[-122.031007,37.390535]]],[[[12.49234,41.890224],[2.294495,48.858241],[-122.0304871,37.3314171],[12.49234,41.890224]]]]}}", object.toString()); }
@Test public void testMultiPoint() { final List<Location> locations = Arrays.asList(Location.EIFFEL_TOWER, Location.COLOSSEUM); final GeoJsonObject object = new GeoJsonBuilder().create(locations, GeoJsonType.MULTI_POINT); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"MultiPoint\",\"coordinates\":[[2.294495,48.858241],[12.49234,41.890224]]}}", object.toString()); object.withNewProperty("property", "value"); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"MultiPoint\",\"coordinates\":[[2.294495,48.858241],[12.49234,41.890224]]},\"properties\":{\"property\":\"value\"}}", object.toString()); object.withNewProperty("property2", "value2"); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"MultiPoint\",\"coordinates\":[[2.294495,48.858241],[12.49234,41.890224]]},\"properties\":{\"property\":\"value\",\"property2\":\"value2\"}}", object.toString()); }
/** * Find the big nodes and save them as geojson in a resource * * @param atlas * The atlas to look at * @param writableResource * Where to save the geojson */ public void findAndSaveBigNodesAsGeoJson(final Atlas atlas, final WritableResource writableResource) { final List<LocationIterableProperties> features = new ArrayList<>(); Iterables.stream(this.find(atlas)).map(BigNode::asGeoJsonBigNode).forEach(features::add); new GeoJsonBuilder().create(features).save(writableResource); }
geometry.add(COORDINATES, coordinates); result.add(GEOMETRY, geometry); return new GeoJsonObject(result);
public static void saveAsGeoJson(final Iterable<? extends Iterable<Location>> geometries, final WritableResource resource) { try (JsonWriter writer = new JsonWriter(resource)) { writer.write(asGeoJson(geometries).jsonObject()); } }
/** * Creates a GeoJson FeatureCollection containing a list of GeoJsonObject Features * * @param objects * the features * @return a GeoJson FeatureCollection */ public GeoJsonObject createFeatureCollection(final Iterable<GeoJsonObject> objects) { final JsonObject result = new JsonObject(); result.addProperty(TYPE, FEATURE_COLLECTION); final JsonArray features = new JsonArray(); int counter = 0; for (final GeoJsonObject object : objects) { if (this.logFrequency > 0 && ++counter % this.logFrequency == 0) { logger.info("Processed {} features.", counter); } if (!Optional.ofNullable(object.jsonObject().get(TYPE)) .filter(jsonObject -> jsonObject.getAsString().equals(FEATURE)).isPresent()) { throw new CoreException("Illegal GeoJson Type for Feature collection"); } features.add(object.jsonObject()); } result.add(FEATURES, features); return new GeoJsonObject(result); }
@Test public void testMultiLineString() { final List<PolyLine> polyLines = new ArrayList<>(); polyLines.add(new PolyLine(Location.TEST_5, Location.TEST_2)); polyLines.add(new PolyLine(Location.COLOSSEUM, Location.EIFFEL_TOWER)); final GeoJsonObject object = new GeoJsonBuilder().createMultiLineStrings(polyLines); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"MultiLineString\",\"coordinates\":[[[-122.031007,37.390535],[-122.028464,37.321628]],[[12.49234,41.890224],[2.294495,48.858241]]]}}", object.toString()); }
/** * Find restricted paths of all bignodes and save them as geojson in a resource * * @param atlas * The atlas to look at * @param writableResource * Where to save the geojson */ public void findAndSaveRestrictedPathsAsGeoJson(final Atlas atlas, final WritableResource writableResource) { final List<LocationIterableProperties> features = new ArrayList<>(); Iterables.stream(this.find(atlas)).flatMap(BigNode::asGeoJsonRestrictedPath) .forEach(features::add); new GeoJsonBuilder().create(features).save(writableResource); }