/** * This takes the currently defined values found inside this instance and converts it to a GeoJson * string. * * @return a JSON string which represents this Feature * @since 1.0.0 */ @Override public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); // Empty properties -> should not appear in json string Feature feature = this; if (properties().size() == 0) { feature = new AutoValue_Feature(TYPE, bbox(), id(), geometry(), null); } return gson.create().toJson(feature); }
/** * This takes the currently defined values found inside this instance and converts it to a GeoJson * string. * * @return a JSON string which represents this Feature * @since 1.0.0 */ @Override public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); // Empty properties -> should not appear in json string Feature feature = this; if (properties().size() == 0) { feature = new AutoValue_Feature(TYPE, bbox(), id(), geometry(), null); } return gson.create().toJson(feature); }
/** * Create a new instance of this class by passing in a formatted valid JSON String. If you are * creating a Feature object from scratch it is better to use one of the other provided static * factory methods such as {@link #fromGeometry(Geometry)}. * * @param json a formatted valid JSON string defining a GeoJson Feature * @return a new instance of this class defined by the values passed inside this static factory * method * @since 1.0.0 */ public static Feature fromJson(@NonNull String json) { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); gson.registerTypeAdapter(Point.class, new PointDeserializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); Feature feature = gson.create().fromJson(json, Feature.class); // Even thought properties are Nullable, // Feature object will be created with properties set to an empty object, // so that addProperties() would work if (feature.properties() != null) { return feature; } return new AutoValue_Feature(TYPE, feature.bbox(), feature.id(), feature.geometry(), new JsonObject()); }
/** * Create a new instance of this class by passing in a formatted valid JSON String. If you are * creating a Feature object from scratch it is better to use one of the other provided static * factory methods such as {@link #fromGeometry(Geometry)}. * * @param json a formatted valid JSON string defining a GeoJson Feature * @return a new instance of this class defined by the values passed inside this static factory * method * @since 1.0.0 */ public static Feature fromJson(@NonNull String json) { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); gson.registerTypeAdapter(Point.class, new PointDeserializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); Feature feature = gson.create().fromJson(json, Feature.class); // Even thought properties are Nullable, // Feature object will be created with properties set to an empty object, // so that addProperties() would work if (feature.properties() != null) { return feature; } return new AutoValue_Feature(TYPE, feature.bbox(), feature.id(), feature.geometry(), new JsonObject()); }
@Test public void bbox_returnsCorrectBbox() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(2.0, 3.0)); LineString lineString = LineString.fromLngLats(points); BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0); Feature feature = Feature.fromGeometry(lineString, bbox); assertNotNull(feature.bbox()); assertEquals(1.0, feature.bbox().west(), DELTA); assertEquals(2.0, feature.bbox().south(), DELTA); assertEquals(3.0, feature.bbox().east(), DELTA); assertEquals(4.0, feature.bbox().north(), DELTA); }
@Test public void bbox_nullWhenNotSet() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(2.0, 3.0)); LineString lineString = LineString.fromLngLats(points); Feature feature = Feature.fromGeometry(lineString); assertNull(feature.bbox()); }