Skip to content

Commit

Permalink
ses, Add bbox to Feature and FeatureCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebisteiner authored and maesenka committed Aug 12, 2022
1 parent c83e92d commit 2ebfee3
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 7 deletions.
2 changes: 2 additions & 0 deletions geom/src/main/java/org/geolatte/geom/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface Feature< P extends Position, ID> {

Map<String, Object> getProperties();

Box<P> getBbox();

default String getType(){
return TYPE;
}
Expand Down
2 changes: 2 additions & 0 deletions geom/src/main/java/org/geolatte/geom/FeatureCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface FeatureCollection<P extends Position, ID> {

List<Feature<P, ID>> getFeatures();

Box<P> getBbox();

default String getType() {
return TYPE;
}
Expand Down
30 changes: 30 additions & 0 deletions json/src/main/java/org/geolatte/geom/json/BoxSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.geolatte.geom.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.geolatte.geom.Box;
import org.geolatte.geom.Position;

import java.io.IOException;
import java.util.Arrays;

public class BoxSerializer<P extends Position> extends JsonSerializer<Box<P>> {

@Override
public void serialize(Box<P> box, JsonGenerator gen, SerializerProvider serializers) throws IOException {
final double[] bbox = concat(box.lowerLeft().toArray(null), box.upperRight().toArray(null));
gen.writeArray(bbox, 0, bbox.length);
}

@Override
public boolean isEmpty(SerializerProvider provider, Box<P> box) {
return box == null || box.isEmpty();
}

private double[] concat(double[] pos1, double[] pos2) {
double[] result = Arrays.copyOf(pos1, pos1.length + pos2.length);
System.arraycopy(pos2, 0, result, pos1.length, pos2.length);
return result;
}
}
10 changes: 10 additions & 0 deletions json/src/main/java/org/geolatte/geom/json/GeoJsonFeature.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.geolatte.geom.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.geolatte.geom.Box;
import org.geolatte.geom.Feature;
import org.geolatte.geom.Geometry;
import org.geolatte.geom.Position;

import java.util.Map;
import java.util.Objects;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

/**
* Created by Karel Maesen, Geovise BVBA on 13/07/2018.
*/
Expand Down Expand Up @@ -40,6 +44,12 @@ public Map<String, Object> getProperties() {
return properties;
}

@Override
@JsonInclude(NON_EMPTY)
public Box<P> getBbox() {
return geometry != null ? geometry.getBoundingBox() : null;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.geolatte.geom.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.geolatte.geom.Box;
import org.geolatte.geom.Feature;
import org.geolatte.geom.FeatureCollection;
import org.geolatte.geom.Position;
Expand All @@ -9,17 +11,19 @@
import java.util.List;
import java.util.Objects;

public class GeoJsonFeatureCollection<P extends Position, ID> implements FeatureCollection<P, ID> {
private final List<Feature<P,ID>> features;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

public GeoJsonFeatureCollection(List<Feature<P, ID>> features){
public class GeoJsonFeatureCollection<P extends Position, ID> implements FeatureCollection<P, ID> {
private final List<Feature<P, ID>> features;

public GeoJsonFeatureCollection(List<Feature<P, ID>> features) {
this.features = new ArrayList<>();
this.features.addAll(features);
}

//usefull for testing, but unsafe
@SafeVarargs
GeoJsonFeatureCollection(Feature<P, ID> ...features){
GeoJsonFeatureCollection(Feature<P, ID>... features) {
this.features = new ArrayList<>();
Collections.addAll(this.features, features);
}
Expand All @@ -29,6 +33,21 @@ public List<Feature<P, ID>> getFeatures() {
return Collections.unmodifiableList(features);
}

@Override
@JsonInclude(NON_EMPTY)
public Box<P> getBbox() {
Box<P> bbox = null;
for (Feature<P, ID> feature : features) {
if (bbox == null) {
bbox = feature.getBbox();
} else {
bbox = bbox.union(feature.getBbox());
}
}

return bbox;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class GeolatteGeomModule extends SimpleModule {

private GeometrySerializer geometrySerializer;
private CrsSerializer crsSerializer;
private BoxSerializer boxSerializer;

public GeolatteGeomModule() {
this(WGS84);
Expand All @@ -41,6 +42,8 @@ public <P extends Position> GeolatteGeomModule(CoordinateReferenceSystem<P> defa
addSerializer(Geometry.class, geometrySerializer); //use raw to get this compiled
crsSerializer = new CrsSerializer<>(defaultCrs, settings);
addSerializer(CoordinateReferenceSystem.class, crsSerializer);
boxSerializer = new BoxSerializer<>();
addSerializer(Box.class, boxSerializer);
dezers.put(Geometry.class, parser);
dezers.put(Point.class, parser);
dezers.put(LineString.class, parser);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package org.geolatte.geom.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.geolatte.geom.Feature;
import org.geolatte.geom.Geometries;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.geolatte.geom.builder.DSL.g;
import static org.geolatte.geom.builder.DSL.linestring;
import static org.geolatte.geom.builder.DSL.point;
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;
import static org.geolatte.geom.json.GeoJsonStrings.feature;
import static org.geolatte.geom.json.GeoJsonStrings.featureEmptyPolygon;
import static org.geolatte.geom.json.GeoJsonStrings.featureNullGeometry;
import static org.geolatte.geom.json.GeoJsonStrings.featureWithLineString;
import static org.geolatte.geom.json.Setting.SUPPRESS_CRS_SERIALIZATION;
import static org.junit.Assert.assertEquals;

/**
Expand All @@ -20,12 +27,39 @@ public class FeatureSerializationTest extends GeoJsonTest {

@Test
public void testSerialize() throws IOException {

Map<String, Object> map = new HashMap<>();
map.put("a", 1);
Feature<?, ?> f = new GeoJsonFeature<>(point(WGS84, g(1, 2)), "1", map);
String rec = mapper.writeValueAsString(f);
assertEquals(feature, rec) ;
assertEquals(feature, rec);
}

@Test
public void testSerializeLineStringFeature() throws IOException {
ObjectMapper mapper = createMapper(SUPPRESS_CRS_SERIALIZATION, true);
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
Feature<?, ?> f = new GeoJsonFeature<>(linestring(WGS84, g(1, 2), g(3, 4)), "1", map);
String rec = mapper.writeValueAsString(f);
assertEquals(featureWithLineString, rec);
}

@Test
public void testSerializeNullGeometry() throws IOException {
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
Feature<?, ?> f = new GeoJsonFeature<>(null, "1", map);
String rec = mapper.writeValueAsString(f);
assertEquals(featureNullGeometry, rec);
}

@Test
public void testSerializeEmptyPolygon() throws IOException {
ObjectMapper mapper = createMapper(SUPPRESS_CRS_SERIALIZATION, true);
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
Feature<?, ?> f = new GeoJsonFeature<>(Geometries.mkEmptyPolygon(WGS84), "1", map);
String rec = mapper.writeValueAsString(f);
assertEquals(featureEmptyPolygon, rec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public class GeoJsonStrings {

// Features
static String feature = "{\"geometry\":{\"type\":\"Point\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[1.0,2.0]},\"id\":\"1\",\"properties\":{\"a\":1},\"type\":\"Feature\"}";
static String featureWithLineString = "{\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[1.0,2.0],[3.0,4.0]]},\"id\":\"1\",\"properties\":{\"a\":1},\"bbox\":[1.0,2.0,3.0,4.0],\"type\":\"Feature\"}";


static String featureIntId = "{\"type\" : \"Feature\", \"id\": 1, \"geometry\": {\"type\":\"Point\",\"coordinates\":[1.0,2.0]}, " +
"\"properties\": { \"a\" : 1 }}";
static String featureNullGeometry = "{\"geometry\":null,\"id\":\"1\",\"properties\":{\"a\":1},\"type\":\"Feature\"}";
static String featureEmptyPolygon = "{\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[]},\"id\":\"1\",\"properties\":{\"a\":1},\"type\":\"Feature\"}";

// crs
static String crswgs84 = "{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}";
Expand All @@ -68,7 +70,7 @@ public class GeoJsonStrings {
static String crswgs84WithLink = "{\"type\":\"link\",\"properties\":{\"href\":\"http://wwww.opengis.net/def/crs/EPSG/4326\"}}";

//FeatureCollection
static String featureCollection = "{\"features\":[{\"geometry\":{\"type\":\"Point\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[102.0,0.5]},\"id\":\"1\",\"properties\":{\"prop0\":\"value0\"},\"type\":\"Feature\"},{\"geometry\":{\"type\":\"LineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[102.0,0.0],[103.0,1.0],[104.0,0.0],[105.0,1.0]]},\"id\":\"2\",\"properties\":{\"prop1\":0.0,\"prop0\":\"value0\"},\"type\":\"Feature\"}],\"type\":\"FeatureCollection\"}";

static String featureCollection = "{\"features\":[{\"geometry\":{\"type\":\"Point\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[102.0,0.5]},\"id\":\"1\",\"properties\":{\"prop0\":\"value0\"},\"type\":\"Feature\"},{\"geometry\":{\"type\":\"LineString\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}},\"coordinates\":[[102.0,0.0],[103.0,1.0],[104.0,0.0],[105.0,1.0]]},\"id\":\"2\",\"properties\":{\"prop1\":0.0,\"prop0\":\"value0\"},\"bbox\":[102.0,0.0,105.0,1.0],\"type\":\"Feature\"}],\"bbox\":[102.0,0.0,105.0,1.0],\"type\":\"FeatureCollection\"}";
static String emptyFeatureCollection = "{\"features\":[],\"type\":\"FeatureCollection\"}";
}

0 comments on commit 2ebfee3

Please sign in to comment.