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