root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FConverter.java @ 4879
History | View | Annotate | Download (26.8 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 |
private final static GeometryFactory geomFactory = new GeometryFactory(); |
94 |
protected 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 |
private static PointConverter pointConverter = new PointConverter() { |
100 |
/* (non-Javadoc)
|
101 |
* @see com.iver.cit.opensig.fmap.FConverter.PointConverter#toViewPoint(com.vividsolutions.jts.geom.Coordinate)
|
102 |
*/
|
103 |
public Point2D toViewPoint(Coordinate modelCoordinate) |
104 |
throws NoninvertibleTransformException { |
105 |
// TODO Auto-generated method stub
|
106 |
return null; |
107 |
} |
108 |
}; |
109 |
|
110 |
//returns true if testPoint is a point in the pointList list.
|
111 |
static boolean pointInList(Coordinate testPoint, Coordinate[] pointList) { |
112 |
int t;
|
113 |
int numpoints;
|
114 |
Coordinate p; |
115 |
|
116 |
numpoints = Array.getLength(pointList);
|
117 |
|
118 |
for (t = 0; t < numpoints; t++) { |
119 |
p = pointList[t]; |
120 |
|
121 |
if ((testPoint.x == p.x) && (testPoint.y == p.y) &&
|
122 |
((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) //nan test; x!=x iff x is nan
|
123 |
) { |
124 |
return true; |
125 |
} |
126 |
} |
127 |
|
128 |
return false; |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Receives a JTS Geometry and returns a fmap IGeometry
|
133 |
* @param jtsGeometry jts Geometry
|
134 |
* @return IGeometry of FMap
|
135 |
* @author azabala
|
136 |
*/
|
137 |
public static IGeometry jts_to_igeometry(Geometry jtsGeometry){ |
138 |
FShape shape = FConverter.jts_to_java2d(jtsGeometry); |
139 |
return ShapeFactory.createGeometry(shape);
|
140 |
} |
141 |
/**
|
142 |
* Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
|
143 |
* "flattened PathIterator". El flattened indica que las curvas las pasa a
|
144 |
* segmentos de l?nea recta AUTOMATICAMENTE!!!.
|
145 |
*
|
146 |
* @param shp FShape que se quiere convertir.
|
147 |
*
|
148 |
* @return Geometry de JTS.
|
149 |
*/
|
150 |
public static Geometry java2d_to_jts(FShape shp) { |
151 |
double flatness = 0.8; // Por ejemplo. Cuanto m?s peque?o, m?s segmentos necesitar? la curva |
152 |
|
153 |
// Es la m?xima distancia que permitimos que el trazo aproximado
|
154 |
// difiera del trazo real.
|
155 |
Geometry geoJTS = null;
|
156 |
Coordinate coord; |
157 |
Coordinate[] coords;
|
158 |
ArrayList arrayCoords = null; |
159 |
ArrayList arrayLines;
|
160 |
LineString lin; |
161 |
LinearRing linRing; |
162 |
LinearRing linRingExt = null;
|
163 |
int theType;
|
164 |
int numParts = 0; |
165 |
|
166 |
// Use this array to store segment coordinate data
|
167 |
double[] theData = new double[6]; |
168 |
PathIterator theIterator;
|
169 |
|
170 |
switch (shp.getShapeType()) {
|
171 |
case FShape.POINT:
|
172 |
case FShape.POINT + FShape.Z:
|
173 |
FPoint2D p = (FPoint2D) shp; |
174 |
coord = new Coordinate(p.getX(), p.getY());
|
175 |
geoJTS = geomFactory.createPoint(coord); |
176 |
|
177 |
break;
|
178 |
|
179 |
case FShape.LINE:
|
180 |
case FShape.ARC:
|
181 |
case FShape.LINE + FShape.Z:
|
182 |
arrayLines = new ArrayList(); |
183 |
theIterator = shp.getPathIterator(null, flatness);
|
184 |
|
185 |
while (!theIterator.isDone()) {
|
186 |
//while not done
|
187 |
theType = theIterator.currentSegment(theData); |
188 |
|
189 |
//Populate a segment of the new
|
190 |
// GeneralPathX object.
|
191 |
//Process the current segment to populate a new
|
192 |
// segment of the new GeneralPathX object.
|
193 |
switch (theType) {
|
194 |
case PathIterator.SEG_MOVETO: |
195 |
|
196 |
// System.out.println("SEG_MOVETO");
|
197 |
if (arrayCoords == null) { |
198 |
arrayCoords = new ArrayList(); |
199 |
} else {
|
200 |
lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray( |
201 |
arrayCoords)); |
202 |
arrayLines.add(lin); |
203 |
arrayCoords = new ArrayList(); |
204 |
} |
205 |
|
206 |
numParts++; |
207 |
arrayCoords.add(new Coordinate(theData[0], |
208 |
theData[1]));
|
209 |
|
210 |
break;
|
211 |
|
212 |
case PathIterator.SEG_LINETO: |
213 |
|
214 |
// System.out.println("SEG_LINETO");
|
215 |
arrayCoords.add(new Coordinate(theData[0], |
216 |
theData[1]));
|
217 |
|
218 |
break;
|
219 |
|
220 |
case PathIterator.SEG_QUADTO: |
221 |
System.out.println("Not supported here"); |
222 |
|
223 |
break;
|
224 |
|
225 |
case PathIterator.SEG_CUBICTO: |
226 |
System.out.println("Not supported here"); |
227 |
|
228 |
break;
|
229 |
|
230 |
case PathIterator.SEG_CLOSE: |
231 |
System.out.println("SEG_CLOSE"); |
232 |
|
233 |
// A?adimos el primer punto para cerrar.
|
234 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
235 |
arrayCoords.add(new Coordinate(firstCoord.x,
|
236 |
firstCoord.y)); |
237 |
|
238 |
break;
|
239 |
} //end switch
|
240 |
|
241 |
theIterator.next(); |
242 |
} //end while loop
|
243 |
|
244 |
lin = new GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
|
245 |
arrayCoords)); |
246 |
|
247 |
// CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
|
248 |
// LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
|
249 |
// O CON GEOTOOLS
|
250 |
// if (numParts > 1) // Generamos una MultiLineString
|
251 |
// {
|
252 |
arrayLines.add(lin); |
253 |
geoJTS = geomFactory.createMultiLineString(GeometryFactory.toLineStringArray( |
254 |
arrayLines)); |
255 |
/* } else {
|
256 |
geoJTS = lin;
|
257 |
} */
|
258 |
|
259 |
break;
|
260 |
|
261 |
case FShape.POLYGON:
|
262 |
case FShape.CIRCLE:
|
263 |
case FShape.POLYGON + FShape.Z:
|
264 |
arrayLines = new ArrayList(); |
265 |
|
266 |
ArrayList shells = new ArrayList(); |
267 |
ArrayList holes = new ArrayList(); |
268 |
Coordinate[] points = null; |
269 |
|
270 |
theIterator = shp.getPathIterator(null, flatness);
|
271 |
|
272 |
while (!theIterator.isDone()) {
|
273 |
//while not done
|
274 |
theType = theIterator.currentSegment(theData); |
275 |
|
276 |
//Populate a segment of the new
|
277 |
// GeneralPathX object.
|
278 |
//Process the current segment to populate a new
|
279 |
// segment of the new GeneralPathX object.
|
280 |
switch (theType) {
|
281 |
case PathIterator.SEG_MOVETO: |
282 |
|
283 |
// System.out.println("SEG_MOVETO");
|
284 |
if (arrayCoords == null) { |
285 |
arrayCoords = new ArrayList(); |
286 |
} else {
|
287 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
288 |
|
289 |
try {
|
290 |
LinearRing ring = geomFactory.createLinearRing(points); |
291 |
|
292 |
if (CGAlgorithms.isCCW(points)) {
|
293 |
holes.add(ring); |
294 |
} else {
|
295 |
shells.add(ring); |
296 |
} |
297 |
} catch (Exception e) { |
298 |
System.err.println(
|
299 |
"Caught Topology exception in GMLLinearRingHandler");
|
300 |
|
301 |
return null; |
302 |
} |
303 |
|
304 |
/* if (numParts == 1)
|
305 |
{
|
306 |
linRingExt = new GeometryFactory().createLinearRing(
|
307 |
CoordinateArrays.toCoordinateArray(arrayCoords));
|
308 |
}
|
309 |
else
|
310 |
{
|
311 |
linRing = new GeometryFactory().createLinearRing(
|
312 |
CoordinateArrays.toCoordinateArray(arrayCoords));
|
313 |
arrayLines.add(linRing);
|
314 |
} */
|
315 |
arrayCoords = new ArrayList(); |
316 |
} |
317 |
|
318 |
numParts++; |
319 |
arrayCoords.add(new Coordinate(theData[0], |
320 |
theData[1]));
|
321 |
|
322 |
break;
|
323 |
|
324 |
case PathIterator.SEG_LINETO: |
325 |
|
326 |
// System.out.println("SEG_LINETO");
|
327 |
arrayCoords.add(new Coordinate(theData[0], |
328 |
theData[1]));
|
329 |
|
330 |
break;
|
331 |
|
332 |
case PathIterator.SEG_QUADTO: |
333 |
System.out.println("SEG_QUADTO Not supported here"); |
334 |
|
335 |
break;
|
336 |
|
337 |
case PathIterator.SEG_CUBICTO: |
338 |
System.out.println("SEG_CUBICTO Not supported here"); |
339 |
|
340 |
break;
|
341 |
|
342 |
case PathIterator.SEG_CLOSE: |
343 |
|
344 |
// A?adimos el primer punto para cerrar.
|
345 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
346 |
arrayCoords.add(new Coordinate(firstCoord.x,
|
347 |
firstCoord.y)); |
348 |
|
349 |
break;
|
350 |
} //end switch
|
351 |
|
352 |
// System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
|
353 |
theIterator.next(); |
354 |
} //end while loop
|
355 |
|
356 |
// arrayCoords.add(arrayCoords.get(0));
|
357 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
358 |
|
359 |
try {
|
360 |
LinearRing ring = geomFactory.createLinearRing(points); |
361 |
|
362 |
if (CGAlgorithms.isCCW(points)) {
|
363 |
holes.add(ring); |
364 |
} else {
|
365 |
shells.add(ring); |
366 |
} |
367 |
} catch (Exception e) { |
368 |
System.err.println(
|
369 |
"Caught Topology exception in GMLLinearRingHandler");
|
370 |
|
371 |
return null; |
372 |
} |
373 |
|
374 |
/* linRing = new GeometryFactory().createLinearRing(
|
375 |
CoordinateArrays.toCoordinateArray(arrayCoords)); */
|
376 |
|
377 |
// System.out.println("NumParts = " + numParts);
|
378 |
//now we have a list of all shells and all holes
|
379 |
ArrayList holesForShells = new ArrayList(shells.size()); |
380 |
|
381 |
for (int i = 0; i < shells.size(); i++) { |
382 |
holesForShells.add(new ArrayList()); |
383 |
} |
384 |
|
385 |
//find homes
|
386 |
for (int i = 0; i < holes.size(); i++) { |
387 |
LinearRing testRing = (LinearRing) holes.get(i); |
388 |
LinearRing minShell = null;
|
389 |
Envelope minEnv = null;
|
390 |
Envelope testEnv = testRing.getEnvelopeInternal(); |
391 |
Coordinate testPt = testRing.getCoordinateN(0);
|
392 |
LinearRing tryRing; |
393 |
|
394 |
for (int j = 0; j < shells.size(); j++) { |
395 |
tryRing = (LinearRing) shells.get(j); |
396 |
|
397 |
Envelope tryEnv = tryRing.getEnvelopeInternal(); |
398 |
|
399 |
if (minShell != null) { |
400 |
minEnv = minShell.getEnvelopeInternal(); |
401 |
} |
402 |
|
403 |
boolean isContained = false; |
404 |
Coordinate[] coordList = tryRing.getCoordinates();
|
405 |
|
406 |
if (tryEnv.contains(testEnv) &&
|
407 |
(CGAlgorithms.isPointInRing(testPt, coordList) || |
408 |
(pointInList(testPt, coordList)))) { |
409 |
isContained = true;
|
410 |
} |
411 |
|
412 |
// check if this new containing ring is smaller than the current minimum ring
|
413 |
if (isContained) {
|
414 |
if ((minShell == null) || minEnv.contains(tryEnv)) { |
415 |
minShell = tryRing; |
416 |
} |
417 |
} |
418 |
} |
419 |
|
420 |
if (minShell == null) { |
421 |
System.out.println(
|
422 |
"polygon found with a hole thats not inside a shell");
|
423 |
} else {
|
424 |
((ArrayList) holesForShells.get(shells.indexOf(minShell))).add(testRing);
|
425 |
} |
426 |
} |
427 |
|
428 |
Polygon[] polygons = new Polygon[shells.size()]; |
429 |
|
430 |
for (int i = 0; i < shells.size(); i++) { |
431 |
polygons[i] = geomFactory.createPolygon((LinearRing) shells.get( |
432 |
i), |
433 |
(LinearRing[]) ((ArrayList) holesForShells.get(i)).toArray( |
434 |
new LinearRing[0])); |
435 |
} |
436 |
// CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
|
437 |
// LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
|
438 |
// O CON GEOTOOLS
|
439 |
// if (numParts > 1) // Generamos una MultiLineString
|
440 |
|
441 |
/* if (polygons.length == 1) {
|
442 |
return polygons[0];
|
443 |
} */
|
444 |
|
445 |
// FIN CAMBIO
|
446 |
|
447 |
holesForShells = null;
|
448 |
shells = null;
|
449 |
holes = null;
|
450 |
|
451 |
//its a multi part
|
452 |
geoJTS = geomFactory.createMultiPolygon(polygons); |
453 |
|
454 |
/* if (numParts > 1) // Generamos un Polygon con agujeros
|
455 |
{
|
456 |
arrayLines.add(linRing);
|
457 |
// geoJTS = new GeometryFactory().createPolygon(linRingExt,
|
458 |
// GeometryFactory.toLinearRingArray(arrayLines));
|
459 |
geoJTS = new GeometryFactory().buildGeometry(arrayLines);
|
460 |
|
461 |
// geoJTS = Polygonizer.class.
|
462 |
}
|
463 |
else
|
464 |
{
|
465 |
geoJTS = new GeometryFactory().createPolygon(linRing,null);
|
466 |
} */
|
467 |
break;
|
468 |
} |
469 |
|
470 |
return geoJTS;
|
471 |
} |
472 |
|
473 |
/**
|
474 |
* Converts JTS Geometry objects into Java 2D Shape objects
|
475 |
*
|
476 |
* @param geo Geometry de JTS.
|
477 |
*
|
478 |
* @return FShape.
|
479 |
*/
|
480 |
public static FShape jts_to_java2d(Geometry geo) { |
481 |
FShape shpNew = null;
|
482 |
|
483 |
try {
|
484 |
if (geo instanceof Point) { |
485 |
shpNew = new FPoint2D(((Point) geo).getX(), ((Point) geo).getY()); |
486 |
} |
487 |
|
488 |
if (geo.isEmpty()) {
|
489 |
shpNew = null;
|
490 |
} |
491 |
|
492 |
if (geo instanceof Polygon) { |
493 |
shpNew = new FPolygon2D(toShape((Polygon) geo)); |
494 |
} |
495 |
|
496 |
if (geo instanceof MultiPolygon) { |
497 |
shpNew = new FPolygon2D(toShape((MultiPolygon) geo));
|
498 |
} |
499 |
|
500 |
if (geo instanceof LineString) { |
501 |
shpNew = new FPolyline2D(toShape((LineString) geo));
|
502 |
} |
503 |
|
504 |
if (geo instanceof MultiLineString) { |
505 |
shpNew = new FPolyline2D(toShape((MultiLineString) geo));
|
506 |
} |
507 |
|
508 |
/* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN GeometryCollection
|
509 |
* No sabremos si queremos una l?nea o un pol?gono.....
|
510 |
* if (geometry instanceof GeometryCollection) {
|
511 |
return toShape((GeometryCollection) geometry);
|
512 |
} */
|
513 |
return shpNew;
|
514 |
} catch (NoninvertibleTransformException e) { |
515 |
// TODO Auto-generated catch block
|
516 |
e.printStackTrace(); |
517 |
} |
518 |
|
519 |
return null; |
520 |
} |
521 |
|
522 |
/**
|
523 |
* DOCUMENT ME!
|
524 |
*
|
525 |
* @param p DOCUMENT ME!
|
526 |
*
|
527 |
* @return DOCUMENT ME!
|
528 |
*/
|
529 |
private static GeneralPathX toShape(Polygon p) { |
530 |
GeneralPathX resul = new GeneralPathX();
|
531 |
Coordinate coord; |
532 |
|
533 |
for (int i = 0; i < p.getExteriorRing().getNumPoints(); i++) { |
534 |
coord = p.getExteriorRing().getCoordinateN(i); |
535 |
|
536 |
if (i == 0) { |
537 |
resul.moveTo(coord.x,coord.y); |
538 |
} else {
|
539 |
resul.lineTo(coord.x,coord.y); |
540 |
} |
541 |
} |
542 |
|
543 |
for (int j = 0; j < p.getNumInteriorRing(); j++) { |
544 |
LineString hole = p.getInteriorRingN(j); |
545 |
|
546 |
for (int k = 0; k < hole.getNumPoints(); k++) { |
547 |
coord = hole.getCoordinateN(k); |
548 |
|
549 |
if (k == 0) { |
550 |
resul.moveTo(coord.x, coord.y); |
551 |
} else {
|
552 |
resul.lineTo(coord.x, coord.y); |
553 |
} |
554 |
} |
555 |
} |
556 |
|
557 |
return resul;
|
558 |
} |
559 |
|
560 |
/**
|
561 |
* DOCUMENT ME!
|
562 |
*
|
563 |
* @param modelCoordinates DOCUMENT ME!
|
564 |
*
|
565 |
* @return DOCUMENT ME!
|
566 |
*
|
567 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
568 |
*
|
569 |
private Coordinate[] toViewCoordinates(Coordinate[] modelCoordinates)
|
570 |
throws NoninvertibleTransformException {
|
571 |
Coordinate[] viewCoordinates = new Coordinate[modelCoordinates.length];
|
572 |
|
573 |
for (int i = 0; i < modelCoordinates.length; i++) {
|
574 |
FPoint2D point2D = coordinate2FPoint2D(modelCoordinates[i]);
|
575 |
viewCoordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
|
576 |
}
|
577 |
|
578 |
return viewCoordinates;
|
579 |
} */
|
580 |
|
581 |
/* private Shape toShape(GeometryCollection gc)
|
582 |
throws NoninvertibleTransformException {
|
583 |
GeometryCollectionShape shape = new GeometryCollectionShape();
|
584 |
for (int i = 0; i < gc.getNumGeometries(); i++) {
|
585 |
Geometry g = (Geometry) gc.getGeometryN(i);
|
586 |
shape.add(toShape(g));
|
587 |
}
|
588 |
return shape;
|
589 |
} */
|
590 |
private static GeneralPathX toShape(MultiLineString mls) |
591 |
throws NoninvertibleTransformException { |
592 |
GeneralPathX path = new GeneralPathX();
|
593 |
|
594 |
for (int i = 0; i < mls.getNumGeometries(); i++) { |
595 |
LineString lineString = (LineString) mls.getGeometryN(i); |
596 |
path.append(toShape(lineString), false);
|
597 |
} |
598 |
|
599 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
600 |
//converted to GeneralPathXs. [Jon Aquino]
|
601 |
return path;
|
602 |
} |
603 |
|
604 |
/**
|
605 |
* DOCUMENT ME!
|
606 |
*
|
607 |
* @param lineString DOCUMENT ME!
|
608 |
*
|
609 |
* @return DOCUMENT ME!
|
610 |
*
|
611 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
612 |
*/
|
613 |
private static GeneralPathX toShape(LineString lineString) |
614 |
throws NoninvertibleTransformException { |
615 |
GeneralPathX shape = new GeneralPathX();
|
616 |
FPoint2D viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(0));
|
617 |
shape.moveTo(viewPoint.getX(), viewPoint.getY()); |
618 |
|
619 |
for (int i = 1; i < lineString.getNumPoints(); i++) { |
620 |
viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(i)); |
621 |
shape.lineTo(viewPoint.getX(), viewPoint.getY()); |
622 |
} |
623 |
|
624 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
625 |
//converted to GeneralPathXs. [Jon Aquino]
|
626 |
return shape;
|
627 |
} |
628 |
|
629 |
/**
|
630 |
* DOCUMENT ME!
|
631 |
*
|
632 |
* @param point DOCUMENT ME!
|
633 |
*
|
634 |
* @return DOCUMENT ME!
|
635 |
*
|
636 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
637 |
*/
|
638 |
private static FPoint2D toShape(Point point) |
639 |
throws NoninvertibleTransformException { |
640 |
FPoint2D viewPoint = coordinate2FPoint2D(point.getCoordinate()); |
641 |
|
642 |
return viewPoint;
|
643 |
} |
644 |
|
645 |
private static GeneralPathX toShape(MultiPolygon mp) |
646 |
throws NoninvertibleTransformException { |
647 |
GeneralPathX path = new GeneralPathX();
|
648 |
|
649 |
for (int i = 0; i < mp.getNumGeometries(); i++) { |
650 |
Polygon polygon = (Polygon) mp.getGeometryN(i); |
651 |
path.append(toShape(polygon), false);
|
652 |
} |
653 |
|
654 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
655 |
//converted to GeneralPathXs. [Jon Aquino]
|
656 |
return path;
|
657 |
} |
658 |
/**
|
659 |
* DOCUMENT ME!
|
660 |
*
|
661 |
* @param coord DOCUMENT ME!
|
662 |
*
|
663 |
* @return DOCUMENT ME!
|
664 |
*/
|
665 |
private static FPoint2D coordinate2FPoint2D(Coordinate coord) { |
666 |
return new FPoint2D(coord.x, coord.y); //,coord.z); |
667 |
} |
668 |
|
669 |
/**
|
670 |
* Convierte una Geometry de JTS a GeneralPathX.
|
671 |
*
|
672 |
* @param geometry Geometry a convertir.
|
673 |
*
|
674 |
* @return GeneralPathX.
|
675 |
*
|
676 |
* @throws NoninvertibleTransformException
|
677 |
* @throws IllegalArgumentException
|
678 |
*/
|
679 |
public static GeneralPathX toShape(Geometry geometry) |
680 |
throws NoninvertibleTransformException { |
681 |
if (geometry.isEmpty()) {
|
682 |
return new GeneralPathX(); |
683 |
} |
684 |
|
685 |
if (geometry instanceof Polygon) { |
686 |
return toShape((Polygon) geometry); |
687 |
} |
688 |
|
689 |
if (geometry instanceof MultiPolygon) { |
690 |
return toShape((MultiPolygon) geometry);
|
691 |
} |
692 |
|
693 |
if (geometry instanceof LineString) { |
694 |
return toShape((LineString) geometry);
|
695 |
} |
696 |
|
697 |
if (geometry instanceof MultiLineString) { |
698 |
return toShape((MultiLineString) geometry);
|
699 |
} |
700 |
|
701 |
if (geometry instanceof GeometryCollection) { |
702 |
return toShape((GeometryCollection) geometry);
|
703 |
} |
704 |
|
705 |
throw new IllegalArgumentException("Unrecognized Geometry class: " + |
706 |
geometry.getClass()); |
707 |
} |
708 |
|
709 |
/**
|
710 |
* Interfazaz para convertir una Coordinate de JTS a Point2D.
|
711 |
*
|
712 |
*/
|
713 |
public static interface PointConverter { |
714 |
/**
|
715 |
* DOCUMENT ME!
|
716 |
*
|
717 |
* @param modelCoordinate DOCUMENT ME!
|
718 |
*
|
719 |
* @return DOCUMENT ME!
|
720 |
*
|
721 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
722 |
*/
|
723 |
public Point2D toViewPoint(Coordinate modelCoordinate) |
724 |
throws NoninvertibleTransformException; |
725 |
} |
726 |
public static GeneralPathX transformToInts(GeneralPathX gp, AffineTransform at) { |
727 |
GeneralPathX newGp = new GeneralPathX();
|
728 |
PathIterator theIterator;
|
729 |
int theType;
|
730 |
int numParts = 0; |
731 |
double[] theData = new double[6]; |
732 |
Point2D ptDst = new Point2D.Double(); |
733 |
Point2D ptSrc = new Point2D.Double(); |
734 |
boolean bFirst = true; |
735 |
int xInt, yInt, antX = -1, antY = -1; |
736 |
|
737 |
theIterator = gp.getPathIterator(null); //, flatness); |
738 |
|
739 |
while (!theIterator.isDone()) {
|
740 |
theType = theIterator.currentSegment(theData); |
741 |
switch (theType) {
|
742 |
case PathIterator.SEG_MOVETO: |
743 |
numParts++; |
744 |
ptSrc.setLocation(theData[0], theData[1]); |
745 |
at.transform(ptSrc, ptDst); |
746 |
antX = (int) ptDst.getX();
|
747 |
antY = (int) ptDst.getY();
|
748 |
newGp.moveTo(antX, antY); |
749 |
bFirst = true;
|
750 |
break;
|
751 |
|
752 |
case PathIterator.SEG_LINETO: |
753 |
ptSrc.setLocation(theData[0], theData[1]); |
754 |
at.transform(ptSrc, ptDst); |
755 |
xInt = (int) ptDst.getX();
|
756 |
yInt = (int) ptDst.getY();
|
757 |
if ((bFirst) || ((xInt != antX) || (yInt != antY)))
|
758 |
{ |
759 |
newGp.lineTo(xInt, yInt); |
760 |
antX = xInt; |
761 |
antY = yInt; |
762 |
bFirst = false;
|
763 |
} |
764 |
break;
|
765 |
|
766 |
case PathIterator.SEG_QUADTO: |
767 |
System.out.println("Not supported here"); |
768 |
|
769 |
break;
|
770 |
|
771 |
case PathIterator.SEG_CUBICTO: |
772 |
System.out.println("Not supported here"); |
773 |
|
774 |
break;
|
775 |
|
776 |
case PathIterator.SEG_CLOSE: |
777 |
newGp.closePath(); |
778 |
|
779 |
break;
|
780 |
} //end switch
|
781 |
|
782 |
theIterator.next(); |
783 |
} //end while loop
|
784 |
|
785 |
return newGp;
|
786 |
} |
787 |
public static FShape transformToInts(IGeometry gp, AffineTransform at) { |
788 |
GeneralPathX newGp = new GeneralPathX();
|
789 |
double[] theData = new double[6]; |
790 |
double[] aux = new double[6]; |
791 |
|
792 |
// newGp.reset();
|
793 |
PathIterator theIterator;
|
794 |
int theType;
|
795 |
int numParts = 0; |
796 |
|
797 |
Point2D ptDst = new Point2D.Double(); |
798 |
Point2D ptSrc = new Point2D.Double(); |
799 |
boolean bFirst = true; |
800 |
int xInt, yInt, antX = -1, antY = -1; |
801 |
|
802 |
|
803 |
theIterator = gp.getPathIterator(null); //, flatness); |
804 |
int numSegmentsAdded = 0; |
805 |
while (!theIterator.isDone()) {
|
806 |
theType = theIterator.currentSegment(theData); |
807 |
|
808 |
switch (theType) {
|
809 |
case PathIterator.SEG_MOVETO: |
810 |
numParts++; |
811 |
ptSrc.setLocation(theData[0], theData[1]); |
812 |
at.transform(ptSrc, ptDst); |
813 |
antX = (int) ptDst.getX();
|
814 |
antY = (int) ptDst.getY();
|
815 |
newGp.moveTo(antX, antY); |
816 |
numSegmentsAdded++; |
817 |
bFirst = true;
|
818 |
break;
|
819 |
|
820 |
case PathIterator.SEG_LINETO: |
821 |
ptSrc.setLocation(theData[0], theData[1]); |
822 |
at.transform(ptSrc, ptDst); |
823 |
xInt = (int) ptDst.getX();
|
824 |
yInt = (int) ptDst.getY();
|
825 |
if ((bFirst) || ((xInt != antX) || (yInt != antY)))
|
826 |
{ |
827 |
newGp.lineTo(xInt, yInt); |
828 |
antX = xInt; |
829 |
antY = yInt; |
830 |
bFirst = false;
|
831 |
numSegmentsAdded++; |
832 |
} |
833 |
break;
|
834 |
|
835 |
case PathIterator.SEG_QUADTO: |
836 |
at.transform(theData,0,aux,0,2); |
837 |
newGp.quadTo(aux[0], aux[1], aux[2], aux[3]); |
838 |
numSegmentsAdded++; |
839 |
break;
|
840 |
|
841 |
case PathIterator.SEG_CUBICTO: |
842 |
at.transform(theData,0,aux,0,3); |
843 |
newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]); |
844 |
numSegmentsAdded++; |
845 |
break;
|
846 |
|
847 |
case PathIterator.SEG_CLOSE: |
848 |
if (numSegmentsAdded < 3) |
849 |
newGp.lineTo(antX, antY); |
850 |
newGp.closePath(); |
851 |
|
852 |
break;
|
853 |
} //end switch
|
854 |
|
855 |
theIterator.next(); |
856 |
} //end while loop
|
857 |
FShape shp = null;
|
858 |
switch (gp.getGeometryType())
|
859 |
{ |
860 |
case FShape.POINT: //Tipo punto |
861 |
case FShape.POINT + FShape.Z:
|
862 |
shp = new FPoint2D(ptDst.getX(), ptDst.getY());
|
863 |
break;
|
864 |
|
865 |
case FShape.LINE:
|
866 |
case FShape.LINE + FShape.Z:
|
867 |
case FShape.ARC:
|
868 |
shp = new FPolyline2D(newGp);
|
869 |
break;
|
870 |
case FShape.POLYGON:
|
871 |
case FShape.POLYGON + FShape.Z:
|
872 |
case FShape.CIRCLE:
|
873 |
case FShape.ELLIPSE:
|
874 |
shp = new FPolygon2D(newGp);
|
875 |
break;
|
876 |
} |
877 |
return shp;
|
878 |
} |
879 |
|
880 |
public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR) |
881 |
{ |
882 |
Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(), |
883 |
jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight()); |
884 |
return r;
|
885 |
} |
886 |
|
887 |
public static Envelope convertRectangle2DtoEnvelope(Rectangle2D r) |
888 |
{ |
889 |
Envelope e = new Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(),
|
890 |
r.getY() + r.getHeight()); |
891 |
return e;
|
892 |
} |
893 |
|
894 |
/**
|
895 |
* Return a correct polygon (no hole)
|
896 |
* @param coordinates
|
897 |
* @return
|
898 |
*/
|
899 |
public static IGeometry getExteriorPolygon(Coordinate[] coordinates) |
900 |
{ |
901 |
// isCCW = true => it's a hole
|
902 |
Coordinate[] vs=new Coordinate[coordinates.length]; |
903 |
if (CGAlgorithms.isCCW(coordinates)){
|
904 |
for (int i=vs.length-1;i>=0;i--){ |
905 |
vs[i]=coordinates[i]; |
906 |
} |
907 |
}else{
|
908 |
vs=coordinates; |
909 |
} |
910 |
LinearRing ring = geomFactory.createLinearRing(vs); |
911 |
|
912 |
try {
|
913 |
return ShapeFactory.createPolygon2D(toShape(ring));
|
914 |
} catch (NoninvertibleTransformException e) { |
915 |
e.printStackTrace(); |
916 |
} |
917 |
return null; |
918 |
} |
919 |
|
920 |
/**
|
921 |
* Return a hole (CCW ordered points)
|
922 |
* @param coordinates
|
923 |
* @return
|
924 |
*/
|
925 |
public static IGeometry getHole(Coordinate[] coordinates) |
926 |
{ |
927 |
// isCCW = true => it's a hole
|
928 |
Coordinate[] vs=new Coordinate[coordinates.length]; |
929 |
if (CGAlgorithms.isCCW(coordinates)){
|
930 |
vs=coordinates; |
931 |
|
932 |
}else{
|
933 |
for (int i=vs.length-1;i>=0;i--){ |
934 |
vs[i]=coordinates[i]; |
935 |
} |
936 |
} |
937 |
LinearRing ring = geomFactory.createLinearRing(vs); |
938 |
|
939 |
try {
|
940 |
return ShapeFactory.createPolygon2D(toShape(ring));
|
941 |
} catch (NoninvertibleTransformException e) { |
942 |
e.printStackTrace(); |
943 |
} |
944 |
return null; |
945 |
} |
946 |
|
947 |
public static Shape getExteriorPolygon(GeneralPathX gp) { |
948 |
Area area = new Area(gp); |
949 |
|
950 |
return area;
|
951 |
|
952 |
|
953 |
} |
954 |
|
955 |
/* public static GeometryCollection convertFGeometryCollection(FGeometryCollection fGeomC)
|
956 |
{
|
957 |
|
958 |
geomFactory.createGeometryCollection(theGeoms);
|
959 |
} */
|
960 |
} |