Class GeometryExtracter

  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.LineString;
 22 import org.locationtech.jts.geom.LinearRing;
 23 import org.locationtech.jts.geom.MultiLineString;
 24 import org.locationtech.jts.geom.MultiPoint;
 25 import org.locationtech.jts.geom.MultiPolygon;
 26 import org.locationtech.jts.geom.Point;
 27 import org.locationtech.jts.geom.Polygon;
 28  
 29 /**
 30  * Extracts the components of a given type from a {@link Geometry}.
 31  *
 32  * @version 1.7
 33  */
 34 public class GeometryExtracter
 35   implements GeometryFilter
 36 {
 37   /**
 38    * Extracts the components of type <tt>clz</tt> from a {@link Geometry}
 39    * and adds them to the provided {@link List}.
 40    * 
 41    * @param geom the geometry from which to extract
 42    * @param list the list to add the extracted elements to
 43    * @deprecated Use {@link GeometryExtracter#extract(Geometry, String, List)}
 44    */
 45   public static List extract(Geometry geom, Class clz, List list)
 46   {
 47       return extract(geom, toGeometryType(clz), list);
 48   }
 49   
 50   /**
 51    * @deprecated
 52    */
 53   private static String toGeometryType(Class clz) {
 54     if (clz == null)
 55       return null;
 56     else if (clz.isAssignableFrom(Point.class))
 57       return Geometry.TYPENAME_POINT;
 58     else if (clz.isAssignableFrom(LineString.class))
 59       return Geometry.TYPENAME_LINESTRING;
 60     else if (clz.isAssignableFrom(LinearRing.class))
 61       return Geometry.TYPENAME_LINEARRING;
 62     else if (clz.isAssignableFrom(Polygon.class))
 63       return Geometry.TYPENAME_POLYGON;
 64     else if (clz.isAssignableFrom(MultiPoint.class))
 65       return Geometry.TYPENAME_MULTIPOINT;
 66     else if (clz.isAssignableFrom(MultiLineString.class))
 67       return Geometry.TYPENAME_MULTILINESTRING;
 68     else if (clz.isAssignableFrom(MultiPolygon.class))
 69       return Geometry.TYPENAME_MULTIPOLYGON;
 70     else if (clz.isAssignableFrom(GeometryCollection.class))
 71       return Geometry.TYPENAME_GEOMETRYCOLLECTION;
 72     throw new RuntimeException("Unsupported class");
 73   }
 74   
 75   /**
 76    * Extracts the components of <tt>geometryType</tt> from a {@link Geometry}
 77    * and adds them to the provided {@link List}.
 78    * 
 79    * @param geom the geometry from which to extract
 80    * @param geometryType Geometry type to extract (null means all types)
 81    * @param list the list to add the extracted elements to
 82    */
 83   public static List extract(Geometry geom, String geometryType, List list)
 84   {
 85       if (geom.getGeometryType() == geometryType) {
 86           list.add(geom);
 87       }
 88       else if (geom instanceof GeometryCollection) {
 89           geom.apply(new GeometryExtracter(geometryType, list));
 90       }
 91       // skip non-LineString elemental geometries
 92       
 93     return list;
 94   }
 95  
 96   /**
 97    * Extracts the components of type <tt>clz</tt> from a {@link Geometry}
 98    * and returns them in a {@link List}.
 99    * 
100    * @param geom the geometry from which to extract
101    * @deprecated Use {@link GeometryExtracter#extract(Geometry, String)}
102    */
103   public static List extract(Geometry geom, Class clz)
104   {
105     return extract(geom, clz, new ArrayList());
106   }
107   
108   public static List extract(Geometry geom, String geometryType)
109   {
110     return extract(geom, geometryType, new ArrayList());
111   }
112  
113   private String geometryType;
114   private List comps;
115   
116   /**
117    * Constructs a filter with a list in which to store the elements found.
118    * 
119    * @param clz the class of the components to extract (null means all types)
120    * @param comps the list to extract into
121    * @deprecated
122    */
123   public GeometryExtracter(Class clz, List comps)
124   {
125       this.geometryType = toGeometryType(clz);
126     this.comps = comps;
127   }
128   
129   /**
130    * Constructs a filter with a list in which to store the elements found.
131    * 
132    * @param geometryType Geometry type to extract (null means all types)
133    * @param comps the list to extract into
134    */
135   public GeometryExtracter(String geometryType, List comps)
136   {
137       this.geometryType = geometryType;
138     this.comps = comps;
139   }
140   
141   protected static boolean isOfType(Geometry geom, String geometryType) {
142     if (geom.getGeometryType() == geometryType) return true;
143     if (geometryType == Geometry.TYPENAME_LINESTRING
144       && geom.getGeometryType() == Geometry.TYPENAME_LINEARRING) return true;
145     return false;
146   }
147  
148   public void filter(Geometry geom) {
149     if (geometryType == null || isOfType(geom, geometryType))
150       comps.add(geom);
151   }
152  
153 }
154