/** * 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); }
/** * 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); }
/** * Creates a MultiLineString type GeoJson Feature * * @param polyLines * geometry * @return a GeoJson Feature */ public GeoJsonObject createMultiLineStrings(final Iterable<PolyLine> polyLines) { // Create the coordinates for each polyline final List<GeoJsonObject> objects = new ArrayList<>(); for (final PolyLine polygon : polyLines) { objects.add(this.create(polygon, GeoJsonType.MULTI_LINESTRING)); } final JsonObject result = new JsonObject(); result.addProperty(TYPE, FEATURE); final JsonArray coordinates = new JsonArray(); // Add the coordinates back for the entire object for (final GeoJsonObject object : objects) { coordinates .add(object.jsonObject().getAsJsonObject(GEOMETRY).getAsJsonArray(COORDINATES)); } final JsonObject geometry = new JsonObject(); geometry.addProperty(TYPE, GeoJsonType.MULTI_LINESTRING.getType()); geometry.add(COORDINATES, coordinates); result.add(GEOMETRY, geometry); return new GeoJsonObject(result); }
.add(object.jsonObject().getAsJsonObject(GEOMETRY).getAsJsonArray(COORDINATES));
.add(object.jsonObject().getAsJsonObject(GEOMETRY).getAsJsonArray(COORDINATES)); coordinates.add(subCoordinates);
/** * 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; }
@Override public void saveAsGeoJson(final WritableResource resource, final Predicate<AtlasEntity> matcher) { try (JsonWriter writer = new JsonWriter(resource)) { writer.write(this.asGeoJson(matcher).jsonObject()); } }
private static void save(final GeoJsonObject object, final WritableResource destination) { final JsonWriter writer = new JsonWriter(destination); writer.write(object.jsonObject()); writer.close(); }
public static void saveAsGeoJson(final Iterable<? extends Iterable<Location>> geometries, final WritableResource resource) { try (JsonWriter writer = new JsonWriter(resource)) { writer.write(asGeoJson(geometries).jsonObject()); } }
@Override protected void handle(final Atlas atlas, final CommandMap command) { getWriter(command).write(atlas.asGeoJson(x -> true).jsonObject()); }
public void saveAsGeoJson(final WritableResource resource) { final JsonWriter writer = new JsonWriter(resource); writer.write(asGeoJson().jsonObject()); writer.close(); }
/** * 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()); } }
/** * @param diffs * An {@link Iterable} of {@link Diff} * @param filter * The filter to apply to the diff * @return A GeoJSON String representation of all the {@link Diff} items in the {@link Iterable} * , which match the filter. */ public static String toGeoJson(final Iterable<Diff> diffs, final Predicate<Diff> filter) { return new GeoJsonBuilder().create( Iterables.stream(diffs).filter(diff -> diff.getItemType() != ItemType.RELATION) .filter(filter).flatMap(Diff::processDiff).collect()) .jsonObject().toString(); }
featureCollection.jsonObject().get("type").getAsString()); Assert.assertEquals(2, featureCollection.jsonObject().get("features").getAsJsonArray().size()); for (int i = 0; i < 2; i++) Assert.assertEquals("Feature", featureCollection.jsonObject().get("features") .getAsJsonArray().get(i).getAsJsonObject().get("type").getAsString()); Assert.assertEquals("LineString", featureCollection.jsonObject().get("features").getAsJsonArray().get(i) .getAsJsonObject().get("geometry").getAsJsonObject().get("type") .getAsString()); Assert.assertEquals( PolyLine.TEST_POLYLINE.asGeoJson().jsonObject().get("features").getAsJsonArray() .get(0).getAsJsonObject().get("geometry"), featureCollection.jsonObject().get("features").getAsJsonArray().get(i) .getAsJsonObject().get("geometry").getAsJsonObject()); Assert.assertEquals(properties.get("prop1"), featureCollection.jsonObject().get("features").getAsJsonArray().get(i) .getAsJsonObject().get("properties").getAsJsonObject().get("prop1") .getAsString()); Assert.assertEquals(((Float[]) properties.get("prop2")).length, featureCollection.jsonObject().get("features").getAsJsonArray().get(i) .getAsJsonObject().get("properties").getAsJsonObject().get("prop2") .getAsJsonArray().size()); featureCollection.jsonObject().get("features").getAsJsonArray().get(i) .getAsJsonObject().get("properties").getAsJsonObject().get("prop2") .getAsJsonArray().get(j).getAsFloat(),
/** * @param diffs * An {@link Iterable} of {@link Diff} * @param filter * The filter to apply to the diff * @return A GeoJSON String representation of all the {@link AtlasItem}s that are a * {@link RelationMember} in a {@link Diff} {@link Relation}, which match the filter. */ public static String toRelationsGeoJson(final Iterable<Diff> diffs, final Predicate<Diff> filter) { return new GeoJsonBuilder().create( Iterables.stream(diffs).filter(diff -> diff.getItemType() == ItemType.RELATION) .filter(filter).flatMap(Diff::processDiff).collect()) .jsonObject().toString(); }
public void saveAsGeoJson(final WritableResource resource) { final JsonWriter writer = new JsonWriter(resource); final GeoJsonObject geoJson = new GeoJsonBuilder().create(Iterables .translate(this.root.leafNodes(Rectangle.MAXIMUM), Node::toGeoJsonBuildingBlock)); writer.write(geoJson.jsonObject()); writer.close(); }
@Override protected int onRun(final CommandMap command) { final File folder = (File) command.get(PATH); final File output = (File) command.get(OUTPUT); final Mode mode = (Mode) command.get(MODE); final String filePrefix = (String) command.get(FILE_PREFIX); // processing the files in sorted order makes testing easier final List<File> files = folder.listFilesRecursively(); Collections.sort(files); final Iterable<PropertiesLocated> jsonItems = readGeoJsonItems(mode, files, filePrefix); final GeoJsonObject result = new GeoJsonBuilder() .createFeatureCollectionFromPropertiesLocated(jsonItems); final JsonWriter writer = new JsonWriter(output); writer.write(result.jsonObject()); writer.close(); return 0; }
public static void saveAsGeojson(final Iterable<ComplexBoundary> complexBoundaries, final WritableResource output) { try (JsonWriter writer = new JsonWriter(output)) { final Iterable<LocationIterableProperties> geojsonObjects = Iterables .stream(complexBoundaries).flatMap(boundary -> { final Map<String, String> tags = boundary.getSource().getTags(); return Iterables .stream(boundary.getOutline().asLocationIterableProperties()) .map(locationIterableProperties -> { locationIterableProperties.getProperties().putAll(tags); return locationIterableProperties; }); }); writer.write(new GeoJsonBuilder().create(geojsonObjects).jsonObject()); } }
@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()); } }
@Override protected int onRun(final CommandMap command) { final File folder = (File) command.get(INPUT_FOLDER); final Atlas atlas = loadAtlas(command); final File bigNodesFile = folder.child("improved.bigNodes.geojson"); final JsonWriter writer = new JsonWriter(bigNodesFile); writer.write(new GeoJsonBuilder().create( Iterables.translate(new BigNodeFinder().find(atlas), BigNode::asGeoJsonBigNode)) .jsonObject()); writer.close(); return 0; } }