Revision 42260
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.fmap.geom.jts.DefaultGeometryLibrary |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/AbstractGeometry.java | ||
---|---|---|
1 |
/* gvSIG. Desktop Geographic Information System. |
|
2 |
* |
|
3 |
* Copyright ? 2007-2015 gvSIG Association |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU General Public License |
|
7 |
* as published by the Free Software Foundation; either version 2 |
|
8 |
* of the License, or (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 |
* MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us |
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.fmap.geom.jts; |
|
24 |
|
|
25 |
import java.awt.Rectangle; |
|
26 |
import java.awt.Shape; |
|
27 |
import java.awt.geom.AffineTransform; |
|
28 |
import java.awt.geom.Rectangle2D; |
|
29 |
|
|
30 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
|
31 |
import com.vividsolutions.jts.geom.Coordinate; |
|
32 |
import com.vividsolutions.jts.io.WKBWriter; |
|
33 |
import com.vividsolutions.jts.io.WKTWriter; |
|
34 |
import com.vividsolutions.jts.operation.distance.DistanceOp; |
|
35 |
import com.vividsolutions.jts.operation.overlay.snap.GeometrySnapper; |
|
36 |
import com.vividsolutions.jts.operation.valid.IsValidOp; |
|
37 |
import com.vividsolutions.jts.operation.valid.TopologyValidationError; |
|
38 |
|
|
39 |
import org.slf4j.Logger; |
|
40 |
import org.slf4j.LoggerFactory; |
|
41 |
|
|
42 |
import org.gvsig.fmap.geom.Geometry; |
|
43 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
44 |
import org.gvsig.fmap.geom.GeometryManager; |
|
45 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
46 |
import org.gvsig.fmap.geom.jts.primitive.Envelope2D; |
|
47 |
import org.gvsig.fmap.geom.jts.primitive.Envelope3D; |
|
48 |
import org.gvsig.fmap.geom.jts.primitive.point.Point3D; |
|
49 |
import org.gvsig.fmap.geom.jts.utils.JTSUtils; |
|
50 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
|
51 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
|
52 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
|
53 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
54 |
import org.gvsig.fmap.geom.primitive.OrientableCurve; |
|
55 |
import org.gvsig.fmap.geom.primitive.OrientableSurface; |
|
56 |
import org.gvsig.fmap.geom.primitive.Point; |
|
57 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
58 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
|
59 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
|
60 |
import org.gvsig.tools.locator.LocatorException; |
|
61 |
|
|
62 |
/** |
|
63 |
* @author fdiaz |
|
64 |
* |
|
65 |
*/ |
|
66 |
public abstract class AbstractGeometry implements GeometryJTS { |
|
67 |
|
|
68 |
private static final Logger logger = LoggerFactory.getLogger(AbstractGeometry.class); |
|
69 |
|
|
70 |
/** |
|
71 |
* |
|
72 |
*/ |
|
73 |
private static final long serialVersionUID = 4999326772576222293L; |
|
74 |
|
|
75 |
private final GeometryType geometryType; |
|
76 |
|
|
77 |
|
|
78 |
/** |
|
79 |
* |
|
80 |
*/ |
|
81 |
public AbstractGeometry(int type, int subtype) { |
|
82 |
try { |
|
83 |
this.geometryType = GeometryLocator.getGeometryManager().getGeometryType(type, subtype); |
|
84 |
} catch (Exception e) { |
|
85 |
// TODO: Ver de crear una excepcion para esto o si hay alguna en el API. |
|
86 |
throw new RuntimeException(e); |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
public GeometryType getGeometryType(){ |
|
91 |
return geometryType; |
|
92 |
} |
|
93 |
|
|
94 |
protected GeometryManager getManager() { |
|
95 |
return GeometryLocator.getGeometryManager(); |
|
96 |
} |
|
97 |
|
|
98 |
/* |
|
99 |
* (non-Javadoc) |
|
100 |
* |
|
101 |
* @see org.gvsig.fmap.geom.Geometry#contains(org.gvsig.fmap.geom.Geometry) |
|
102 |
*/ |
|
103 |
public boolean contains(Geometry geometry) throws GeometryOperationNotSupportedException, |
|
104 |
GeometryOperationException { |
|
105 |
if (!(geometry instanceof GeometryJTS)) { |
|
106 |
return false; |
|
107 |
} |
|
108 |
return getJTS().contains(((GeometryJTS) geometry).getJTS()); |
|
109 |
} |
|
110 |
|
|
111 |
/* |
|
112 |
* (non-Javadoc) |
|
113 |
* |
|
114 |
* @see java.lang.Comparable#compareTo(java.lang.Object) |
|
115 |
*/ |
|
116 |
public int compareTo(Object o) { |
|
117 |
return getJTS().compareTo(((GeometryJTS) o).getJTS()); |
|
118 |
} |
|
119 |
|
|
120 |
/* |
|
121 |
* (non-Javadoc) |
|
122 |
* |
|
123 |
* @see java.awt.Shape#contains(double, double) |
|
124 |
*/ |
|
125 |
public boolean contains(double x, double y) { |
|
126 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface"); |
|
127 |
Shape shp = getShape(); |
|
128 |
return shp.contains(x, y); |
|
129 |
} |
|
130 |
|
|
131 |
/* |
|
132 |
* (non-Javadoc) |
|
133 |
* |
|
134 |
* @see java.awt.Shape#intersects(double, double, double, double) |
|
135 |
*/ |
|
136 |
public boolean intersects(double x, double y, double w, double h) { |
|
137 |
|
|
138 |
notifyDeprecated("Calling deprecated method of geometry intersects from shape interface"); |
|
139 |
Shape shp = this.getShape(); |
|
140 |
return shp.intersects(x, y, w, h); |
|
141 |
} |
|
142 |
|
|
143 |
/* |
|
144 |
* (non-Javadoc) |
|
145 |
* |
|
146 |
* @see java.awt.Shape#contains(double, double, double, double) |
|
147 |
*/ |
|
148 |
public boolean contains(double x, double y, double w, double h) { |
|
149 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface"); |
|
150 |
Shape shp = this.getShape(); |
|
151 |
return shp.contains(x, y, w, h); |
|
152 |
} |
|
153 |
|
|
154 |
/* |
|
155 |
* (non-Javadoc) |
|
156 |
* |
|
157 |
* @see java.awt.Shape#contains(java.awt.geom.Point2D) |
|
158 |
*/ |
|
159 |
public boolean contains(java.awt.geom.Point2D p) { |
|
160 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface"); |
|
161 |
Shape shp = this.getShape(); |
|
162 |
return shp.contains(p); |
|
163 |
} |
|
164 |
|
|
165 |
/* |
|
166 |
* (non-Javadoc) |
|
167 |
* |
|
168 |
* @see java.awt.Shape#contains(java.awt.geom.Rectangle2D) |
|
169 |
*/ |
|
170 |
public boolean contains(Rectangle2D r) { |
|
171 |
notifyDeprecated("Calling deprecated method of geometry contains from shape interface"); |
|
172 |
Shape shp = this.getShape(); |
|
173 |
return shp.contains(r); |
|
174 |
} |
|
175 |
|
|
176 |
/* |
|
177 |
* (non-Javadoc) |
|
178 |
* |
|
179 |
* @see org.gvsig.fmap.geom.Geometry#distance(org.gvsig.fmap.geom.Geometry) |
|
180 |
*/ |
|
181 |
public double distance(org.gvsig.fmap.geom.Geometry other) throws GeometryOperationNotSupportedException, |
|
182 |
GeometryOperationException { |
|
183 |
return getJTS().distance(((GeometryJTS) other).getJTS()); |
|
184 |
} |
|
185 |
|
|
186 |
/* |
|
187 |
* (non-Javadoc) |
|
188 |
* |
|
189 |
* @see |
|
190 |
* org.gvsig.fmap.geom.Geometry#isWithinDistance(org.gvsig.fmap.geom.Geometry |
|
191 |
* , double) |
|
192 |
*/ |
|
193 |
public boolean isWithinDistance(org.gvsig.fmap.geom.Geometry other, double distance) |
|
194 |
throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
195 |
return com.vividsolutions.jts.operation.distance.DistanceOp.isWithinDistance(this.getJTS(), |
|
196 |
((GeometryJTS) other).getJTS(), distance); |
|
197 |
} |
|
198 |
|
|
199 |
/* |
|
200 |
* (non-Javadoc) |
|
201 |
* |
|
202 |
* @see org.gvsig.fmap.geom.Geometry#overlaps(org.gvsig.fmap.geom.Geometry) |
|
203 |
*/ |
|
204 |
public boolean overlaps(org.gvsig.fmap.geom.Geometry geometry) throws GeometryOperationNotSupportedException, |
|
205 |
GeometryOperationException { |
|
206 |
// TODO: this method can be implemented throw invokeOperation |
|
207 |
return getJTS().overlaps(((GeometryJTS) geometry).getJTS()); |
|
208 |
} |
|
209 |
|
|
210 |
public boolean coveredBy(Geometry geometry) throws GeometryOperationNotSupportedException, |
|
211 |
GeometryOperationException { |
|
212 |
|
|
213 |
return getJTS().coveredBy(((GeometryJTS) geometry).getJTS()); |
|
214 |
} |
|
215 |
|
|
216 |
public boolean covers(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
217 |
// TODO: this method can be implemented throw invokeOperation |
|
218 |
return getJTS().covers(((GeometryJTS) geometry).getJTS()); |
|
219 |
|
|
220 |
} |
|
221 |
|
|
222 |
public boolean crosses(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
223 |
// TODO: this method can be implemented throw invokeOperation |
|
224 |
return getJTS().crosses(((GeometryJTS) geometry).getJTS()); |
|
225 |
} |
|
226 |
|
|
227 |
public boolean intersects(Geometry geometry) throws GeometryOperationNotSupportedException, |
|
228 |
GeometryOperationException { |
|
229 |
return getJTS().intersects(((GeometryJTS) geometry).getJTS()); |
|
230 |
} |
|
231 |
|
|
232 |
public boolean touches(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
233 |
// TODO: this method can be implemented throw invokeOperation |
|
234 |
return getJTS().touches(((GeometryJTS) geometry).getJTS()); |
|
235 |
} |
|
236 |
|
|
237 |
public boolean disjoint(Geometry geometry) throws GeometryOperationNotSupportedException, |
|
238 |
GeometryOperationException { |
|
239 |
// TODO: this method can be implemented throw invokeOperation |
|
240 |
return getJTS().disjoint(((GeometryJTS) geometry).getJTS()); |
|
241 |
} |
|
242 |
|
|
243 |
public boolean within(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
244 |
// TODO: this method can be implemented throw invokeOperation |
|
245 |
return getJTS().within(((GeometryJTS) geometry).getJTS()); |
|
246 |
} |
|
247 |
|
|
248 |
public double area() throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
249 |
return getJTS().getArea(); |
|
250 |
} |
|
251 |
|
|
252 |
public double perimeter() throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
253 |
return getJTS().getLength(); |
|
254 |
} |
|
255 |
|
|
256 |
/* |
|
257 |
* (non-Javadoc) |
|
258 |
* |
|
259 |
* @see org.gvsig.fmap.geom.Geometry#intersects(java.awt.geom.Rectangle2D) |
|
260 |
*/ |
|
261 |
public boolean intersects(Rectangle2D r) { |
|
262 |
double x = r.getMinX(); |
|
263 |
double y = r.getMinY(); |
|
264 |
double w = r.getWidth(); |
|
265 |
double h = r.getHeight(); |
|
266 |
|
|
267 |
return fastIntersects(x, y, w, h); |
|
268 |
} |
|
269 |
|
|
270 |
/* |
|
271 |
* (non-Javadoc) |
|
272 |
* |
|
273 |
* @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double, |
|
274 |
* double) |
|
275 |
*/ |
|
276 |
public boolean fastIntersects(double x, double y, double w, double h) { |
|
277 |
com.vividsolutions.jts.geom.Geometry rect = null; |
|
278 |
com.vividsolutions.jts.geom.Coordinate[] coord = new com.vividsolutions.jts.geom.Coordinate[5]; |
|
279 |
coord[0] = new com.vividsolutions.jts.geom.Coordinate(x, y); |
|
280 |
coord[1] = new com.vividsolutions.jts.geom.Coordinate(x + w, y); |
|
281 |
coord[2] = new com.vividsolutions.jts.geom.Coordinate(x + w, y + w); |
|
282 |
coord[3] = new com.vividsolutions.jts.geom.Coordinate(x, y + w); |
|
283 |
coord[4] = new com.vividsolutions.jts.geom.Coordinate(x, y); |
|
284 |
rect = JTSUtils.factory.createPolygon(coord); |
|
285 |
|
|
286 |
return getJTS().intersects(rect); |
|
287 |
} |
|
288 |
|
|
289 |
/* |
|
290 |
* (non-Javadoc) |
|
291 |
* |
|
292 |
* @see org.gvsig.fmap.geom.Geometry#getEnvelope() |
|
293 |
*/ |
|
294 |
public Envelope getEnvelope() { |
|
295 |
if (is3D()) { |
|
296 |
Coordinate[] coordinates = getJTS().getCoordinates(); |
|
297 |
|
|
298 |
double minx = Double.POSITIVE_INFINITY; |
|
299 |
double miny = Double.POSITIVE_INFINITY; |
|
300 |
double minz = Double.POSITIVE_INFINITY; |
|
301 |
|
|
302 |
double maxx = Double.NEGATIVE_INFINITY; |
|
303 |
double maxy = Double.NEGATIVE_INFINITY; |
|
304 |
double maxz = Double.NEGATIVE_INFINITY; |
|
305 |
|
|
306 |
double x; |
|
307 |
double y; |
|
308 |
double z; |
|
309 |
|
|
310 |
for (int i = 0; i < coordinates.length; i++) { |
|
311 |
x = coordinates[i].x; |
|
312 |
y = coordinates[i].x; |
|
313 |
z = coordinates[i].x; |
|
314 |
minx = Math.min(x, minx); |
|
315 |
miny = Math.min(x, miny); |
|
316 |
minz = Math.min(x, minz); |
|
317 |
maxx = Math.max(x, maxx); |
|
318 |
maxy = Math.max(x, maxy); |
|
319 |
maxz = Math.max(x, maxz); |
|
320 |
} |
|
321 |
|
|
322 |
if (minx <= maxx && miny <= maxy && minz <= maxz) { |
|
323 |
Point min = new Point3D(minx, miny, minz); |
|
324 |
Point max = new Point3D(maxx, maxy, maxz); |
|
325 |
return new Envelope3D(min, max); |
|
326 |
} |
|
327 |
return new Envelope3D(); |
|
328 |
} else { |
|
329 |
com.vividsolutions.jts.geom.Envelope envelope = getJTS().getEnvelopeInternal(); |
|
330 |
return new Envelope2D(envelope); |
|
331 |
} |
|
332 |
} |
|
333 |
|
|
334 |
/* |
|
335 |
* (non-Javadoc) |
|
336 |
* |
|
337 |
* @see org.gvsig.fmap.geom.Geometry#isSimple() |
|
338 |
*/ |
|
339 |
public boolean isSimple() { |
|
340 |
return this.getJTS().isSimple(); |
|
341 |
} |
|
342 |
|
|
343 |
/* |
|
344 |
* (non-Javadoc) |
|
345 |
* |
|
346 |
* @see org.gvsig.fmap.geom.Geometry#isCCW() |
|
347 |
*/ |
|
348 |
public boolean isCCW() throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
349 |
return CGAlgorithms.isCCW(this.getJTS().getCoordinates()); |
|
350 |
} |
|
351 |
|
|
352 |
/* |
|
353 |
* (non-Javadoc) |
|
354 |
* |
|
355 |
* @see org.gvsig.fmap.geom.Geometry#invokeOperation(int, |
|
356 |
* org.gvsig.fmap.geom.operation.GeometryOperationContext) |
|
357 |
*/ |
|
358 |
public Object invokeOperation(int index, GeometryOperationContext ctx) |
|
359 |
throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
360 |
return getManager().invokeOperation(index, this, ctx); |
|
361 |
|
|
362 |
} |
|
363 |
|
|
364 |
/* |
|
365 |
* (non-Javadoc) |
|
366 |
* |
|
367 |
* @see org.gvsig.fmap.geom.Geometry#invokeOperation(java.lang.String, |
|
368 |
* org.gvsig.fmap.geom.operation.GeometryOperationContext) |
|
369 |
*/ |
|
370 |
public Object invokeOperation(String opName, GeometryOperationContext ctx) |
|
371 |
throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
372 |
return getManager().invokeOperation(opName, this, ctx); |
|
373 |
|
|
374 |
} |
|
375 |
|
|
376 |
/* |
|
377 |
* (non-Javadoc) |
|
378 |
* |
|
379 |
* @see org.gvsig.fmap.geom.Geometry#getType() |
|
380 |
*/ |
|
381 |
public int getType() { |
|
382 |
return this.getGeometryType().getType(); |
|
383 |
} |
|
384 |
|
|
385 |
/* |
|
386 |
* (non-Javadoc) |
|
387 |
* |
|
388 |
* @see org.gvsig.fmap.geom.Geometry#convertToWKB() |
|
389 |
*/ |
|
390 |
public byte[] convertToWKB() throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
391 |
int subType = getGeometryType().getSubType(); |
|
392 |
boolean is3D = subType == 1 || subType == 3; |
|
393 |
|
|
394 |
WKBWriter write = null; |
|
395 |
if (is3D) |
|
396 |
write = new WKBWriter(3); |
|
397 |
else |
|
398 |
write = new WKBWriter(2); |
|
399 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
|
400 |
return write.write(jts); |
|
401 |
} |
|
402 |
|
|
403 |
/* |
|
404 |
* (non-Javadoc) |
|
405 |
* |
|
406 |
* @see org.gvsig.fmap.geom.Geometry#convertToWKB(int) |
|
407 |
*/ |
|
408 |
public byte[] convertToWKB(int srs) throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
409 |
int subType = getGeometryType().getSubType(); |
|
410 |
boolean is3D = subType == 1 || subType == 3; |
|
411 |
|
|
412 |
WKBWriter write = null; |
|
413 |
if (is3D) |
|
414 |
write = new WKBWriter(3); |
|
415 |
else |
|
416 |
write = new WKBWriter(2); |
|
417 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
|
418 |
jts.setSRID(srs); |
|
419 |
return write.write(jts); |
|
420 |
} |
|
421 |
|
|
422 |
/* |
|
423 |
* (non-Javadoc) |
|
424 |
* |
|
425 |
* @see org.gvsig.fmap.geom.Geometry#convertToWKBForcingType(int, int) |
|
426 |
*/ |
|
427 |
public byte[] convertToWKBForcingType(int srs, int type) throws GeometryOperationNotSupportedException, |
|
428 |
GeometryOperationException { |
|
429 |
int subType = getGeometryType().getSubType(); |
|
430 |
boolean is3D = subType == 1 || subType == 3; |
|
431 |
|
|
432 |
WKBWriter write = null; |
|
433 |
if (is3D) |
|
434 |
write = new WKBWriter(3); |
|
435 |
else |
|
436 |
write = new WKBWriter(2); |
|
437 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
|
438 |
jts = JTSUtils.convertTypes(jts, this.getType(), type); |
|
439 |
jts.setSRID(srs); |
|
440 |
return write.write(jts); |
|
441 |
} |
|
442 |
|
|
443 |
/* |
|
444 |
* (non-Javadoc) |
|
445 |
* |
|
446 |
* @see org.gvsig.fmap.geom.Geometry#convertToWKT() |
|
447 |
*/ |
|
448 |
public String convertToWKT() throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
449 |
int subType = getGeometryType().getSubType(); |
|
450 |
boolean is3D = subType == 1 || subType == 3; |
|
451 |
|
|
452 |
WKTWriter write = null; |
|
453 |
if (is3D) |
|
454 |
write = new WKTWriter(3); |
|
455 |
else |
|
456 |
write = new WKTWriter(2); |
|
457 |
com.vividsolutions.jts.geom.Geometry jts = getJTS(); |
|
458 |
return write.write(jts); |
|
459 |
} |
|
460 |
|
|
461 |
/* |
|
462 |
* (non-Javadoc) |
|
463 |
* |
|
464 |
* @see org.gvsig.fmap.geom.Geometry#buffer(double) |
|
465 |
*/ |
|
466 |
public org.gvsig.fmap.geom.Geometry buffer(double distance) throws GeometryOperationNotSupportedException, |
|
467 |
GeometryOperationException { |
|
468 |
return JTSUtils.createGeometry(getJTS().buffer(distance)); |
|
469 |
} |
|
470 |
|
|
471 |
/* |
|
472 |
* (non-Javadoc) |
|
473 |
* |
|
474 |
* @see org.gvsig.fmap.geom.Geometry#snapTo(org.gvsig.fmap.geom.Geometry, |
|
475 |
* double) |
|
476 |
*/ |
|
477 |
public org.gvsig.fmap.geom.Geometry snapTo(org.gvsig.fmap.geom.Geometry other, double snapTolerance) |
|
478 |
throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
479 |
Geometry result = null; |
|
480 |
GeometrySnapper snapper = new GeometrySnapper(getJTS()); |
|
481 |
com.vividsolutions.jts.geom.Geometry jts_result = snapper.snapTo(((GeometryJTS) other).getJTS(), snapTolerance); |
|
482 |
result = JTSUtils.createGeometry(jts_result); |
|
483 |
return result; |
|
484 |
} |
|
485 |
|
|
486 |
/* |
|
487 |
* (non-Javadoc) |
|
488 |
* |
|
489 |
* @see org.gvsig.fmap.geom.Geometry#getInteriorPoint() |
|
490 |
*/ |
|
491 |
public Point getInteriorPoint() throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
492 |
|
|
493 |
com.vividsolutions.jts.geom.Geometry geometry = getJTS(); |
|
494 |
com.vividsolutions.jts.geom.Point point = geometry.getInteriorPoint(); |
|
495 |
Geometry result = JTSUtils.createGeometry(point); |
|
496 |
return (Point) result; |
|
497 |
} |
|
498 |
|
|
499 |
/* |
|
500 |
* (non-Javadoc) |
|
501 |
* |
|
502 |
* @see org.gvsig.fmap.geom.Geometry#isValid() |
|
503 |
*/ |
|
504 |
public boolean isValid() { |
|
505 |
return getJTS().isValid(); |
|
506 |
} |
|
507 |
|
|
508 |
/* |
|
509 |
* (non-Javadoc) |
|
510 |
* |
|
511 |
* @see org.gvsig.fmap.geom.Geometry#getValidationStatus() |
|
512 |
*/ |
|
513 |
public ValidationStatus getValidationStatus() { |
|
514 |
DefaultValidationStatus status = new DefaultValidationStatus(ValidationStatus.VALID, null); |
|
515 |
com.vividsolutions.jts.geom.Geometry jtsgeom = null; |
|
516 |
try { |
|
517 |
jtsgeom = this.getJTS(); |
|
518 |
IsValidOp validOp = new IsValidOp(jtsgeom); |
|
519 |
if (validOp != null) { |
|
520 |
status.setValidationError(validOp.getValidationError()); |
|
521 |
} |
|
522 |
} catch (Exception ex) { |
|
523 |
status.setStatusCode(ValidationStatus.CURRUPTED); |
|
524 |
status.setMesage("The geometry is corrupted."); |
|
525 |
if (this instanceof OrientableSurface) { |
|
526 |
int vertices = ((OrientableSurface) this).getNumVertices(); |
|
527 |
if (vertices < 3) { |
|
528 |
status.setStatusCode(ValidationStatus.TOO_FEW_POINTS); |
|
529 |
status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]); |
|
530 |
} |
|
531 |
} else if (this instanceof OrientableCurve) { |
|
532 |
int vertices = ((OrientableCurve) this).getNumVertices(); |
|
533 |
if (vertices < 2) { |
|
534 |
status.setStatusCode(ValidationStatus.TOO_FEW_POINTS); |
|
535 |
status.setMesage(TopologyValidationError.errMsg[TopologyValidationError.TOO_FEW_POINTS]); |
|
536 |
} |
|
537 |
} |
|
538 |
} |
|
539 |
return status; |
|
540 |
} |
|
541 |
|
|
542 |
/* |
|
543 |
* (non-Javadoc) |
|
544 |
* |
|
545 |
* @see org.gvsig.fmap.geom.Geometry#makeValid() |
|
546 |
*/ |
|
547 |
public org.gvsig.fmap.geom.Geometry makeValid() { |
|
548 |
try { |
|
549 |
ValidationStatus vs = this.getValidationStatus(); |
|
550 |
if (vs.isValid()) { |
|
551 |
return this; |
|
552 |
} |
|
553 |
Geometry g = null; |
|
554 |
switch (vs.getStatusCode()) { |
|
555 |
case Geometry.ValidationStatus.RING_SELF_INTERSECTION: |
|
556 |
case Geometry.ValidationStatus.SELF_INTERSECTION: |
|
557 |
g = this.buffer(0); |
|
558 |
if (g.isValid()) { |
|
559 |
return g; |
|
560 |
} |
|
561 |
break; |
|
562 |
|
|
563 |
case Geometry.ValidationStatus.TOO_FEW_POINTS: |
|
564 |
if (this instanceof OrientableCurve) { |
|
565 |
int vertices = ((OrientableCurve) this).getNumVertices(); |
|
566 |
if (vertices < 2) { |
|
567 |
return null; // new |
|
568 |
// DefaultNullGeometry(this.getGeometryType()); |
|
569 |
} |
|
570 |
} |
|
571 |
if (this instanceof OrientableSurface) { |
|
572 |
int vertices = ((OrientableSurface) this).getNumVertices(); |
|
573 |
if (vertices < 3) { |
|
574 |
return null; // new |
|
575 |
// DefaultNullGeometry(this.getGeometryType()); |
|
576 |
} |
|
577 |
} |
|
578 |
} |
|
579 |
} catch (Exception ex) { |
|
580 |
return null; |
|
581 |
} |
|
582 |
return null; |
|
583 |
} |
|
584 |
|
|
585 |
/* |
|
586 |
* (non-Javadoc) |
|
587 |
* |
|
588 |
* @see org.gvsig.fmap.geom.Geometry#getBounds2D() |
|
589 |
*/ |
|
590 |
public Rectangle2D getBounds2D() { |
|
591 |
com.vividsolutions.jts.geom.Envelope envInternal = getJTS().getEnvelopeInternal(); |
|
592 |
return new Rectangle2D.Double(envInternal.getMinX(), envInternal.getMinY(), envInternal.getWidth(), |
|
593 |
envInternal.getHeight()); |
|
594 |
} |
|
595 |
|
|
596 |
/* |
|
597 |
* (non-Javadoc) |
|
598 |
* |
|
599 |
* @see java.awt.Shape#getBounds() |
|
600 |
*/ |
|
601 |
public Rectangle getBounds() { |
|
602 |
return this.getShape().getBounds(); |
|
603 |
} |
|
604 |
|
|
605 |
/* |
|
606 |
* (non-Javadoc) |
|
607 |
* |
|
608 |
* @see org.gvsig.fmap.geom.Geometry#getInternalShape() |
|
609 |
*/ |
|
610 |
public Shape getInternalShape() { |
|
611 |
return getShape(); |
|
612 |
} |
|
613 |
|
|
614 |
public void rotate(double radAngle, double basex, double basey) { |
|
615 |
|
|
616 |
AffineTransform at = new AffineTransform(); |
|
617 |
at.rotate(radAngle, basex, basey); |
|
618 |
this.transform(at); |
|
619 |
} |
|
620 |
|
|
621 |
public void move(double dx, double dy) { |
|
622 |
|
|
623 |
AffineTransform at = new AffineTransform(); |
|
624 |
at.translate(dx, dy); |
|
625 |
this.transform(at); |
|
626 |
} |
|
627 |
|
|
628 |
public void scale(Point basePoint, double sx, double sy) { |
|
629 |
|
|
630 |
AffineTransform at = new AffineTransform(); |
|
631 |
at.setToTranslation(basePoint.getX(), basePoint.getY()); |
|
632 |
at.scale(sx, sy); |
|
633 |
at.translate(-basePoint.getX(), -basePoint.getY()); |
|
634 |
this.transform(at); |
|
635 |
} |
|
636 |
|
|
637 |
public Geometry[] closestPoints(Geometry other) throws GeometryOperationNotSupportedException, |
|
638 |
GeometryOperationException { |
|
639 |
Point[] points = null; |
|
640 |
|
|
641 |
Coordinate[] jts_points = DistanceOp.nearestPoints(getJTS(), ((GeometryJTS) other).getJTS()); |
|
642 |
points = new Point[jts_points.length]; |
|
643 |
for (int i = 0; i < jts_points.length; i++) { |
|
644 |
try { |
|
645 |
points[i] = JTSUtils.createPoint(this.getGeometryType(), jts_points[i]); |
|
646 |
} catch (CreateGeometryException e) { |
|
647 |
throw new GeometryOperationException(e); |
|
648 |
} |
|
649 |
} |
|
650 |
|
|
651 |
return (Geometry[]) points; |
|
652 |
} |
|
653 |
|
|
654 |
public Geometry convexHull() throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
655 |
|
|
656 |
return JTSUtils.createGeometry(getJTS().convexHull()); |
|
657 |
} |
|
658 |
|
|
659 |
public Geometry difference(Geometry other) throws GeometryOperationNotSupportedException, |
|
660 |
GeometryOperationException { |
|
661 |
return JTSUtils.createGeometry(getJTS().difference(((GeometryJTS) other).getJTS())); |
|
662 |
} |
|
663 |
|
|
664 |
public Geometry intersection(Geometry other) throws GeometryOperationNotSupportedException, |
|
665 |
GeometryOperationException { |
|
666 |
return JTSUtils.createGeometry(getJTS().intersection(((GeometryJTS) other).getJTS())); |
|
667 |
} |
|
668 |
|
|
669 |
public Geometry union(Geometry other) throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
670 |
return JTSUtils.createGeometry(getJTS().union(((GeometryJTS) other).getJTS())); |
|
671 |
} |
|
672 |
|
|
673 |
public org.gvsig.fmap.geom.primitive.Point centroid() throws GeometryOperationNotSupportedException, |
|
674 |
GeometryOperationException { |
|
675 |
try { |
|
676 |
return JTSUtils.createPoint(this.getGeometryType(), getJTS().getCentroid().getCoordinate()); |
|
677 |
} catch (CreateGeometryException e) { |
|
678 |
throw new GeometryOperationException(e); |
|
679 |
} |
|
680 |
} |
|
681 |
|
|
682 |
protected void notifyDeprecated(String message) { |
|
683 |
logger.info(message); |
|
684 |
|
|
685 |
} |
|
686 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/coerce/CoerceToString.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.geom.jts.coerce; |
|
25 |
|
|
26 |
import org.gvsig.fmap.geom.Geometry; |
|
27 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
28 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
|
29 |
|
|
30 |
/** |
|
31 |
* Convert a Geometry to String encoded as WKT. |
|
32 |
* |
|
33 |
* |
|
34 |
* @author gvSIG Team |
|
35 |
* @version $Id$ |
|
36 |
* |
|
37 |
*/ |
|
38 |
public class CoerceToString implements Coercion { |
|
39 |
|
|
40 |
Coercion previous = null; |
|
41 |
|
|
42 |
public CoerceToString() { |
|
43 |
// Do nothing |
|
44 |
} |
|
45 |
|
|
46 |
public CoerceToString(Coercion previous) { |
|
47 |
this.previous = previous; |
|
48 |
} |
|
49 |
|
|
50 |
|
|
51 |
public Object coerce(Object value) throws CoercionException { |
|
52 |
try { |
|
53 |
if( value == null || value instanceof String ) { |
|
54 |
return value; |
|
55 |
} |
|
56 |
if( value instanceof Geometry ) { |
|
57 |
return ((Geometry)value).convertToWKT(); |
|
58 |
} |
|
59 |
if( previous != null ) { |
|
60 |
return previous.coerce(value); |
|
61 |
} |
|
62 |
} catch (Exception e) { |
|
63 |
throw new CoercionException(e); |
|
64 |
} |
|
65 |
throw new CoercionException(); |
|
66 |
} |
|
67 |
|
|
68 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/coerce/CoerceToGeometry.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.geom.jts.coerce; |
|
25 |
|
|
26 |
import org.gvsig.fmap.geom.Geometry; |
|
27 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
28 |
import org.gvsig.fmap.geom.GeometryManager; |
|
29 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
30 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
|
31 |
|
|
32 |
/** |
|
33 |
* Convert a object to Geometry. |
|
34 |
* |
|
35 |
* Support convert: |
|
36 |
* - Geometry to Geometry (do nothing) |
|
37 |
* - WKB (byte[]) to Geometry |
|
38 |
* - WKT (String/toString) to Geometry |
|
39 |
* |
|
40 |
* |
|
41 |
* @author gvSIG Team |
|
42 |
* @version $Id$ |
|
43 |
* |
|
44 |
*/ |
|
45 |
public class CoerceToGeometry implements Coercion { |
|
46 |
|
|
47 |
public Object coerce(Object value) throws CoercionException { |
|
48 |
try { |
|
49 |
if( value == null || value instanceof Geometry ) { |
|
50 |
return value; |
|
51 |
} |
|
52 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
|
53 |
if( value instanceof byte[] ) { |
|
54 |
return manager.createFrom((byte[])value); |
|
55 |
} |
|
56 |
return manager.createFrom(value.toString()); |
|
57 |
} catch (Exception e) { |
|
58 |
throw new CoercionException(e); |
|
59 |
} |
|
60 |
|
|
61 |
} |
|
62 |
|
|
63 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/coerce/CoerceToEnvelope.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.geom.jts.coerce; |
|
25 |
|
|
26 |
import org.gvsig.fmap.geom.Geometry; |
|
27 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
28 |
import org.gvsig.fmap.geom.GeometryManager; |
|
29 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
30 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
31 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
|
32 |
/** |
|
33 |
* Convert a object to Envelope. |
|
34 |
* |
|
35 |
* Support convert: |
|
36 |
* - Envelope to Envelope (do nothing) |
|
37 |
* - Geometry to Envelope |
|
38 |
* - WKB (byte[]) to Geometry |
|
39 |
* - WKT (String/toString) to Geometry |
|
40 |
* |
|
41 |
* |
|
42 |
* @author gvSIG Team |
|
43 |
* @version $Id$ |
|
44 |
* |
|
45 |
*/ |
|
46 |
public class CoerceToEnvelope implements Coercion { |
|
47 |
|
|
48 |
public Object coerce(Object value) throws CoercionException { |
|
49 |
try { |
|
50 |
if(value == null || value instanceof Envelope ) { |
|
51 |
return value; |
|
52 |
} |
|
53 |
if( value instanceof Geometry ) { |
|
54 |
return ((Geometry)value).getEnvelope(); |
|
55 |
} |
|
56 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
|
57 |
if( value instanceof byte[] ) { |
|
58 |
Geometry geom = manager.createFrom((byte[])value); |
|
59 |
return geom.getEnvelope(); |
|
60 |
} |
|
61 |
Geometry geom = manager.createFrom(value.toString()); |
|
62 |
return geom.getEnvelope(); |
|
63 |
} catch (Exception e) { |
|
64 |
throw new CoercionException(e); |
|
65 |
} |
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/coerce/CoerceToByteArray.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
23 |
*/ |
|
24 |
package org.gvsig.fmap.geom.jts.coerce; |
|
25 |
|
|
26 |
import org.gvsig.fmap.geom.Geometry; |
|
27 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
28 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
|
29 |
|
|
30 |
/** |
|
31 |
* Convert a Geometry to Byte Array encoded as WKB. |
|
32 |
* |
|
33 |
* @author gvSIG Team |
|
34 |
* @version $Id$ |
|
35 |
* |
|
36 |
*/ |
|
37 |
public class CoerceToByteArray implements Coercion { |
|
38 |
|
|
39 |
Coercion previous = null; |
|
40 |
|
|
41 |
public CoerceToByteArray() { |
|
42 |
// Do nothing |
|
43 |
} |
|
44 |
|
|
45 |
public CoerceToByteArray(Coercion previous) { |
|
46 |
this.previous = previous; |
|
47 |
} |
|
48 |
|
|
49 |
|
|
50 |
public Object coerce(Object value) throws CoercionException { |
|
51 |
try { |
|
52 |
if( value == null || value instanceof byte[] ) { |
|
53 |
return value; |
|
54 |
} |
|
55 |
if( value instanceof Geometry ) { |
|
56 |
return ((Geometry)value).convertToWKT(); |
|
57 |
} |
|
58 |
if( previous != null ) { |
|
59 |
return previous.coerce(value); |
|
60 |
} |
|
61 |
} catch (Exception e) { |
|
62 |
throw new CoercionException(e); |
|
63 |
} |
|
64 |
throw new CoercionException(); |
|
65 |
} |
|
66 |
|
|
67 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/utils/ArrayListCoordinateSequence.java | ||
---|---|---|
1 |
/* gvSIG. Desktop Geographic Information System. |
|
2 |
* |
|
3 |
* Copyright ? 2007-2015 gvSIG Association |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU General Public License |
|
7 |
* as published by the Free Software Foundation; either version 2 |
|
8 |
* of the License, or (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 |
* MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us |
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.fmap.geom.jts.utils; |
|
24 |
|
|
25 |
import java.util.ArrayList; |
|
26 |
import java.util.Collection; |
|
27 |
|
|
28 |
import com.vividsolutions.jts.geom.Coordinate; |
|
29 |
import com.vividsolutions.jts.geom.CoordinateSequence; |
|
30 |
import com.vividsolutions.jts.geom.Envelope; |
|
31 |
|
|
32 |
|
|
33 |
/** |
|
34 |
* @author fdiaz |
|
35 |
* |
|
36 |
*/ |
|
37 |
public class ArrayListCoordinateSequence extends ArrayList<Coordinate> implements CoordinateSequence { |
|
38 |
|
|
39 |
|
|
40 |
/** |
|
41 |
* |
|
42 |
*/ |
|
43 |
public ArrayListCoordinateSequence(Collection<Coordinate> coordinates) { |
|
44 |
super(coordinates); |
|
45 |
} |
|
46 |
|
|
47 |
/* (non-Javadoc) |
|
48 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#getDimension() |
|
49 |
*/ |
|
50 |
public int getDimension() { |
|
51 |
// TODO Auto-generated method stub |
|
52 |
return 0; |
|
53 |
} |
|
54 |
|
|
55 |
/* (non-Javadoc) |
|
56 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#getCoordinate(int) |
|
57 |
*/ |
|
58 |
public com.vividsolutions.jts.geom.Coordinate getCoordinate(int i) { |
|
59 |
return (com.vividsolutions.jts.geom.Coordinate) get(i); |
|
60 |
} |
|
61 |
|
|
62 |
/* (non-Javadoc) |
|
63 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#getCoordinateCopy(int) |
|
64 |
*/ |
|
65 |
public com.vividsolutions.jts.geom.Coordinate getCoordinateCopy(int i) { |
|
66 |
return (com.vividsolutions.jts.geom.Coordinate) ((com.vividsolutions.jts.geom.Coordinate)get(i)).clone(); |
|
67 |
} |
|
68 |
|
|
69 |
/* (non-Javadoc) |
|
70 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#getCoordinate(int, com.vividsolutions.jts.geom.Coordinate) |
|
71 |
*/ |
|
72 |
public void getCoordinate(int index, com.vividsolutions.jts.geom.Coordinate coord) { |
|
73 |
// TODO Auto-generated method stub |
|
74 |
|
|
75 |
} |
|
76 |
|
|
77 |
/* (non-Javadoc) |
|
78 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#getX(int) |
|
79 |
*/ |
|
80 |
public double getX(int index) { |
|
81 |
// TODO Auto-generated method stub |
|
82 |
return 0; |
|
83 |
} |
|
84 |
|
|
85 |
/* (non-Javadoc) |
|
86 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#getY(int) |
|
87 |
*/ |
|
88 |
public double getY(int index) { |
|
89 |
// TODO Auto-generated method stub |
|
90 |
return 0; |
|
91 |
} |
|
92 |
|
|
93 |
/* (non-Javadoc) |
|
94 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#getOrdinate(int, int) |
|
95 |
*/ |
|
96 |
public double getOrdinate(int index, int ordinateIndex) { |
|
97 |
// TODO Auto-generated method stub |
|
98 |
return 0; |
|
99 |
} |
|
100 |
|
|
101 |
/* (non-Javadoc) |
|
102 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#setOrdinate(int, int, double) |
|
103 |
*/ |
|
104 |
public void setOrdinate(int index, int ordinateIndex, double value) { |
|
105 |
// TODO Auto-generated method stub |
|
106 |
|
|
107 |
} |
|
108 |
|
|
109 |
/* (non-Javadoc) |
|
110 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#toCoordinateArray() |
|
111 |
*/ |
|
112 |
public com.vividsolutions.jts.geom.Coordinate[] toCoordinateArray() { |
|
113 |
// TODO Auto-generated method stub |
|
114 |
return null; |
|
115 |
} |
|
116 |
|
|
117 |
/* (non-Javadoc) |
|
118 |
* @see com.vividsolutions.jts.geom.CoordinateSequence#expandEnvelope(com.vividsolutions.jts.geom.Envelope) |
|
119 |
*/ |
|
120 |
public Envelope expandEnvelope(Envelope env) { |
|
121 |
// TODO Auto-generated method stub |
|
122 |
return null; |
|
123 |
} |
|
124 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/utils/JTSUtils.java | ||
---|---|---|
1 |
/* gvSIG. Desktop Geographic Information System. |
|
2 |
* |
|
3 |
* Copyright ? 2007-2015 gvSIG Association |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU General Public License |
|
7 |
* as published by the Free Software Foundation; either version 2 |
|
8 |
* of the License, or (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 |
* MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us |
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.fmap.geom.jts.utils; |
|
24 |
|
|
25 |
import com.vividsolutions.jts.geom.Coordinate; |
|
26 |
import com.vividsolutions.jts.geom.CoordinateSequence; |
|
27 |
import com.vividsolutions.jts.geom.LineString; |
|
28 |
import com.vividsolutions.jts.geom.LinearRing; |
|
29 |
import com.vividsolutions.jts.geom.MultiLineString; |
|
30 |
import com.vividsolutions.jts.geom.Polygon; |
|
31 |
|
|
32 |
import org.gvsig.fmap.geom.Geometry; |
|
33 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
34 |
import org.gvsig.fmap.geom.jts.MCoordinate; |
|
35 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D; |
|
36 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2DM; |
|
37 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3D; |
|
38 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3DM; |
|
39 |
import org.gvsig.fmap.geom.jts.primitive.point.Point2D; |
|
40 |
import org.gvsig.fmap.geom.jts.primitive.point.Point2DM; |
|
41 |
import org.gvsig.fmap.geom.jts.primitive.point.Point3D; |
|
42 |
import org.gvsig.fmap.geom.jts.primitive.point.Point3DM; |
|
43 |
import org.gvsig.fmap.geom.primitive.Point; |
|
44 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
45 |
|
|
46 |
/** |
|
47 |
* @author fdiaz |
|
48 |
* |
|
49 |
*/ |
|
50 |
public class JTSUtils { |
|
51 |
|
|
52 |
public static final com.vividsolutions.jts.geom.GeometryFactory factory = |
|
53 |
new com.vividsolutions.jts.geom.GeometryFactory(); |
|
54 |
|
|
55 |
public static Point createPoint(GeometryType type, Coordinate coordinate) throws CreateGeometryException { |
|
56 |
|
|
57 |
switch (type.getSubType()) { |
|
58 |
case Geometry.SUBTYPES.GEOM2D: |
|
59 |
return new Point2D(coordinate); |
|
60 |
case Geometry.SUBTYPES.GEOM2DM: |
|
61 |
return new Point2DM(coordinate); |
|
62 |
case Geometry.SUBTYPES.GEOM3D: |
|
63 |
return new Point3D(coordinate); |
|
64 |
case Geometry.SUBTYPES.GEOM3DM: |
|
65 |
return new Point3DM(coordinate); |
|
66 |
default: |
|
67 |
Point p = null; |
|
68 |
p = (Point) type.create(); |
|
69 |
for (int i = 0; i < p.getDimension(); i++) { |
|
70 |
p.setCoordinateAt(i, coordinate.getOrdinate(i)); |
|
71 |
} |
|
72 |
break; |
|
73 |
} |
|
74 |
|
|
75 |
return null; |
|
76 |
} |
|
77 |
|
|
78 |
public static LineString createJTSLineString(CoordinateSequence coordinates){ |
|
79 |
return factory.createLineString(coordinates); |
|
80 |
} |
|
81 |
|
|
82 |
public static Polygon createJTSPolygon(CoordinateSequence coordinates){ |
|
83 |
return factory.createPolygon(coordinates); |
|
84 |
} |
|
85 |
|
|
86 |
public static Geometry createGeometry(com.vividsolutions.jts.geom.Geometry jtsGeom) { |
|
87 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.Point){ |
|
88 |
Coordinate coordinate = jtsGeom.getCoordinate(); |
|
89 |
if(coordinate instanceof MCoordinate){ |
|
90 |
if(Double.isNaN(coordinate.z)){ |
|
91 |
return new Point2DM(coordinate); |
|
92 |
} else { |
|
93 |
return new Point3DM(coordinate); |
|
94 |
} |
|
95 |
} else { |
|
96 |
if(Double.isNaN(coordinate.z)){ |
|
97 |
return new Point2D(coordinate); |
|
98 |
} else { |
|
99 |
return new Point3D(coordinate); |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.LineString){ |
|
105 |
Coordinate[] coordinates = jtsGeom.getCoordinates(); |
|
106 |
Coordinate coordinate = jtsGeom.getCoordinate(); |
|
107 |
if(coordinate instanceof MCoordinate){ |
|
108 |
if(Double.isNaN(coordinate.z)){ |
|
109 |
return new Line2DM(coordinates); |
|
110 |
} else { |
|
111 |
return new Line3DM(coordinates); |
|
112 |
} |
|
113 |
} else { |
|
114 |
if(Double.isNaN(coordinate.z)){ |
|
115 |
return new Line2D(coordinates); |
|
116 |
} else { |
|
117 |
return new Line3D(coordinates); |
|
118 |
} |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.Polygon){ |
|
123 |
|
|
124 |
} |
|
125 |
|
|
126 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.GeometryCollection){ |
|
127 |
|
|
128 |
} |
|
129 |
|
|
130 |
|
|
131 |
return null; |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* This function is called when the we need force types, that is the |
|
136 |
* destination |
|
137 |
* type does not match with the input geometry type |
|
138 |
* |
|
139 |
* @param g |
|
140 |
* @param sourceType |
|
141 |
* @param destinationType |
|
142 |
* @return |
|
143 |
*/ |
|
144 |
public static com.vividsolutions.jts.geom.Geometry convertTypes(com.vividsolutions.jts.geom.Geometry g, |
|
145 |
int sourceType, int destinationType) { |
|
146 |
if ((sourceType == Geometry.TYPES.CURVE || sourceType == Geometry.TYPES.SPLINE |
|
147 |
|| sourceType == Geometry.TYPES.ARC || sourceType == Geometry.TYPES.ELLIPTICARC) |
|
148 |
&& destinationType == Geometry.TYPES.MULTISURFACE) { |
|
149 |
if (g instanceof MultiLineString) { |
|
150 |
Polygon[] poly = new Polygon[((MultiLineString) g).getNumGeometries()]; |
|
151 |
for (int i = 0; i < ((MultiLineString) g).getNumGeometries(); i++) { |
|
152 |
com.vividsolutions.jts.geom.Geometry lineString = ((MultiLineString) g).getGeometryN(i); |
|
153 |
poly[i] = convertLineStringToPolygon((LineString) lineString); |
|
154 |
} |
|
155 |
return factory.createMultiPolygon(poly); |
|
156 |
} else { |
|
157 |
return convertLineStringToPolygon((LineString) g); |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
if ((sourceType == Geometry.TYPES.CIRCLE || sourceType == Geometry.TYPES.ELLIPSE) |
|
162 |
&& destinationType == Geometry.TYPES.MULTICURVE) { |
|
163 |
if (g instanceof Polygon) { |
|
164 |
Polygon poly = (Polygon) g; |
|
165 |
LineString lineString = factory.createLinearRing(poly.getCoordinates()); |
|
166 |
return factory.createMultiLineString(new LineString[] { lineString }); |
|
167 |
} |
|
168 |
} |
|
169 |
return g; |
|
170 |
} |
|
171 |
|
|
172 |
private static com.vividsolutions.jts.geom.Polygon convertLineStringToPolygon(LineString line) { |
|
173 |
Coordinate[] coordinates = line.getCoordinates(); |
|
174 |
LinearRing shell = factory.createLinearRing(coordinates); |
|
175 |
Polygon pol = factory.createPolygon(shell, null); |
|
176 |
return pol; |
|
177 |
} |
|
178 |
|
|
179 |
public static MCoordinate createMCoordinate(double x, double y, double m) { |
|
180 |
return (MCoordinate) MCoordinate.create2dWithMeasure(x, y, m); |
|
181 |
} |
|
182 |
|
|
183 |
public static MCoordinate createMCoordinate(double x, double y, double z, double m) { |
|
184 |
return (MCoordinate) MCoordinate.create3dWithMeasure(x, y, z, m); |
|
185 |
} |
|
186 |
|
|
187 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/utils/ReadOnlyCoordinates.java | ||
---|---|---|
1 |
/* gvSIG. Desktop Geographic Information System. |
|
2 |
* |
|
3 |
* Copyright ? 2007-2015 gvSIG Association |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU General Public License |
|
7 |
* as published by the Free Software Foundation; either version 2 |
|
8 |
* of the License, or (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 |
* MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us |
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.fmap.geom.jts.utils; |
|
24 |
|
|
25 |
import java.util.Collection; |
|
26 |
import java.util.Iterator; |
|
27 |
|
|
28 |
import com.vividsolutions.jts.geom.Coordinate; |
|
29 |
|
|
30 |
|
|
31 |
/** |
|
32 |
* @author fdiaz |
|
33 |
* |
|
34 |
*/ |
|
35 |
public class ReadOnlyCoordinates implements Collection<Coordinate> { |
|
36 |
|
|
37 |
Coordinate coordinates[]; |
|
38 |
|
|
39 |
/** |
|
40 |
* |
|
41 |
*/ |
|
42 |
public ReadOnlyCoordinates(Coordinate coordinates[]) { |
|
43 |
this.coordinates = coordinates; |
|
44 |
} |
|
45 |
|
|
46 |
/* (non-Javadoc) |
|
47 |
* @see java.util.Collection#size() |
|
48 |
*/ |
|
49 |
public int size() { |
|
50 |
return this.coordinates.length; |
|
51 |
} |
|
52 |
|
|
53 |
/* (non-Javadoc) |
|
54 |
* @see java.util.Collection#isEmpty() |
|
55 |
*/ |
|
56 |
public boolean isEmpty() { |
|
57 |
return this.coordinates.length==0; |
|
58 |
} |
|
59 |
|
|
60 |
/* (non-Javadoc) |
|
61 |
* @see java.util.Collection#contains(java.lang.Object) |
|
62 |
*/ |
|
63 |
public boolean contains(Object o) { |
|
64 |
for (int i = 0; i < coordinates.length; i++) { |
|
65 |
if(coordinates[i].equals(o)){ |
|
66 |
return true; |
|
67 |
} |
|
68 |
} |
|
69 |
return false; |
|
70 |
} |
|
71 |
|
|
72 |
/* (non-Javadoc) |
|
73 |
* @see java.util.Collection#iterator() |
|
74 |
*/ |
|
75 |
public Iterator<Coordinate> iterator() { |
|
76 |
return new Iterator<Coordinate>() { |
|
77 |
|
|
78 |
int i=0; |
|
79 |
|
|
80 |
public boolean hasNext() { |
|
81 |
return i<coordinates.length; |
|
82 |
} |
|
83 |
|
|
84 |
public Coordinate next() { |
|
85 |
return coordinates[i++]; |
|
86 |
} |
|
87 |
|
|
88 |
public void remove() { |
|
89 |
throw new UnsupportedOperationException(); |
|
90 |
} |
|
91 |
}; |
|
92 |
} |
|
93 |
|
|
94 |
/* (non-Javadoc) |
|
95 |
* @see java.util.Collection#toArray() |
|
96 |
*/ |
|
97 |
public Object[] toArray() { |
|
98 |
return this.coordinates; |
|
99 |
} |
|
100 |
|
|
101 |
/* (non-Javadoc) |
|
102 |
* @see java.util.Collection#toArray(java.lang.Object[]) |
|
103 |
*/ |
|
104 |
public <T> T[] toArray(T[] a) { |
|
105 |
return (T[]) this.coordinates; |
|
106 |
} |
|
107 |
|
|
108 |
/* (non-Javadoc) |
|
109 |
* @see java.util.Collection#add(java.lang.Object) |
|
110 |
*/ |
|
111 |
public boolean add(Coordinate e) { |
|
112 |
throw new UnsupportedOperationException(); |
|
113 |
} |
|
114 |
|
|
115 |
/* (non-Javadoc) |
|
116 |
* @see java.util.Collection#remove(java.lang.Object) |
|
117 |
*/ |
|
118 |
public boolean remove(Object o) { |
|
119 |
throw new UnsupportedOperationException(); |
|
120 |
} |
|
121 |
|
|
122 |
/* (non-Javadoc) |
|
123 |
* @see java.util.Collection#containsAll(java.util.Collection) |
|
124 |
*/ |
|
125 |
public boolean containsAll(Collection<?> c) { |
|
126 |
for (Iterator iterator = c.iterator(); iterator.hasNext();) { |
|
127 |
Object object = (Object) iterator.next(); |
|
128 |
if(contains(object)){ |
|
129 |
return true; |
|
130 |
} |
|
131 |
} |
|
132 |
return false; |
|
133 |
} |
|
134 |
|
|
135 |
/* (non-Javadoc) |
|
136 |
* @see java.util.Collection#addAll(java.util.Collection) |
|
137 |
*/ |
|
138 |
public boolean addAll(Collection<? extends Coordinate> c) { |
|
139 |
throw new UnsupportedOperationException(); |
|
140 |
} |
|
141 |
|
|
142 |
/* (non-Javadoc) |
|
143 |
* @see java.util.Collection#removeAll(java.util.Collection) |
|
144 |
*/ |
|
145 |
public boolean removeAll(Collection<?> c) { |
|
146 |
throw new UnsupportedOperationException(); |
|
147 |
} |
|
148 |
|
|
149 |
/* (non-Javadoc) |
|
150 |
* @see java.util.Collection#retainAll(java.util.Collection) |
|
151 |
*/ |
|
152 |
public boolean retainAll(Collection<?> c) { |
|
153 |
throw new UnsupportedOperationException(); |
|
154 |
} |
|
155 |
|
|
156 |
/* (non-Javadoc) |
|
157 |
* @see java.util.Collection#clear() |
|
158 |
*/ |
|
159 |
public void clear() { |
|
160 |
throw new UnsupportedOperationException(); |
|
161 |
} |
|
162 |
|
|
163 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/DefaultGeometryType.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2013 gvSIG Association. |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 3 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
* For any additional information, do not hesitate to contact us |
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
Also available in: Unified diff