TurfConversion { public static FeatureCollection combine(@NonNull FeatureCollection originalFeatureCollection) { if (originalFeatureCollection.features() == null) { throw new TurfException("Your FeatureCollection is null."); } else if (originalFeatureCollection.features().size() == 0) { throw new TurfException("Your FeatureCollection doesn't have any Feature objects in it."); } List<Point> pointList = new ArrayList<>(0); List<LineString> lineStringList = new ArrayList<>(0); List<Polygon> polygonList = new ArrayList<>(0); for (Feature singleFeature : originalFeatureCollection.features()) { Geometry singleFeatureGeometry = singleFeature.geometry(); if (singleFeatureGeometry instanceof Point || singleFeatureGeometry instanceof MultiPoint) { if (singleFeatureGeometry instanceof Point) { pointList.add((Point) singleFeatureGeometry); } else { pointList.addAll(((MultiPoint) singleFeatureGeometry).coordinates()); } } else if (singleFeatureGeometry instanceof LineString || singleFeatureGeometry instanceof MultiLineString) { if (singleFeatureGeometry instanceof LineString) { lineStringList.add((LineString) singleFeatureGeometry); } else { lineStringList.addAll(((MultiLineString) singleFeatureGeometry).lineStrings()); } } else if (singleFeatureGeometry instanceof Polygon || singleFeatureGeometry instanceof MultiPolygon) { if (singleFeatureGeometry instanceof Polygon) { polygonList.add((Polygon) singleFeatureGeometry); } else { polygonList.addAll(((MultiPolygon) singleFeatureGeometry).polygons()); } } } List<Feature> finalFeatureList = new ArrayList<>(0); if (!pointList.isEmpty()) { finalFeatureList.add(Feature.fromGeometry(MultiPoint.fromLngLats(pointList))); } if (!lineStringList.isEmpty()) { finalFeatureList.add(Feature.fromGeometry(MultiLineString.fromLineStrings(lineStringList))); } if (!polygonList.isEmpty()) { finalFeatureList.add(Feature.fromGeometry(MultiPolygon.fromPolygons(polygonList))); } return finalFeatureList.isEmpty() ? originalFeatureCollection : FeatureCollection.fromFeatures(finalFeatureList); } private  TurfConversion(); static double lengthToDegrees(double distance, @TurfUnitCriteria String units); static double degreesToRadians(double degrees); static double radiansToDegrees(double radians); static double radiansToLength(double radians); static double radiansToLength(double radians, @NonNull @TurfUnitCriteria String units); static double lengthToRadians(double distance); static double lengthToRadians(double distance, @NonNull @TurfUnitCriteria String units); static double convertLength(@FloatRange(from = 0) double distance,
                                     @NonNull @TurfUnitCriteria String originalUnit); static double convertLength(@FloatRange(from = 0) double distance,
                                     @NonNull @TurfUnitCriteria String originalUnit,
                                     @Nullable @TurfUnitCriteria String finalUnit); static FeatureCollection explode(@NonNull FeatureCollection featureCollection); static FeatureCollection explode(@NonNull Feature feature); static Feature polygonToLine(@NonNull Feature feature); static Feature polygonToLine(@NonNull Feature feature, @Nullable JsonObject properties); static Feature polygonToLine(@NonNull Polygon polygon); static FeatureCollection polygonToLine(@NonNull MultiPolygon multiPolygon); static Feature polygonToLine(@NonNull Polygon polygon, @Nullable JsonObject properties); static FeatureCollection polygonToLine(@NonNull MultiPolygon multiPolygon,
                                                @Nullable JsonObject properties); static FeatureCollection multiPolygonToLine(@NonNull Feature feature); static FeatureCollection multiPolygonToLine(@NonNull Feature feature,
                                                     @Nullable JsonObject properties); static FeatureCollection combine(@NonNull FeatureCollection originalFeatureCollection);  }
@Test public void combineTwoLineStringsToMultiLineString() throws Exception { FeatureCollection lineStringFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry(LineString.fromLngLats( Arrays.asList(Point.fromLngLat(-11.25, 55.7765), Point.fromLngLat(41.1328, 22.91792)))), Feature.fromGeometry(LineString.fromLngLats( Arrays.asList(Point.fromLngLat(3.8671, 19.3111), Point.fromLngLat(20.742, -20.3034)))) )); FeatureCollection featureCollectionWithNewMultiLineStringObject = TurfConversion.combine(lineStringFeatureCollection); assertNotNull(featureCollectionWithNewMultiLineStringObject); MultiLineString multiLineString = (MultiLineString) featureCollectionWithNewMultiLineStringObject.features().get(0).geometry(); assertNotNull(multiLineString); assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA); assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA); assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA); assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA); }
@Test public void combineLineStringAndMultiLineStringToMultiLineString() throws Exception { FeatureCollection lineStringFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry(LineString.fromLngLats(Arrays.asList( Point.fromLngLat(-11.25, 55.7765), Point.fromLngLat(41.1328, 22.91792)))), Feature.fromGeometry( MultiLineString.fromLineStrings(Arrays.asList( LineString.fromLngLats(Arrays.asList( Point.fromLngLat(102, -10), Point.fromLngLat(130.0, 4.0) )), LineString.fromLngLats(Arrays.asList( Point.fromLngLat(40.0, -20.0), Point.fromLngLat(150.0, 18.0) )) ))) )); FeatureCollection featureCollectionWithNewMultiLineStringObject = TurfConversion.combine(lineStringFeatureCollection); assertNotNull(featureCollectionWithNewMultiLineStringObject); MultiLineString multiLineString = (MultiLineString) featureCollectionWithNewMultiLineStringObject. features().get(0).geometry(); assertNotNull(multiLineString); assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA); assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA); assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA); assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA); assertEquals(102, multiLineString.coordinates().get(1).get(0).longitude(), DELTA); assertEquals(-10, multiLineString.coordinates().get(1).get(0).latitude(), DELTA); assertEquals(130.0, multiLineString.coordinates().get(1).get(1).longitude(), DELTA); assertEquals(4.0, multiLineString.coordinates().get(1).get(1).latitude(), DELTA); assertEquals(40.0, multiLineString.coordinates().get(2).get(0).longitude(), DELTA); assertEquals(-20.0, multiLineString.coordinates().get(2).get(0).latitude(), DELTA); assertEquals(150.0, multiLineString.coordinates().get(2).get(1).longitude(), DELTA); assertEquals(18.0, multiLineString.coordinates().get(2).get(1).latitude(), DELTA); }
@Test public void combinePolygonToMultiPolygon() throws Exception { FeatureCollection polygonFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry(Polygon.fromLngLats(Arrays.asList( Arrays.asList( Point.fromLngLat(61.938950426660604, 5.9765625), Point.fromLngLat(52.696361078274485, 33.046875), Point.fromLngLat(69.90011762668541, 28.828124999999996), Point.fromLngLat(61.938950426660604, 5.9765625))))), Feature.fromGeometry(Polygon.fromLngLats(Arrays.asList( Arrays.asList( Point.fromLngLat(11.42578125, 16.636191878397664), Point.fromLngLat(7.91015625, -9.102096738726443), Point.fromLngLat(31.113281249999996, 17.644022027872726), Point.fromLngLat(11.42578125, 16.636191878397664) )))) )); FeatureCollection featureCollectionWithNewMultiPolygonObject = TurfConversion.combine(polygonFeatureCollection); assertNotNull(featureCollectionWithNewMultiPolygonObject); MultiPolygon multiPolygon = (MultiPolygon) featureCollectionWithNewMultiPolygonObject.features().get(0).geometry(); assertNotNull(multiPolygon); assertEquals(61.938950426660604, multiPolygon.coordinates().get(0).get(0).get(0).longitude(), DELTA); assertEquals(5.9765625, multiPolygon.coordinates().get(0).get(0).get(0).latitude(), DELTA); assertEquals(52.696361078274485, multiPolygon.coordinates().get(0).get(0).get(1).longitude(), DELTA); assertEquals(33.046875, multiPolygon.coordinates().get(0).get(0).get(1).latitude(), DELTA); assertEquals(11.42578125, multiPolygon.coordinates().get(1).get(0).get(0).longitude(), DELTA); assertEquals(16.636191878397664, multiPolygon.coordinates().get(1).get(0).get(0).latitude(), DELTA); assertEquals(7.91015625, multiPolygon.coordinates().get(1).get(0).get(1).longitude(), DELTA); assertEquals(-9.102096738726443, multiPolygon.coordinates().get(1).get(0).get(1).latitude(), DELTA); }
@Test public void combinePolygonAndMultiPolygonToMultiPolygon() throws Exception { FeatureCollection polygonFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry( Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(61.938950426660604, 5.9765625), Point.fromLngLat(52.696361078274485, 33.046875), Point.fromLngLat(69.90011762668541, 28.828124999999996), Point.fromLngLat(61.938950426660604, 5.9765625))))), Feature.fromGeometry(MultiPolygon.fromPolygons(Arrays.asList( Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(11.42578125, 16.636191878397664), Point.fromLngLat(7.91015625, -9.102096738726443), Point.fromLngLat(31.113281249999996, 17.644022027872726), Point.fromLngLat(11.42578125, 16.636191878397664) ))), Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(30.0, 0.0), Point.fromLngLat(102.0, 0.0), Point.fromLngLat(103.0, 1.0), Point.fromLngLat(30.0, 0.0) ))) ))) )); FeatureCollection combinedFeatureCollection = TurfConversion.combine(polygonFeatureCollection); assertNotNull(combinedFeatureCollection); MultiPolygon multiPolygon = (MultiPolygon) combinedFeatureCollection.features().get(0).geometry(); assertNotNull(multiPolygon); assertEquals(61.938950426660604, multiPolygon.coordinates().get(0).get(0).get(0).longitude(), DELTA); assertEquals(5.9765625, multiPolygon.coordinates().get(0).get(0).get(0).latitude(), DELTA); assertEquals(52.696361078274485, multiPolygon.coordinates().get(0).get(0).get(1).longitude(), DELTA); assertEquals(33.046875, multiPolygon.coordinates().get(0).get(0).get(1).latitude(), DELTA); assertEquals(11.42578125, multiPolygon.coordinates().get(1).get(0).get(0).longitude(), DELTA); assertEquals(16.636191878397664, multiPolygon.coordinates().get(1).get(0).get(0).latitude(), DELTA); assertEquals(7.91015625, multiPolygon.coordinates().get(1).get(0).get(1).longitude(), DELTA); assertEquals(-9.102096738726443, multiPolygon.coordinates().get(1).get(0).get(1).latitude(), DELTA); assertEquals(30.0, multiPolygon.coordinates().get(2).get(0).get(0).longitude(), DELTA); assertEquals(0.0, multiPolygon.coordinates().get(2).get(0).get(0).latitude(), DELTA); assertEquals(102.0, multiPolygon.coordinates().get(2).get(0).get(1).longitude(), DELTA); assertEquals(0.0, multiPolygon.coordinates().get(2).get(0).get(1).latitude(), DELTA); }
@Test public void combinePolygonAndMultiPolygonAndPointToMultiPolygon() throws Exception { FeatureCollection featureCollectionWithPointPolygonAndMultiPolygon = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry( Point.fromLngLat(-2.46, 27.6835)), Feature.fromGeometry( Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(61.938950426660604, 5.9765625), Point.fromLngLat(52.696361078274485, 33.046875), Point.fromLngLat(69.90011762668541, 28.828124999999996), Point.fromLngLat(61.938950426660604, 5.9765625))))), Feature.fromGeometry( MultiPolygon.fromPolygons(Arrays.asList( Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(11.42578125, 16.636191878397664), Point.fromLngLat(7.91015625, -9.102096738726443), Point.fromLngLat(31.113281249999996, 17.644022027872726), Point.fromLngLat(11.42578125, 16.636191878397664) ))), Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(30.0, 0.0), Point.fromLngLat(102.0, 0.0), Point.fromLngLat(103.0, 1.0), Point.fromLngLat(30.0, 0.0) ))) ))) )); FeatureCollection combinedFeatureCollection = TurfConversion.combine(featureCollectionWithPointPolygonAndMultiPolygon); assertNotNull(combinedFeatureCollection); MultiPolygon multiPolygon = null; MultiPoint multiPoint = null; for (int x = 0; x < combinedFeatureCollection.features().size(); x++) { Feature singleFeature = combinedFeatureCollection.features().get(x); if (singleFeature.geometry() instanceof MultiPolygon) { multiPolygon = (MultiPolygon) combinedFeatureCollection.features().get(x).geometry(); } if (singleFeature.geometry() instanceof MultiPoint) { multiPoint = (MultiPoint) combinedFeatureCollection.features().get(x).geometry(); } } assertNotNull(multiPolygon); assertNotNull(multiPoint); assertEquals(61.938950426660604, multiPolygon.coordinates().get(0).get(0).get(0).longitude(), DELTA); assertEquals(5.9765625, multiPolygon.coordinates().get(0).get(0).get(0).latitude(), DELTA); assertEquals(52.696361078274485, multiPolygon.coordinates().get(0).get(0).get(1).longitude(), DELTA); assertEquals(33.046875, multiPolygon.coordinates().get(0).get(0).get(1).latitude(), DELTA); assertEquals(11.42578125, multiPolygon.coordinates().get(1).get(0).get(0).longitude(), DELTA); assertEquals(16.636191878397664, multiPolygon.coordinates().get(1).get(0).get(0).latitude(), DELTA); assertEquals(7.91015625, multiPolygon.coordinates().get(1).get(0).get(1).longitude(), DELTA); assertEquals(-9.102096738726443, multiPolygon.coordinates().get(1).get(0).get(1).latitude(), DELTA); assertEquals(30.0, multiPolygon.coordinates().get(2).get(0).get(0).longitude(), DELTA); assertEquals(0.0, multiPolygon.coordinates().get(2).get(0).get(0).latitude(), DELTA); assertEquals(102.0, multiPolygon.coordinates().get(2).get(0).get(1).longitude(), DELTA); assertEquals(0.0, multiPolygon.coordinates().get(2).get(0).get(1).latitude(), DELTA); }
@Test public void combinePointAndLineStringGeometry() throws Exception { FeatureCollection pointAndLineStringFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)), Feature.fromGeometry( LineString.fromLngLats( Arrays.asList(Point.fromLngLat(-11.25, 55.7765), Point.fromLngLat(41.1328, 22.91792))) ))); FeatureCollection combinedFeatureCollection = TurfConversion.combine(pointAndLineStringFeatureCollection); assertNotNull(combinedFeatureCollection); MultiPoint multiPoint = null; MultiLineString multiLineString = null; for (int x = 0; x < combinedFeatureCollection.features().size(); x++) { Feature singleFeature = combinedFeatureCollection.features().get(x); if (singleFeature.geometry() instanceof MultiPoint) { multiPoint = (MultiPoint) combinedFeatureCollection.features().get(x).geometry(); } if (singleFeature.geometry() instanceof MultiLineString) { multiLineString = (MultiLineString) combinedFeatureCollection.features().get(x).geometry(); } } assertNotNull(multiPoint); assertNotNull(multiLineString); assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA); assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA); assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA); assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA); assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA); assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA); }
@Test public void combinePointAndMultiPolygonAndLineStringGeometry() throws Exception { FeatureCollection pointMultiPolygonAndLineStringFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)), Feature.fromGeometry(MultiPolygon.fromPolygons(Arrays.asList( Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(11.42578125, 16.636191878397664), Point.fromLngLat(7.91015625, -9.102096738726443), Point.fromLngLat(31.113281249999996, 17.644022027872726), Point.fromLngLat(11.42578125, 16.636191878397664) )))))), Feature.fromGeometry(LineString.fromLngLats( Arrays.asList(Point.fromLngLat(-11.25, 55.7765), Point.fromLngLat(41.1328, 22.91792))) ))); FeatureCollection combinedFeatureCollection = TurfConversion.combine(pointMultiPolygonAndLineStringFeatureCollection); assertNotNull(combinedFeatureCollection); MultiPoint multiPoint = null; MultiLineString multiLineString = null; MultiPolygon multiPolygon = null; for (int x = 0; x < combinedFeatureCollection.features().size(); x++) { Feature singleFeature = combinedFeatureCollection.features().get(x); if (singleFeature.geometry() instanceof MultiPoint) { multiPoint = (MultiPoint) combinedFeatureCollection.features().get(x).geometry(); } if (singleFeature.geometry() instanceof MultiLineString) { multiLineString = (MultiLineString) combinedFeatureCollection.features().get(x).geometry(); } if (singleFeature.geometry() instanceof MultiPolygon) { multiPolygon = (MultiPolygon) combinedFeatureCollection.features().get(x).geometry(); } } assertNotNull(multiPoint); assertNotNull(multiLineString); assertNotNull(multiPolygon); assertEquals(11.42578125, multiPolygon.coordinates().get(0).get(0).get(0).longitude(), DELTA); assertEquals(16.636191878397664, multiPolygon.coordinates().get(0).get(0).get(0).latitude(), DELTA); assertEquals(7.91015625, multiPolygon.coordinates().get(0).get(0).get(1).longitude(), DELTA); assertEquals(-9.102096738726443, multiPolygon.coordinates().get(0).get(0).get(1).latitude(), DELTA); assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA); assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA); assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA); assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA); assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA); assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA); }
@Test public void combine_featureCollectionSizeCheck() throws Exception { FeatureCollection pointMultiPolygonAndLineStringFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)), Feature.fromGeometry(MultiPolygon.fromPolygons(Arrays.asList( Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(11.42578125, 16.636191878397664), Point.fromLngLat(7.91015625, -9.102096738726443), Point.fromLngLat(31.113281249999996, 17.644022027872726), Point.fromLngLat(11.42578125, 16.636191878397664) )))))), Feature.fromGeometry(LineString.fromLngLats( Arrays.asList(Point.fromLngLat(-11.25, 55.7765), Point.fromLngLat(41.1328, 22.91792))) ))); FeatureCollection combinedFeatureCollection = TurfConversion.combine(pointMultiPolygonAndLineStringFeatureCollection); assertNotNull(combinedFeatureCollection); assertEquals(3, combinedFeatureCollection.features().size()); }
@Test public void combineEmptyFeatureCollectionThrowsException() throws Exception { thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Your FeatureCollection doesn't have any Feature objects in it.")); TurfConversion.combine(FeatureCollection.fromJson( "{\n" + " \"type\": \"FeatureCollection\",\n" + " \"features\": []\n" + "}" )); }
@Test public void combinePointsToMultiPoint() throws Exception { FeatureCollection pointFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)), Feature.fromGeometry(Point.fromLngLat(41.83, 7.3624)) )); FeatureCollection featureCollectionWithNewMultiPointObject = TurfConversion.combine(pointFeatureCollection); assertNotNull(featureCollectionWithNewMultiPointObject); MultiPoint multiPoint = (MultiPoint) featureCollectionWithNewMultiPointObject.features().get(0).geometry(); assertNotNull(multiPoint); assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA); assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA); assertEquals(41.83, multiPoint.coordinates().get(1).longitude(), DELTA); assertEquals(7.3624, multiPoint.coordinates().get(1).latitude(), DELTA); }
@Test public void combinePointAndMultiPointToMultiPoint() throws Exception { FeatureCollection pointAndMultiPointFeatureCollection = FeatureCollection.fromFeatures( Arrays.asList( Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)), Feature.fromGeometry(MultiPoint.fromLngLats( Arrays.asList(Point.fromLngLat(41.83, 7.3624), Point.fromLngLat(100, 101))) ))); FeatureCollection combinedFeatureCollection = TurfConversion.combine(pointAndMultiPointFeatureCollection); assertNotNull(combinedFeatureCollection); MultiPoint multiPoint = (MultiPoint) combinedFeatureCollection.features().get(0).geometry(); assertNotNull(multiPoint); assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA); assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA); assertEquals(41.83, multiPoint.coordinates().get(1).longitude(), DELTA); assertEquals(7.3624, multiPoint.coordinates().get(1).latitude(), DELTA); assertEquals(100, multiPoint.coordinates().get(2).longitude(), DELTA); assertEquals(101, multiPoint.coordinates().get(2).latitude(), DELTA); }