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