@Test public void testPropertiesObjects() { final List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3 }); final Map<String, Object> properties = new HashMap<>(); properties.put("prop1", 1); properties.put("prop2", list); properties.put("prop3", "mystring"); final GeometryWithProperties geometryWithProperties = new GeometryWithProperties( PolyLine.TEST_POLYLINE, properties); final JsonObject jsonObject = new GeoJsonBuilder().create(geometryWithProperties); Assert.assertEquals(1, jsonObject.get("properties").getAsJsonObject().get("prop1").getAsInt()); final List<Integer> resultList = new ArrayList<>(); for (final JsonElement element : jsonObject.get("properties").getAsJsonObject().get("prop2") .getAsJsonArray()) { resultList.add(element.getAsInt()); } Assert.assertEquals(list, resultList); Assert.assertEquals("mystring", jsonObject.get("properties").getAsJsonObject().get("prop3").getAsString()); }
final Iterable<Location> geometry = geometryWithProperties.getGeometry(); final GeoJsonType geoJsonType; if (geometry instanceof Location)
@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())); } } }
/** * 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()); } }
@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()); }
@Test public void testGeometryCollectionFeatureMultipleForm() { 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 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 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()); }
/** * Converts deprecated LocationIterableProperties to GeometryWithProperties. * * @param locationIterableProperties * LocationIterableProperties object * @return GeometryWithProperties object */ protected static final GeometryWithProperties toGeometryWithProperties( final LocationIterableProperties locationIterableProperties) { final Map<String, Object> propertiesObjects = new HashMap<>(); propertiesObjects.putAll(locationIterableProperties.getProperties()); return new GeometryWithProperties(locationIterableProperties.getLocations(), propertiesObjects); }