Skip to content

Commit

Permalink
initial additions
Browse files Browse the repository at this point in the history
  • Loading branch information
langsmith committed Apr 19, 2019
1 parent 9c390ff commit c95c4c1
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/turf-port.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Below's an on going list of the Turf functions which currently exist inside the

## Feature Conversion
- [ ] turf-combine
- [ ] turf-explode
- [x] turf-explode
- [ ] turf-flatten
- [ ] turf-line-to-polygon
- [ ] turf-polygonize
Expand Down
92 changes: 92 additions & 0 deletions services-turf/src/main/java/com/mapbox/turf/TurfConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Geometry;
import com.mapbox.geojson.GeometryCollection;
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.MultiLineString;
import com.mapbox.geojson.MultiPoint;
import com.mapbox.geojson.MultiPolygon;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.turf.TurfConstants.TurfUnitCriteria;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -163,4 +176,83 @@ public static double convertLength(@FloatRange(from = 0) double distance,
}
return radiansToLength(lengthToRadians(distance, originalUnit), finalUnit);
}

/**
* Takes a {@link FeatureCollection} and
* returns all positions as {@link Point} objects.
*
* @param featureCollection a {@link FeatureCollection} object
* @return a new {@link FeatureCollection} object with {@link Point} objects
* @since 4.7.0
*/
public static FeatureCollection explode(@NonNull FeatureCollection featureCollection) {
List<Feature> featuresForFinalFeatureCollection = new ArrayList<>();
for (Feature singleFeature : featureCollection.features()) {
for (int i = 0; i < explode(singleFeature.geometry()).features().size(); i++) {
featuresForFinalFeatureCollection.add(explode(singleFeature.geometry()).features().get(i));
}
}
return FeatureCollection.fromFeatures(featuresForFinalFeatureCollection);
}

/**
* Takes a {@link Feature} and
* returns its position as a {@link Point} objects.
*
* @param feature a {@link Feature} object
* @return a new {@link FeatureCollection} object with {@link Point} objects
* @since 4.7.0
*/
public static FeatureCollection explode(@NonNull Feature feature) {
return explode(feature.geometry());
}

/**
* Takes a {@link Geometry} object, checks what type of geometry it is,
* and then returns all positions as {@link Point} objects.
*
* @param geometry a {@link Geometry} object
* @return a new {@link FeatureCollection} object with {@link Point} objects
* @since 4.7.0
*/
private static FeatureCollection explode(@NonNull Geometry geometry) {
List<Feature> featuresForFinalFeatureCollection = new ArrayList<>();
if (geometry instanceof Point) {
featuresForFinalFeatureCollection.add(Feature.fromGeometry(geometry));
} else if (geometry instanceof MultiPoint) {
for (Point singlePoint : TurfMeta.coordAll((MultiPoint) geometry)) {
featuresForFinalFeatureCollection.add(Feature.fromGeometry(singlePoint));
}
} else if (geometry instanceof LineString) {
for (Point singlePoint : TurfMeta.coordAll((LineString) geometry)) {
featuresForFinalFeatureCollection.add(Feature.fromGeometry(singlePoint));
}
} else if (geometry instanceof MultiLineString) {
for (Point singlePoint : TurfMeta.coordAll((MultiLineString) geometry)) {
featuresForFinalFeatureCollection.add(Feature.fromGeometry(singlePoint));
}
} else if (geometry instanceof Polygon) {
for (Point singlePoint : TurfMeta.coordAll((Polygon) geometry, true)) {
featuresForFinalFeatureCollection.add(Feature.fromGeometry(singlePoint));
}
} else if (geometry instanceof MultiPolygon) {
List<Point> resultCoords = TurfMeta.coordAll((MultiPolygon) geometry, true);
for (Point singlePoint : resultCoords) {
featuresForFinalFeatureCollection.add(Feature.fromGeometry(singlePoint));
}
} else if (geometry instanceof GeometryCollection) {
// recursive
for (Geometry singleGeometry : ((GeometryCollection) geometry).geometries()) {
if (explode(singleGeometry).features() != null
&& explode(singleGeometry).features().size() > 0) {
for (Feature singleFeature : explode(singleGeometry).features()) {
featuresForFinalFeatureCollection.add(singleFeature);
}
}
}
} else {
throw new RuntimeException(("Unknown geometry class: " + geometry.getClass()));
}
return FeatureCollection.fromFeatures(featuresForFinalFeatureCollection);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package com.mapbox.turf;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.GeometryCollection;
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.MultiLineString;
import com.mapbox.geojson.MultiPoint;
import com.mapbox.geojson.MultiPolygon;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class TurfConversionTest extends TestUtils {

private static final String TURF_EXPLODE_POINT = "turf-explode/point.geojson";
private static final String TURF_EXPLODE_MULTI_POINT = "turf-explode/multipoint.geojson";
private static final String TURF_EXPLODE_LINESTRING = "turf-explode/linestring.geojson";
private static final String TURF_EXPLODE_POLYGON = "turf-explode/polygon.geojson";
private static final String TURF_EXPLODE_MULTILINESTRING = "turf-explode/multilinestring.geojson";
private static final String TURF_EXPLODE_MULTIPOLYGON = "turf-explode/multipolygon.geojson";
private static final String TURF_EXPLODE_GEOMETRY_COLLECTION = "turf-explode/geometrycollection.geojson";

@Rule
public ExpectedException thrown = ExpectedException.none();

Expand Down Expand Up @@ -58,4 +79,60 @@ public void convertDistance() throws TurfException {
TurfConversion.convertLength(1, TurfConstants.UNIT_METERS,
TurfConstants.UNIT_CENTIMETERS), DELTA);
}

@Test
public void explodePointSingleFeature() throws IOException, NullPointerException {
Point point = Point.fromLngLat(102, 0.5);
assertEquals(1, TurfConversion.explode(Feature.fromGeometry(point)).features().size());
}

@Test
public void explodeMultiPointSingleFeature() throws IOException, NullPointerException {
MultiPoint multiPoint = MultiPoint.fromJson(loadJsonFixture(TURF_EXPLODE_MULTI_POINT));
assertEquals(4, TurfConversion.explode(Feature.fromGeometry(multiPoint)).features().size());
}

@Test
public void explodeLineStringSingleFeature() throws IOException, NullPointerException {
LineString lineString = LineString.fromJson(loadJsonFixture(TURF_EXPLODE_LINESTRING));
assertEquals(4, TurfConversion.explode(Feature.fromGeometry(lineString)).features().size());
}

@Test
public void explodePolygonSingleFeature() throws IOException, NullPointerException {
Polygon polygon = Polygon.fromLngLats(Arrays.asList(
Arrays.asList(
Point.fromLngLat(0, 101),
Point.fromLngLat(1, 101),
Point.fromLngLat(1, 100),
Point.fromLngLat(0, 100))));
assertEquals(3, TurfConversion.explode(Feature.fromGeometry(polygon)).features().size());
}

@Test
public void explodeMultiLineStringSingleFeature() throws IOException, NullPointerException {
MultiLineString multiLineString = MultiLineString.fromJson(loadJsonFixture(TURF_EXPLODE_MULTILINESTRING));
assertEquals(4, TurfConversion.explode(Feature.fromGeometry(multiLineString)).features().size());
}

@Test
public void explodeMultiPolygonSingleFeature() throws IOException, NullPointerException {
MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_EXPLODE_MULTIPOLYGON));
assertEquals(12, TurfConversion.explode(Feature.fromGeometry(multiPolygon)).features().size());
}

@Test
public void explodeGeometryCollectionSingleFeature() throws IOException, NullPointerException {
GeometryCollection geometryCollection = GeometryCollection.fromJson(loadJsonFixture(TURF_EXPLODE_GEOMETRY_COLLECTION));
assertEquals(3, TurfConversion.explode(Feature.fromGeometry(geometryCollection)).features().size());
}

@Test
public void explodeFeatureCollection() throws IOException, NullPointerException {
FeatureCollection featureCollection = FeatureCollection.fromFeatures(new Feature[] {
Feature.fromGeometry(MultiLineString.fromJson(loadJsonFixture(TURF_EXPLODE_MULTILINESTRING))),
Feature.fromGeometry(MultiPolygon.fromJson(loadJsonFixture(TURF_EXPLODE_MULTIPOLYGON)))
});
assertEquals(16, TurfConversion.explode(featureCollection).features().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
102,
0.5
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[
102,
-10
],
[
103,
1
],
[
104,
0
],
[
130,
4
]
]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
20,
0
],
[
101,
0
],
[
101,
1
],
[
100,
1
],
[
100,
0
]
]
]
},
"properties": {}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [100.0, 0.0]
},
{
"type": "LineString",
"coordinates": [
[101.0, 0.0], [102.0, 1.0]
]
}
]
}
21 changes: 21 additions & 0 deletions services-turf/src/test/resources/turf-explode/linestring.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "LineString",
"coordinates": [
[
102,
-10
],
[
103,
1
],
[
104,
0
],
[
130,
4
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "MultiLineString",
"coordinates": [
[
[
100,
0
],
[
101,
1
]
],
[
[
102,
2
],
[
103,
3
]
]
]
}
21 changes: 21 additions & 0 deletions services-turf/src/test/resources/turf-explode/multipoint.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "MultiPoint",
"coordinates": [
[
102,
-10
],
[
103,
1
],
[
104,
0
],
[
130,
4
]
]
}
Loading

0 comments on commit c95c4c1

Please sign in to comment.