svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / util / Converter.java @ 28396
History | View | Annotate | Download (37.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.fmap.geom.util; |
42 |
|
43 |
import java.awt.Shape; |
44 |
import java.awt.geom.AffineTransform; |
45 |
import java.awt.geom.Area; |
46 |
import java.awt.geom.NoninvertibleTransformException; |
47 |
import java.awt.geom.PathIterator; |
48 |
import java.awt.geom.Rectangle2D; |
49 |
import java.lang.reflect.Array; |
50 |
import java.util.ArrayList; |
51 |
|
52 |
import org.gvsig.fmap.geom.Geometry; |
53 |
import org.gvsig.fmap.geom.GeometryLocator; |
54 |
import org.gvsig.fmap.geom.GeometryManager; |
55 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
56 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
57 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
58 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
59 |
import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
60 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
61 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
62 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
63 |
import org.gvsig.fmap.geom.primitive.Surface; |
64 |
import org.slf4j.Logger; |
65 |
import org.slf4j.LoggerFactory; |
66 |
|
67 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
68 |
import com.vividsolutions.jts.algorithm.RobustCGAlgorithms; |
69 |
import com.vividsolutions.jts.geom.Coordinate; |
70 |
import com.vividsolutions.jts.geom.CoordinateArrays; |
71 |
import com.vividsolutions.jts.geom.Envelope; |
72 |
import com.vividsolutions.jts.geom.GeometryCollection; |
73 |
import com.vividsolutions.jts.geom.LineString; |
74 |
import com.vividsolutions.jts.geom.LinearRing; |
75 |
import com.vividsolutions.jts.geom.MultiLineString; |
76 |
import com.vividsolutions.jts.geom.MultiPolygon; |
77 |
import com.vividsolutions.jts.geom.Point; |
78 |
import com.vividsolutions.jts.geom.Polygon; |
79 |
|
80 |
|
81 |
/**
|
82 |
* Clase con varios m?todos est?ticos utilizados para pasar de java2d a jts y
|
83 |
* viceversa.
|
84 |
*
|
85 |
* @author fjp
|
86 |
*/
|
87 |
public class Converter { |
88 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
89 |
private static final Logger logger = LoggerFactory.getLogger(Converter.class); |
90 |
|
91 |
//private static Logger logger = Logger.getLogger(Converter.class);
|
92 |
|
93 |
/**
|
94 |
* ?QU? PODEMOS HACER CON LOS MULTIPOINT??? => DEBER?AMOS TRABAJAR CON UN
|
95 |
* ARRAY DE PUNTOS EN FShape.....Pensarlo bien.
|
96 |
*/
|
97 |
public final static com.vividsolutions.jts.geom.GeometryFactory geomFactory = new com.vividsolutions.jts.geom.GeometryFactory(); |
98 |
public static CGAlgorithms cga = new RobustCGAlgorithms(); |
99 |
// private final static AffineTransform at = new AffineTransform();
|
100 |
//private static double POINT_MARKER_SIZE = 3.0;
|
101 |
|
102 |
/**
|
103 |
* Es la m?xima distancia que permitimos que el trazo aproximado
|
104 |
* difiera del trazo real.
|
105 |
*/
|
106 |
public static double FLATNESS =0.8;// Por ejemplo. Cuanto m?s peque?o, m?s segmentos necesitar? la curva |
107 |
|
108 |
private static GeometryManager manager = GeometryLocator.getGeometryManager(); |
109 |
|
110 |
|
111 |
//returns true if testPoint is a point in the pointList list.
|
112 |
static boolean pointInList(Coordinate testPoint, Coordinate[] pointList) { |
113 |
int t;
|
114 |
int numpoints;
|
115 |
Coordinate p; |
116 |
|
117 |
numpoints = Array.getLength(pointList);
|
118 |
|
119 |
for (t = 0; t < numpoints; t++) { |
120 |
p = pointList[t]; |
121 |
|
122 |
if ((testPoint.x == p.x) && (testPoint.y == p.y) &&
|
123 |
((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) //nan test; x!=x iff x is nan
|
124 |
) { |
125 |
return true; |
126 |
} |
127 |
} |
128 |
|
129 |
return false; |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Receives a JTS Geometry and returns a fmap IGeometry
|
134 |
* @param jtsGeometry jts Geometry
|
135 |
* @return IGeometry of FMap
|
136 |
* @author azabala
|
137 |
*/
|
138 |
public static Geometry jtsToGeometry(com.vividsolutions.jts.geom.Geometry geo){ |
139 |
Geometry shpNew = null;
|
140 |
|
141 |
try {
|
142 |
if (geo instanceof Point) { |
143 |
shpNew = new org.gvsig.fmap.geom.primitive.impl.Point2D(null, null, ((Point) geo).getX(), ((Point) geo).getY()); |
144 |
} |
145 |
|
146 |
if (geo.isEmpty()) {
|
147 |
shpNew = null;
|
148 |
} |
149 |
|
150 |
try{
|
151 |
if (geo instanceof Polygon) { |
152 |
shpNew = geomManager.createSurface(toShape((Polygon) geo), SUBTYPES.GEOM2D);
|
153 |
} |
154 |
|
155 |
if (geo instanceof MultiPolygon) { |
156 |
shpNew = geomManager.createSurface(toShape((MultiPolygon) geo), SUBTYPES.GEOM2D); |
157 |
} |
158 |
|
159 |
if (geo instanceof LineString) { |
160 |
shpNew = geomManager.createCurve(toShape((LineString) geo), SUBTYPES.GEOM2D); |
161 |
} |
162 |
|
163 |
if (geo instanceof MultiLineString) { |
164 |
shpNew = geomManager.createCurve(toShape((MultiLineString) geo), SUBTYPES.GEOM2D); |
165 |
} |
166 |
}catch(CreateGeometryException e){
|
167 |
logger.error("Error creating a geometry", e);
|
168 |
} |
169 |
|
170 |
/* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN GeometryCollection
|
171 |
* No sabremos si queremos una l?nea o un pol?gono.....
|
172 |
* if (geometry instanceof GeometryCollection) {
|
173 |
return toShape((GeometryCollection) geometry);
|
174 |
} */
|
175 |
return shpNew;
|
176 |
} catch (NoninvertibleTransformException e) { |
177 |
// TODO Auto-generated catch block
|
178 |
e.printStackTrace(); |
179 |
} |
180 |
|
181 |
return null; |
182 |
|
183 |
//
|
184 |
// FShape shape = Converter.jts_to_java2d(jtsGeometry);
|
185 |
// return factory.createGeometry(shape);
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* Convierte un MultiPoint2D a un MultiPoint de JTS
|
190 |
* @param geom
|
191 |
* @return
|
192 |
*/
|
193 |
public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPoint geom) {
|
194 |
Coordinate[] theGeoms = new Coordinate[geom.getPrimitivesNumber()]; |
195 |
for (int i = 0; i < theGeoms.length; i++) { |
196 |
java.awt.geom.Point2D p = geom.getPrimitiveAt(i) |
197 |
.getHandlers(Geometry.SELECTHANDLER)[0].getPoint();
|
198 |
Coordinate c = new Coordinate(p.getX(), p.getY());
|
199 |
theGeoms[i] = c; |
200 |
} |
201 |
com.vividsolutions.jts.geom.MultiPoint geomCol = new com.vividsolutions.jts.geom.GeometryFactory()
|
202 |
.createMultiPoint(theGeoms); |
203 |
return geomCol;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* Convierte una MultiCurve2D en una MultiLineString de JTS
|
208 |
* @param geom
|
209 |
* @return
|
210 |
*/
|
211 |
public static com.vividsolutions.jts.geom.Geometry geometryToJts(MultiCurve geom) { |
212 |
LineString[] lines = new LineString[geom.getPrimitivesNumber()]; |
213 |
for (int i = 0; i < lines.length; i++){ |
214 |
lines[i] = (LineString) geometryToJts((geom.getPrimitiveAt(i))); |
215 |
} |
216 |
return new com.vividsolutions.jts.geom.GeometryFactory().createMultiLineString(lines); |
217 |
} |
218 |
|
219 |
/**
|
220 |
* Convierte una MultiSurface2D en un MultiPolygon de JTS
|
221 |
* @return
|
222 |
*/
|
223 |
public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiSurface geom) {
|
224 |
Polygon[] polygons = new Polygon[geom.getPrimitivesNumber()]; |
225 |
for (int i = 0; i < polygons.length; i++){ |
226 |
polygons[i] = (Polygon) geometryToJts((geom.getPrimitiveAt(i)));
|
227 |
} |
228 |
return new com.vividsolutions.jts.geom.GeometryFactory().createMultiPolygon(polygons); |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Convierte una BaseMultiPrimitive en una GeometryCollection de JTS
|
233 |
* @return
|
234 |
*/
|
235 |
public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPrimitive geom) {
|
236 |
com.vividsolutions.jts.geom.Geometry[] geometriesAux = new LineString[geom.getPrimitivesNumber()]; |
237 |
for (int i = 0; i < geometriesAux.length; i++) { |
238 |
geometriesAux[i] = geometryToJts((geom.getPrimitiveAt(i))); |
239 |
} |
240 |
return new com.vividsolutions.jts.geom.GeometryFactory().createGeometryCollection(geometriesAux); |
241 |
} |
242 |
|
243 |
public static com.vividsolutions.jts.geom.Geometry geometryToJtsWithSRID( |
244 |
Geometry geom, int srid) {
|
245 |
// logger.debug(geom.getClass());
|
246 |
// logger.debug(new Integer(geom.getShapeType()));
|
247 |
return geometryToJts(geom, geom.getType(), srid);
|
248 |
} |
249 |
|
250 |
|
251 |
public static com.vividsolutions.jts.geom.Geometry geometryToJts(Geometry geom) { |
252 |
//logger.debug(geom.getClass());
|
253 |
//logger.debug(new Integer(geom.getShapeType()));
|
254 |
return geometryToJts(geom, geom.getType(), -1); |
255 |
} |
256 |
|
257 |
// public static com.vividsolutions.jts.geom.Geometry java2d_to_jts(FShape shp) {
|
258 |
// return java2d_to_jts(shp, shp.getShapeType());
|
259 |
// }
|
260 |
/**
|
261 |
* Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
|
262 |
* "flattened PathIterator". El flattened indica que las curvas las pasa a
|
263 |
* segmentos de l?nea recta AUTOMATICAMENTE!!!.
|
264 |
*
|
265 |
* @param shp FShape que se quiere convertir.
|
266 |
*
|
267 |
* @return Geometry de JTS.
|
268 |
*/
|
269 |
private static com.vividsolutions.jts.geom.Geometry geometryToJts( |
270 |
Geometry shp, int shapeType, int srid) { |
271 |
|
272 |
|
273 |
com.vividsolutions.jts.geom.Geometry geoJTS = null;
|
274 |
Coordinate coord; |
275 |
//Coordinate[] coords;
|
276 |
ArrayList arrayCoords = null; |
277 |
ArrayList arrayLines;
|
278 |
LineString lin; |
279 |
//LinearRing linRing;
|
280 |
//LinearRing linRingExt = null;
|
281 |
int theType;
|
282 |
int numParts = 0; |
283 |
|
284 |
// Use this array to store segment coordinate data
|
285 |
double[] theData = new double[6]; |
286 |
PathIterator theIterator;
|
287 |
|
288 |
//logger.debug(shp.toString());
|
289 |
|
290 |
|
291 |
switch (shapeType) {
|
292 |
case Geometry.TYPES.POINT:
|
293 |
org.gvsig.fmap.geom.primitive.impl.Point2D p = (org.gvsig.fmap.geom.primitive.impl.Point2D) shp; |
294 |
coord = new Coordinate(p.getX(), p.getY());
|
295 |
geoJTS = geomFactory.createPoint(coord); |
296 |
geoJTS.setSRID(srid); |
297 |
|
298 |
break;
|
299 |
|
300 |
case Geometry.TYPES.CURVE:
|
301 |
case Geometry.TYPES.ARC:
|
302 |
arrayLines = new ArrayList(); |
303 |
theIterator = shp.getPathIterator(null, FLATNESS);
|
304 |
|
305 |
while (!theIterator.isDone()) {
|
306 |
//while not done
|
307 |
theType = theIterator.currentSegment(theData); |
308 |
|
309 |
//Populate a segment of the new
|
310 |
// GeneralPathX object.
|
311 |
//Process the current segment to populate a new
|
312 |
// segment of the new GeneralPathX object.
|
313 |
switch (theType) {
|
314 |
case PathIterator.SEG_MOVETO: |
315 |
|
316 |
// System.out.println("SEG_MOVETO");
|
317 |
if (arrayCoords == null) { |
318 |
arrayCoords = new ArrayList(); |
319 |
} else {
|
320 |
lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray( |
321 |
arrayCoords)); |
322 |
lin.setSRID(srid); |
323 |
arrayLines.add(lin); |
324 |
arrayCoords = new ArrayList(); |
325 |
} |
326 |
|
327 |
numParts++; |
328 |
coord = new Coordinate(theData[0], theData[1]); |
329 |
|
330 |
arrayCoords.add(coord); |
331 |
|
332 |
break;
|
333 |
|
334 |
case PathIterator.SEG_LINETO: |
335 |
|
336 |
// System.out.println("SEG_LINETO");
|
337 |
arrayCoords.add(new Coordinate(theData[0], |
338 |
theData[1]));
|
339 |
|
340 |
break;
|
341 |
|
342 |
case PathIterator.SEG_QUADTO: |
343 |
System.out.println("Not supported here"); |
344 |
|
345 |
break;
|
346 |
|
347 |
case PathIterator.SEG_CUBICTO: |
348 |
System.out.println("Not supported here"); |
349 |
|
350 |
break;
|
351 |
|
352 |
case PathIterator.SEG_CLOSE: |
353 |
// A?adimos el primer punto para cerrar.
|
354 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
355 |
arrayCoords.add(new Coordinate(firstCoord.x,
|
356 |
firstCoord.y)); |
357 |
|
358 |
break;
|
359 |
} //end switch
|
360 |
|
361 |
theIterator.next(); |
362 |
} //end while loop
|
363 |
|
364 |
if (arrayCoords.size()<2) { |
365 |
break;
|
366 |
} |
367 |
lin = new com.vividsolutions.jts.geom.GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
|
368 |
arrayCoords)); |
369 |
|
370 |
lin.setSRID(srid); |
371 |
// CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
|
372 |
// LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
|
373 |
// O CON GEOTOOLS
|
374 |
// if (numParts > 1) // Generamos una MultiLineString
|
375 |
// {
|
376 |
arrayLines.add(lin); |
377 |
geoJTS = geomFactory.createMultiLineString(com.vividsolutions.jts.geom.GeometryFactory.toLineStringArray( |
378 |
arrayLines)); |
379 |
geoJTS.setSRID(srid); |
380 |
/* } else {
|
381 |
geoJTS = lin;
|
382 |
} */
|
383 |
|
384 |
break;
|
385 |
|
386 |
case Geometry.TYPES.SURFACE:
|
387 |
case Geometry.TYPES.CIRCLE:
|
388 |
case Geometry.TYPES.ELLIPSE:
|
389 |
arrayLines = new ArrayList(); |
390 |
|
391 |
ArrayList shells = new ArrayList(); |
392 |
ArrayList holes = new ArrayList(); |
393 |
Coordinate[] points = null; |
394 |
|
395 |
theIterator = shp.getPathIterator(null, FLATNESS);
|
396 |
|
397 |
while (!theIterator.isDone()) {
|
398 |
//while not done
|
399 |
theType = theIterator.currentSegment(theData); |
400 |
|
401 |
//Populate a segment of the new
|
402 |
// GeneralPathX object.
|
403 |
//Process the current segment to populate a new
|
404 |
// segment of the new GeneralPathX object.
|
405 |
switch (theType) {
|
406 |
case PathIterator.SEG_MOVETO: |
407 |
|
408 |
// System.out.println("SEG_MOVETO");
|
409 |
if (arrayCoords == null) { |
410 |
arrayCoords = new ArrayList(); |
411 |
} else {
|
412 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
413 |
|
414 |
try {
|
415 |
LinearRing ring = geomFactory.createLinearRing(points); |
416 |
|
417 |
if (CGAlgorithms.isCCW(points)) {
|
418 |
holes.add(ring); |
419 |
} else {
|
420 |
shells.add(ring); |
421 |
} |
422 |
} catch (Exception e) { |
423 |
/* (jaume) caso cuando todos los puntos son iguales
|
424 |
* devuelvo el propio punto
|
425 |
*/
|
426 |
boolean same = true; |
427 |
for (int i = 0; i < points.length-1 && same; i++) { |
428 |
if (points[i].x != points[i+1].x || |
429 |
points[i].y != points[i+1].y /*|| |
430 |
points[i].z != points[i+1].z*/
|
431 |
) { |
432 |
same = false;
|
433 |
} |
434 |
} |
435 |
if (same) {
|
436 |
return geomFactory.createPoint(points[0]); |
437 |
} |
438 |
/*
|
439 |
* caso cuando es una l?nea de 3 puntos, no creo un LinearRing, sino
|
440 |
* una linea
|
441 |
*/
|
442 |
if (points.length>1 && points.length<=3) { |
443 |
// return geomFactory.createLineString(points);
|
444 |
return geomFactory.createMultiLineString(new LineString[] {geomFactory.createLineString(points)}); |
445 |
} |
446 |
|
447 |
System.err.println(
|
448 |
"Caught Topology exception in GMLLinearRingHandler");
|
449 |
|
450 |
return null; |
451 |
} |
452 |
|
453 |
/* if (numParts == 1)
|
454 |
{
|
455 |
linRingExt = new GeometryFactory().createLinearRing(
|
456 |
CoordinateArrays.toCoordinateArray(arrayCoords));
|
457 |
}
|
458 |
else
|
459 |
{
|
460 |
linRing = new GeometryFactory().createLinearRing(
|
461 |
CoordinateArrays.toCoordinateArray(arrayCoords));
|
462 |
arrayLines.add(linRing);
|
463 |
} */
|
464 |
arrayCoords = new ArrayList(); |
465 |
} |
466 |
|
467 |
numParts++; |
468 |
arrayCoords.add(new Coordinate(theData[0], |
469 |
theData[1]));
|
470 |
|
471 |
break;
|
472 |
|
473 |
case PathIterator.SEG_LINETO: |
474 |
|
475 |
// System.out.println("SEG_LINETO");
|
476 |
arrayCoords.add(new Coordinate(theData[0], |
477 |
theData[1]));
|
478 |
|
479 |
break;
|
480 |
|
481 |
case PathIterator.SEG_QUADTO: |
482 |
System.out.println("SEG_QUADTO Not supported here"); |
483 |
|
484 |
break;
|
485 |
|
486 |
case PathIterator.SEG_CUBICTO: |
487 |
System.out.println("SEG_CUBICTO Not supported here"); |
488 |
|
489 |
break;
|
490 |
|
491 |
case PathIterator.SEG_CLOSE: |
492 |
|
493 |
// A?adimos el primer punto para cerrar.
|
494 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
495 |
arrayCoords.add(new Coordinate(firstCoord.x,
|
496 |
firstCoord.y)); |
497 |
|
498 |
break;
|
499 |
} //end switch
|
500 |
|
501 |
// System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
|
502 |
theIterator.next(); |
503 |
} //end while loop
|
504 |
|
505 |
arrayCoords.add(arrayCoords.get(0));
|
506 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
507 |
|
508 |
try {
|
509 |
LinearRing ring = geomFactory.createLinearRing(points); |
510 |
|
511 |
if (CGAlgorithms.isCCW(points)) {
|
512 |
holes.add(ring); |
513 |
} else {
|
514 |
shells.add(ring); |
515 |
} |
516 |
ring.setSRID(srid); |
517 |
} catch (Exception e) { |
518 |
/* (jaume) caso cuando todos los puntos son iguales
|
519 |
* devuelvo el propio punto
|
520 |
*/
|
521 |
boolean same = true; |
522 |
for (int i = 0; i < points.length-1 && same; i++) { |
523 |
if (points[i].x != points[i+1].x || |
524 |
points[i].y != points[i+1].y /*|| |
525 |
points[i].z != points[i+1].z*/
|
526 |
) { |
527 |
same = false;
|
528 |
} |
529 |
} |
530 |
if (same) {
|
531 |
geoJTS = geomFactory.createPoint(points[0]);
|
532 |
geoJTS.setSRID(srid); |
533 |
return geoJTS;
|
534 |
} |
535 |
/*
|
536 |
* caso cuando es una l?nea de 3 puntos, no creo un LinearRing, sino
|
537 |
* una linea
|
538 |
*/
|
539 |
if (points.length>1 && points.length<=3) { |
540 |
// return geomFactory.createLineString(points);
|
541 |
geoJTS = geomFactory |
542 |
.createMultiLineString(new LineString[] { geomFactory |
543 |
.createLineString(points) }); |
544 |
geoJTS.setSRID(srid); |
545 |
return geoJTS;
|
546 |
} |
547 |
System.err.println(
|
548 |
"Caught Topology exception in GMLLinearRingHandler");
|
549 |
|
550 |
return null; |
551 |
} |
552 |
|
553 |
/* linRing = new GeometryFactory().createLinearRing(
|
554 |
CoordinateArrays.toCoordinateArray(arrayCoords)); */
|
555 |
|
556 |
// System.out.println("NumParts = " + numParts);
|
557 |
//now we have a list of all shells and all holes
|
558 |
ArrayList holesForShells = new ArrayList(shells.size()); |
559 |
|
560 |
for (int i = 0; i < shells.size(); i++) { |
561 |
holesForShells.add(new ArrayList()); |
562 |
} |
563 |
|
564 |
//find homes
|
565 |
for (int i = 0; i < holes.size(); i++) { |
566 |
LinearRing testRing = (LinearRing) holes.get(i); |
567 |
LinearRing minShell = null;
|
568 |
Envelope minEnv = null;
|
569 |
Envelope testEnv = testRing.getEnvelopeInternal(); |
570 |
Coordinate testPt = testRing.getCoordinateN(0);
|
571 |
LinearRing tryRing = null;
|
572 |
|
573 |
for (int j = 0; j < shells.size(); j++) { |
574 |
tryRing = (LinearRing) shells.get(j); |
575 |
|
576 |
Envelope tryEnv = tryRing.getEnvelopeInternal(); |
577 |
|
578 |
if (minShell != null) { |
579 |
minEnv = minShell.getEnvelopeInternal(); |
580 |
} |
581 |
|
582 |
boolean isContained = false; |
583 |
Coordinate[] coordList = tryRing.getCoordinates();
|
584 |
|
585 |
if (tryEnv.contains(testEnv) &&
|
586 |
(CGAlgorithms.isPointInRing(testPt, coordList) || |
587 |
(pointInList(testPt, coordList)))) { |
588 |
isContained = true;
|
589 |
} |
590 |
|
591 |
// check if this new containing ring is smaller than the current minimum ring
|
592 |
if (isContained) {
|
593 |
if ((minShell == null) || minEnv.contains(tryEnv)) { |
594 |
minShell = tryRing; |
595 |
} |
596 |
} |
597 |
} |
598 |
|
599 |
if (minShell == null) { |
600 |
// System.out.println(
|
601 |
// "polygon found with a hole thats not inside a shell");
|
602 |
// azabala: we do the assumption that this hole is really a shell (polygon)
|
603 |
// whose point werent digitized in the right order
|
604 |
Coordinate[] cs = testRing.getCoordinates();
|
605 |
Coordinate[] reversed = new Coordinate[cs.length]; |
606 |
int pointIndex = 0; |
607 |
for(int z = cs.length-1; z >= 0; z--){ |
608 |
reversed[pointIndex] = cs[z]; |
609 |
pointIndex++; |
610 |
} |
611 |
LinearRing newRing = geomFactory.createLinearRing(reversed); |
612 |
shells.add(newRing); |
613 |
holesForShells.add(new ArrayList()); |
614 |
} else {
|
615 |
((ArrayList) holesForShells.get(shells.indexOf(minShell))).add(testRing);
|
616 |
} |
617 |
} |
618 |
|
619 |
Polygon[] polygons = new Polygon[shells.size()]; |
620 |
|
621 |
for (int i = 0; i < shells.size(); i++) { |
622 |
polygons[i] = geomFactory.createPolygon((LinearRing) shells.get( |
623 |
i), |
624 |
(LinearRing[]) ((ArrayList) holesForShells.get(i)).toArray( |
625 |
new LinearRing[0])); |
626 |
polygons[i].setSRID(srid); |
627 |
} |
628 |
// CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
|
629 |
// LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
|
630 |
// O CON GEOTOOLS
|
631 |
// if (numParts > 1) // Generamos una MultiLineString
|
632 |
|
633 |
/* if (polygons.length == 1) {
|
634 |
return polygons[0];
|
635 |
} */
|
636 |
|
637 |
// FIN CAMBIO
|
638 |
|
639 |
holesForShells = null;
|
640 |
shells = null;
|
641 |
holes = null;
|
642 |
|
643 |
//its a multi part
|
644 |
geoJTS = geomFactory.createMultiPolygon(polygons); |
645 |
geoJTS.setSRID(srid); |
646 |
|
647 |
/* if (numParts > 1) // Generamos un Polygon con agujeros
|
648 |
{
|
649 |
arrayLines.add(linRing);
|
650 |
// geoJTS = new GeometryFactory().createPolygon(linRingExt,
|
651 |
// GeometryFactory.toLinearRingArray(arrayLines));
|
652 |
geoJTS = new GeometryFactory().buildGeometry(arrayLines);
|
653 |
|
654 |
// geoJTS = Polygonizer.class.
|
655 |
}
|
656 |
else
|
657 |
{
|
658 |
geoJTS = new GeometryFactory().createPolygon(linRing,null);
|
659 |
} */
|
660 |
break;
|
661 |
} |
662 |
|
663 |
geoJTS.setSRID(srid); |
664 |
return geoJTS;
|
665 |
} |
666 |
|
667 |
/**
|
668 |
* Converts JTS Geometry objects into Java 2D Shape objects
|
669 |
*
|
670 |
* @param geo Geometry de JTS.
|
671 |
*
|
672 |
* @return FShape.
|
673 |
*/
|
674 |
// public static FShape jts_to_java2d(com.vividsolutions.jts.geom.Geometry geo) {
|
675 |
// FShape shpNew = null;
|
676 |
//
|
677 |
// try {
|
678 |
// if (geo instanceof Point) {
|
679 |
// shpNew = new org.gvsig.fmap.geom.primitive.Point2D(null, null, ((Point) geo).getX(), ((Point) geo).getY());
|
680 |
// }
|
681 |
//
|
682 |
// if (geo.isEmpty()) {
|
683 |
// shpNew = null;
|
684 |
// }
|
685 |
//
|
686 |
// if (geo instanceof Polygon) {
|
687 |
// shpNew = new Surface2D(null, null, toShape((Polygon) geo));
|
688 |
// }
|
689 |
//
|
690 |
// if (geo instanceof MultiPolygon) {
|
691 |
// shpNew = new Surface2D(null, null, toShape((MultiPolygon) geo));
|
692 |
// }
|
693 |
//
|
694 |
// if (geo instanceof LineString) {
|
695 |
// shpNew = new Curve2D(null, null, toShape((LineString) geo));
|
696 |
// }
|
697 |
//
|
698 |
// if (geo instanceof MultiLineString) {
|
699 |
// shpNew = new Curve2D(null, null, toShape((MultiLineString) geo));
|
700 |
// }
|
701 |
//
|
702 |
// /* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN GeometryCollection
|
703 |
// * No sabremos si queremos una l?nea o un pol?gono.....
|
704 |
// * if (geometry instanceof GeometryCollection) {
|
705 |
// return toShape((GeometryCollection) geometry);
|
706 |
// } */
|
707 |
// return shpNew;
|
708 |
// } catch (NoninvertibleTransformException e) {
|
709 |
// // TODO Auto-generated catch block
|
710 |
// e.printStackTrace();
|
711 |
// }
|
712 |
//
|
713 |
// return null;
|
714 |
// }
|
715 |
|
716 |
/**
|
717 |
* DOCUMENT ME!
|
718 |
*
|
719 |
* @param p DOCUMENT ME!
|
720 |
*
|
721 |
* @return DOCUMENT ME!
|
722 |
*/
|
723 |
private static GeneralPathX toShape(Polygon p) { |
724 |
GeneralPathX resul = new GeneralPathX();
|
725 |
Coordinate coord; |
726 |
|
727 |
for (int i = 0; i < p.getExteriorRing().getNumPoints(); i++) { |
728 |
coord = p.getExteriorRing().getCoordinateN(i); |
729 |
|
730 |
if (i == 0) { |
731 |
resul.moveTo(coord.x,coord.y); |
732 |
} else {
|
733 |
resul.lineTo(coord.x,coord.y); |
734 |
} |
735 |
} |
736 |
|
737 |
for (int j = 0; j < p.getNumInteriorRing(); j++) { |
738 |
LineString hole = p.getInteriorRingN(j); |
739 |
|
740 |
for (int k = 0; k < hole.getNumPoints(); k++) { |
741 |
coord = hole.getCoordinateN(k); |
742 |
|
743 |
if (k == 0) { |
744 |
resul.moveTo(coord.x, coord.y); |
745 |
} else {
|
746 |
resul.lineTo(coord.x, coord.y); |
747 |
} |
748 |
} |
749 |
} |
750 |
|
751 |
return resul;
|
752 |
} |
753 |
|
754 |
/**
|
755 |
* DOCUMENT ME!
|
756 |
*
|
757 |
* @param modelCoordinates DOCUMENT ME!
|
758 |
*
|
759 |
* @return DOCUMENT ME!
|
760 |
*
|
761 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
762 |
*
|
763 |
private Coordinate[] toViewCoordinates(Coordinate[] modelCoordinates)
|
764 |
throws NoninvertibleTransformException {
|
765 |
Coordinate[] viewCoordinates = new Coordinate[modelCoordinates.length];
|
766 |
|
767 |
for (int i = 0; i < modelCoordinates.length; i++) {
|
768 |
FPoint2D point2D = coordinate2FPoint2D(modelCoordinates[i]);
|
769 |
viewCoordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
|
770 |
}
|
771 |
|
772 |
return viewCoordinates;
|
773 |
} */
|
774 |
|
775 |
/* private Shape toShape(GeometryCollection gc)
|
776 |
throws NoninvertibleTransformException {
|
777 |
GeometryCollectionShape shape = new GeometryCollectionShape();
|
778 |
for (int i = 0; i < gc.getNumGeometries(); i++) {
|
779 |
Geometry g = (Geometry) gc.getGeometryN(i);
|
780 |
shape.add(toShape(g));
|
781 |
}
|
782 |
return shape;
|
783 |
} */
|
784 |
private static GeneralPathX toShape(MultiLineString mls) |
785 |
throws NoninvertibleTransformException { |
786 |
GeneralPathX path = new GeneralPathX();
|
787 |
|
788 |
for (int i = 0; i < mls.getNumGeometries(); i++) { |
789 |
LineString lineString = (LineString) mls.getGeometryN(i); |
790 |
path.append(toShape(lineString), false);
|
791 |
} |
792 |
|
793 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
794 |
//converted to GeneralPathXs. [Jon Aquino]
|
795 |
return path;
|
796 |
} |
797 |
|
798 |
/**
|
799 |
* DOCUMENT ME!
|
800 |
*
|
801 |
* @param lineString DOCUMENT ME!
|
802 |
*
|
803 |
* @return DOCUMENT ME!
|
804 |
*
|
805 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
806 |
*/
|
807 |
private static GeneralPathX toShape(LineString lineString) |
808 |
throws NoninvertibleTransformException { |
809 |
GeneralPathX shape = new GeneralPathX();
|
810 |
org.gvsig.fmap.geom.primitive.impl.Point2D viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(0));
|
811 |
shape.moveTo(viewPoint.getX(), viewPoint.getY()); |
812 |
|
813 |
for (int i = 1; i < lineString.getNumPoints(); i++) { |
814 |
viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(i)); |
815 |
shape.lineTo(viewPoint.getX(), viewPoint.getY()); |
816 |
} |
817 |
|
818 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
819 |
//converted to GeneralPathXs. [Jon Aquino]
|
820 |
return shape;
|
821 |
} |
822 |
|
823 |
/* TODO No se usa
|
824 |
* DOCUMENT ME!
|
825 |
*
|
826 |
* @param point DOCUMENT ME!
|
827 |
*
|
828 |
* @return DOCUMENT ME!
|
829 |
*
|
830 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
831 |
*
|
832 |
private static Point2D toShape(Point point)
|
833 |
throws NoninvertibleTransformException {
|
834 |
Point2D viewPoint = coordinate2FPoint2D(point.getCoordinate());
|
835 |
|
836 |
return viewPoint;
|
837 |
}
|
838 |
*/
|
839 |
|
840 |
/**
|
841 |
*
|
842 |
*/
|
843 |
private static GeneralPathX toShape(MultiPolygon mp) |
844 |
throws NoninvertibleTransformException { |
845 |
GeneralPathX path = new GeneralPathX();
|
846 |
|
847 |
for (int i = 0; i < mp.getNumGeometries(); i++) { |
848 |
Polygon polygon = (Polygon) mp.getGeometryN(i); |
849 |
path.append(toShape(polygon), false);
|
850 |
} |
851 |
|
852 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
853 |
//converted to GeneralPathXs. [Jon Aquino]
|
854 |
return path;
|
855 |
} |
856 |
/**
|
857 |
* DOCUMENT ME!
|
858 |
*
|
859 |
* @param coord DOCUMENT ME!
|
860 |
*
|
861 |
* @return DOCUMENT ME!
|
862 |
*/
|
863 |
public static org.gvsig.fmap.geom.primitive.impl.Point2D coordinate2FPoint2D(Coordinate coord) { |
864 |
return new org.gvsig.fmap.geom.primitive.impl.Point2D(null, null, coord.x, coord.y); //,coord.z); |
865 |
} |
866 |
|
867 |
/**
|
868 |
* Convierte una Geometry de JTS a GeneralPathX.
|
869 |
*
|
870 |
* @param geometry Geometry a convertir.
|
871 |
*
|
872 |
* @return GeneralPathX.
|
873 |
*
|
874 |
* @throws NoninvertibleTransformException
|
875 |
* @throws IllegalArgumentException
|
876 |
*/
|
877 |
public static GeneralPathX toShape(com.vividsolutions.jts.geom.Geometry geometry) |
878 |
throws NoninvertibleTransformException { |
879 |
if (geometry.isEmpty()) {
|
880 |
return new GeneralPathX(); |
881 |
} |
882 |
|
883 |
if (geometry instanceof Polygon) { |
884 |
return toShape((Polygon) geometry); |
885 |
} |
886 |
|
887 |
if (geometry instanceof MultiPolygon) { |
888 |
return toShape((MultiPolygon) geometry);
|
889 |
} |
890 |
|
891 |
if (geometry instanceof LineString) { |
892 |
return toShape((LineString) geometry);
|
893 |
} |
894 |
|
895 |
if (geometry instanceof MultiLineString) { |
896 |
return toShape((MultiLineString) geometry);
|
897 |
} |
898 |
|
899 |
if (geometry instanceof GeometryCollection) { |
900 |
return toShape(geometry);
|
901 |
} |
902 |
|
903 |
throw new IllegalArgumentException("Unrecognized Geometry class: " + |
904 |
geometry.getClass()); |
905 |
} |
906 |
|
907 |
|
908 |
public static GeneralPathX transformToInts(GeneralPathX gp, AffineTransform at) { |
909 |
GeneralPathX newGp = new GeneralPathX();
|
910 |
PathIterator theIterator;
|
911 |
int theType;
|
912 |
int numParts = 0; |
913 |
double[] theData = new double[6]; |
914 |
java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
|
915 |
java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
|
916 |
boolean bFirst = true; |
917 |
int xInt, yInt, antX = -1, antY = -1; |
918 |
|
919 |
theIterator = gp.getPathIterator(null); //, flatness); |
920 |
|
921 |
while (!theIterator.isDone()) {
|
922 |
theType = theIterator.currentSegment(theData); |
923 |
switch (theType) {
|
924 |
case PathIterator.SEG_MOVETO: |
925 |
numParts++; |
926 |
ptSrc.setLocation(theData[0], theData[1]); |
927 |
at.transform(ptSrc, ptDst); |
928 |
antX = (int) ptDst.getX();
|
929 |
antY = (int) ptDst.getY();
|
930 |
newGp.moveTo(antX, antY); |
931 |
bFirst = true;
|
932 |
break;
|
933 |
|
934 |
case PathIterator.SEG_LINETO: |
935 |
ptSrc.setLocation(theData[0], theData[1]); |
936 |
at.transform(ptSrc, ptDst); |
937 |
xInt = (int) ptDst.getX();
|
938 |
yInt = (int) ptDst.getY();
|
939 |
if ((bFirst) || ((xInt != antX) || (yInt != antY)))
|
940 |
{ |
941 |
newGp.lineTo(xInt, yInt); |
942 |
antX = xInt; |
943 |
antY = yInt; |
944 |
bFirst = false;
|
945 |
} |
946 |
break;
|
947 |
|
948 |
case PathIterator.SEG_QUADTO: |
949 |
System.out.println("Not supported here"); |
950 |
|
951 |
break;
|
952 |
|
953 |
case PathIterator.SEG_CUBICTO: |
954 |
System.out.println("Not supported here"); |
955 |
|
956 |
break;
|
957 |
|
958 |
case PathIterator.SEG_CLOSE: |
959 |
newGp.closePath(); |
960 |
|
961 |
break;
|
962 |
} //end switch
|
963 |
|
964 |
theIterator.next(); |
965 |
} //end while loop
|
966 |
|
967 |
return newGp;
|
968 |
} |
969 |
public static Geometry transformToInts(Geometry gp, AffineTransform at) { |
970 |
GeneralPathX newGp = new GeneralPathX();
|
971 |
double[] theData = new double[6]; |
972 |
double[] aux = new double[6]; |
973 |
|
974 |
// newGp.reset();
|
975 |
PathIterator theIterator;
|
976 |
int theType;
|
977 |
int numParts = 0; |
978 |
|
979 |
java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
|
980 |
java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
|
981 |
boolean bFirst = true; |
982 |
int xInt, yInt, antX = -1, antY = -1; |
983 |
|
984 |
|
985 |
theIterator = gp.getPathIterator(null); //, flatness); |
986 |
int numSegmentsAdded = 0; |
987 |
while (!theIterator.isDone()) {
|
988 |
theType = theIterator.currentSegment(theData); |
989 |
|
990 |
switch (theType) {
|
991 |
case PathIterator.SEG_MOVETO: |
992 |
numParts++; |
993 |
ptSrc.setLocation(theData[0], theData[1]); |
994 |
at.transform(ptSrc, ptDst); |
995 |
antX = (int) ptDst.getX();
|
996 |
antY = (int) ptDst.getY();
|
997 |
newGp.moveTo(antX, antY); |
998 |
numSegmentsAdded++; |
999 |
bFirst = true;
|
1000 |
break;
|
1001 |
|
1002 |
case PathIterator.SEG_LINETO: |
1003 |
ptSrc.setLocation(theData[0], theData[1]); |
1004 |
at.transform(ptSrc, ptDst); |
1005 |
xInt = (int) ptDst.getX();
|
1006 |
yInt = (int) ptDst.getY();
|
1007 |
if ((bFirst) || ((xInt != antX) || (yInt != antY)))
|
1008 |
{ |
1009 |
newGp.lineTo(xInt, yInt); |
1010 |
antX = xInt; |
1011 |
antY = yInt; |
1012 |
bFirst = false;
|
1013 |
numSegmentsAdded++; |
1014 |
} |
1015 |
break;
|
1016 |
|
1017 |
case PathIterator.SEG_QUADTO: |
1018 |
at.transform(theData,0,aux,0,2); |
1019 |
newGp.quadTo(aux[0], aux[1], aux[2], aux[3]); |
1020 |
numSegmentsAdded++; |
1021 |
break;
|
1022 |
|
1023 |
case PathIterator.SEG_CUBICTO: |
1024 |
at.transform(theData,0,aux,0,3); |
1025 |
newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]); |
1026 |
numSegmentsAdded++; |
1027 |
break;
|
1028 |
|
1029 |
case PathIterator.SEG_CLOSE: |
1030 |
if (numSegmentsAdded < 3) { |
1031 |
newGp.lineTo(antX, antY); |
1032 |
} |
1033 |
newGp.closePath(); |
1034 |
|
1035 |
break;
|
1036 |
} //end switch
|
1037 |
|
1038 |
theIterator.next(); |
1039 |
} //end while loop
|
1040 |
|
1041 |
Geometry shp = null;
|
1042 |
switch (gp.getType())
|
1043 |
{ |
1044 |
case Geometry.TYPES.POINT:
|
1045 |
shp = new org.gvsig.fmap.geom.primitive.impl.Point2D(null, null, ptDst.getX(), ptDst.getY()); |
1046 |
break;
|
1047 |
|
1048 |
case Geometry.TYPES.CURVE:
|
1049 |
case Geometry.TYPES.ARC:
|
1050 |
try {
|
1051 |
shp = geomManager.createCurve(newGp, SUBTYPES.GEOM2D); |
1052 |
} catch (CreateGeometryException e1) {
|
1053 |
logger.error("Error creating a curve", e1);
|
1054 |
} |
1055 |
break;
|
1056 |
|
1057 |
case Geometry.TYPES.SURFACE:
|
1058 |
case Geometry.TYPES.CIRCLE:
|
1059 |
case Geometry.TYPES.ELLIPSE:
|
1060 |
|
1061 |
try {
|
1062 |
shp = geomManager.createSurface(newGp, SUBTYPES.GEOM2D); |
1063 |
} catch (CreateGeometryException e) {
|
1064 |
logger.error("Error creating a surface", e);
|
1065 |
} |
1066 |
break;
|
1067 |
} |
1068 |
return shp;
|
1069 |
} |
1070 |
|
1071 |
public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR) |
1072 |
{ |
1073 |
Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(), |
1074 |
jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight()); |
1075 |
return r;
|
1076 |
} |
1077 |
|
1078 |
public static Envelope convertEnvelopeToJTS(org.gvsig.fmap.geom.primitive.Envelope r) |
1079 |
{ |
1080 |
Envelope e = new Envelope(r.getMinimum(0), r.getMaximum(0), r.getMinimum(1), |
1081 |
r.getMaximum(1));
|
1082 |
return e;
|
1083 |
} |
1084 |
|
1085 |
/**
|
1086 |
* Return a correct polygon (no hole)
|
1087 |
* @param coordinates
|
1088 |
* @return
|
1089 |
*/
|
1090 |
public static Geometry getExteriorPolygon(Coordinate[] coordinates) |
1091 |
{ |
1092 |
// isCCW = true => it's a hole
|
1093 |
Coordinate[] vs=new Coordinate[coordinates.length]; |
1094 |
if (CGAlgorithms.isCCW(coordinates)){
|
1095 |
for (int i=vs.length-1;i>=0;i--){ |
1096 |
vs[i]=coordinates[i]; |
1097 |
} |
1098 |
}else{
|
1099 |
vs=coordinates; |
1100 |
} |
1101 |
LinearRing ring = geomFactory.createLinearRing(vs); |
1102 |
|
1103 |
try {
|
1104 |
Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D); |
1105 |
surface.setGeneralPath(toShape(ring)); |
1106 |
return surface;
|
1107 |
} catch (NoninvertibleTransformException e) { |
1108 |
e.printStackTrace(); |
1109 |
} catch (CreateGeometryException e) {
|
1110 |
e.printStackTrace(); |
1111 |
} |
1112 |
return null; |
1113 |
} |
1114 |
|
1115 |
public static boolean isCCW(Point[] points) |
1116 |
{ |
1117 |
int length = points.length;
|
1118 |
Coordinate[] vs;
|
1119 |
// CGAlgorithms.isCCW asume que la lista de puntos tienen el primer
|
1120 |
// y el ultimo puntos iguales.... y que este algoritmo est? solo
|
1121 |
// garantizado con anillos v?lidos.
|
1122 |
if (points[0].getX() != points[length-1].getX() || points[0].getY() != points[length-1].getY()) { |
1123 |
vs=new Coordinate[length+1]; |
1124 |
vs[points.length] = new Coordinate(points[0].getX(), points[0].getY()); |
1125 |
} else {
|
1126 |
vs=new Coordinate[length];
|
1127 |
} |
1128 |
for (int i=0; i<length; i++){ |
1129 |
vs[i] = new Coordinate(points[i].getX(), points[i].getY());
|
1130 |
} |
1131 |
|
1132 |
return CGAlgorithms.isCCW(vs);
|
1133 |
} |
1134 |
|
1135 |
public static boolean isCCW(Surface pol) |
1136 |
{ |
1137 |
com.vividsolutions.jts.geom.Geometry jtsGeom = Converter.geometryToJts(pol); |
1138 |
if (jtsGeom.getNumGeometries() == 1) |
1139 |
{ |
1140 |
Coordinate[] coords = jtsGeom.getCoordinates();
|
1141 |
return CGAlgorithms.isCCW(coords);
|
1142 |
} |
1143 |
return false; |
1144 |
|
1145 |
} |
1146 |
|
1147 |
|
1148 |
/**
|
1149 |
* Return a hole (CCW ordered points)
|
1150 |
* @param coordinates
|
1151 |
* @return
|
1152 |
*/
|
1153 |
public static Geometry getHole(Coordinate[] coordinates) |
1154 |
{ |
1155 |
// isCCW = true => it's a hole
|
1156 |
Coordinate[] vs=new Coordinate[coordinates.length]; |
1157 |
if (CGAlgorithms.isCCW(coordinates)){
|
1158 |
vs=coordinates; |
1159 |
|
1160 |
}else{
|
1161 |
for (int i=vs.length-1;i>=0;i--){ |
1162 |
vs[i]=coordinates[i]; |
1163 |
} |
1164 |
} |
1165 |
LinearRing ring = geomFactory.createLinearRing(vs); |
1166 |
|
1167 |
try {
|
1168 |
Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D); |
1169 |
surface.setGeneralPath(toShape(ring)); |
1170 |
return surface;
|
1171 |
} catch (NoninvertibleTransformException e) { |
1172 |
e.printStackTrace(); |
1173 |
} catch (CreateGeometryException e) {
|
1174 |
e.printStackTrace(); |
1175 |
} |
1176 |
return null; |
1177 |
} |
1178 |
|
1179 |
public static Shape getExteriorPolygon(GeneralPathX gp) { |
1180 |
Area area = new Area(gp); |
1181 |
area.isSingular(); |
1182 |
return area;
|
1183 |
|
1184 |
|
1185 |
|
1186 |
} |
1187 |
/**
|
1188 |
* Use it ONLY for NOT multipart polygons.
|
1189 |
* @param pol
|
1190 |
* @return
|
1191 |
*/
|
1192 |
public static Geometry getNotHolePolygon(Surface pol) { |
1193 |
// isCCW == true => hole
|
1194 |
Coordinate[] coords;
|
1195 |
ArrayList arrayCoords = null; |
1196 |
int theType;
|
1197 |
int numParts = 0; |
1198 |
|
1199 |
// Use this array to store segment coordinate data
|
1200 |
double[] theData = new double[6]; |
1201 |
PathIterator theIterator;
|
1202 |
|
1203 |
ArrayList shells = new ArrayList(); |
1204 |
ArrayList holes = new ArrayList(); |
1205 |
Coordinate[] points = null; |
1206 |
|
1207 |
theIterator = pol.getPathIterator(null, FLATNESS);
|
1208 |
|
1209 |
while (!theIterator.isDone()) {
|
1210 |
//while not done
|
1211 |
theType = theIterator.currentSegment(theData); |
1212 |
|
1213 |
//Populate a segment of the new
|
1214 |
// GeneralPathX object.
|
1215 |
//Process the current segment to populate a new
|
1216 |
// segment of the new GeneralPathX object.
|
1217 |
switch (theType) {
|
1218 |
case PathIterator.SEG_MOVETO: |
1219 |
|
1220 |
// System.out.println("SEG_MOVETO");
|
1221 |
if (arrayCoords == null) { |
1222 |
arrayCoords = new ArrayList(); |
1223 |
} else {
|
1224 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
1225 |
|
1226 |
try {
|
1227 |
LinearRing ring = geomFactory.createLinearRing(points); |
1228 |
|
1229 |
if (CGAlgorithms.isCCW(points)) {
|
1230 |
holes.add(ring); |
1231 |
} else {
|
1232 |
shells.add(ring); |
1233 |
} |
1234 |
} catch (Exception e) { |
1235 |
System.err.println(
|
1236 |
"Caught Topology exception in GMLLinearRingHandler");
|
1237 |
|
1238 |
return null; |
1239 |
} |
1240 |
|
1241 |
/* if (numParts == 1)
|
1242 |
{
|
1243 |
linRingExt = new GeometryFactory().createLinearRing(
|
1244 |
CoordinateArrays.toCoordinateArray(arrayCoords));
|
1245 |
}
|
1246 |
else
|
1247 |
{
|
1248 |
linRing = new GeometryFactory().createLinearRing(
|
1249 |
CoordinateArrays.toCoordinateArray(arrayCoords));
|
1250 |
arrayLines.add(linRing);
|
1251 |
} */
|
1252 |
arrayCoords = new ArrayList(); |
1253 |
} |
1254 |
|
1255 |
numParts++; |
1256 |
arrayCoords.add(new Coordinate(theData[0], |
1257 |
theData[1]));
|
1258 |
|
1259 |
break;
|
1260 |
|
1261 |
case PathIterator.SEG_LINETO: |
1262 |
|
1263 |
// System.out.println("SEG_LINETO");
|
1264 |
arrayCoords.add(new Coordinate(theData[0], |
1265 |
theData[1]));
|
1266 |
|
1267 |
break;
|
1268 |
|
1269 |
case PathIterator.SEG_QUADTO: |
1270 |
System.out.println("SEG_QUADTO Not supported here"); |
1271 |
|
1272 |
break;
|
1273 |
|
1274 |
case PathIterator.SEG_CUBICTO: |
1275 |
System.out.println("SEG_CUBICTO Not supported here"); |
1276 |
|
1277 |
break;
|
1278 |
|
1279 |
case PathIterator.SEG_CLOSE: |
1280 |
|
1281 |
// A?adimos el primer punto para cerrar.
|
1282 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
1283 |
arrayCoords.add(new Coordinate(firstCoord.x,
|
1284 |
firstCoord.y)); |
1285 |
|
1286 |
break;
|
1287 |
} //end switch
|
1288 |
|
1289 |
// System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
|
1290 |
theIterator.next(); |
1291 |
} //end while loop
|
1292 |
|
1293 |
arrayCoords.add(arrayCoords.get(0));
|
1294 |
coords = CoordinateArrays.toCoordinateArray(arrayCoords); |
1295 |
|
1296 |
|
1297 |
if (numParts == 1) |
1298 |
{ |
1299 |
return getExteriorPolygon(coords);
|
1300 |
} |
1301 |
return pol;
|
1302 |
|
1303 |
} |
1304 |
|
1305 |
|
1306 |
/* public static GeometryCollection convertFGeometryCollection(FGeometryCollection fGeomC)
|
1307 |
{
|
1308 |
|
1309 |
geomFactory.createGeometryCollection(theGeoms);
|
1310 |
} */
|
1311 |
} |