| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package org.locationtech.jts.geom.prep; |
| 13 |
|
| 14 |
|
| 15 |
import org.locationtech.jts.algorithm.locate.IndexedPointInAreaLocator; |
| 16 |
import org.locationtech.jts.algorithm.locate.PointOnGeometryLocator; |
| 17 |
import org.locationtech.jts.geom.Geometry; |
| 18 |
import org.locationtech.jts.geom.MultiPolygon; |
| 19 |
import org.locationtech.jts.geom.Polygon; |
| 20 |
import org.locationtech.jts.geom.Polygonal; |
| 21 |
import org.locationtech.jts.noding.FastSegmentSetIntersectionFinder; |
| 22 |
import org.locationtech.jts.noding.SegmentStringUtil; |
| 23 |
import org.locationtech.jts.operation.predicate.RectangleContains; |
| 24 |
import org.locationtech.jts.operation.predicate.RectangleIntersects; |
| 25 |
|
| 26 |
/** |
| 27 |
* A prepared version for {@link Polygonal} geometries. |
| 28 |
* This class supports both {@link Polygon}s and {@link MultiPolygon}s. |
| 29 |
* <p> |
| 30 |
* This class does <b>not</b> support MultiPolygons which are non-valid |
| 31 |
* (e.g. with overlapping elements). |
| 32 |
* <p> |
| 33 |
* Instances of this class are thread-safe and immutable. |
| 34 |
* |
| 35 |
* @author mbdavis |
| 36 |
* |
| 37 |
*/ |
| 38 |
public class PreparedPolygon |
| 39 |
extends BasicPreparedGeometry |
| 40 |
{ |
| 41 |
private final boolean isRectangle; |
| 42 |
|
| 43 |
private FastSegmentSetIntersectionFinder segIntFinder = null; |
| 44 |
private PointOnGeometryLocator pia = null; |
| 45 |
|
| 46 |
public PreparedPolygon(Polygonal poly) { |
| 47 |
super((Geometry) poly); |
| 48 |
isRectangle = getGeometry().isRectangle(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Gets the indexed intersection finder for this geometry. |
| 53 |
* |
| 54 |
* @return the intersection finder |
| 55 |
*/ |
| 56 |
public synchronized FastSegmentSetIntersectionFinder getIntersectionFinder() |
| 57 |
{ |
| 58 |
/** |
| 59 |
* MD - Another option would be to use a simple scan for |
| 60 |
* segment testing for small geometries. |
| 61 |
* However, testing indicates that there is no particular advantage |
| 62 |
* to this approach. |
| 63 |
*/ |
| 64 |
if (segIntFinder == null) |
| 65 |
segIntFinder = new FastSegmentSetIntersectionFinder(SegmentStringUtil.extractSegmentStrings(getGeometry())); |
| 66 |
return segIntFinder; |
| 67 |
} |
| 68 |
|
| 69 |
public synchronized PointOnGeometryLocator getPointLocator() |
| 70 |
{ |
| 71 |
if (pia == null) |
| 72 |
pia = new IndexedPointInAreaLocator(getGeometry()); |
| 73 |
|
| 74 |
return pia; |
| 75 |
} |
| 76 |
|
| 77 |
public boolean intersects(Geometry g) |
| 78 |
{ |
| 79 |
|
| 80 |
if (! envelopesIntersect(g)) return false; |
| 81 |
|
| 82 |
|
| 83 |
if (isRectangle) { |
| 84 |
return RectangleIntersects.intersects((Polygon) getGeometry(), g); |
| 85 |
} |
| 86 |
|
| 87 |
return PreparedPolygonIntersects.intersects(this, g); |
| 88 |
} |
| 89 |
|
| 90 |
public boolean contains(Geometry g) |
| 91 |
{ |
| 92 |
|
| 93 |
if (! envelopeCovers(g)) |
| 94 |
return false; |
| 95 |
|
| 96 |
|
| 97 |
if (isRectangle) { |
| 98 |
return RectangleContains.contains((Polygon) getGeometry(), g); |
| 99 |
} |
| 100 |
|
| 101 |
return PreparedPolygonContains.contains(this, g); |
| 102 |
} |
| 103 |
|
| 104 |
public boolean containsProperly(Geometry g) |
| 105 |
{ |
| 106 |
|
| 107 |
if (! envelopeCovers(g)) |
| 108 |
return false; |
| 109 |
return PreparedPolygonContainsProperly.containsProperly(this, g); |
| 110 |
} |
| 111 |
|
| 112 |
public boolean covers(Geometry g) |
| 113 |
{ |
| 114 |
|
| 115 |
if (! envelopeCovers(g)) |
| 116 |
return false; |
| 117 |
|
| 118 |
if (isRectangle) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
return PreparedPolygonCovers.covers(this, g); |
| 122 |
} |
| 123 |
} |
| 124 |
|