@Override public void write(JsonWriter out, FeatureCollection value) throws IOException { if (value == null) { out.nullValue(); } else { out.beginObject(); out.name("type").value("FeatureCollection"); out.name("features"); out.beginArray(); for(Feature feature : value.features()) { featureAdapter.write(out, feature); } out.endArray(); out.endObject(); } }
public static FeatureCollection of(Feature...features) { return new FeatureCollection(Arrays.asList(features)); }
@Test public void shouldHandleFeatureCollectionWithFeatures() { List<Feature> features = Arrays.asList( FeatureUtils.featureWithId("test1"), FeatureUtils.featureWithId("test2") ); FeatureCollection collection = new FeatureCollection(features); FeatureCollection parsed = toTest.fromJson(toTest.toJson(collection), FeatureCollection.class); assertThat(parsed.features(), hasSize(2)); assertThat(parsed.features().get(0).id().get(), equalTo("test1")); assertThat(parsed.features().get(1).id().get(), equalTo("test2")); }
@Override public void write(JsonWriter out, FeatureCollection value) throws IOException { if (value == null) { out.nullValue(); } else { out.beginObject(); out.name("type").value("FeatureCollection"); out.name("features"); out.beginArray(); for(Feature feature : value.features()) { featureAdapter.write(out, feature); } out.endArray(); out.endObject(); } }
public static FeatureCollection of(Feature...features) { return new FeatureCollection(Arrays.asList(features)); }
/** * Static method to be used with jexl * Checks if Point is inside any of the given polygonal geofences based on the winding number algorithm. * * @param latitude latitude of the location to verify * @param longitude longitude of the location to verify * @param geofences GeoJson representation of the fence area * @return true if location is inside any of the given geofences, else false */ public static Boolean inFence(double latitude, double longitude, String geofences) { Coordinates location = Coordinates.of(longitude, latitude); Boolean inzone = false; FeatureCollection featureCollection; try { featureCollection = GSON.fromJson(geofences, FeatureCollection.class); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(String.format("String %s is not a valid geoJson representation of fence", geofences), e); } catch (JsonSyntaxException e) { throw new IllegalArgumentException(String.format("String %s is not a valid Json string", geofences), e); } for (Feature feature : featureCollection.features()) { inzone = inzone || isPointInside(feature, location); } return inzone; }
featureCollection = new FeatureCollection(features); in.endObject();
@Test public void shouldParseFeatureCollection() { String json = "{ \"type\": \"FeatureCollection\", \"features\": [ { \"type\": \"Feature\", \"geometry\": {\"type\": \"Point\", \"coordinates\": [102.0, 0.5]}, \"properties\": {\"prop0\": \"value0\"} }, { \"type\": \"Feature\", \"geometry\": { \"type\": \"LineString\", \"coordinates\": [ [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ] }, \"properties\": { \"prop0\": \"value0\", \"prop1\": 0.0 } }, { \"type\": \"Feature\", \"geometry\": { \"type\": \"Polygon\", \"coordinates\": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }, \"properties\": { \"prop0\": \"value0\", \"prop1\": {\"this\": \"that\"} } } ] }"; FeatureCollection parsed = toTest.fromJson(json, FeatureCollection.class); assertThat(parsed.features(), hasSize(3)); }
featureCollection = new FeatureCollection(features); in.endObject();
@Test public void shouldParseRealFeatureCollection() throws IOException { String json = readJson("feature-collection.json"); FeatureCollection parsed = toTest.fromJson(json, FeatureCollection.class); assertThat(parsed.features(), hasSize(202)); }
@Test public void shouldHandleEmptyFeatureCollection() { FeatureCollection collection = new FeatureCollection(Collections.<Feature>emptyList()); final String json = toTest.toJson(collection); assertThat(json, is("{\"type\":\"FeatureCollection\",\"features\":[]}")); FeatureCollection parsed = toTest.fromJson(json, FeatureCollection.class); assertThat(parsed, equalTo(collection)); }
private void fetchLineStrings() { for (Feature feature : fc.features()) { if (debug) { // LOGGER.log(Level.INFO,feature.toString()); } Geometry<?> geometry = feature.geometry(); if (geometry instanceof MultiPolygon) { MultiPolygon mpolygon = (MultiPolygon) geometry; for (Polygon polygon : mpolygon.polygons()) { addLineString(polygon); } } if (geometry instanceof Polygon) { Polygon polygon = (Polygon) geometry; addLineString(polygon); } } }