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