|
10 | 10 | import com.mapbox.geojson.Geometry; |
11 | 11 | import com.mapbox.geojson.LineString; |
12 | 12 | import com.mapbox.geojson.MultiLineString; |
| 13 | +import com.mapbox.geojson.MultiPoint; |
13 | 14 | import com.mapbox.geojson.MultiPolygon; |
14 | 15 | import com.mapbox.geojson.Point; |
15 | 16 | import com.mapbox.geojson.Polygon; |
@@ -346,4 +347,73 @@ private static Feature coordsToLine(@NotNull List<List<Point>> coordinates, |
346 | 347 | } |
347 | 348 | return null; |
348 | 349 | } |
| 350 | + |
| 351 | + /** |
| 352 | + * Combines a FeatureCollection of geometries and returns |
| 353 | + * a {@link FeatureCollection} with "Multi-" geometries in it. |
| 354 | + * If the original FeatureCollection parameter has {@link Point}(s) |
| 355 | + * and/or {@link MultiPoint}s), the returned |
| 356 | + * FeatureCollection will include a {@link MultiPoint} object. |
| 357 | + * |
| 358 | + * If the original FeatureCollection parameter has |
| 359 | + * {@link LineString}(s) and/or {@link MultiLineString}s), the returned |
| 360 | + * FeatureCollection will include a {@link MultiLineString} object. |
| 361 | + * |
| 362 | + * If the original FeatureCollection parameter has |
| 363 | + * {@link Polygon}(s) and/or {@link MultiPolygon}s), the returned |
| 364 | + * FeatureCollection will include a {@link MultiPolygon} object. |
| 365 | + * |
| 366 | + * @param originalFeatureCollection a {@link FeatureCollection} |
| 367 | + * |
| 368 | + * @return a {@link FeatureCollection} with a "Multi-" geometry |
| 369 | + * or "Multi-" geometries. |
| 370 | + * |
| 371 | + * @since 4.10.0 |
| 372 | + **/ |
| 373 | + public static FeatureCollection combine(@NonNull FeatureCollection originalFeatureCollection) { |
| 374 | + if (originalFeatureCollection.features() == null) { |
| 375 | + throw new TurfException("Your FeatureCollection is null."); |
| 376 | + } else if (originalFeatureCollection.features().size() == 0) { |
| 377 | + throw new TurfException("Your FeatureCollection doesn't have any Feature objects in it."); |
| 378 | + } |
| 379 | + List<Point> pointList = new ArrayList<>(0); |
| 380 | + List<LineString> lineStringList = new ArrayList<>(0); |
| 381 | + List<Polygon> polygonList = new ArrayList<>(0); |
| 382 | + for (Feature singleFeature : originalFeatureCollection.features()) { |
| 383 | + Geometry singleFeatureGeometry = singleFeature.geometry(); |
| 384 | + if (singleFeatureGeometry instanceof Point || singleFeatureGeometry instanceof MultiPoint) { |
| 385 | + if (singleFeatureGeometry instanceof Point) { |
| 386 | + pointList.add((Point) singleFeatureGeometry); |
| 387 | + } else { |
| 388 | + pointList.addAll(((MultiPoint) singleFeatureGeometry).coordinates()); |
| 389 | + } |
| 390 | + } else if (singleFeatureGeometry instanceof LineString || singleFeatureGeometry |
| 391 | + instanceof MultiLineString) { |
| 392 | + if (singleFeatureGeometry instanceof LineString) { |
| 393 | + lineStringList.add((LineString) singleFeatureGeometry); |
| 394 | + } else { |
| 395 | + lineStringList.addAll(((MultiLineString) singleFeatureGeometry).lineStrings()); |
| 396 | + } |
| 397 | + } else if (singleFeatureGeometry instanceof Polygon || singleFeatureGeometry |
| 398 | + instanceof MultiPolygon) { |
| 399 | + if (singleFeatureGeometry instanceof Polygon) { |
| 400 | + polygonList.add((Polygon) singleFeatureGeometry); |
| 401 | + } else { |
| 402 | + polygonList.addAll(((MultiPolygon) singleFeatureGeometry).polygons()); |
| 403 | + } |
| 404 | + } |
| 405 | + } |
| 406 | + List<Feature> finalFeatureList = new ArrayList<>(0); |
| 407 | + if (!pointList.isEmpty()) { |
| 408 | + finalFeatureList.add(Feature.fromGeometry(MultiPoint.fromLngLats(pointList))); |
| 409 | + } |
| 410 | + if (!lineStringList.isEmpty()) { |
| 411 | + finalFeatureList.add(Feature.fromGeometry(MultiLineString.fromLineStrings(lineStringList))); |
| 412 | + } |
| 413 | + if (!polygonList.isEmpty()) { |
| 414 | + finalFeatureList.add(Feature.fromGeometry(MultiPolygon.fromPolygons(polygonList))); |
| 415 | + } |
| 416 | + return finalFeatureList.isEmpty() ? originalFeatureCollection |
| 417 | + : FeatureCollection.fromFeatures(finalFeatureList); |
| 418 | + } |
349 | 419 | } |
0 commit comments