1 | /*
|
---|
2 | * The JTS Topology Suite is a collection of Java classes that
|
---|
3 | * implement the fundamental operations required to validate a given
|
---|
4 | * geo-spatial data set to a known topological specification.
|
---|
5 | *
|
---|
6 | * Copyright (C) 2001 Vivid Solutions
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Lesser General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2.1 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Lesser General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Lesser General Public
|
---|
19 | * License along with this library; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
21 | *
|
---|
22 | * For more information, contact:
|
---|
23 | *
|
---|
24 | * Vivid Solutions
|
---|
25 | * Suite #1A
|
---|
26 | * 2328 Government Street
|
---|
27 | * Victoria BC V8T 5G5
|
---|
28 | * Canada
|
---|
29 | *
|
---|
30 | * (250)385-6040
|
---|
31 | * www.vividsolutions.com
|
---|
32 | */
|
---|
33 | package com.vividsolutions.jts.geom.prep;
|
---|
34 |
|
---|
35 | import java.util.Iterator;
|
---|
36 | import java.util.List;
|
---|
37 |
|
---|
38 | import com.vividsolutions.jts.algorithm.PointLocator;
|
---|
39 | import com.vividsolutions.jts.geom.Coordinate;
|
---|
40 | import com.vividsolutions.jts.geom.Geometry;
|
---|
41 | import com.vividsolutions.jts.geom.GeometryCollection;
|
---|
42 | import com.vividsolutions.jts.geom.util.ComponentCoordinateExtracter;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * A base class for {@link PreparedGeometry} subclasses.
|
---|
46 | * Contains default implementations for methods, which simply delegate
|
---|
47 | * to the equivalent {@link Geometry} methods.
|
---|
48 | * This class may be used as a "no-op" class for Geometry types
|
---|
49 | * which do not have a corresponding {@link PreparedGeometry} implementation.
|
---|
50 | *
|
---|
51 | * @author Martin Davis
|
---|
52 | *
|
---|
53 | */
|
---|
54 | class BasicPreparedGeometry
|
---|
55 | implements PreparedGeometry
|
---|
56 | {
|
---|
57 | private Geometry baseGeom;
|
---|
58 | private List representativePts; // List<Coordinate>
|
---|
59 |
|
---|
60 | public BasicPreparedGeometry(Geometry geom)
|
---|
61 | {
|
---|
62 | this.baseGeom = geom;
|
---|
63 | representativePts = ComponentCoordinateExtracter.getCoordinates(geom);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public Geometry getGeometry() { return baseGeom; }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Gets the list of representative points for this geometry.
|
---|
70 | * One vertex is included for every component of the geometry
|
---|
71 | * (i.e. including one for every ring of polygonal geometries)
|
---|
72 | *
|
---|
73 | * @return a List of Coordinate
|
---|
74 | */
|
---|
75 | public List getRepresentativePoints()
|
---|
76 | {
|
---|
77 | return representativePts;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Tests whether any representative of the target geometry
|
---|
82 | * intersects the test geometry.
|
---|
83 | * This is useful in A/A, A/L, A/P, L/P, and P/P cases.
|
---|
84 | *
|
---|
85 | * @param geom the test geometry
|
---|
86 | * @param repPts the representative points of the target geometry
|
---|
87 | * @return true if any component intersects the areal test geometry
|
---|
88 | */
|
---|
89 | public boolean isAnyTargetComponentInTest(Geometry testGeom)
|
---|
90 | {
|
---|
91 | PointLocator locator = new PointLocator();
|
---|
92 | for (Iterator i = representativePts.iterator(); i.hasNext(); ) {
|
---|
93 | Coordinate p = (Coordinate) i.next();
|
---|
94 | if (locator.intersects(p, testGeom))
|
---|
95 | return true;
|
---|
96 | }
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Determines whether a Geometry g interacts with
|
---|
102 | * this geometry by testing the geometry envelopes.
|
---|
103 | *
|
---|
104 | * @param g a Geometry
|
---|
105 | * @return true if the envelopes intersect
|
---|
106 | */
|
---|
107 | protected boolean envelopesIntersect(Geometry g)
|
---|
108 | {
|
---|
109 | if (! baseGeom.getEnvelopeInternal().intersects(g.getEnvelopeInternal()))
|
---|
110 | return false;
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Determines whether the envelope of
|
---|
116 | * this geometry covers the Geometry g.
|
---|
117 | *
|
---|
118 | *
|
---|
119 | * @param g a Geometry
|
---|
120 | * @return true if g is contained in this envelope
|
---|
121 | */
|
---|
122 | protected boolean envelopeCovers(Geometry g)
|
---|
123 | {
|
---|
124 | if (! baseGeom.getEnvelopeInternal().covers(g.getEnvelopeInternal()))
|
---|
125 | return false;
|
---|
126 | return true;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Default implementation.
|
---|
131 | */
|
---|
132 | public boolean contains(Geometry g)
|
---|
133 | {
|
---|
134 | return baseGeom.contains(g);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Default implementation.
|
---|
139 | */
|
---|
140 | public boolean containsProperly(Geometry g)
|
---|
141 | {
|
---|
142 | // since raw relate is used, provide some optimizations
|
---|
143 |
|
---|
144 | // short-circuit test
|
---|
145 | if (! baseGeom.getEnvelopeInternal().contains(g.getEnvelopeInternal()))
|
---|
146 | return false;
|
---|
147 |
|
---|
148 | // otherwise, compute using relate mask
|
---|
149 | return baseGeom.relate(g, "T**FF*FF*");
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Default implementation.
|
---|
155 | */
|
---|
156 | public boolean covers(Geometry g)
|
---|
157 | {
|
---|
158 | return baseGeom.covers(g);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Standard implementation for all geometries.
|
---|
164 | * Supports {@link GeometryCollection}s as input.
|
---|
165 | */
|
---|
166 | public boolean disjoint(Geometry g)
|
---|
167 | {
|
---|
168 | return ! intersects(g);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Default implementation.
|
---|
173 | */
|
---|
174 | public boolean intersects(Geometry g)
|
---|
175 | {
|
---|
176 | return baseGeom.intersects(g);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | public String toString()
|
---|
181 | {
|
---|
182 | return baseGeom.toString();
|
---|
183 | }
|
---|
184 | }
|
---|