Skip to content

Commit 3da1ad5

Browse files
authored
BoundingBoxSerializer should do unshifting before serialization (#907)
similar to PointSerializer
1 parent bd7b0e5 commit 3da1ad5

3 files changed

Lines changed: 68 additions & 15 deletions

File tree

services-geojson/src/main/java/com/mapbox/geojson/gson/BoundingBoxSerializer.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import com.google.gson.JsonSerializationContext;
77
import com.google.gson.JsonSerializer;
88
import com.mapbox.geojson.BoundingBox;
9+
import com.mapbox.geojson.Point;
10+
import com.mapbox.geojson.shifter.CoordinateShifterManager;
911

1012
import java.lang.reflect.Type;
13+
import java.util.List;
1114

1215
/**
1316
* Serializer used for converting the {@link BoundingBox} object inside a GeoJson object to a JSON
@@ -41,17 +44,24 @@ public JsonElement serialize(BoundingBox src, Type typeOfSrc, JsonSerializationC
4144
JsonArray bbox = new JsonArray();
4245

4346
// Southwest
44-
bbox.add(new JsonPrimitive(src.southwest().longitude()));
45-
bbox.add(new JsonPrimitive(src.southwest().latitude()));
46-
if (src.southwest().hasAltitude()) {
47-
bbox.add(new JsonPrimitive(src.southwest().altitude()));
47+
Point point = src.southwest();
48+
List<Double> unshiftedCoordinates =
49+
CoordinateShifterManager.getCoordinateShifter().unshiftPoint(point);
50+
51+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(0)));
52+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(1)));
53+
if (point.hasAltitude()) {
54+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(2)));
4855
}
4956

5057
// Northeast
51-
bbox.add(new JsonPrimitive(src.northeast().longitude()));
52-
bbox.add(new JsonPrimitive(src.northeast().latitude()));
53-
if (src.southwest().hasAltitude()) {
54-
bbox.add(new JsonPrimitive(src.northeast().altitude()));
58+
point = src.northeast();
59+
unshiftedCoordinates =
60+
CoordinateShifterManager.getCoordinateShifter().unshiftPoint(point);
61+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(0)));
62+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(1)));
63+
if (point.hasAltitude()) {
64+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(2)));
5565
}
5666
return bbox;
5767
}

services-geojson/src/test/java/com/mapbox/geojson/LineStringTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ public void bbox_doesSerializeWhenPresent() throws Exception {
8787
points.add(Point.fromLngLat(3.0, 3.0));
8888
BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0);
8989
LineString lineString = LineString.fromLngLats(points, bbox);
90-
compareJson(lineString.toJson(),
91-
"{\"coordinates\":[[1,1],[2,2],[3,3]],"
92-
+ "\"type\":\"LineString\",\"bbox\":[1.0,2.0,3.0,4.0]}");
90+
String lineStringJson = lineString.toJson();
91+
compareJson("{\"coordinates\":[[1,1],[2,2],[3,3]],"
92+
+ "\"type\":\"LineString\",\"bbox\":[1.0,2.0,3.0,4.0]}",
93+
lineStringJson);
9394
}
9495

9596
@Test

services-geojson/src/test/java/com/mapbox/geojson/shifter/ShifterTest.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.mapbox.geojson.shifter;
22

3+
import com.google.gson.JsonParser;
34
import com.mapbox.geojson.BoundingBox;
5+
import com.mapbox.geojson.LineString;
46
import com.mapbox.geojson.Point;
57

68
import static com.mapbox.core.TestUtils.DELTA;
79
import static org.junit.Assert.assertNotNull;
810
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertThat;
912

13+
import org.hamcrest.Matchers;
1014
import org.junit.Test;
1115

1216
import java.lang.reflect.Array;
17+
import java.util.ArrayList;
1318
import java.util.Arrays;
1419
import java.util.List;
1520

@@ -46,7 +51,7 @@ public List<Double> unshiftPoint(List<Double> coordinates) {
4651
};
4752

4853
@Test
49-
public void basic_shift() throws Exception {
54+
public void point_basic_shift() throws Exception {
5055

5156
Point southwest = Point.fromLngLat(2.0, 2.0);
5257
Point northeast = Point.fromLngLat(4.0, 4.0);
@@ -68,16 +73,27 @@ public void basic_shift() throws Exception {
6873
assertEquals(southwestManualShifted, southwestShifted);
6974
assertEquals(northeastManualShifted, northeastShifted);
7075

76+
CoordinateShifterManager.setCoordinateShifter(null);
77+
}
78+
79+
@Test
80+
public void bbox_basic_shift() throws Exception {
81+
82+
CoordinateShifter shifter = new TestCoordinateShifter();
83+
7184
BoundingBox boundingBoxFromDouble = BoundingBox.fromLngLats(2.0, 2.0, 4.0, 4.0);
72-
BoundingBox boundingBoxFromPoints = BoundingBox.fromPoints(southwestShifted, northeastShifted);
85+
86+
BoundingBox boundingBoxFromPoints =
87+
BoundingBox.fromPoints(Point.fromLngLat(2.0, 2.0),
88+
Point.fromLngLat(4.0, 4.0));
7389

7490
assertEquals(boundingBoxFromDouble, boundingBoxFromPoints);
7591

7692
CoordinateShifterManager.setCoordinateShifter(null);
7793
}
7894

7995
@Test
80-
public void toJson() throws Exception {
96+
public void point_toJson() throws Exception {
8197

8298
// set shifter
8399
CoordinateShifterManager.setCoordinateShifter(new TestCoordinateShifter());
@@ -91,7 +107,7 @@ public void toJson() throws Exception {
91107
}
92108

93109
@Test
94-
public void fromJson() throws Exception {
110+
public void point_fromJson() throws Exception {
95111

96112
// set shifter
97113
CoordinateShifterManager.setCoordinateShifter(new TestCoordinateShifter());
@@ -104,4 +120,30 @@ public void fromJson() throws Exception {
104120

105121
CoordinateShifterManager.setCoordinateShifter(null);
106122
}
123+
124+
@Test
125+
public void linestring_basic_shift_with_bbox() {
126+
// set shifter
127+
CoordinateShifterManager.setCoordinateShifter(new TestCoordinateShifter());
128+
129+
List<Point> points = new ArrayList<>();
130+
points.add(Point.fromLngLat(1.0, 1.0));
131+
points.add(Point.fromLngLat(2.0, 2.0));
132+
points.add(Point.fromLngLat(3.0, 3.0));
133+
BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0);
134+
LineString lineString = LineString.fromLngLats(points, bbox);
135+
136+
String jsonString = lineString.toJson();
137+
compareJson("{\"coordinates\":[[1,1],[2,2],[3,3]],"
138+
+ "\"type\":\"LineString\",\"bbox\":[1.0,2.0,3.0,4.0]}",
139+
jsonString);
140+
141+
CoordinateShifterManager.setCoordinateShifter(null);
142+
}
143+
144+
public void compareJson(String expectedJson, String actualJson) {
145+
JsonParser parser = new JsonParser();
146+
assertThat(parser.parse(actualJson), Matchers.equalTo(parser.parse(expectedJson)));
147+
}
148+
107149
}

0 commit comments

Comments
 (0)