diff --git a/src/main/java/com/github/nramc/dev/journey/api/geojson/Geometry.java b/src/main/java/com/github/nramc/dev/journey/api/geojson/Geometry.java new file mode 100644 index 00000000..f7ee5c9d --- /dev/null +++ b/src/main/java/com/github/nramc/dev/journey/api/geojson/Geometry.java @@ -0,0 +1,24 @@ +package com.github.nramc.dev.journey.api.geojson; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.github.nramc.dev.journey.api.geojson.types.GeoJsonType; + + +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, visible = true, property = "type") +@JsonSubTypes({ + @JsonSubTypes.Type(value = Point.class, name = GeoJsonType.Constants.POINT_VALUE), + @JsonSubTypes.Type(value = MultiPoint.class, name = GeoJsonType.Constants.MULTI_POINT_VALUE), + @JsonSubTypes.Type(value = LineString.class, name = GeoJsonType.Constants.LINE_STRING_VALUE), + @JsonSubTypes.Type(value = MultiLineString.class, name = GeoJsonType.Constants.MULTI_LINE_STRING_VALUE), + @JsonSubTypes.Type(value = Polygon.class, name = GeoJsonType.Constants.POLYGON_VALUE), + @JsonSubTypes.Type(value = MultiPolygon.class, name = GeoJsonType.Constants.MULTI_POLYGON_VALUE), + @JsonSubTypes.Type(value = GeometryCollection.class, name = GeoJsonType.Constants.GEOMETRY_COLLECTION_VALUE) +}) +public abstract sealed class Geometry extends GeoJson permits + Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection { + + protected Geometry(GeoJsonType type) { + super(type); + } +} diff --git a/src/test/java/com/github/nramc/dev/journey/api/geojson/GeometryTest.java b/src/test/java/com/github/nramc/dev/journey/api/geojson/GeometryTest.java new file mode 100644 index 00000000..8de59e9d --- /dev/null +++ b/src/test/java/com/github/nramc/dev/journey/api/geojson/GeometryTest.java @@ -0,0 +1,84 @@ +package com.github.nramc.dev.journey.api.geojson; + +import com.github.nramc.dev.journey.api.geojson.types.GeoJsonType; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.json.JacksonTester; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.assertj.core.api.Assertions.assertThat; + +@JsonTest +class GeometryTest { + @Autowired + private JacksonTester jacksonTester; + + @Test + void deserialization_withPoint() throws IOException { + Geometry geometry = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/point.json"))); + assertThat(geometry).isNotNull() + .satisfies(obj -> assertThat(obj.getType()).isEqualTo(GeoJsonType.Constants.POINT_VALUE)) + .isInstanceOf(Point.class); + } + + @Test + void deserialization_withMultiPoint() throws IOException { + Geometry geometry = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/multi-point.json"))); + assertThat(geometry).isNotNull() + .satisfies(obj -> assertThat(obj.getType()).isEqualTo(GeoJsonType.Constants.MULTI_POINT_VALUE)) + .isInstanceOf(MultiPoint.class); + } + + @Test + void deserialization_withLineString() throws IOException { + Geometry geometry = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/line-string.json"))); + assertThat(geometry).isNotNull() + .satisfies(obj -> assertThat(obj.getType()).isEqualTo(GeoJsonType.Constants.LINE_STRING_VALUE)) + .isInstanceOf(LineString.class); + } + + @Test + void deserialization_withMultiLineString() throws IOException { + Geometry geometry = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/multi-line-string.json"))); + assertThat(geometry).isNotNull() + .satisfies(obj -> assertThat(obj.getType()).isEqualTo(GeoJsonType.Constants.MULTI_LINE_STRING_VALUE)) + .isInstanceOf(MultiLineString.class); + } + + @Test + void deserialization_withPolygonAndWithoutHoles() throws IOException { + Geometry geometry = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/polygon-without-holes.json"))); + assertThat(geometry).isNotNull() + .satisfies(obj -> assertThat(obj.getType()).isEqualTo(GeoJsonType.Constants.POLYGON_VALUE)) + .isInstanceOf(Polygon.class); + } + + @Test + void deserialization_withPolygonAndWithHoles() throws IOException { + Geometry geometry = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/polygon-with-holes.json"))); + assertThat(geometry).isNotNull() + .satisfies(obj -> assertThat(obj.getType()).isEqualTo(GeoJsonType.Constants.POLYGON_VALUE)) + .isInstanceOf(Polygon.class); + } + + @Test + void deserialization_withMultiPolygon() throws IOException { + Geometry geometry = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/multi-polygon.json"))); + assertThat(geometry).isNotNull() + .satisfies(obj -> assertThat(obj.getType()).isEqualTo(GeoJsonType.Constants.MULTI_POLYGON_VALUE)) + .isInstanceOf(MultiPolygon.class); + } + + @Test + void deserialization_withGeometryCollection() throws IOException { + Geometry geometry = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/geometry-collection.json"))); + assertThat(geometry).isNotNull() + .satisfies(obj -> assertThat(obj.getType()).isEqualTo(GeoJsonType.Constants.GEOMETRY_COLLECTION_VALUE)) + .isInstanceOf(GeometryCollection.class); + } + +} \ No newline at end of file diff --git a/src/test/resources/data/line-string.json b/src/test/resources/data/line-string.json new file mode 100644 index 00000000..84d5dab7 --- /dev/null +++ b/src/test/resources/data/line-string.json @@ -0,0 +1,7 @@ +{ + "type": "LineString", + "coordinates": [ + [101.0, 0.0], + [102.0, 1.0] + ] +} \ No newline at end of file diff --git a/src/test/resources/data/multi-line-string.json b/src/test/resources/data/multi-line-string.json new file mode 100644 index 00000000..968a26ac --- /dev/null +++ b/src/test/resources/data/multi-line-string.json @@ -0,0 +1,13 @@ +{ + "type": "MultiLineString", + "coordinates": [ + [ + [100.0, 0.0], + [101.0, 1.0] + ], + [ + [102.0, 2.0], + [103.0, 3.0] + ] + ] +} \ No newline at end of file diff --git a/src/test/resources/data/multi-point.json b/src/test/resources/data/multi-point.json new file mode 100644 index 00000000..27ddacd9 --- /dev/null +++ b/src/test/resources/data/multi-point.json @@ -0,0 +1,7 @@ +{ + "type": "MultiPoint", + "coordinates": [ + [100.0, 0.0], + [101.0, 1.0] + ] +} \ No newline at end of file diff --git a/src/test/resources/data/multi-polygon.json b/src/test/resources/data/multi-polygon.json new file mode 100644 index 00000000..37147f53 --- /dev/null +++ b/src/test/resources/data/multi-polygon.json @@ -0,0 +1,30 @@ +{ + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [102.0, 2.0], + [103.0, 2.0], + [103.0, 3.0], + [102.0, 3.0], + [102.0, 2.0] + ] + ], + [ + [ + [100.0, 0.0], + [101.0, 0.0], + [101.0, 1.0], + [100.0, 1.0], + [100.0, 0.0] + ], + [ + [100.2, 0.2], + [100.2, 0.8], + [100.8, 0.8], + [100.8, 0.2], + [100.2, 0.2] + ] + ] + ] +} \ No newline at end of file diff --git a/src/test/resources/data/point.json b/src/test/resources/data/point.json new file mode 100644 index 00000000..ec93677e --- /dev/null +++ b/src/test/resources/data/point.json @@ -0,0 +1,4 @@ +{ + "type": "Point", + "coordinates": [100.0, 0.0] +} \ No newline at end of file diff --git a/src/test/resources/data/polygon-with-holes.json b/src/test/resources/data/polygon-with-holes.json new file mode 100644 index 00000000..eb86dba2 --- /dev/null +++ b/src/test/resources/data/polygon-with-holes.json @@ -0,0 +1,19 @@ +{ + "type": "Polygon", + "coordinates": [ + [ + [100.0, 0.0], + [101.0, 0.0], + [101.0, 1.0], + [100.0, 1.0], + [100.0, 0.0] + ], + [ + [100.8, 0.8], + [100.8, 0.2], + [100.2, 0.2], + [100.2, 0.8], + [100.8, 0.8] + ] + ] +} \ No newline at end of file diff --git a/src/test/resources/data/polygon-without-holes.json b/src/test/resources/data/polygon-without-holes.json new file mode 100644 index 00000000..9ef2c977 --- /dev/null +++ b/src/test/resources/data/polygon-without-holes.json @@ -0,0 +1,12 @@ +{ + "type": "Polygon", + "coordinates": [ + [ + [100.0, 0.0], + [101.0, 0.0], + [101.0, 1.0], + [100.0, 1.0], + [100.0, 0.0] + ] + ] +} \ No newline at end of file