Skip to content

Commit

Permalink
[feat] #7 geojson data modeling added for Geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramachandran Nellaiyappan committed Mar 21, 2024
1 parent 7e0d023 commit a911ecb
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Geometry> 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);
}

}
7 changes: 7 additions & 0 deletions src/test/resources/data/line-string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "LineString",
"coordinates": [
[101.0, 0.0],
[102.0, 1.0]
]
}
13 changes: 13 additions & 0 deletions src/test/resources/data/multi-line-string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "MultiLineString",
"coordinates": [
[
[100.0, 0.0],
[101.0, 1.0]
],
[
[102.0, 2.0],
[103.0, 3.0]
]
]
}
7 changes: 7 additions & 0 deletions src/test/resources/data/multi-point.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "MultiPoint",
"coordinates": [
[100.0, 0.0],
[101.0, 1.0]
]
}
30 changes: 30 additions & 0 deletions src/test/resources/data/multi-polygon.json
Original file line number Diff line number Diff line change
@@ -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]
]
]
]
}
4 changes: 4 additions & 0 deletions src/test/resources/data/point.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Point",
"coordinates": [100.0, 0.0]
}
19 changes: 19 additions & 0 deletions src/test/resources/data/polygon-with-holes.json
Original file line number Diff line number Diff line change
@@ -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]
]
]
}
12 changes: 12 additions & 0 deletions src/test/resources/data/polygon-without-holes.json
Original file line number Diff line number Diff line change
@@ -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]
]
]
}

0 comments on commit a911ecb

Please sign in to comment.