Skip to content

Commit 034efb7

Browse files
author
Cameron Mace
authored
Round position to 7 digits (#259)
* round position to 7 digits * moved rounding to gson serializing, added test, and moved all geojson test * removed old implementation * fixed checkstyle running on BuildConfig files * fixed tidy fixtures * fixed rounding issue * removed unused import
1 parent 59e826e commit 034efb7

30 files changed

Lines changed: 2332 additions & 112 deletions

mapbox/gradle-checkstyle.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ task checkstyle(type: Checkstyle) {
1212
source 'src'
1313
include '**/*.java'
1414
exclude '**/gen/**'
15+
exclude '**/BuildConfig.java'
1516
classpath = files()
1617
ignoreFailures = false
1718
}

mapbox/libjava-core/src/main/java/com/mapbox/services/commons/models/Position.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public double getAltitude() {
6464
*/
6565
public double[] getCoordinates() {
6666
if (hasAltitude()) {
67-
return new double[]{getLongitude(), getLatitude(), getAltitude()};
67+
return new double[] {getLongitude(), getLatitude(), getAltitude()};
6868
} else {
69-
return new double[]{getLongitude(), getLatitude()};
69+
return new double[] {getLongitude(), getLatitude()};
7070
}
7171
}
7272

mapbox/libjava-geojson/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ dependencies {
99

1010
// External dependencies
1111
compile rootProject.ext.dep.gson
12+
13+
// Testing
14+
testCompile rootProject.ext.dep.okhttp3Mockwebserver
15+
testCompile rootProject.ext.dep.junit
16+
testCompile rootProject.ext.dep.hamcrestJunit
1217
}
1318

1419
apply from: 'gradle-javadoc.gradle'

mapbox/libjava-geojson/src/main/java/com/mapbox/services/commons/geojson/custom/PositionSerializer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.mapbox.services.commons.models.Position;
99

1010
import java.lang.reflect.Type;
11+
import java.math.BigDecimal;
12+
import java.math.RoundingMode;
1113

1214
/**
1315
* Required to handle the special case where the altitude might be a Double.NaN, which isn't a valid
@@ -33,8 +35,15 @@ public class PositionSerializer implements JsonSerializer<Position> {
3335
@Override
3436
public JsonElement serialize(Position src, Type typeOfSrc, JsonSerializationContext context) {
3537
JsonArray rawCoordinates = new JsonArray();
36-
rawCoordinates.add(new JsonPrimitive(src.getLongitude()));
37-
rawCoordinates.add(new JsonPrimitive(src.getLatitude()));
38+
39+
BigDecimal lat = new BigDecimal(src.getLatitude());
40+
lat = lat.setScale(7, RoundingMode.HALF_UP).stripTrailingZeros();
41+
42+
BigDecimal lon = new BigDecimal(src.getLongitude());
43+
lon = lon.setScale(7, RoundingMode.HALF_UP).stripTrailingZeros();
44+
45+
rawCoordinates.add(new JsonPrimitive(lon));
46+
rawCoordinates.add(new JsonPrimitive(lat));
3847

3948
// Includes altitude
4049
if (src.hasAltitude()) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"type": "Feature",
3+
"geometry": {
4+
"type": "Point",
5+
"coordinates": [
6+
125.6,
7+
10.1
8+
]
9+
},
10+
"properties": {
11+
"name": "Dinagat Islands"
12+
}
13+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{
5+
"type": "Feature",
6+
"geometry": {
7+
"type": "Point",
8+
"coordinates": [
9+
102,
10+
0.5
11+
]
12+
},
13+
"properties": {
14+
"prop0": "value0"
15+
}
16+
},
17+
{
18+
"type": "Feature",
19+
"geometry": {
20+
"type": "LineString",
21+
"coordinates": [
22+
[
23+
102,
24+
0
25+
],
26+
[
27+
103,
28+
1
29+
],
30+
[
31+
104,
32+
0
33+
],
34+
[
35+
105,
36+
1
37+
]
38+
]
39+
},
40+
"properties": {
41+
"prop0": "value0",
42+
"prop1": 0
43+
}
44+
},
45+
{
46+
"type": "Feature",
47+
"geometry": {
48+
"type": "Polygon",
49+
"coordinates": [
50+
[
51+
[
52+
100,
53+
0
54+
],
55+
[
56+
101,
57+
0
58+
],
59+
[
60+
101,
61+
1
62+
],
63+
[
64+
100,
65+
1
66+
],
67+
[
68+
100,
69+
0
70+
]
71+
]
72+
]
73+
},
74+
"properties": {
75+
"prop0": "value0",
76+
"prop1": {
77+
"this": "that"
78+
}
79+
}
80+
}
81+
]
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"features": [
3+
{
4+
"type": "Feature",
5+
"geometry": {
6+
"type": "Point",
7+
"coordinates": [
8+
1.1234568,
9+
1.1234568
10+
]
11+
},
12+
"properties": {}
13+
},
14+
{
15+
"type": "Feature",
16+
"geometry": {
17+
"type": "Point",
18+
"coordinates": [
19+
1.1234,
20+
1.12345
21+
]
22+
},
23+
"properties": {}
24+
},
25+
{
26+
"type": "Feature",
27+
"geometry": {
28+
"type": "Point",
29+
"coordinates": [
30+
1.1234567,
31+
1.1234567
32+
]
33+
},
34+
"properties": {}
35+
},
36+
{
37+
"type": "Feature",
38+
"geometry": {
39+
"type": "Point",
40+
"coordinates": [
41+
1.1234567,
42+
1.1234568
43+
]
44+
},
45+
"properties": {}
46+
},
47+
{
48+
"type": "Feature",
49+
"geometry": {
50+
"type": "Point",
51+
"coordinates": [
52+
1.1234568,
53+
1.1234567
54+
]
55+
},
56+
"properties": {}
57+
},
58+
{
59+
"type": "Feature",
60+
"geometry": {
61+
"type": "Point",
62+
"coordinates": [
63+
105.1234568,
64+
89.1234567
65+
]
66+
},
67+
"properties": {}
68+
},
69+
{
70+
"type": "Feature",
71+
"geometry": {
72+
"type": "Point",
73+
"coordinates": [
74+
-105.1234568,
75+
-89.1234567
76+
]
77+
},
78+
"properties": {}
79+
}
80+
],
81+
"type": "FeatureCollection"
82+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"type": "GeometryCollection",
3+
"geometries": [
4+
{
5+
"type": "Point",
6+
"coordinates": [
7+
100,
8+
0
9+
]
10+
},
11+
{
12+
"type": "LineString",
13+
"coordinates": [
14+
[
15+
101,
16+
0
17+
],
18+
[
19+
102,
20+
1
21+
]
22+
]
23+
}
24+
]
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"type": "LineString",
3+
"coordinates": [
4+
[
5+
100,
6+
0
7+
],
8+
[
9+
101,
10+
1
11+
]
12+
]
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"type": "MultiLineString",
3+
"coordinates": [
4+
[
5+
[
6+
100,
7+
0
8+
],
9+
[
10+
101,
11+
1
12+
]
13+
],
14+
[
15+
[
16+
102,
17+
2
18+
],
19+
[
20+
103,
21+
3
22+
]
23+
]
24+
]
25+
}

0 commit comments

Comments
 (0)