Class PolygonExtracter

 1  
 2 /*
 3  * Copyright (c) 2016 Vivid Solutions.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
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       // skip non-Polygonal elemental geometries
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