root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FConverter.java @ 2354
History | View | Annotate | Download (18.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 com.iver.cit.gvsig.fmap.core.FPoint2D; |
50 |
import com.iver.cit.gvsig.fmap.core.FPolygon2D; |
51 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
52 |
import com.iver.cit.gvsig.fmap.core.FShape; |
53 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
54 |
|
55 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
56 |
import com.vividsolutions.jts.algorithm.RobustCGAlgorithms; |
57 |
import com.vividsolutions.jts.geom.Coordinate; |
58 |
import com.vividsolutions.jts.geom.CoordinateArrays; |
59 |
import com.vividsolutions.jts.geom.Envelope; |
60 |
import com.vividsolutions.jts.geom.Geometry; |
61 |
import com.vividsolutions.jts.geom.GeometryCollection; |
62 |
import com.vividsolutions.jts.geom.GeometryFactory; |
63 |
import com.vividsolutions.jts.geom.LineString; |
64 |
import com.vividsolutions.jts.geom.LinearRing; |
65 |
import com.vividsolutions.jts.geom.MultiLineString; |
66 |
import com.vividsolutions.jts.geom.MultiPolygon; |
67 |
import com.vividsolutions.jts.geom.Point; |
68 |
import com.vividsolutions.jts.geom.Polygon; |
69 |
|
70 |
import java.awt.geom.AffineTransform; |
71 |
import java.awt.geom.NoninvertibleTransformException; |
72 |
import java.awt.geom.PathIterator; |
73 |
import java.awt.geom.Point2D; |
74 |
|
75 |
import java.lang.reflect.Array; |
76 |
|
77 |
import java.util.ArrayList; |
78 |
|
79 |
|
80 |
/**
|
81 |
* Clase con varios m?todos est?ticos utilizados para pasar de java2d a jts y
|
82 |
* viceversa.
|
83 |
*
|
84 |
* @author fjp
|
85 |
*/
|
86 |
public class FConverter { |
87 |
/**
|
88 |
* ?QU? PODEMOS HACER CON LOS MULTIPOINT??? => DEBER?AMOS TRABAJAR CON UN
|
89 |
* ARRAY DE PUNTOS EN FShape.....Pensarlo bien.
|
90 |
*/
|
91 |
private final static GeometryFactory geomFactory = new GeometryFactory(); |
92 |
protected static CGAlgorithms cga = new RobustCGAlgorithms(); |
93 |
private final static AffineTransform at = new AffineTransform(); |
94 |
private static double POINT_MARKER_SIZE = 3.0; |
95 |
private static PointConverter pointConverter = new PointConverter() { |
96 |
/* (non-Javadoc)
|
97 |
* @see com.iver.cit.opensig.fmap.FConverter.PointConverter#toViewPoint(com.vividsolutions.jts.geom.Coordinate)
|
98 |
*/
|
99 |
public Point2D toViewPoint(Coordinate modelCoordinate) |
100 |
throws NoninvertibleTransformException { |
101 |
// TODO Auto-generated method stub
|
102 |
return null; |
103 |
} |
104 |
}; |
105 |
|
106 |
//returns true if testPoint is a point in the pointList list.
|
107 |
static boolean pointInList(Coordinate testPoint, Coordinate[] pointList) { |
108 |
int t;
|
109 |
int numpoints;
|
110 |
Coordinate p; |
111 |
|
112 |
numpoints = Array.getLength(pointList);
|
113 |
|
114 |
for (t = 0; t < numpoints; t++) { |
115 |
p = pointList[t]; |
116 |
|
117 |
if ((testPoint.x == p.x) && (testPoint.y == p.y) &&
|
118 |
((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) //nan test; x!=x iff x is nan
|
119 |
) { |
120 |
return true; |
121 |
} |
122 |
} |
123 |
|
124 |
return false; |
125 |
} |
126 |
|
127 |
/**
|
128 |
* Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
|
129 |
* "flattened PathIterator". El flattened indica que las curvas las pasa a
|
130 |
* segmentos de l?nea recta AUTOMATICAMENTE!!!.
|
131 |
*
|
132 |
* @param shp FShape que se quiere convertir.
|
133 |
*
|
134 |
* @return Geometry de JTS.
|
135 |
*/
|
136 |
public static Geometry java2d_to_jts(FShape shp) { |
137 |
double flatness = 0.8; // Por ejemplo. Cuanto m?s peque?o, m?s segmentos necesitar? la curva |
138 |
|
139 |
// Es la m?xima distancia que permitimos que el trazo aproximado
|
140 |
// difiera del trazo real.
|
141 |
Geometry geoJTS = null;
|
142 |
Coordinate coord; |
143 |
Coordinate[] coords;
|
144 |
ArrayList arrayCoords = null; |
145 |
ArrayList arrayLines;
|
146 |
LineString lin; |
147 |
LinearRing linRing; |
148 |
LinearRing linRingExt = null;
|
149 |
int theType;
|
150 |
int numParts = 0; |
151 |
|
152 |
// Use this array to store segment coordinate data
|
153 |
double[] theData = new double[6]; |
154 |
PathIterator theIterator;
|
155 |
|
156 |
switch (shp.getShapeType()) {
|
157 |
case FShape.POINT:
|
158 |
case FShape.POINT + FShape.Z:
|
159 |
FPoint2D p = (FPoint2D) shp; |
160 |
coord = new Coordinate(p.getX(), p.getY());
|
161 |
geoJTS = new GeometryFactory().createPoint(coord);
|
162 |
|
163 |
break;
|
164 |
|
165 |
case FShape.LINE:
|
166 |
case FShape.LINE + FShape.Z:
|
167 |
arrayLines = new ArrayList(); |
168 |
theIterator = shp.getPathIterator(null); //, flatness); |
169 |
|
170 |
while (!theIterator.isDone()) {
|
171 |
//while not done
|
172 |
theType = theIterator.currentSegment(theData); |
173 |
|
174 |
//Populate a segment of the new
|
175 |
// GeneralPathX object.
|
176 |
//Process the current segment to populate a new
|
177 |
// segment of the new GeneralPathX object.
|
178 |
switch (theType) {
|
179 |
case PathIterator.SEG_MOVETO: |
180 |
|
181 |
// System.out.println("SEG_MOVETO");
|
182 |
if (arrayCoords == null) { |
183 |
arrayCoords = new ArrayList(); |
184 |
} else {
|
185 |
lin = new GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
|
186 |
arrayCoords)); |
187 |
arrayLines.add(lin); |
188 |
arrayCoords = new ArrayList(); |
189 |
} |
190 |
|
191 |
numParts++; |
192 |
arrayCoords.add(new Coordinate(theData[0], |
193 |
theData[1]));
|
194 |
|
195 |
break;
|
196 |
|
197 |
case PathIterator.SEG_LINETO: |
198 |
|
199 |
// System.out.println("SEG_LINETO");
|
200 |
arrayCoords.add(new Coordinate(theData[0], |
201 |
theData[1]));
|
202 |
|
203 |
break;
|
204 |
|
205 |
case PathIterator.SEG_QUADTO: |
206 |
System.out.println("Not supported here"); |
207 |
|
208 |
break;
|
209 |
|
210 |
case PathIterator.SEG_CUBICTO: |
211 |
System.out.println("Not supported here"); |
212 |
|
213 |
break;
|
214 |
|
215 |
case PathIterator.SEG_CLOSE: |
216 |
System.out.println("SEG_CLOSE"); |
217 |
|
218 |
// A?adimos el primer punto para cerrar.
|
219 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
220 |
arrayCoords.add(new Coordinate(firstCoord.x,
|
221 |
firstCoord.y)); |
222 |
|
223 |
break;
|
224 |
} //end switch
|
225 |
|
226 |
theIterator.next(); |
227 |
} //end while loop
|
228 |
|
229 |
lin = new GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
|
230 |
arrayCoords)); |
231 |
|
232 |
if (numParts > 1) // Generamos una MultiLineString |
233 |
{ |
234 |
arrayLines.add(lin); |
235 |
geoJTS = new GeometryFactory().createMultiLineString(GeometryFactory.toLineStringArray(
|
236 |
arrayLines)); |
237 |
} else {
|
238 |
geoJTS = lin; |
239 |
} |
240 |
|
241 |
break;
|
242 |
|
243 |
case FShape.POLYGON:
|
244 |
case FShape.POLYGON + FShape.Z:
|
245 |
arrayLines = new ArrayList(); |
246 |
|
247 |
ArrayList shells = new ArrayList(); |
248 |
ArrayList holes = new ArrayList(); |
249 |
Coordinate[] points = null; |
250 |
|
251 |
theIterator = shp.getPathIterator(at, flatness); |
252 |
|
253 |
while (!theIterator.isDone()) {
|
254 |
//while not done
|
255 |
theType = theIterator.currentSegment(theData); |
256 |
|
257 |
//Populate a segment of the new
|
258 |
// GeneralPathX object.
|
259 |
//Process the current segment to populate a new
|
260 |
// segment of the new GeneralPathX object.
|
261 |
switch (theType) {
|
262 |
case PathIterator.SEG_MOVETO: |
263 |
|
264 |
// System.out.println("SEG_MOVETO");
|
265 |
if (arrayCoords == null) { |
266 |
arrayCoords = new ArrayList(); |
267 |
} else {
|
268 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
269 |
|
270 |
try {
|
271 |
LinearRing ring = geomFactory.createLinearRing(points); |
272 |
|
273 |
if (CGAlgorithms.isCCW(points)) {
|
274 |
holes.add(ring); |
275 |
} else {
|
276 |
shells.add(ring); |
277 |
} |
278 |
} catch (Exception e) { |
279 |
System.err.println(
|
280 |
"Caught Topology exception in GMLLinearRingHandler");
|
281 |
|
282 |
return null; |
283 |
} |
284 |
|
285 |
/* if (numParts == 1)
|
286 |
{
|
287 |
linRingExt = new GeometryFactory().createLinearRing(
|
288 |
CoordinateArrays.toCoordinateArray(arrayCoords));
|
289 |
}
|
290 |
else
|
291 |
{
|
292 |
linRing = new GeometryFactory().createLinearRing(
|
293 |
CoordinateArrays.toCoordinateArray(arrayCoords));
|
294 |
arrayLines.add(linRing);
|
295 |
} */
|
296 |
arrayCoords = new ArrayList(); |
297 |
} |
298 |
|
299 |
numParts++; |
300 |
arrayCoords.add(new Coordinate(theData[0], |
301 |
theData[1]));
|
302 |
|
303 |
break;
|
304 |
|
305 |
case PathIterator.SEG_LINETO: |
306 |
|
307 |
// System.out.println("SEG_LINETO");
|
308 |
arrayCoords.add(new Coordinate(theData[0], |
309 |
theData[1]));
|
310 |
|
311 |
break;
|
312 |
|
313 |
case PathIterator.SEG_QUADTO: |
314 |
System.out.println("SEG_QUADTO Not supported here"); |
315 |
|
316 |
break;
|
317 |
|
318 |
case PathIterator.SEG_CUBICTO: |
319 |
System.out.println("SEG_CUBICTO Not supported here"); |
320 |
|
321 |
break;
|
322 |
|
323 |
case PathIterator.SEG_CLOSE: |
324 |
|
325 |
// A?adimos el primer punto para cerrar.
|
326 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
327 |
arrayCoords.add(new Coordinate(firstCoord.x,
|
328 |
firstCoord.y)); |
329 |
|
330 |
break;
|
331 |
} //end switch
|
332 |
|
333 |
// System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
|
334 |
theIterator.next(); |
335 |
} //end while loop
|
336 |
|
337 |
// arrayCoords.add(arrayCoords.get(0));
|
338 |
points = CoordinateArrays.toCoordinateArray(arrayCoords); |
339 |
|
340 |
try {
|
341 |
LinearRing ring = geomFactory.createLinearRing(points); |
342 |
|
343 |
if (CGAlgorithms.isCCW(points)) {
|
344 |
holes.add(ring); |
345 |
} else {
|
346 |
shells.add(ring); |
347 |
} |
348 |
} catch (Exception e) { |
349 |
System.err.println(
|
350 |
"Caught Topology exception in GMLLinearRingHandler");
|
351 |
|
352 |
return null; |
353 |
} |
354 |
|
355 |
/* linRing = new GeometryFactory().createLinearRing(
|
356 |
CoordinateArrays.toCoordinateArray(arrayCoords)); */
|
357 |
|
358 |
// System.out.println("NumParts = " + numParts);
|
359 |
//now we have a list of all shells and all holes
|
360 |
ArrayList holesForShells = new ArrayList(shells.size()); |
361 |
|
362 |
for (int i = 0; i < shells.size(); i++) { |
363 |
holesForShells.add(new ArrayList()); |
364 |
} |
365 |
|
366 |
//find homes
|
367 |
for (int i = 0; i < holes.size(); i++) { |
368 |
LinearRing testRing = (LinearRing) holes.get(i); |
369 |
LinearRing minShell = null;
|
370 |
Envelope minEnv = null;
|
371 |
Envelope testEnv = testRing.getEnvelopeInternal(); |
372 |
Coordinate testPt = testRing.getCoordinateN(0);
|
373 |
LinearRing tryRing; |
374 |
|
375 |
for (int j = 0; j < shells.size(); j++) { |
376 |
tryRing = (LinearRing) shells.get(j); |
377 |
|
378 |
Envelope tryEnv = tryRing.getEnvelopeInternal(); |
379 |
|
380 |
if (minShell != null) { |
381 |
minEnv = minShell.getEnvelopeInternal(); |
382 |
} |
383 |
|
384 |
boolean isContained = false; |
385 |
Coordinate[] coordList = tryRing.getCoordinates();
|
386 |
|
387 |
if (tryEnv.contains(testEnv) &&
|
388 |
(CGAlgorithms.isPointInRing(testPt, coordList) || |
389 |
(pointInList(testPt, coordList)))) { |
390 |
isContained = true;
|
391 |
} |
392 |
|
393 |
// check if this new containing ring is smaller than the current minimum ring
|
394 |
if (isContained) {
|
395 |
if ((minShell == null) || minEnv.contains(tryEnv)) { |
396 |
minShell = tryRing; |
397 |
} |
398 |
} |
399 |
} |
400 |
|
401 |
if (minShell == null) { |
402 |
System.out.println(
|
403 |
"polygon found with a hole thats not inside a shell");
|
404 |
} else {
|
405 |
((ArrayList) holesForShells.get(shells.indexOf(minShell))).add(testRing);
|
406 |
} |
407 |
} |
408 |
|
409 |
Polygon[] polygons = new Polygon[shells.size()]; |
410 |
|
411 |
for (int i = 0; i < shells.size(); i++) { |
412 |
polygons[i] = geomFactory.createPolygon((LinearRing) shells.get( |
413 |
i), |
414 |
(LinearRing[]) ((ArrayList) holesForShells.get(i)).toArray( |
415 |
new LinearRing[0])); |
416 |
} |
417 |
|
418 |
if (polygons.length == 1) { |
419 |
return polygons[0]; |
420 |
} |
421 |
|
422 |
holesForShells = null;
|
423 |
shells = null;
|
424 |
holes = null;
|
425 |
|
426 |
//its a multi part
|
427 |
geoJTS = geomFactory.createMultiPolygon(polygons); |
428 |
|
429 |
/* if (numParts > 1) // Generamos un Polygon con agujeros
|
430 |
{
|
431 |
arrayLines.add(linRing);
|
432 |
// geoJTS = new GeometryFactory().createPolygon(linRingExt,
|
433 |
// GeometryFactory.toLinearRingArray(arrayLines));
|
434 |
geoJTS = new GeometryFactory().buildGeometry(arrayLines);
|
435 |
|
436 |
// geoJTS = Polygonizer.class.
|
437 |
}
|
438 |
else
|
439 |
{
|
440 |
geoJTS = new GeometryFactory().createPolygon(linRing,null);
|
441 |
} */
|
442 |
break;
|
443 |
} |
444 |
|
445 |
return geoJTS;
|
446 |
} |
447 |
|
448 |
/**
|
449 |
* Converts JTS Geometry objects into Java 2D Shape objects
|
450 |
*
|
451 |
* @param geo Geometry de JTS.
|
452 |
*
|
453 |
* @return FShape.
|
454 |
*/
|
455 |
public static FShape jts_to_java2d(Geometry geo) { |
456 |
FShape shpNew = null;
|
457 |
|
458 |
try {
|
459 |
if (geo instanceof Point) { |
460 |
shpNew = new FPoint2D(((Point) geo).getX(), ((Point) geo).getY()); |
461 |
} |
462 |
|
463 |
if (geo.isEmpty()) {
|
464 |
shpNew = null;
|
465 |
} |
466 |
|
467 |
if (geo instanceof Polygon) { |
468 |
shpNew = new FPolygon2D(toShape((Polygon) geo)); |
469 |
} |
470 |
|
471 |
if (geo instanceof MultiPolygon) { |
472 |
shpNew = new FPolygon2D(toShape((MultiPolygon) geo));
|
473 |
} |
474 |
|
475 |
if (geo instanceof LineString) { |
476 |
shpNew = new FPolyline2D(toShape((LineString) geo));
|
477 |
} |
478 |
|
479 |
if (geo instanceof MultiLineString) { |
480 |
shpNew = new FPolyline2D(toShape((MultiLineString) geo));
|
481 |
} |
482 |
|
483 |
/* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN GeometryCollection
|
484 |
* No sabremos si queremos una l?nea o un pol?gono.....
|
485 |
* if (geometry instanceof GeometryCollection) {
|
486 |
return toShape((GeometryCollection) geometry);
|
487 |
} */
|
488 |
return shpNew;
|
489 |
} catch (NoninvertibleTransformException e) { |
490 |
// TODO Auto-generated catch block
|
491 |
e.printStackTrace(); |
492 |
} |
493 |
|
494 |
return null; |
495 |
} |
496 |
|
497 |
/**
|
498 |
* DOCUMENT ME!
|
499 |
*
|
500 |
* @param p DOCUMENT ME!
|
501 |
*
|
502 |
* @return DOCUMENT ME!
|
503 |
*/
|
504 |
private static GeneralPathX toShape(Polygon p) { |
505 |
GeneralPathX resul = new GeneralPathX();
|
506 |
Coordinate coord; |
507 |
|
508 |
for (int i = 0; i < p.getExteriorRing().getNumPoints(); i++) { |
509 |
coord = p.getExteriorRing().getCoordinateN(i); |
510 |
|
511 |
if (i == 0) { |
512 |
resul.moveTo((float) coord.x, (float) coord.y); |
513 |
} else {
|
514 |
resul.lineTo((float) coord.x, (float) coord.y); |
515 |
} |
516 |
} |
517 |
|
518 |
for (int j = 0; j < p.getNumInteriorRing(); j++) { |
519 |
LineString hole = p.getInteriorRingN(j); |
520 |
|
521 |
for (int k = 0; k < hole.getNumPoints(); k++) { |
522 |
coord = hole.getCoordinateN(k); |
523 |
|
524 |
if (k == 0) { |
525 |
resul.moveTo((float) coord.x, (float) coord.y); |
526 |
} else {
|
527 |
resul.lineTo((float) coord.x, (float) coord.y); |
528 |
} |
529 |
} |
530 |
} |
531 |
|
532 |
return resul;
|
533 |
} |
534 |
|
535 |
/**
|
536 |
* DOCUMENT ME!
|
537 |
*
|
538 |
* @param modelCoordinates DOCUMENT ME!
|
539 |
*
|
540 |
* @return DOCUMENT ME!
|
541 |
*
|
542 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
543 |
*/
|
544 |
private Coordinate[] toViewCoordinates(Coordinate[] modelCoordinates) |
545 |
throws NoninvertibleTransformException { |
546 |
Coordinate[] viewCoordinates = new Coordinate[modelCoordinates.length]; |
547 |
|
548 |
for (int i = 0; i < modelCoordinates.length; i++) { |
549 |
FPoint2D point2D = toViewPoint(modelCoordinates[i]); |
550 |
viewCoordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
|
551 |
} |
552 |
|
553 |
return viewCoordinates;
|
554 |
} |
555 |
|
556 |
/* private Shape toShape(GeometryCollection gc)
|
557 |
throws NoninvertibleTransformException {
|
558 |
GeometryCollectionShape shape = new GeometryCollectionShape();
|
559 |
for (int i = 0; i < gc.getNumGeometries(); i++) {
|
560 |
Geometry g = (Geometry) gc.getGeometryN(i);
|
561 |
shape.add(toShape(g));
|
562 |
}
|
563 |
return shape;
|
564 |
} */
|
565 |
private static GeneralPathX toShape(MultiLineString mls) |
566 |
throws NoninvertibleTransformException { |
567 |
GeneralPathX path = new GeneralPathX();
|
568 |
|
569 |
for (int i = 0; i < mls.getNumGeometries(); i++) { |
570 |
LineString lineString = (LineString) mls.getGeometryN(i); |
571 |
path.append(toShape(lineString), false);
|
572 |
} |
573 |
|
574 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
575 |
//converted to GeneralPathXs. [Jon Aquino]
|
576 |
return path;
|
577 |
} |
578 |
|
579 |
/**
|
580 |
* DOCUMENT ME!
|
581 |
*
|
582 |
* @param lineString DOCUMENT ME!
|
583 |
*
|
584 |
* @return DOCUMENT ME!
|
585 |
*
|
586 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
587 |
*/
|
588 |
private static GeneralPathX toShape(LineString lineString) |
589 |
throws NoninvertibleTransformException { |
590 |
GeneralPathX shape = new GeneralPathX();
|
591 |
FPoint2D viewPoint = toViewPoint(lineString.getCoordinateN(0));
|
592 |
shape.moveTo((float) viewPoint.getX(), (float) viewPoint.getY()); |
593 |
|
594 |
for (int i = 1; i < lineString.getNumPoints(); i++) { |
595 |
viewPoint = toViewPoint(lineString.getCoordinateN(i)); |
596 |
shape.lineTo((float) viewPoint.getX(), (float) viewPoint.getY()); |
597 |
} |
598 |
|
599 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
600 |
//converted to GeneralPathXs. [Jon Aquino]
|
601 |
return shape;
|
602 |
} |
603 |
|
604 |
/**
|
605 |
* DOCUMENT ME!
|
606 |
*
|
607 |
* @param point DOCUMENT ME!
|
608 |
*
|
609 |
* @return DOCUMENT ME!
|
610 |
*
|
611 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
612 |
*/
|
613 |
private static FPoint2D toShape(Point point) |
614 |
throws NoninvertibleTransformException { |
615 |
FPoint2D viewPoint = toViewPoint(point.getCoordinate()); |
616 |
|
617 |
return viewPoint;
|
618 |
} |
619 |
|
620 |
private static GeneralPathX toShape(MultiPolygon mp) |
621 |
throws NoninvertibleTransformException { |
622 |
GeneralPathX path = new GeneralPathX();
|
623 |
|
624 |
for (int i = 0; i < mp.getNumGeometries(); i++) { |
625 |
Polygon polygon = (Polygon) mp.getGeometryN(i); |
626 |
path.append(toShape(polygon), false);
|
627 |
} |
628 |
|
629 |
//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
|
630 |
//converted to GeneralPathXs. [Jon Aquino]
|
631 |
return path;
|
632 |
} |
633 |
/**
|
634 |
* DOCUMENT ME!
|
635 |
*
|
636 |
* @param coord DOCUMENT ME!
|
637 |
*
|
638 |
* @return DOCUMENT ME!
|
639 |
*/
|
640 |
private static FPoint2D toViewPoint(Coordinate coord) { |
641 |
return new FPoint2D(coord.x, coord.y); //,coord.z); |
642 |
} |
643 |
|
644 |
/**
|
645 |
* Convierte una Geometry de JTS a GeneralPathX.
|
646 |
*
|
647 |
* @param geometry Geometry a convertir.
|
648 |
*
|
649 |
* @return GeneralPathX.
|
650 |
*
|
651 |
* @throws NoninvertibleTransformException
|
652 |
* @throws IllegalArgumentException
|
653 |
*/
|
654 |
public static GeneralPathX toShape(Geometry geometry) |
655 |
throws NoninvertibleTransformException { |
656 |
if (geometry.isEmpty()) {
|
657 |
return new GeneralPathX(); |
658 |
} |
659 |
|
660 |
if (geometry instanceof Polygon) { |
661 |
return toShape((Polygon) geometry); |
662 |
} |
663 |
|
664 |
if (geometry instanceof MultiPolygon) { |
665 |
return toShape((MultiPolygon) geometry);
|
666 |
} |
667 |
|
668 |
if (geometry instanceof LineString) { |
669 |
return toShape((LineString) geometry);
|
670 |
} |
671 |
|
672 |
if (geometry instanceof MultiLineString) { |
673 |
return toShape((MultiLineString) geometry);
|
674 |
} |
675 |
|
676 |
if (geometry instanceof GeometryCollection) { |
677 |
return toShape((GeometryCollection) geometry);
|
678 |
} |
679 |
|
680 |
throw new IllegalArgumentException("Unrecognized Geometry class: " + |
681 |
geometry.getClass()); |
682 |
} |
683 |
|
684 |
/**
|
685 |
* Interfazaz para convertir una Coordinate de JTS a Point2D.
|
686 |
*
|
687 |
*/
|
688 |
public static interface PointConverter { |
689 |
/**
|
690 |
* DOCUMENT ME!
|
691 |
*
|
692 |
* @param modelCoordinate DOCUMENT ME!
|
693 |
*
|
694 |
* @return DOCUMENT ME!
|
695 |
*
|
696 |
* @throws NoninvertibleTransformException DOCUMENT ME!
|
697 |
*/
|
698 |
public Point2D toViewPoint(Coordinate modelCoordinate) |
699 |
throws NoninvertibleTransformException; |
700 |
} |
701 |
} |