public GeoJsonObject withNewProperty(final String key, final Object value) { final Map<String, Object> properties = new HashMap<>(); properties.put(key, value); return this.withNewProperties(properties); } }
/** * 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 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 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 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()); }