public GeoJsonObject asGeoJson() { return new GeoJsonBuilder().create(this); }
/** * Creates a GeoJson FeatureCollection containing a list of Features * * @param objects * used to build each Feature * @return a GeoJson FeatureCollection * @deprecated use {@link #createFromGeometriesWithProperties(Iterable)} instead */ @Deprecated public GeoJsonObject create(final Iterable<LocationIterableProperties> objects) { return createFromGeometriesWithProperties(toGeometriesWithProperties(objects)); }
/** * Creates a GeometryCollection type Feature containing geometries derived from a collection of * {@link LocationIterableProperties}. <strong>Note:</strong> feature parameters are not present * in the resulting GeometryCollection and must be handled separately to avoid data loss. * * @deprecated use {@link #createGeometryCollectionFeature(Iterable)} instead * @param objects * used to build each geometry * @return a GeoJson Feature */ @Deprecated public GeoJsonObject createGeometryCollection( final Iterable<LocationIterableProperties> objects) { return createGeometryCollectionFeature(toGeometriesWithProperties(objects)); }
/** * Creates a Json Feature from a {@link LocationIterableProperties} * * @deprecated use {@link #create(GeometryWithProperties)} instead * @param object * {@link LocationIterableProperties} * @return a GeoJson Feature */ @Deprecated public JsonObject create(final LocationIterableProperties object) { final GeometryWithProperties geometryWithProperties = toGeometryWithProperties(object); return create(geometryWithProperties); }
/** * Creates a Point type GeoJson Feature * * @param location * geometry * @return a Feature */ public GeoJsonObject create(final Location location) { return this.create(location, GeoJsonType.POINT); }
@Test public void testGeometryCollectionFeatureSingularForm() { final Map<String, Object> properties = new HashMap<>(); final List<GeometryWithProperties> items = new ArrayList<>(); items.add(new GeometryWithProperties(Location.TEST_1, properties)); items.add(new GeometryWithProperties( new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6), properties)); items.add(new GeometryWithProperties(new PolyLine(Location.TEST_5, Location.TEST_2), properties)); final GeoJsonObject object = new GeoJsonBuilder().createGeometryCollectionFeature(items); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[-122.009566,37.33531]},{\"type\":\"Polygon\",\"coordinates\":[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544]]]},{\"type\":\"LineString\",\"coordinates\":[[-122.031007,37.390535],[-122.028464,37.321628]]}]}}", object.toString()); }
if (points.size() > 1) geometries.add(create(Iterables.translate(points, Iterables::head), GeoJsonType.MULTI_POINT).jsonObject().getAsJsonObject(GEOMETRY)); geometries.add(create(Iterables.head(points), GeoJsonType.POINT).jsonObject() .getAsJsonObject(GEOMETRY)); if (polygons.size() > 1) geometries.add(createMultiPolygons(Iterables.stream(polygons).map(Polygon::new)) .jsonObject().getAsJsonObject(GEOMETRY)); geometries.add(create(Iterables.head(polygons), GeoJsonType.POLYGON).jsonObject() .getAsJsonObject(GEOMETRY)); createMultiLineStrings(Iterables.stream(multiPolylines).map(PolyLine::new)) .jsonObject().getAsJsonObject(GEOMETRY)); geometries.add(create(Iterables.head(multiPolylines), GeoJsonType.LINESTRING) .jsonObject().getAsJsonObject(GEOMETRY));
@Test public void testGeometryCollectionSingularForm() { final Map<String, String> properties = new HashMap<>(); final List<LocationIterableProperties> items = new ArrayList<>(); items.add(new LocationIterableProperties(Location.TEST_1, properties)); items.add(new LocationIterableProperties( new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6), properties)); items.add(new LocationIterableProperties(new PolyLine(Location.TEST_5, Location.TEST_2), properties)); final GeoJsonObject object = new GeoJsonBuilder().createGeometryCollection(items); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[-122.009566,37.33531]},{\"type\":\"Polygon\",\"coordinates\":[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544]]]},{\"type\":\"LineString\",\"coordinates\":[[-122.031007,37.390535],[-122.028464,37.321628]]}]}}", object.toString()); }
@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 GeoJsonObject asGeoJsonFeatureCollection() { final GeoJsonBuilder builder = new GeoJsonBuilder(); return builder.createFeatureCollection(Iterables.translate(outers(), outerPolygon -> builder.createOneOuterMultiPolygon( new MultiIterable<>(Collections.singleton(outerPolygon), this.outerToInners.get(outerPolygon))))); }
if (geometries.size() == 1) feature = GEOJSON_BUILDER.create(geometries.get(0)); feature = GEOJSON_BUILDER.createGeometryCollection(geometries).jsonObject();
@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()); }
@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 testCreateFromGeometries() { final Map<String, Object> properties = new HashMap<>(); properties.put("property", "value"); properties.put("property2", "value2"); final List<GeometryWithProperties> items = new ArrayList<>(); items.add(new GeometryWithProperties(Location.TEST_1, properties)); items.add(new GeometryWithProperties( new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6), properties)); items.add(new GeometryWithProperties(new PolyLine(Location.TEST_5, Location.TEST_2), properties)); final GeoJsonObject object = new GeoJsonBuilder().createFromGeometriesWithProperties(items); Assert.assertEquals( "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\"" + ":[-122.009566,37.33531]},\"properties\":{\"property2\":\"value2\",\"property\":\"value\"}},{\"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\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\"" + ":[[-122.031007,37.390535],[-122.028464,37.321628]]},\"properties\":{\"property2\":\"value2\",\"property\":\"value\"}}]}", object.toString()); }
/** * Converts iterable of deprecated LocationIterableProperties to iterable of * GeometryWithProperties. * * @param objects * iterable of LocationIterableProperties * @return iterable of GeometryWithProperties */ protected static final Iterable<GeometryWithProperties> toGeometriesWithProperties( final Iterable<LocationIterableProperties> objects) { final Iterable<GeometryWithProperties> geometriesWithProperties = Iterables .translate(objects, locationIterableProperties -> { return toGeometryWithProperties(locationIterableProperties); }); return geometriesWithProperties; }
/** * Creates a LineString type GeoJson Feature * * @param polyLine * geometry * @return a GeoJson Feature */ public GeoJsonObject create(final PolyLine polyLine) { return this.create(polyLine, GeoJsonType.LINESTRING); }
@Test public void testGeometryCollectionFeature() { final Map<String, Object> properties = new HashMap<>(); final List<GeometryWithProperties> items = new ArrayList<>(); items.add(new GeometryWithProperties(Location.TEST_1, properties)); items.add(new GeometryWithProperties(Location.TEST_1, properties)); items.add(new GeometryWithProperties( new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6), properties)); items.add(new GeometryWithProperties( new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6), properties)); items.add(new GeometryWithProperties(new PolyLine(Location.TEST_5, Location.TEST_2), properties)); items.add(new GeometryWithProperties(new PolyLine(Location.TEST_5, Location.TEST_2), properties)); final GeoJsonObject object = new GeoJsonBuilder().createGeometryCollectionFeature(items); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"MultiPoint\",\"coordinates\":[[-122.009566,37.33531],[-122.009566,37.33531]]},{\"type\":\"MultiPolygon\",\"coordinates\":[[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544],[-122.031007,37.390535]]],[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544],[-122.031007,37.390535]]]]},{\"type\":\"MultiLineString\",\"coordinates\":[[[-122.031007,37.390535],[-122.028464,37.321628]],[[-122.031007,37.390535],[-122.028464,37.321628]]]}]}}", object.toString()); }
@Test public void testGeometryCollectionMultipleForm() { final Map<String, String> properties = new HashMap<>(); final List<LocationIterableProperties> items = new ArrayList<>(); items.add(new LocationIterableProperties(Location.TEST_1, properties)); items.add(new LocationIterableProperties(Location.TEST_1, properties)); items.add(new LocationIterableProperties( new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6), properties)); items.add(new LocationIterableProperties( new Polygon(Location.TEST_5, Location.TEST_2, Location.TEST_6), properties)); items.add(new LocationIterableProperties(new PolyLine(Location.TEST_5, Location.TEST_2), properties)); items.add(new LocationIterableProperties(new PolyLine(Location.TEST_5, Location.TEST_2), properties)); final GeoJsonObject object = new GeoJsonBuilder().createGeometryCollection(items); Assert.assertEquals( "{\"type\":\"Feature\",\"geometry\":{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"MultiPoint\",\"coordinates\":[[-122.009566,37.33531],[-122.009566,37.33531]]},{\"type\":\"MultiPolygon\",\"coordinates\":[[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544],[-122.031007,37.390535]]],[[[-122.031007,37.390535],[-122.028464,37.321628],[-122.033948,37.32544],[-122.031007,37.390535]]]]},{\"type\":\"MultiLineString\",\"coordinates\":[[[-122.031007,37.390535],[-122.028464,37.321628]],[[-122.031007,37.390535],[-122.028464,37.321628]]]}]}}", object.toString()); }
propertiesObject); final GeoJsonObject featureCollection = new GeoJsonBuilder() .createFeatureCollectionFromPropertiesLocated( Iterables.from(propertiesLocated1, propertiesLocated2));
@Test public void testToGeometryWithProperties() { final Map<String, String> stringProperties = new HashMap<>(); stringProperties.put("prop1", "val1"); stringProperties.put("prop2", "val2"); final LocationIterableProperties locationIterableProperties = new LocationIterableProperties( PolyLine.TEST_POLYLINE, stringProperties); final GeometryWithProperties geometryWithProperties = GeoJsonBuilder .toGeometryWithProperties(locationIterableProperties); Assert.assertEquals(PolyLine.TEST_POLYLINE, geometryWithProperties.getGeometry()); for (final Entry<String, String> stringPropertiesEntry : stringProperties.entrySet()) { Assert.assertEquals(stringPropertiesEntry.getValue(), geometryWithProperties.getProperties().get(stringPropertiesEntry.getKey())); } } }