-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] #7 geojson data modeling added for FeatureCollection
- Loading branch information
Ramachandran Nellaiyappan
committed
Mar 21, 2024
1 parent
21b04e3
commit 4735067
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/com/github/nramc/dev/journey/api/geojson/FeatureCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.nramc.dev.journey.api.geojson; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.github.nramc.dev.journey.api.geojson.types.GeoJsonType; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
|
||
import java.util.List; | ||
|
||
|
||
@Getter | ||
@ToString | ||
@EqualsAndHashCode(callSuper = false) | ||
public final class FeatureCollection extends GeoJson { | ||
private final List<Feature> features; | ||
|
||
public FeatureCollection(List<Feature> features) { | ||
super(GeoJsonType.FEATURE_COLLECTION); | ||
this.features = CollectionUtils.emptyIfNull(features).stream().toList(); | ||
} | ||
|
||
@JsonCreator | ||
public static FeatureCollection of(GeoJsonType type, List<Feature> features) { | ||
if (type != GeoJsonType.FEATURE_COLLECTION) { | ||
throw new IllegalArgumentException("Invalid type. expected 'FeatureCollection', but got " + type); | ||
} | ||
return new FeatureCollection(features); | ||
} | ||
|
||
public static FeatureCollection of(List<Feature> features) { | ||
return of(GeoJsonType.FEATURE_COLLECTION, features); | ||
} | ||
|
||
public static FeatureCollection of(Feature... features) { | ||
return of(List.of(features)); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/com/github/nramc/dev/journey/api/geojson/FeatureCollectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 FeatureCollectionTest { | ||
@Autowired | ||
private JacksonTester<FeatureCollection> jacksonTester; | ||
|
||
@Test | ||
void deserialization() throws IOException { | ||
FeatureCollection object = jacksonTester.parseObject(Files.readString(Path.of("src/test/resources/data/feature-collection.json"))); | ||
assertThat(object).isNotNull() | ||
.satisfies(featureCollection -> assertThat(featureCollection.getType()).isEqualTo(GeoJsonType.Constants.FEATURE_COLLECTION_VALUE)) | ||
.extracting(FeatureCollection::getFeatures) | ||
.asList().isNotEmpty() | ||
.hasSize(3); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"id": "ID_001", | ||
"type": "Feature", | ||
"properties": { | ||
"name": "Olympic Park", | ||
"size": "85 hectares" | ||
}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [100.0, 0.0] | ||
} | ||
}, | ||
{ | ||
"id": "ID_002", | ||
"type": "Feature", | ||
"properties": { | ||
"name": "English garden", | ||
"size": "384 hectares" | ||
}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[101.0, 0.0], | ||
[102.0, 1.0] | ||
] | ||
} | ||
}, | ||
{ | ||
"id": "ID_003", | ||
"type": "Feature", | ||
"properties": { | ||
"name": "Hirschgarten", | ||
"size": "40 hectares" | ||
}, | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[ | ||
[100.0, 0.0], | ||
[101.0, 0.0], | ||
[101.0, 1.0], | ||
[100.0, 1.0], | ||
[100.0, 0.0] | ||
] | ||
] | ||
} | ||
} | ||
] | ||
} |