Skip to content

Commit

Permalink
[feat] #7 geojson data modeling added for FeatureCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramachandran Nellaiyappan committed Mar 21, 2024
1 parent 21b04e3 commit 4735067
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
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));
}

}
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);
}

}
52 changes: 52 additions & 0 deletions src/test/resources/data/feature-collection.json
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]
]
]
}
}
]
}

0 comments on commit 4735067

Please sign in to comment.