From 182c488323b9fc78f335d428ea57777dadb9f5e7 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Tue, 13 Mar 2018 19:50:20 +0200 Subject: [PATCH] Make GeometryCollection a Geometry as specified in the spec at sections 1.4 and 3.1.8 See: - https://tools.ietf.org/html/rfc7946#section-1.4 - https://tools.ietf.org/html/rfc7946#section-3.1.8 --- .../v5/models/CarmenFeatureTest.java | 10 +++++---- .../mapbox/geojson/CoordinateContainer.java | 21 +++++++++++++++++++ .../java/com/mapbox/geojson/Geometry.java | 15 ++----------- .../mapbox/geojson/GeometryCollection.java | 15 ++++++------- .../java/com/mapbox/geojson/LineString.java | 2 +- .../com/mapbox/geojson/MultiLineString.java | 2 +- .../java/com/mapbox/geojson/MultiPoint.java | 2 +- .../java/com/mapbox/geojson/MultiPolygon.java | 2 +- .../main/java/com/mapbox/geojson/Point.java | 2 +- .../main/java/com/mapbox/geojson/Polygon.java | 2 +- .../geojson/gson/GeometryTypeAdapter.java | 5 ++++- 11 files changed, 47 insertions(+), 31 deletions(-) create mode 100644 services-geojson/src/main/java/com/mapbox/geojson/CoordinateContainer.java diff --git a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenFeatureTest.java b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenFeatureTest.java index 575b4ca58..d1dce31e3 100644 --- a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenFeatureTest.java +++ b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenFeatureTest.java @@ -2,6 +2,7 @@ import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertTrue; +import static org.hamcrest.Matchers.isA; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.IsEqual.equalTo; @@ -11,6 +12,7 @@ import com.mapbox.api.geocoding.v5.GeocodingTestUtils; import com.mapbox.api.geocoding.v5.MapboxGeocoding; import com.mapbox.core.TestUtils; +import com.mapbox.geojson.CoordinateContainer; import com.mapbox.geojson.Point; import org.junit.Test; import retrofit2.Response; @@ -81,8 +83,8 @@ public void fromJson_handlesConversionCorrectly() throws Exception { assertThat(feature.type(), equalTo("Feature")); assertEquals(5, feature.context().size()); assertThat(feature.geometry().type(), equalTo("Point")); - assertThat(feature.geometry().coordinates().toString(), equalTo("[-77.036543, " - + "38.897702]")); + assertThat(((CoordinateContainer) feature.geometry()).coordinates().toString(), + equalTo("[-77.036543, 38.897702]")); assertThat(feature.address(), equalTo("1600")); assertThat(feature.id(), equalTo("address.3982178573139850")); assertEquals(1, feature.placeType().size()); @@ -132,8 +134,8 @@ public void ForwardGeocode_withValidChineseResponse() throws Exception { assertEquals(3, feature.context().size()); assertThat(feature.geometry().type(), equalTo("Point")); - assertThat(feature.geometry().coordinates().toString(), equalTo("[106.820552, " - + "39.458115]")); + assertThat(((CoordinateContainer) feature.geometry()).coordinates().toString(), + equalTo("[106.820552, 39.458115]")); assertThat(feature.id(), equalTo("place.10514057239276310")); assertThat(feature.relevance(), equalTo(0.99)); assertThat(feature.placeName(), equalTo("中国内蒙古乌海市海南区")); diff --git a/services-geojson/src/main/java/com/mapbox/geojson/CoordinateContainer.java b/services-geojson/src/main/java/com/mapbox/geojson/CoordinateContainer.java new file mode 100644 index 000000000..4f040a193 --- /dev/null +++ b/services-geojson/src/main/java/com/mapbox/geojson/CoordinateContainer.java @@ -0,0 +1,21 @@ +package com.mapbox.geojson; + +/** + * Each of the s geometries which make up GeoJson implement this interface and consume a varying + * dimension of {@link Point} list. Since this is varying, each geometry object fulfills the + * contract by replacing the generic with a well defined list of Points. + * + * @param a generic allowing varying dimensions for each GeoJson geometry + * @since 3.0.0 + */ +public interface CoordinateContainer extends Geometry { + + /** + * the coordinates which define the geometry. Typically a list of points but for some geometry + * such as polygon this can be a list of a list of points, thus the return is generic here. + * + * @return the {@link Point}s which make up the coordinates defining the geometry + * @since 3.0.0 + */ + T coordinates(); +} diff --git a/services-geojson/src/main/java/com/mapbox/geojson/Geometry.java b/services-geojson/src/main/java/com/mapbox/geojson/Geometry.java index 4f1819b9e..ba1c289ab 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/Geometry.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/Geometry.java @@ -1,21 +1,10 @@ package com.mapbox.geojson; /** - * Each of the seven geometries which make up GeoJson implement this interface and consume a varying - * dimension of {@link Point} list. Since this is varying, each geometry object fulfills the - * contract by replacing the generic with a well defined list of Points. + * Each of the six geometries and {@link GeometryCollection} which make up GeoJson implement this interface. * - * @param a generic allowing varying dimensions for each GeoJson geometry * @since 1.0.0 */ -public interface Geometry extends GeoJson { +public interface Geometry extends GeoJson { - /** - * the coordinates which define the geometry. Typically a list of points but for some geometry - * such as polygon this can be a list of a list of points, thus the return is generic here. - * - * @return the {@link Point}s which make up the coordinates defining the geometry - * @since 1.0.0 - */ - T coordinates(); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java b/services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java index 91b75c729..0a6f0eed1 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java @@ -2,6 +2,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; + import com.google.auto.value.AutoValue; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -61,7 +62,7 @@ * @since 1.0.0 */ @AutoValue -public abstract class GeometryCollection implements GeoJson, Serializable { +public abstract class GeometryCollection implements Geometry, Serializable { private static final String TYPE = "GeometryCollection"; @@ -72,7 +73,7 @@ public abstract class GeometryCollection implements GeoJson, Serializable { * * @param json a formatted valid JSON string defining a GeoJson Geometry Collection * @return a new instance of this class defined by the values passed inside this static factory - * method + * method * @since 1.0.0 */ public static GeometryCollection fromJson(String json) { @@ -89,7 +90,7 @@ public static GeometryCollection fromJson(String json) { * * @param geometries a non-null list of geometry which makes up this collection * @return a new instance of this class defined by the values passed inside this static factory - * method + * method * @since 1.0.0 */ public static GeometryCollection fromGeometries(@NonNull List geometries) { @@ -101,7 +102,7 @@ public static GeometryCollection fromGeometries(@NonNull List geometri * * @param geometry a non-null object of type geometry which makes up this collection * @return a new instance of this class defined by the values passed inside this static factory - * method + * method * @since 3.0.0 */ public static GeometryCollection fromGeometry(@NonNull Geometry geometry) { @@ -115,7 +116,7 @@ public static GeometryCollection fromGeometry(@NonNull Geometry geometry) { * @param geometries a non-null list of geometry which makes up this collection * @param bbox optionally include a bbox definition as a double array * @return a new instance of this class defined by the values passed inside this static factory - * method + * method * @since 1.0.0 */ public static GeometryCollection fromGeometries(@NonNull List geometries, @@ -129,7 +130,7 @@ public static GeometryCollection fromGeometries(@NonNull List geometri * @param geometry a non-null object of type geometry which makes up this collection * @param bbox optionally include a bbox definition as a double array * @return a new instance of this class defined by the values passed inside this static factory - * method + * method * @since 3.0.0 */ public static GeometryCollection fromGeometry(@NonNull Geometry geometry, @@ -143,7 +144,7 @@ public static GeometryCollection fromGeometry(@NonNull Geometry geometry, * {@link GeometryCollection}. * * @return a String which describes the TYPE of geometry, for this object it will always return - * {@code GeometryCollection} + * {@code GeometryCollection} * @since 1.0.0 */ @NonNull diff --git a/services-geojson/src/main/java/com/mapbox/geojson/LineString.java b/services-geojson/src/main/java/com/mapbox/geojson/LineString.java index 0da8e60dc..d0280378d 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/LineString.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/LineString.java @@ -51,7 +51,7 @@ * @since 1.0.0 */ @AutoValue -public abstract class LineString implements Geometry>, Serializable { +public abstract class LineString implements CoordinateContainer>, Serializable { private static final String TYPE = "LineString"; diff --git a/services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java b/services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java index b28b8c09c..a1afacdcc 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java @@ -51,7 +51,7 @@ * @since 1.0.0 */ @AutoValue -public abstract class MultiLineString implements Geometry>>, Serializable { +public abstract class MultiLineString implements CoordinateContainer>>, Serializable { private static final String TYPE = "MultiLineString"; diff --git a/services-geojson/src/main/java/com/mapbox/geojson/MultiPoint.java b/services-geojson/src/main/java/com/mapbox/geojson/MultiPoint.java index bb7490ed0..8de336b83 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/MultiPoint.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/MultiPoint.java @@ -36,7 +36,7 @@ * @since 1.0.0 */ @AutoValue -public abstract class MultiPoint implements Geometry>, Serializable { +public abstract class MultiPoint implements CoordinateContainer>, Serializable { private static final String TYPE = "MultiPoint"; diff --git a/services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java b/services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java index dbfeeac30..e859fd94f 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java @@ -69,7 +69,7 @@ * @since 1.0.0 */ @AutoValue -public abstract class MultiPolygon implements Geometry>>>, Serializable { +public abstract class MultiPolygon implements CoordinateContainer>>>, Serializable { private static final String TYPE = "MultiPolygon"; diff --git a/services-geojson/src/main/java/com/mapbox/geojson/Point.java b/services-geojson/src/main/java/com/mapbox/geojson/Point.java index c92905226..d05c4e241 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/Point.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/Point.java @@ -58,7 +58,7 @@ * @since 1.0.0 */ @AutoValue -public abstract class Point implements Geometry>, Serializable { +public abstract class Point implements CoordinateContainer>, Serializable { private static final String TYPE = "Point"; diff --git a/services-geojson/src/main/java/com/mapbox/geojson/Polygon.java b/services-geojson/src/main/java/com/mapbox/geojson/Polygon.java index 662284789..114b3593e 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/Polygon.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/Polygon.java @@ -59,7 +59,7 @@ * @since 1.0.0 */ @AutoValue -public abstract class Polygon implements Geometry>>, Serializable { +public abstract class Polygon implements CoordinateContainer>>, Serializable { private static final String TYPE = "Polygon"; diff --git a/services-geojson/src/main/java/com/mapbox/geojson/gson/GeometryTypeAdapter.java b/services-geojson/src/main/java/com/mapbox/geojson/gson/GeometryTypeAdapter.java index 73ab05d47..b35751690 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/gson/GeometryTypeAdapter.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/gson/GeometryTypeAdapter.java @@ -3,6 +3,7 @@ import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; +import com.mapbox.geojson.CoordinateContainer; import com.mapbox.geojson.Geometry; import java.io.IOException; @@ -16,7 +17,9 @@ public void write(JsonWriter out, Geometry value) throws IOException { if (value.bbox() != null) { out.name("bbox").jsonValue(value.bbox().toJson()); } - out.name("coordinates").jsonValue(value.coordinates().toString()); + if (value instanceof CoordinateContainer) { + out.name("coordinates").jsonValue(((CoordinateContainer) value).coordinates().toString()); + } out.endObject(); }