Skip to content

Commit d167cc6

Browse files
authored
Adding Shifter implementation (#922)
* Adding Shifter implementation * bumped @SInCE annotations to 4.1.1 * javadoc writing update * checkstyle line spacing fix * updated @SInCE and modified BBox Test
1 parent 6e34494 commit d167cc6

8 files changed

Lines changed: 332 additions & 22 deletions

File tree

services-geojson/src/main/java/com/mapbox/geojson/Point.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import com.mapbox.geojson.gson.BoundingBoxSerializer;
1818
import com.mapbox.geojson.gson.CoordinateTypeAdapter;
1919
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
20+
import com.mapbox.geojson.shifter.CoordinateShifterManager;
2021

2122
import java.io.Serializable;
22-
import java.util.Arrays;
2323
import java.util.List;
2424

2525
/**
@@ -97,7 +97,8 @@ public static Point fromLngLat(
9797
@FloatRange(from = MIN_LONGITUDE, to = MAX_LONGITUDE) double longitude,
9898
@FloatRange(from = MIN_LATITUDE, to = MAX_LATITUDE) double latitude) {
9999

100-
List<Double> coordinates = Arrays.asList(longitude, latitude);
100+
List<Double> coordinates =
101+
CoordinateShifterManager.getCoordinateShifter().shiftLonLat(longitude, latitude);
101102
return new AutoValue_Point(TYPE, null, coordinates);
102103
}
103104

@@ -121,7 +122,8 @@ public static Point fromLngLat(
121122
@FloatRange(from = MIN_LATITUDE, to = MAX_LATITUDE) double latitude,
122123
@Nullable BoundingBox bbox) {
123124

124-
List<Double> coordinates = Arrays.asList(longitude, latitude);
125+
List<Double> coordinates =
126+
CoordinateShifterManager.getCoordinateShifter().shiftLonLat(longitude, latitude);
125127
return new AutoValue_Point(TYPE, bbox, coordinates);
126128
}
127129

@@ -146,9 +148,9 @@ public static Point fromLngLat(
146148
@FloatRange(from = MIN_LATITUDE, to = MAX_LATITUDE) double latitude,
147149
double altitude) {
148150

149-
List<Double> coordinates = Double.isNaN(altitude)
150-
? Arrays.asList(longitude, latitude) :
151-
Arrays.asList(longitude, latitude, altitude);
151+
List<Double> coordinates =
152+
CoordinateShifterManager.getCoordinateShifter().shiftLonLatAlt(longitude, latitude, altitude);
153+
152154
return new AutoValue_Point(TYPE, null, coordinates);
153155
}
154156

@@ -174,9 +176,8 @@ public static Point fromLngLat(
174176
@FloatRange(from = MIN_LATITUDE, to = MAX_LATITUDE) double latitude,
175177
double altitude, @Nullable BoundingBox bbox) {
176178

177-
List<Double> coordinates = Double.isNaN(altitude)
178-
? Arrays.asList(longitude, latitude) :
179-
Arrays.asList(longitude, latitude, altitude);
179+
List<Double> coordinates =
180+
CoordinateShifterManager.getCoordinateShifter().shiftLonLatAlt(longitude, latitude, altitude);
180181
return new AutoValue_Point(TYPE, bbox, coordinates);
181182
}
182183

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import com.google.gson.JsonSerializer;
88
import com.mapbox.geojson.BoundingBox;
99
import com.mapbox.geojson.Point;
10+
import com.mapbox.geojson.shifter.CoordinateShifterManager;
1011

1112
import java.lang.reflect.Type;
13+
import java.util.List;
1214

1315
/**
1416
* Serializer used for converting the {@link BoundingBox} object inside a GeoJson object to a JSON
@@ -43,18 +45,23 @@ public JsonElement serialize(BoundingBox src, Type typeOfSrc, JsonSerializationC
4345

4446
// Southwest
4547
Point point = src.southwest();
46-
bbox.add(new JsonPrimitive(point.longitude()));
47-
bbox.add(new JsonPrimitive(point.latitude()));
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)));
4853
if (point.hasAltitude()) {
49-
bbox.add(new JsonPrimitive(point.altitude()));
54+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(2)));
5055
}
5156

5257
// Northeast
5358
point = src.northeast();
54-
bbox.add(new JsonPrimitive(point.longitude()));
55-
bbox.add(new JsonPrimitive(point.latitude()));
59+
unshiftedCoordinates =
60+
CoordinateShifterManager.getCoordinateShifter().unshiftPoint(point);
61+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(0)));
62+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(1)));
5663
if (point.hasAltitude()) {
57-
bbox.add(new JsonPrimitive(point.altitude()));
64+
bbox.add(new JsonPrimitive(unshiftedCoordinates.get(2)));
5865
}
5966

6067
return bbox;

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.google.gson.stream.JsonReader;
55
import com.google.gson.stream.JsonWriter;
66

7+
import com.mapbox.geojson.shifter.CoordinateShifterManager;
8+
79
import java.io.IOException;
810
import java.math.BigDecimal;
911
import java.math.RoundingMode;
@@ -21,11 +23,15 @@ public void write(JsonWriter out, List<Double> value) throws IOException {
2123

2224
out.beginArray();
2325

24-
BigDecimal lon = BigDecimal.valueOf(value.get(0));
26+
// Unshift coordinates
27+
List<Double> unshiftedCoordinates =
28+
CoordinateShifterManager.getCoordinateShifter().unshiftPoint(value);
29+
30+
BigDecimal lon = BigDecimal.valueOf(unshiftedCoordinates.get(0));
2531
String lonString = lon.setScale(7, RoundingMode.HALF_UP)
2632
.stripTrailingZeros().toPlainString();
2733

28-
BigDecimal lat = BigDecimal.valueOf(value.get(1));
34+
BigDecimal lat = BigDecimal.valueOf(unshiftedCoordinates.get(1));
2935
String latString = lat.setScale(7, RoundingMode.HALF_UP)
3036
.stripTrailingZeros().toPlainString();
3137

@@ -34,7 +40,7 @@ public void write(JsonWriter out, List<Double> value) throws IOException {
3440

3541
// Includes altitude
3642
if (value.size() > 2) {
37-
out.value(value.get(2));
43+
out.value(unshiftedCoordinates.get(2));
3844
}
3945
out.endArray();
4046
}
@@ -48,6 +54,11 @@ public List<Double> read(JsonReader in) throws IOException {
4854
}
4955
in.endArray();
5056

51-
return coordinates;
57+
if (coordinates.size() > 2) {
58+
return CoordinateShifterManager.getCoordinateShifter()
59+
.shiftLonLatAlt(coordinates.get(0), coordinates.get(1), coordinates.get(2));
60+
}
61+
return CoordinateShifterManager.getCoordinateShifter()
62+
.shiftLonLat(coordinates.get(0), coordinates.get(1));
5263
}
5364
}

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

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

1011
import java.lang.reflect.Type;
1112
import java.math.BigDecimal;
1213
import java.math.RoundingMode;
14+
import java.util.List;
1315

1416
/**
1517
* Required to handle the special case where the altitude might be a Double.NaN, which isn't a valid
@@ -44,11 +46,15 @@ public PointSerializer() {
4446
public JsonElement serialize(Point src, Type typeOfSrc, JsonSerializationContext context) {
4547
JsonArray rawCoordinates = new JsonArray();
4648

47-
BigDecimal lon = BigDecimal.valueOf(src.longitude());
49+
// Unshift coordinates
50+
List<Double> unshiftedCoordinates =
51+
CoordinateShifterManager.getCoordinateShifter().unshiftPoint(src);
52+
53+
BigDecimal lon = BigDecimal.valueOf(unshiftedCoordinates.get(0));
4854
String lonString = lon.setScale(7, RoundingMode.HALF_UP)
4955
.stripTrailingZeros().toPlainString();
5056

51-
BigDecimal lat = BigDecimal.valueOf(src.latitude());
57+
BigDecimal lat = BigDecimal.valueOf(unshiftedCoordinates.get(1));
5258
String latString = lat.setScale(7, RoundingMode.HALF_UP)
5359
.stripTrailingZeros().toPlainString();
5460

@@ -57,7 +63,7 @@ public JsonElement serialize(Point src, Type typeOfSrc, JsonSerializationContext
5763

5864
// Includes altitude
5965
if (src.hasAltitude()) {
60-
rawCoordinates.add(new JsonPrimitive(src.altitude()));
66+
rawCoordinates.add(new JsonPrimitive(unshiftedCoordinates.get(2)));
6167
}
6268

6369
return rawCoordinates;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.mapbox.geojson.shifter;
2+
3+
import com.mapbox.geojson.Point;
4+
5+
import java.util.List;
6+
7+
/**
8+
* ShifterManager allows the movement of all Point objects according to a custom algorithm.
9+
* Once set, it will be applied to all Point objects created through this method.
10+
*
11+
* @since 4.2.0
12+
*/
13+
public interface CoordinateShifter {
14+
15+
/**
16+
* Shifted coordinate values according to its algorithm.
17+
*
18+
* @param lon unshifted longitude
19+
* @param lat unshifted latitude
20+
* @return shifted longitude, shifted latitude in the form of a List of Double values
21+
* @since 4.2.0
22+
*/
23+
List<Double> shiftLonLat(double lon, double lat);
24+
25+
/**
26+
* Shifted coordinate values according to its algorithm.
27+
*
28+
* @param lon unshifted longitude
29+
* @param lat unshifted latitude
30+
* @param altitude unshifted altitude
31+
* @return shifted longitude, shifted latitude, shifted altitude in the form of a
32+
* List of Double values
33+
* @since 4.2.0
34+
*/
35+
List<Double> shiftLonLatAlt(double lon, double lat, double altitude);
36+
37+
/**
38+
* Unshifted coordinate values according to its algorithm.
39+
*
40+
* @param shiftedPoint shifted point
41+
* @return unshifted longitude, shifted latitude,
42+
* and altitude (if present) in the form of List of Double
43+
* @since 4.2.0
44+
*/
45+
List<Double> unshiftPoint(Point shiftedPoint);
46+
47+
48+
/**
49+
* Unshifted coordinate values according to its algorithm.
50+
*
51+
* @param shiftedCoordinates shifted point
52+
* @return unshifted longitude, shifted latitude,
53+
* and altitude (if present) in the form of List of Double
54+
* @since 4.2.0
55+
*/
56+
List<Double> unshiftPoint(List<Double> shiftedCoordinates);
57+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.mapbox.geojson.shifter;
2+
3+
import com.mapbox.geojson.Point;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
/**
9+
* CoordinateShifterManager keeps track of currently set CoordinateShifter.
10+
*
11+
* @since 4.2.0
12+
*/
13+
public final class CoordinateShifterManager {
14+
15+
private static final CoordinateShifter DEFAULT = new CoordinateShifter() {
16+
@Override
17+
public List<Double> shiftLonLat(double lon, double lat) {
18+
return Arrays.asList(lon, lat);
19+
}
20+
21+
@Override
22+
public List<Double> shiftLonLatAlt(double lon, double lat, double alt) {
23+
return Double.isNaN(alt)
24+
? Arrays.asList(lon, lat) :
25+
Arrays.asList(lon, lat, alt);
26+
}
27+
28+
@Override
29+
public List<Double> unshiftPoint(Point point) {
30+
return point.coordinates();
31+
}
32+
33+
@Override
34+
public List<Double> unshiftPoint(List<Double> coordinates) {
35+
return coordinates;
36+
}
37+
};
38+
39+
private static volatile CoordinateShifter coordinateShifter = DEFAULT;
40+
41+
/**
42+
* Currently set CoordinateShifterManager.
43+
*
44+
* @return Currently set CoordinateShifterManager
45+
* @since 4.2.0
46+
*/
47+
public static CoordinateShifter getCoordinateShifter() {
48+
return coordinateShifter;
49+
}
50+
51+
/**
52+
* Sets CoordinateShifterManager.
53+
*
54+
* @param coordinateShifter CoordinateShifterManager to be set
55+
* @since 4.2.0
56+
*/
57+
public static void setCoordinateShifter(CoordinateShifter coordinateShifter) {
58+
CoordinateShifterManager.coordinateShifter =
59+
coordinateShifter == null ? DEFAULT : coordinateShifter;
60+
}
61+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Contains Utility for universally applying a shifting algorithm to all Geometry types.
3+
*/
4+
package com.mapbox.geojson.shifter;

0 commit comments

Comments
 (0)