Skip to content

Commit 86b0594

Browse files
author
Langston Smith
authored
Adding explode turf method (#1007)
1 parent 02ff671 commit 86b0594

12 files changed

Lines changed: 386 additions & 1 deletion

File tree

docs/turf-port.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Below's an on going list of the Turf functions which currently exist inside the
5959

6060
## Feature Conversion
6161
- [ ] turf-combine
62-
- [ ] turf-explode
62+
- [x] turf-explode
6363
- [ ] turf-flatten
6464
- [ ] turf-line-to-polygon
6565
- [ ] turf-polygonize

services-turf/src/main/java/com/mapbox/turf/TurfConversion.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
import android.support.annotation.FloatRange;
44
import android.support.annotation.NonNull;
55
import android.support.annotation.Nullable;
6+
7+
import com.mapbox.geojson.Feature;
8+
import com.mapbox.geojson.FeatureCollection;
9+
import com.mapbox.geojson.Point;
610
import com.mapbox.turf.TurfConstants.TurfUnitCriteria;
711

12+
import java.util.ArrayList;
813
import java.util.HashMap;
14+
import java.util.List;
915
import java.util.Map;
1016

1117
/**
@@ -163,4 +169,36 @@ public static double convertLength(@FloatRange(from = 0) double distance,
163169
}
164170
return radiansToLength(lengthToRadians(distance, originalUnit), finalUnit);
165171
}
172+
173+
/**
174+
* Takes a {@link FeatureCollection} and
175+
* returns all positions as {@link Point} objects.
176+
*
177+
* @param featureCollection a {@link FeatureCollection} object
178+
* @return a new {@link FeatureCollection} object with {@link Point} objects
179+
* @since 4.8.0
180+
*/
181+
public static FeatureCollection explode(@NonNull FeatureCollection featureCollection) {
182+
List<Feature> finalFeatureList = new ArrayList<>();
183+
for (Point singlePoint : TurfMeta.coordAll(featureCollection, true)) {
184+
finalFeatureList.add(Feature.fromGeometry(singlePoint));
185+
}
186+
return FeatureCollection.fromFeatures(finalFeatureList);
187+
}
188+
189+
/**
190+
* Takes a {@link Feature} and
191+
* returns its position as a {@link Point} objects.
192+
*
193+
* @param feature a {@link Feature} object
194+
* @return a new {@link FeatureCollection} object with {@link Point} objects
195+
* @since 4.8.0
196+
*/
197+
public static FeatureCollection explode(@NonNull Feature feature) {
198+
List<Feature> finalFeatureList = new ArrayList<>();
199+
for (Point singlePoint : TurfMeta.coordAll(feature, true)) {
200+
finalFeatureList.add(Feature.fromGeometry(singlePoint));
201+
}
202+
return FeatureCollection.fromFeatures(finalFeatureList);
203+
}
166204
}

services-turf/src/test/java/com/mapbox/turf/TurfConversionTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
package com.mapbox.turf;
22

3+
import com.mapbox.geojson.Feature;
4+
import com.mapbox.geojson.FeatureCollection;
5+
import com.mapbox.geojson.GeometryCollection;
6+
import com.mapbox.geojson.LineString;
7+
import com.mapbox.geojson.MultiLineString;
8+
import com.mapbox.geojson.MultiPoint;
9+
import com.mapbox.geojson.MultiPolygon;
10+
import com.mapbox.geojson.Point;
11+
import com.mapbox.geojson.Polygon;
12+
313
import org.junit.Rule;
414
import org.junit.Test;
515
import org.junit.rules.ExpectedException;
616

17+
import java.io.IOException;
18+
import java.util.Arrays;
19+
720
import static org.junit.Assert.assertEquals;
821

922
public class TurfConversionTest extends TestUtils {
1023

24+
private static final String TURF_EXPLODE_MULTI_POINT = "turf-explode/multipoint.geojson";
25+
private static final String TURF_EXPLODE_LINESTRING = "turf-explode/linestring.geojson";
26+
private static final String TURF_EXPLODE_MULTILINESTRING = "turf-explode/multilinestring.geojson";
27+
private static final String TURF_EXPLODE_MULTIPOLYGON = "turf-explode/multipolygon.geojson";
28+
private static final String TURF_EXPLODE_GEOMETRY_COLLECTION = "turf-explode/geometrycollection.geojson";
29+
1130
@Rule
1231
public ExpectedException thrown = ExpectedException.none();
1332

@@ -58,4 +77,60 @@ public void convertDistance() throws TurfException {
5877
TurfConversion.convertLength(1, TurfConstants.UNIT_METERS,
5978
TurfConstants.UNIT_CENTIMETERS), DELTA);
6079
}
80+
81+
@Test
82+
public void explodePointSingleFeature() throws IOException, NullPointerException {
83+
Point point = Point.fromLngLat(102, 0.5);
84+
assertEquals(1, TurfConversion.explode(Feature.fromGeometry(point)).features().size());
85+
}
86+
87+
@Test
88+
public void explodeMultiPointSingleFeature() throws IOException, NullPointerException {
89+
MultiPoint multiPoint = MultiPoint.fromJson(loadJsonFixture(TURF_EXPLODE_MULTI_POINT));
90+
assertEquals(4, TurfConversion.explode(Feature.fromGeometry(multiPoint)).features().size());
91+
}
92+
93+
@Test
94+
public void explodeLineStringSingleFeature() throws IOException, NullPointerException {
95+
LineString lineString = LineString.fromJson(loadJsonFixture(TURF_EXPLODE_LINESTRING));
96+
assertEquals(4, TurfConversion.explode(Feature.fromGeometry(lineString)).features().size());
97+
}
98+
99+
@Test
100+
public void explodePolygonSingleFeature() throws IOException, NullPointerException {
101+
Polygon polygon = Polygon.fromLngLats(Arrays.asList(
102+
Arrays.asList(
103+
Point.fromLngLat(0, 101),
104+
Point.fromLngLat(1, 101),
105+
Point.fromLngLat(1, 100),
106+
Point.fromLngLat(0, 100))));
107+
assertEquals(3, TurfConversion.explode(Feature.fromGeometry(polygon)).features().size());
108+
}
109+
110+
@Test
111+
public void explodeMultiLineStringSingleFeature() throws IOException, NullPointerException {
112+
MultiLineString multiLineString = MultiLineString.fromJson(loadJsonFixture(TURF_EXPLODE_MULTILINESTRING));
113+
assertEquals(4, TurfConversion.explode(Feature.fromGeometry(multiLineString)).features().size());
114+
}
115+
116+
@Test
117+
public void explodeMultiPolygonSingleFeature() throws IOException, NullPointerException {
118+
MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_EXPLODE_MULTIPOLYGON));
119+
assertEquals(12, TurfConversion.explode(Feature.fromGeometry(multiPolygon)).features().size());
120+
}
121+
122+
@Test
123+
public void explodeGeometryCollectionSingleFeature() throws IOException, NullPointerException {
124+
GeometryCollection geometryCollection = GeometryCollection.fromJson(loadJsonFixture(TURF_EXPLODE_GEOMETRY_COLLECTION));
125+
assertEquals(3, TurfConversion.explode(Feature.fromGeometry(geometryCollection)).features().size());
126+
}
127+
128+
@Test
129+
public void explodeFeatureCollection() throws IOException, NullPointerException {
130+
FeatureCollection featureCollection = FeatureCollection.fromFeatures(new Feature[] {
131+
Feature.fromGeometry(MultiLineString.fromJson(loadJsonFixture(TURF_EXPLODE_MULTILINESTRING))),
132+
Feature.fromGeometry(MultiPolygon.fromJson(loadJsonFixture(TURF_EXPLODE_MULTIPOLYGON)))
133+
});
134+
assertEquals(16, TurfConversion.explode(featureCollection).features().size());
135+
}
61136
}

services-turf/src/test/resources/turf-bbox/point.geojson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"type": "Feature",
3+
"properties": {},
34
"geometry": {
45
"type": "Point",
56
"coordinates": [
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
},
15+
{
16+
"type": "Feature",
17+
"geometry": {
18+
"type": "LineString",
19+
"coordinates": [
20+
[
21+
102,
22+
-10
23+
],
24+
[
25+
103,
26+
1
27+
],
28+
[
29+
104,
30+
0
31+
],
32+
[
33+
130,
34+
4
35+
]
36+
]
37+
},
38+
"properties": {}
39+
},
40+
{
41+
"type": "Feature",
42+
"geometry": {
43+
"type": "Polygon",
44+
"coordinates": [
45+
[
46+
[
47+
20,
48+
0
49+
],
50+
[
51+
101,
52+
0
53+
],
54+
[
55+
101,
56+
1
57+
],
58+
[
59+
100,
60+
1
61+
],
62+
[
63+
100,
64+
0
65+
]
66+
]
67+
]
68+
},
69+
"properties": {}
70+
}
71+
]
72+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"type": "GeometryCollection",
3+
"geometries": [
4+
{
5+
"type": "Point",
6+
"coordinates": [100.0, 0.0]
7+
},
8+
{
9+
"type": "LineString",
10+
"coordinates": [
11+
[101.0, 0.0], [102.0, 1.0]
12+
]
13+
}
14+
]
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "LineString",
3+
"coordinates": [
4+
[
5+
102,
6+
-10
7+
],
8+
[
9+
103,
10+
1
11+
],
12+
[
13+
104,
14+
0
15+
],
16+
[
17+
130,
18+
4
19+
]
20+
]
21+
}
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "MultiPoint",
3+
"coordinates": [
4+
[
5+
102,
6+
-10
7+
],
8+
[
9+
103,
10+
1
11+
],
12+
[
13+
104,
14+
0
15+
],
16+
[
17+
130,
18+
4
19+
]
20+
]
21+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"type": "MultiPolygon",
3+
"coordinates": [
4+
[
5+
[
6+
[
7+
102,
8+
2
9+
],
10+
[
11+
103,
12+
2
13+
],
14+
[
15+
103,
16+
3
17+
],
18+
[
19+
102,
20+
3
21+
],
22+
[
23+
102,
24+
2
25+
]
26+
]
27+
],
28+
[
29+
[
30+
[
31+
100,
32+
0
33+
],
34+
[
35+
101,
36+
0
37+
],
38+
[
39+
101,
40+
1
41+
],
42+
[
43+
100,
44+
1
45+
],
46+
[
47+
100,
48+
0
49+
]
50+
],
51+
[
52+
[
53+
100.2,
54+
0.2
55+
],
56+
[
57+
100.8,
58+
0.2
59+
],
60+
[
61+
100.8,
62+
0.8
63+
],
64+
[
65+
100.2,
66+
0.8
67+
],
68+
[
69+
100.2,
70+
0.2
71+
]
72+
]
73+
]
74+
]
75+
}

0 commit comments

Comments
 (0)