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