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