Skip to content

Commit 5c2e4fc

Browse files
authored
add tests to TurfMisc.lineSlice (and fix it) (#106)
1 parent f82402a commit 5c2e4fc

8 files changed

Lines changed: 183 additions & 27 deletions

File tree

libjava/lib/src/main/java/com/mapbox/services/commons/geojson/Feature.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public Geometry getGeometry() {
5858
* @return the properties of this feature
5959
*/
6060
public JsonObject getProperties() {
61+
if (properties == null) properties = new JsonObject();
6162
return properties;
6263
}
6364

@@ -135,7 +136,7 @@ public String toJson() {
135136
* @param value the String value associated with the member
136137
*/
137138
public void addStringProperty(String key, String value) {
138-
this.properties.addProperty(key, value);
139+
getProperties().addProperty(key, value);
139140
}
140141

141142
/**
@@ -144,7 +145,7 @@ public void addStringProperty(String key, String value) {
144145
* @param value the Number value associated with the member
145146
*/
146147
public void addNumberProperty(String key, Number value) {
147-
this.properties.addProperty(key, value);
148+
getProperties().addProperty(key, value);
148149
}
149150

150151
/**
@@ -153,7 +154,7 @@ public void addNumberProperty(String key, Number value) {
153154
* @param value the Boolean value associated with the member
154155
*/
155156
public void addBooleanProperty(String key, Boolean value) {
156-
this.properties.addProperty(key, value);
157+
getProperties().addProperty(key, value);
157158
}
158159

159160
/**
@@ -162,7 +163,7 @@ public void addBooleanProperty(String key, Boolean value) {
162163
* @param value the Character value associated with the member
163164
*/
164165
public void addCharacterProperty(String key, Character value) {
165-
this.properties.addProperty(key, value);
166+
getProperties().addProperty(key, value);
166167
}
167168

168169
/**
@@ -171,7 +172,7 @@ public void addCharacterProperty(String key, Character value) {
171172
* @param value the JsonElement value associated with the member
172173
*/
173174
public void addProperty(String key, JsonElement value) {
174-
this.properties.add(key, value);
175+
getProperties().add(key, value);
175176
}
176177

177178
/**
@@ -180,7 +181,7 @@ public void addProperty(String key, JsonElement value) {
180181
* @return the value of the member, null if it doesn't exist
181182
*/
182183
public String getStringProperty(String key) {
183-
return this.properties.get(key).getAsString();
184+
return getProperties().get(key).getAsString();
184185
}
185186

186187
/**
@@ -189,7 +190,7 @@ public String getStringProperty(String key) {
189190
* @return the value of the member, null if it doesn't exist
190191
*/
191192
public Number getNumberProperty(String key) {
192-
return this.properties.get(key).getAsNumber();
193+
return getProperties().get(key).getAsNumber();
193194
}
194195

195196
/**
@@ -198,7 +199,7 @@ public Number getNumberProperty(String key) {
198199
* @return the value of the member, null if it doesn't exist
199200
*/
200201
public Boolean getBooleanProperty(String key) {
201-
return this.properties.get(key).getAsBoolean();
202+
return getProperties().get(key).getAsBoolean();
202203
}
203204

204205
/**
@@ -207,7 +208,7 @@ public Boolean getBooleanProperty(String key) {
207208
* @return the value of the member, null if it doesn't exist
208209
*/
209210
public Character getCharacterProperty(String key) {
210-
return this.properties.get(key).getAsCharacter();
211+
return getProperties().get(key).getAsCharacter();
211212
}
212213

213214
/**
@@ -216,7 +217,7 @@ public Character getCharacterProperty(String key) {
216217
* @return the value of the member, null if it doesn't exist
217218
*/
218219
public JsonElement getProperty(String key) {
219-
return this.properties.get(key);
220+
return getProperties().get(key);
220221
}
221222

222223
/**
@@ -225,7 +226,7 @@ public JsonElement getProperty(String key) {
225226
* @return
226227
*/
227228
public JsonElement removeProperty(String key) {
228-
return this.properties.remove(key);
229+
return getProperties().remove(key);
229230
}
230231

231232
/**
@@ -234,6 +235,6 @@ public JsonElement removeProperty(String key) {
234235
* @return
235236
*/
236237
public boolean hasProperty(String key) {
237-
return this.properties.has(key);
238+
return getProperties().has(key);
238239
}
239240
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "Feature",
3+
"properties": {},
4+
"geometry": {
5+
"type": "LineString",
6+
"coordinates": [
7+
[
8+
-97.88131713867188,
9+
22.466878364528448
10+
],
11+
[
12+
-97.82089233398438,
13+
22.175960091218524
14+
],
15+
[
16+
-97.6190185546875,
17+
21.8704201873689
18+
]
19+
]
20+
}
21+
}

libjava/lib/src/test/fixtures/turf-line-slice/route1.geojson

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

libjava/lib/src/test/fixtures/turf-line-slice/route2.geojson

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"type": "Feature",
3+
"properties": {},
4+
"geometry": {
5+
"type": "LineString",
6+
"coordinates": [
7+
[
8+
-121.25447809696198,
9+
38.70582415504791
10+
],
11+
[
12+
-121.25449419021606,
13+
38.709767459877554
14+
]
15+
]
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mapbox.services.turf;
2+
3+
import java.io.IOException;
4+
import java.nio.charset.StandardCharsets;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
8+
/**
9+
* Created by antonio on 7/8/16.
10+
*/
11+
public class BaseTurf {
12+
13+
protected final static double DELTA = 1E-10;
14+
15+
protected String loadJsonFixture(String folder, String filename) throws IOException {
16+
byte[] content = Files.readAllBytes(Paths.get("src/test/fixtures/" + folder + "/" + filename));
17+
return new String(content, StandardCharsets.UTF_8);
18+
}
19+
20+
}

libjava/lib/src/test/java/com/mapbox/services/turf/TurfMeasurementTest.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@
2626
/**
2727
* Created by antonio on 5/12/16.
2828
*/
29-
public class TurfMeasurementTest {
30-
31-
private final static double DELTA = 1E-10;
32-
33-
private String loadJsonFixture(String filename) throws IOException {
34-
byte[] content = Files.readAllBytes(Paths.get("src/test/fixtures/turf-line-distance/" + filename));
35-
return new String(content, StandardCharsets.UTF_8);
36-
}
29+
public class TurfMeasurementTest extends BaseTurf {
3730

3831
@Rule
3932
public ExpectedException thrown = ExpectedException.none();
@@ -79,37 +72,37 @@ public void testDistance() throws TurfException {
7972

8073
@Test
8174
public void testLineDistanceLineString() throws IOException, TurfException {
82-
Feature route1 = Feature.fromJson(loadJsonFixture("route1.geojson"));
83-
Feature route2 = Feature.fromJson(loadJsonFixture("route2.geojson"));
75+
Feature route1 = Feature.fromJson(loadJsonFixture("turf-line-distance", "route1.geojson"));
76+
Feature route2 = Feature.fromJson(loadJsonFixture("turf-line-distance", "route2.geojson"));
8477
assertEquals(Math.round(TurfMeasurement.lineDistance(route1, "miles")), 202);
8578
assertTrue((TurfMeasurement.lineDistance(route2, "kilometers") - 742) < 1
8679
&& (TurfMeasurement.lineDistance(route2, "kilometers") - 742) > (-1) );
8780
}
8881

8982
@Test
9083
public void testLineDistanceWithGeometries() throws IOException, TurfException {
91-
Feature route1 = Feature.fromJson(loadJsonFixture("route1.geojson"));
92-
Feature route2 = Feature.fromJson(loadJsonFixture("route2.geojson"));
84+
Feature route1 = Feature.fromJson(loadJsonFixture("turf-line-distance", "route1.geojson"));
85+
Feature route2 = Feature.fromJson(loadJsonFixture("turf-line-distance", "route2.geojson"));
9386
assertEquals(Math.round(TurfMeasurement.lineDistance(route1.getGeometry(), "miles")), 202);
9487
assertTrue((TurfMeasurement.lineDistance(route2.getGeometry(), "kilometers") - 742) < 1
9588
&& (TurfMeasurement.lineDistance(route2.getGeometry(), "kilometers") - 742) > (-1) );
9689
}
9790

9891
@Test
9992
public void testLineDistancePolygon() throws IOException, TurfException {
100-
Feature feat = Feature.fromJson(loadJsonFixture("polygon.geojson"));
93+
Feature feat = Feature.fromJson(loadJsonFixture("turf-line-distance", "polygon.geojson"));
10194
assertEquals(Math.round(1000 * TurfMeasurement.lineDistance(feat, "kilometers")), 5599);
10295
}
10396

10497
@Test
10598
public void testLineDistanceMultiLineString() throws IOException, TurfException {
106-
Feature feat = Feature.fromJson(loadJsonFixture("multilinestring.geojson"));
99+
Feature feat = Feature.fromJson(loadJsonFixture("turf-line-distance", "multilinestring.geojson"));
107100
assertEquals(Math.round(1000 * TurfMeasurement.lineDistance(feat, "kilometers")), 4705);
108101
}
109102

110103
@Test
111104
public void testLineDistanceFeatureCollection() throws IOException, TurfException {
112-
FeatureCollection feat = FeatureCollection.fromJson(loadJsonFixture("featurecollection.geojson"));
105+
FeatureCollection feat = FeatureCollection.fromJson(loadJsonFixture("turf-line-distance", "featurecollection.geojson"));
113106
assertEquals(Math.round(1000 * TurfMeasurement.lineDistance(feat, "kilometers")), 10304);
114107
}
115108

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.mapbox.services.turf;
2+
3+
import com.mapbox.services.commons.geojson.Feature;
4+
import com.mapbox.services.commons.geojson.LineString;
5+
import com.mapbox.services.commons.geojson.Point;
6+
import com.mapbox.services.commons.models.Position;
7+
import com.mapbox.services.commons.turf.TurfException;
8+
import com.mapbox.services.commons.turf.TurfMisc;
9+
10+
import org.junit.Test;
11+
12+
import java.io.IOException;
13+
import java.util.ArrayList;
14+
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertNotEquals;
17+
import static org.junit.Assert.assertNotNull;
18+
19+
/**
20+
* Created by antonio on 7/8/16.
21+
*/
22+
public class TurfMiscTest extends BaseTurf {
23+
24+
@Test
25+
public void testTurfLineSliceLine1() throws IOException, TurfException {
26+
Point start = Point.fromCoordinates(Position.fromCoordinates(-97.79617309570312, 22.254624939561698));
27+
Point stop = Point.fromCoordinates(Position.fromCoordinates(-97.72750854492188, 22.057641623615734));
28+
29+
Feature line1 = Feature.fromJson(loadJsonFixture("turf-line-slice", "line1.geojson"));
30+
31+
LineString sliced = TurfMisc.lineSlice(start, stop, line1);
32+
assertNotNull(sliced);
33+
}
34+
35+
@Test
36+
public void testTurfLineSliceRawGeometry() throws IOException, TurfException {
37+
Point start = Point.fromCoordinates(Position.fromCoordinates(-97.79617309570312,22.254624939561698));
38+
Point stop = Point.fromCoordinates(Position.fromCoordinates(-97.72750854492188,22.057641623615734));
39+
40+
Feature line1 = Feature.fromJson(loadJsonFixture("turf-line-slice", "line1.geojson"));
41+
42+
LineString sliced = TurfMisc.lineSlice(start, stop, (LineString)line1.getGeometry());
43+
assertNotNull(sliced);
44+
}
45+
46+
@Test
47+
public void testTurfLineSliceLine2() throws TurfException {
48+
Point start = Point.fromCoordinates(Position.fromCoordinates(0,0.1));
49+
Point stop = Point.fromCoordinates(Position.fromCoordinates(.9,.8));
50+
51+
ArrayList<Position> coordinates = new ArrayList<>();
52+
coordinates.add(Position.fromCoordinates(0, 0));
53+
coordinates.add(Position.fromCoordinates(1, 1));
54+
LineString line2 = LineString.fromCoordinates(coordinates);
55+
56+
LineString sliced = TurfMisc.lineSlice(start, stop, line2);
57+
assertNotNull(sliced);
58+
}
59+
60+
@Test
61+
public void testTurfLineSliceRoute1() throws IOException, TurfException {
62+
Point start = Point.fromCoordinates(Position.fromCoordinates(-79.0850830078125,37.60117623656667));
63+
Point stop = Point.fromCoordinates(Position.fromCoordinates(-77.7667236328125,38.65119833229951));
64+
65+
Feature route1 = Feature.fromJson(loadJsonFixture("turf-line-slice", "route1.geojson"));
66+
67+
LineString sliced = TurfMisc.lineSlice(start, stop, route1);
68+
assertNotNull(sliced);
69+
}
70+
71+
@Test
72+
public void testTurfLineSliceRoute2() throws IOException, TurfException {
73+
Point start = Point.fromCoordinates(Position.fromCoordinates(-112.60660171508789,45.96021963947196));
74+
Point stop = Point.fromCoordinates(Position.fromCoordinates(-111.97265625,48.84302835299516));
75+
76+
Feature route2 = Feature.fromJson(loadJsonFixture("turf-line-slice", "route2.geojson"));
77+
78+
LineString sliced = TurfMisc.lineSlice(start, stop, route2);
79+
assertNotNull(sliced);
80+
}
81+
82+
@Test
83+
public void testTurfLineSliceVertical() throws IOException, TurfException {
84+
Point start = Point.fromCoordinates(Position.fromCoordinates(-121.25447809696198, 38.70582415504791));
85+
Point stop = Point.fromCoordinates(Position.fromCoordinates(-121.25447809696198, 38.70634324369764));
86+
87+
Feature vertical = Feature.fromJson(loadJsonFixture("turf-line-slice", "vertical.geojson"));
88+
89+
LineString sliced = TurfMisc.lineSlice(start, stop, vertical);
90+
assertNotNull(sliced);
91+
92+
// No duplicated coords
93+
assertEquals(sliced.getCoordinates().size(), 2);
94+
95+
// Vertical slice does not collapse to 1st coord
96+
assertNotEquals(
97+
sliced.getCoordinates().get(0).getCoordinates(),
98+
sliced.getCoordinates().get(1).getCoordinates());
99+
}
100+
}

0 commit comments

Comments
 (0)