| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package org.locationtech.jts.geom.util; |
| 14 |
|
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
import org.locationtech.jts.geom.Geometry; |
| 19 |
import org.locationtech.jts.geom.GeometryCollection; |
| 20 |
import org.locationtech.jts.geom.GeometryFilter; |
| 21 |
import org.locationtech.jts.geom.Polygon; |
| 22 |
|
| 23 |
/** |
| 24 |
* Extracts all the {@link Polygon} elements from a {@link Geometry}. |
| 25 |
* |
| 26 |
* @version 1.7 |
| 27 |
* @see GeometryExtracter |
| 28 |
*/ |
| 29 |
public class PolygonExtracter |
| 30 |
implements GeometryFilter |
| 31 |
{ |
| 32 |
/** |
| 33 |
* Extracts the {@link Polygon} elements from a single {@link Geometry} |
| 34 |
* and adds them to the provided {@link List}. |
| 35 |
* |
| 36 |
* @param geom the geometry from which to extract |
| 37 |
* @param list the list to add the extracted elements to |
| 38 |
*/ |
| 39 |
public static List getPolygons(Geometry geom, List list) |
| 40 |
{ |
| 41 |
if (geom instanceof Polygon) { |
| 42 |
list.add(geom); |
| 43 |
} |
| 44 |
else if (geom instanceof GeometryCollection) { |
| 45 |
geom.apply(new PolygonExtracter(list)); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
return list; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Extracts the {@link Polygon} elements from a single {@link Geometry} |
| 54 |
* and returns them in a {@link List}. |
| 55 |
* |
| 56 |
* @param geom the geometry from which to extract |
| 57 |
*/ |
| 58 |
public static List getPolygons(Geometry geom) |
| 59 |
{ |
| 60 |
return getPolygons(geom, new ArrayList()); |
| 61 |
} |
| 62 |
|
| 63 |
private List comps; |
| 64 |
/** |
| 65 |
* Constructs a PolygonExtracterFilter with a list in which to store Polygons found. |
| 66 |
*/ |
| 67 |
public PolygonExtracter(List comps) |
| 68 |
{ |
| 69 |
this.comps = comps; |
| 70 |
} |
| 71 |
|
| 72 |
public void filter(Geometry geom) |
| 73 |
{ |
| 74 |
if (geom instanceof Polygon) comps.add(geom); |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|