Class BasicPreparedGeometry

  1 /*
  2  * Copyright (c) 2016 Vivid Solutions.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.geom.prep;
 13  
 14 import java.util.Iterator;
 15 import java.util.List;
 16  
 17 import org.locationtech.jts.algorithm.PointLocator;
 18 import org.locationtech.jts.geom.Coordinate;
 19 import org.locationtech.jts.geom.Geometry;
 20 import org.locationtech.jts.geom.GeometryCollection;
 21 import org.locationtech.jts.geom.util.ComponentCoordinateExtracter;
 22  
 23  
 24 /**
 25  * A base class for {@link PreparedGeometry} subclasses.
 26  * Contains default implementations for methods, which simply delegate
 27  * to the equivalent {@link Geometry} methods.
 28  * This class may be used as a "no-op" class for Geometry types
 29  * which do not have a corresponding {@link PreparedGeometry} implementation.
 30  * 
 31  * @author Martin Davis
 32  *
 33  */
 34 class BasicPreparedGeometry 
 35   implements PreparedGeometry
 36 {
 37   private final Geometry baseGeom;
 38   private final List representativePts;  // List<Coordinate>
 39  
 40   public BasicPreparedGeometry(Geometry geom) 
 41   {
 42     baseGeom = geom;
 43     representativePts = ComponentCoordinateExtracter.getCoordinates(geom);
 44   }
 45  
 46   public Geometry getGeometry() { return baseGeom; }
 47  
 48   /**
 49    * Gets the list of representative points for this geometry.
 50    * One vertex is included for every component of the geometry
 51    * (i.e. including one for every ring of polygonal geometries).
 52    * 
 53    * Do not modify the returned list!
 54    * 
 55    * @return a List of Coordinate
 56    */
 57   public List getRepresentativePoints()
 58   {
 59     //TODO wrap in unmodifiable?
 60     return representativePts;
 61   }
 62   
 63     /**
 64      * Tests whether any representative of the target geometry 
 65      * intersects the test geometry.
 66      * This is useful in A/A, A/L, A/P, L/P, and P/P cases.
 67      * 
 68      * @param testGeom the test geometry
 69      * @return true if any component intersects the areal test geometry
 70      */
 71     public boolean isAnyTargetComponentInTest(Geometry testGeom)
 72     {
 73         PointLocator locator = new PointLocator();
 74     for (Iterator i = representativePts.iterator(); i.hasNext(); ) {
 75       Coordinate p = (Coordinate) i.next();
 76       if (locator.intersects(p, testGeom))
 77         return true;
 78     }
 79         return false;
 80     }
 81  
 82   /**
 83    * Determines whether a Geometry g interacts with 
 84    * this geometry by testing the geometry envelopes.
 85    *  
 86    * @param g a Geometry
 87    * @return true if the envelopes intersect
 88    */
 89   protected boolean envelopesIntersect(Geometry g)
 90   {
 91     if (! baseGeom.getEnvelopeInternal().intersects(g.getEnvelopeInternal()))
 92       return false;
 93     return true;
 94   }
 95   
 96   /**
 97    * Determines whether the envelope of 
 98    * this geometry covers the Geometry g.
 99    * 
100    *  
101    * @param g a Geometry
102    * @return true if g is contained in this envelope
103    */
104   protected boolean envelopeCovers(Geometry g)
105   {
106     if (! baseGeom.getEnvelopeInternal().covers(g.getEnvelopeInternal()))
107       return false;
108     return true;
109   }
110   
111   /**
112    * Default implementation.
113    */
114   public boolean contains(Geometry g)
115   {
116     return baseGeom.contains(g);
117   }
118  
119   /**
120    * Default implementation.
121    */
122   public boolean containsProperly(Geometry g)
123   {
124       // since raw relate is used, provide some optimizations
125       
126     // short-circuit test
127     if (! baseGeom.getEnvelopeInternal().contains(g.getEnvelopeInternal()))
128       return false;
129       
130     // otherwise, compute using relate mask
131     return baseGeom.relate(g, "T**FF*FF*");
132   }
133  
134   /**
135    * Default implementation.
136    */
137   public boolean coveredBy(Geometry g)
138   {
139     return baseGeom.coveredBy(g);
140   }
141  
142   /**
143    * Default implementation.
144    */
145   public boolean covers(Geometry g)
146   {
147     return baseGeom.covers(g);
148   }
149  
150   /**
151    * Default implementation.
152    */
153   public boolean crosses(Geometry g)
154   {
155     return baseGeom.crosses(g);
156   }
157   
158   /**
159    * Standard implementation for all geometries.
160    * Supports {@link GeometryCollection}s as input.
161    */
162   public boolean disjoint(Geometry g)
163   {
164     return ! intersects(g);
165   }
166   
167   /**
168    * Default implementation.
169    */
170   public boolean intersects(Geometry g)
171   {
172     return baseGeom.intersects(g);
173   }
174   
175   /**
176    * Default implementation.
177    */
178   public boolean overlaps(Geometry g)
179   {
180     return baseGeom.overlaps(g);
181   }
182   
183   /**
184    * Default implementation.
185    */
186   public boolean touches(Geometry g)
187   {
188     return baseGeom.touches(g);
189   }
190   
191   /**
192    * Default implementation.
193    */
194   public boolean within(Geometry g)
195   {
196     return baseGeom.within(g);
197   }
198   
199   public String toString()
200   {
201       return baseGeom.toString();
202   }
203 }
204