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