Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / util / Converter.java @ 38185

History | View | Annotate | Download (37.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.geom.util;
42

    
43
import java.awt.Shape;
44
import java.awt.geom.AffineTransform;
45
import java.awt.geom.Area;
46
import java.awt.geom.NoninvertibleTransformException;
47
import java.awt.geom.PathIterator;
48
import java.awt.geom.Rectangle2D;
49
import java.lang.reflect.Array;
50
import java.util.ArrayList;
51

    
52
import com.vividsolutions.jts.algorithm.CGAlgorithms;
53
import com.vividsolutions.jts.algorithm.RobustCGAlgorithms;
54
import com.vividsolutions.jts.geom.Coordinate;
55
import com.vividsolutions.jts.geom.CoordinateArrays;
56
import com.vividsolutions.jts.geom.Envelope;
57
import com.vividsolutions.jts.geom.GeometryCollection;
58
import com.vividsolutions.jts.geom.LineString;
59
import com.vividsolutions.jts.geom.LinearRing;
60
import com.vividsolutions.jts.geom.MultiLineString;
61
import com.vividsolutions.jts.geom.MultiPolygon;
62
import com.vividsolutions.jts.geom.Point;
63
import com.vividsolutions.jts.geom.Polygon;
64

    
65
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
67

    
68
import org.gvsig.fmap.geom.Geometry;
69
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
70
import org.gvsig.fmap.geom.Geometry.TYPES;
71
import org.gvsig.fmap.geom.GeometryLocator;
72
import org.gvsig.fmap.geom.GeometryManager;
73
import org.gvsig.fmap.geom.aggregate.MultiCurve;
74
import org.gvsig.fmap.geom.aggregate.MultiPoint;
75
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
76
import org.gvsig.fmap.geom.aggregate.MultiSurface;
77
import org.gvsig.fmap.geom.exception.CreateGeometryException;
78
import org.gvsig.fmap.geom.primitive.GeneralPathX;
79
import org.gvsig.fmap.geom.primitive.Surface;
80

    
81
/**
82
 * Clase con varios m?todos est?ticos utilizados para pasar de java2d a jts
83
 * y viceversa.
84
 * 
85
 * @author fjp
86
 * @deprecated to be removed or moved from API to implementation in gvSIG 2.1.0
87
 */
88
public class Converter {
89
        private static final GeometryManager geomManager = GeometryLocator
90
                        .getGeometryManager();
91
        private static final Logger logger = LoggerFactory
92
                        .getLogger(Converter.class);
93

    
94
        // private static Logger logger = Logger.getLogger(Converter.class);
95

    
96
        /**
97
         * ?QU? PODEMOS HACER CON LOS MULTIPOINT??? => DEBER?AMOS TRABAJAR CON
98
         * UN ARRAY DE PUNTOS EN FShape.....Pensarlo bien.
99
         */
100
        public final static com.vividsolutions.jts.geom.GeometryFactory geomFactory = new com.vividsolutions.jts.geom.GeometryFactory();
101
        public static CGAlgorithms cga = new RobustCGAlgorithms();
102
        // private final static AffineTransform at = new AffineTransform();
103
        // private static double POINT_MARKER_SIZE = 3.0;
104

    
105
        private static GeometryManager manager = GeometryLocator
106
                        .getGeometryManager();
107

    
108
        // returns true if testPoint is a point in the pointList list.
109
        static boolean pointInList(Coordinate testPoint, Coordinate[] pointList) {
110
                int t;
111
                int numpoints;
112
                Coordinate p;
113

    
114
                numpoints = Array.getLength(pointList);
115

    
116
                for (t = 0; t < numpoints; t++) {
117
                        p = pointList[t];
118

    
119
                        if ((testPoint.x == p.x)
120
                                        && (testPoint.y == p.y)
121
                                        && ((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) // nan
122
                                                                                                                                                                        // test;
123
                                                                                                                                                                        // x!=x
124
                                                                                                                                                                        // iff
125
                                                                                                                                                                        // x
126
                                                                                                                                                                        // is
127
                                                                                                                                                                        // nan
128
                        ) {
129
                                return true;
130
                        }
131
                }
132

    
133
                return false;
134
        }
135

    
136
        /**
137
         * Receives a JTS Geometry and returns a fmap IGeometry
138
         * 
139
         * @param jtsGeometry
140
         *            jts Geometry
141
         * @return IGeometry of FMap
142
         * @author azabala
143
         * @throws CreateGeometryException
144
         */
145
        public static Geometry jtsToGeometry(
146
                        com.vividsolutions.jts.geom.Geometry geo)
147
                        throws CreateGeometryException {
148
                Geometry shpNew = null;
149

    
150
                try {
151
                        if (geo instanceof Point) {
152
                                shpNew = geomManager.createPoint(((Point) geo).getX(),
153
                                                ((Point) geo).getY(), SUBTYPES.GEOM2D);
154
                        }
155

    
156
                        if (geo.isEmpty()) {
157
                                shpNew = null;
158
                        }
159

    
160
                        try {
161
                                if (geo instanceof Polygon) {
162
                                        shpNew = geomManager.createSurface(toShape((Polygon) geo),
163
                                                        SUBTYPES.GEOM2D);
164
                                }
165

    
166
                                if (geo instanceof MultiPolygon) {
167
                                        shpNew = geomManager.createSurface(
168
                                                        toShape((MultiPolygon) geo), SUBTYPES.GEOM2D);
169
                                }
170

    
171
                                if (geo instanceof LineString) {
172
                                        shpNew = geomManager.createCurve(toShape((LineString) geo),
173
                                                        SUBTYPES.GEOM2D);
174
                                }
175

    
176
                                if (geo instanceof MultiLineString) {
177
                                        shpNew = geomManager.createCurve(
178
                                                        toShape((MultiLineString) geo), SUBTYPES.GEOM2D);
179
                                }
180
                        } catch (CreateGeometryException e) {
181
                                logger.error("Error creating a geometry", e);
182
                        }
183

    
184
                        /*
185
                         * OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN
186
                         * GeometryCollection No sabremos si queremos una l?nea o un
187
                         * pol?gono..... if (geometry instanceof GeometryCollection) {
188
                         * return toShape((GeometryCollection) geometry); }
189
                         */
190
                        return shpNew;
191
                } catch (NoninvertibleTransformException e) {
192
                        // TODO Auto-generated catch block
193
                        e.printStackTrace();
194
                }
195

    
196
                return null;
197

    
198
                //
199
                // FShape shape = Converter.jts_to_java2d(jtsGeometry);
200
                // return factory.createGeometry(shape);
201
        }
202

    
203
        /**
204
         * Convierte un MultiPoint2D a un MultiPoint de JTS
205
         * 
206
         * @param geom
207
         * @return
208
         */
209
        public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPoint geom) {
210
                Coordinate[] theGeoms = new Coordinate[geom.getPrimitivesNumber()];
211
                for (int i = 0; i < theGeoms.length; i++) {
212
                        java.awt.geom.Point2D p = geom.getPrimitiveAt(i).getHandlers(
213
                                        Geometry.SELECTHANDLER)[0].getPoint();
214
                        Coordinate c = new Coordinate(p.getX(), p.getY());
215
                        theGeoms[i] = c;
216
                }
217
                com.vividsolutions.jts.geom.MultiPoint geomCol = new com.vividsolutions.jts.geom.GeometryFactory()
218
                                .createMultiPoint(theGeoms);
219
                return geomCol;
220
        }
221

    
222
        /**
223
         * Convierte una MultiCurve2D en una MultiLineString de JTS
224
         * 
225
         * @param geom
226
         * @return
227
         */
228
        public static com.vividsolutions.jts.geom.Geometry geometryToJts(
229
                        MultiCurve geom) {
230
                LineString[] lines = new LineString[geom.getPrimitivesNumber()];
231
                for (int i = 0; i < lines.length; i++) {
232
                        lines[i] = (LineString) geometryToJts((geom.getPrimitiveAt(i)));
233
                }
234
                return new com.vividsolutions.jts.geom.GeometryFactory()
235
                                .createMultiLineString(lines);
236
        }
237

    
238
        /**
239
         * Convierte una MultiSurface2D en un MultiPolygon de JTS
240
         * 
241
         * @return
242
         */
243
        public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiSurface geom) {
244
                Polygon[] polygons = new Polygon[geom.getPrimitivesNumber()];
245
                for (int i = 0; i < polygons.length; i++) {
246
                        polygons[i] = (Polygon) geometryToJts((geom.getPrimitiveAt(i)));
247
                }
248
                return new com.vividsolutions.jts.geom.GeometryFactory()
249
                                .createMultiPolygon(polygons);
250
        }
251

    
252
        /**
253
         * Convierte una BaseMultiPrimitive en una GeometryCollection de JTS
254
         * 
255
         * @return
256
         */
257
        public com.vividsolutions.jts.geom.Geometry geometryToJts(
258
                        MultiPrimitive geom) {
259
                com.vividsolutions.jts.geom.Geometry[] geometriesAux = new LineString[geom
260
                                .getPrimitivesNumber()];
261
                for (int i = 0; i < geometriesAux.length; i++) {
262
                        geometriesAux[i] = geometryToJts((geom.getPrimitiveAt(i)));
263
                }
264
                return new com.vividsolutions.jts.geom.GeometryFactory()
265
                                .createGeometryCollection(geometriesAux);
266
        }
267

    
268
        public static com.vividsolutions.jts.geom.Geometry geometryToJtsWithSRID(
269
                        Geometry geom, int srid) {
270
                // logger.debug(geom.getClass());
271
                // logger.debug(new Integer(geom.getShapeType()));
272
                return geometryToJts(geom, geom.getType(), srid);
273
        }
274

    
275
        public static com.vividsolutions.jts.geom.Geometry geometryToJts(
276
                        Geometry geom) {
277
                // logger.debug(geom.getClass());
278
                // logger.debug(new Integer(geom.getShapeType()));
279
                return geometryToJts(geom, geom.getType(), -1);
280
        }
281

    
282
        // public static com.vividsolutions.jts.geom.Geometry java2d_to_jts(FShape
283
        // shp) {
284
        // return java2d_to_jts(shp, shp.getShapeType());
285
        // }
286

    
287
        private static boolean isClosed(Coordinate firstCoordinate,
288
                        Coordinate lastCoordinate) {
289
                double diff = Math.abs(lastCoordinate.x - firstCoordinate.x);
290
                if (diff > 0.000001) {
291
                        return false;
292
                }
293
                diff = Math.abs(lastCoordinate.y - firstCoordinate.y);
294
                if (diff > 0.000001) {
295
                        return false;
296
                }
297
                return true;
298
        }
299

    
300
        /**
301
         * Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
302
         * "flattened PathIterator". El flattened indica que las curvas las pasa a
303
         * segmentos de l?nea recta AUTOMATICAMENTE!!!.
304
         * 
305
         * @param shp
306
         *            FShape que se quiere convertir.
307
         * 
308
         * @return Geometry de JTS.
309
         */
310
        private static com.vividsolutions.jts.geom.Geometry geometryToJts(
311
                        Geometry shp, int shapeType, int srid) {
312

    
313
                com.vividsolutions.jts.geom.Geometry geoJTS = null;
314
                Coordinate coord;
315
                // Coordinate[] coords;
316
                ArrayList arrayCoords = null;
317
                ArrayList arrayLines;
318
                LineString lin;
319
                // LinearRing linRing;
320
                // LinearRing linRingExt = null;
321
                int theType;
322
                int numParts = 0;
323

    
324
                // Use this array to store segment coordinate data
325
                double[] theData = new double[6];
326
                PathIterator theIterator;
327

    
328
                // logger.debug(shp.toString());
329

    
330
                switch (shapeType) {
331
                case Geometry.TYPES.POINT:
332
                        org.gvsig.fmap.geom.primitive.impl.Point2D p = (org.gvsig.fmap.geom.primitive.impl.Point2D) shp;
333
                        coord = new Coordinate(p.getX(), p.getY());
334
                        geoJTS = geomFactory.createPoint(coord);
335
                        geoJTS.setSRID(srid);
336

    
337
                        break;
338

    
339
                case Geometry.TYPES.MULTIPOINT:
340
                        org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D mp = (org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D) shp;
341
                        int numPoints = mp.getPrimitivesNumber();
342
                        Coordinate[] coordinates = new Coordinate[numPoints];
343
                        for (int i = 0; i < numPoints; i++) {
344
                                p = mp.getPoint(i);
345
                                coordinates[i] = new Coordinate(p.getX(), p.getY());
346
                        }
347
                        geoJTS = geomFactory.createMultiPoint(coordinates);
348
                        geoJTS.setSRID(srid);
349

    
350
                        break;
351

    
352
                case Geometry.TYPES.CURVE:
353
                case Geometry.TYPES.ARC:
354
                case Geometry.TYPES.SPLINE:
355
                        arrayLines = new ArrayList();
356
                        theIterator = shp.getPathIterator(null, manager.getFlatness());
357

    
358
                        while (!theIterator.isDone()) {
359
                                // while not done
360
                                theType = theIterator.currentSegment(theData);
361

    
362
                                // Populate a segment of the new
363
                                // GeneralPathX object.
364
                                // Process the current segment to populate a new
365
                                // segment of the new GeneralPathX object.
366
                                switch (theType) {
367
                                case PathIterator.SEG_MOVETO:
368

    
369
                                        // System.out.println("SEG_MOVETO");
370
                                        if (arrayCoords == null) {
371
                                                arrayCoords = new ArrayList();
372
                                        } else {
373
                                                lin = geomFactory.createLineString(CoordinateArrays
374
                                                                .toCoordinateArray(arrayCoords));
375
                                                lin.setSRID(srid);
376
                                                arrayLines.add(lin);
377
                                                arrayCoords = new ArrayList();
378
                                        }
379

    
380
                                        numParts++;
381
                                        coord = new Coordinate(theData[0], theData[1]);
382

    
383
                                        arrayCoords.add(coord);
384

    
385
                                        break;
386

    
387
                                case PathIterator.SEG_LINETO:
388

    
389
                                        // System.out.println("SEG_LINETO");
390
                                        arrayCoords.add(new Coordinate(theData[0], theData[1]));
391

    
392
                                        break;
393

    
394
                                case PathIterator.SEG_QUADTO:
395
                                        System.out.println("Not supported here");
396

    
397
                                        break;
398

    
399
                                case PathIterator.SEG_CUBICTO:
400
                                        System.out.println("Not supported here");
401

    
402
                                        break;
403

    
404
                                case PathIterator.SEG_CLOSE:
405
                                        // A?adimos el primer punto para cerrar.
406
                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
407
                                        // Solo anyadimos cuando no esta ya cerrado
408
                                        arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y));
409

    
410
                                        break;
411
                                } // end switch
412

    
413
                                theIterator.next();
414
                        } // end while loop
415

    
416
                        if (arrayCoords.size() < 2) {
417
                                break;
418
                        }
419
                        lin = new com.vividsolutions.jts.geom.GeometryFactory()
420
                                        .createLineString(CoordinateArrays
421
                                                        .toCoordinateArray(arrayCoords));
422

    
423
                        lin.setSRID(srid);
424
                        // CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
425
                        // LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
426
                        // O CON GEOTOOLS
427
                        // if (numParts > 1) // Generamos una MultiLineString
428
                        // {
429
                        arrayLines.add(lin);
430
                        geoJTS = geomFactory
431
                                        .createMultiLineString(com.vividsolutions.jts.geom.GeometryFactory
432
                                                        .toLineStringArray(arrayLines));
433
                        geoJTS.setSRID(srid);
434
                        /*
435
                         * } else { geoJTS = lin; }
436
                         */
437

    
438
                        break;
439

    
440
                case Geometry.TYPES.SURFACE:
441
                case Geometry.TYPES.CIRCLE:
442
                case Geometry.TYPES.ELLIPSE:
443
                        arrayLines = new ArrayList();
444

    
445
                        ArrayList shells = new ArrayList();
446
                        ArrayList holes = new ArrayList();
447
                        Coordinate[] points = null;
448

    
449
                        theIterator = shp.getPathIterator(null, manager.getFlatness());
450

    
451
                        while (!theIterator.isDone()) {
452
                                // while not done
453
                                theType = theIterator.currentSegment(theData);
454

    
455
                                // Populate a segment of the new
456
                                // GeneralPathX object.
457
                                // Process the current segment to populate a new
458
                                // segment of the new GeneralPathX object.
459
                                switch (theType) {
460
                                case PathIterator.SEG_MOVETO:
461

    
462
                                        // System.out.println("SEG_MOVETO");
463
                                        if (arrayCoords == null) {
464
                                                arrayCoords = new ArrayList();
465
                                        } else {
466
                                                points = CoordinateArrays
467
                                                                .toCoordinateArray(arrayCoords);
468

    
469
                                                try {
470
                                                        LinearRing ring = geomFactory
471
                                                                        .createLinearRing(points);
472

    
473
                                                        if (CGAlgorithms.isCCW(points)) {
474
                                                                holes.add(ring);
475
                                                        } else {
476
                                                                shells.add(ring);
477
                                                        }
478
                                                } catch (Exception e) {
479
                                                        /*
480
                                                         * (jaume) caso cuando todos los puntos son iguales
481
                                                         * devuelvo el propio punto
482
                                                         */
483
                                                        boolean same = true;
484
                                                        for (int i = 0; i < points.length - 1 && same; i++) {
485
                                                                if (points[i].x != points[i + 1].x
486
                                                                                || points[i].y != points[i + 1].y /*
487
                                                                                                                                                 * ||
488
                                                                                                                                                 * points
489
                                                                                                                                                 * [i].z
490
                                                                                                                                                 * !=
491
                                                                                                                                                 * points
492
                                                                                                                                                 * [
493
                                                                                                                                                 * i+1].
494
                                                                                                                                                 * z
495
                                                                                                                                                 */
496
                                                                ) {
497
                                                                        same = false;
498
                                                                }
499
                                                        }
500
                                                        if (same) {
501
                                                                return geomFactory.createPoint(points[0]);
502
                                                        }
503
                                                        /*
504
                                                         * caso cuando es una l?nea de 3 puntos, no creo
505
                                                         * un LinearRing, sino una linea
506
                                                         */
507
                                                        if (points.length > 1 && points.length <= 3) {
508
                                                                // return geomFactory.createLineString(points);
509
                                                                return geomFactory
510
                                                                                .createMultiLineString(new LineString[] { geomFactory
511
                                                                                                .createLineString(points) });
512
                                                        }
513

    
514
                                                        System.err
515
                                                                        .println("Caught Topology exception in GMLLinearRingHandler");
516

    
517
                                                        return null;
518
                                                }
519

    
520
                                                /*
521
                                                 * if (numParts == 1) { linRingExt = new
522
                                                 * GeometryFactory().createLinearRing(
523
                                                 * CoordinateArrays.toCoordinateArray(arrayCoords)); }
524
                                                 * else { linRing = new
525
                                                 * GeometryFactory().createLinearRing(
526
                                                 * CoordinateArrays.toCoordinateArray(arrayCoords));
527
                                                 * arrayLines.add(linRing); }
528
                                                 */
529
                                                arrayCoords = new ArrayList();
530
                                        }
531

    
532
                                        numParts++;
533
                                        arrayCoords.add(new Coordinate(theData[0], theData[1]));
534

    
535
                                        break;
536

    
537
                                case PathIterator.SEG_LINETO:
538

    
539
                                        // System.out.println("SEG_LINETO");
540
                                        arrayCoords.add(new Coordinate(theData[0], theData[1]));
541

    
542
                                        break;
543

    
544
                                case PathIterator.SEG_QUADTO:
545
                                        System.out.println("SEG_QUADTO Not supported here");
546

    
547
                                        break;
548

    
549
                                case PathIterator.SEG_CUBICTO:
550
                                        System.out.println("SEG_CUBICTO Not supported here");
551

    
552
                                        break;
553

    
554
                                case PathIterator.SEG_CLOSE:
555

    
556
                                        // A?adimos el primer punto para cerrar.
557
                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
558
                                        arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y));
559

    
560
                                        break;
561
                                } // end switch
562

    
563
                                // System.out.println("theData[0] = " + theData[0] +
564
                                // " theData[1]=" + theData[1]);
565
                                theIterator.next();
566
                        } // end while loop
567

    
568
                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
569
                        Coordinate lastCoord = (Coordinate) arrayCoords.get(arrayCoords
570
                                        .size() - 1);
571
                        if (!isClosed(firstCoord, lastCoord)) {
572
                                arrayCoords.add(firstCoord);
573
                        }
574
                        points = CoordinateArrays.toCoordinateArray(arrayCoords);
575

    
576
                        try {
577
                                LinearRing ring = geomFactory.createLinearRing(points);
578

    
579
                                if (CGAlgorithms.isCCW(points)) {
580
                                        holes.add(ring);
581
                                } else {
582
                                        shells.add(ring);
583
                                }
584
                                ring.setSRID(srid);
585
                        } catch (Exception e) {
586
                                /*
587
                                 * (jaume) caso cuando todos los puntos son iguales devuelvo el
588
                                 * propio punto
589
                                 */
590
                                boolean same = true;
591
                                for (int i = 0; i < points.length - 1 && same; i++) {
592
                                        if (points[i].x != points[i + 1].x
593
                                                        || points[i].y != points[i + 1].y /*
594
                                                                                                                         * || points[i].z !=
595
                                                                                                                         * points[i+1].z
596
                                                                                                                         */
597
                                        ) {
598
                                                same = false;
599
                                        }
600
                                }
601
                                if (same) {
602
                                        geoJTS = geomFactory.createPoint(points[0]);
603
                                        geoJTS.setSRID(srid);
604
                                        return geoJTS;
605
                                }
606
                                /*
607
                                 * caso cuando es una l?nea de 3 puntos, no creo un
608
                                 * LinearRing, sino una linea
609
                                 */
610
                                if (points.length > 1 && points.length <= 3) {
611
                                        // return geomFactory.createLineString(points);
612
                                        geoJTS = geomFactory
613
                                                        .createMultiLineString(new LineString[] { geomFactory
614
                                                                        .createLineString(points) });
615
                                        geoJTS.setSRID(srid);
616
                                        return geoJTS;
617
                                }
618
                                System.err
619
                                                .println("Caught Topology exception in GMLLinearRingHandler");
620

    
621
                                return null;
622
                        }
623

    
624
                        /*
625
                         * linRing = new GeometryFactory().createLinearRing(
626
                         * CoordinateArrays.toCoordinateArray(arrayCoords));
627
                         */
628

    
629
                        // System.out.println("NumParts = " + numParts);
630
                        // now we have a list of all shells and all holes
631
                        ArrayList holesForShells = new ArrayList(shells.size());
632

    
633
                        for (int i = 0; i < shells.size(); i++) {
634
                                holesForShells.add(new ArrayList());
635
                        }
636

    
637
                        // find homes
638
                        for (int i = 0; i < holes.size(); i++) {
639
                                LinearRing testRing = (LinearRing) holes.get(i);
640
                                LinearRing minShell = null;
641
                                Envelope minEnv = null;
642
                                Envelope testEnv = testRing.getEnvelopeInternal();
643
                                Coordinate testPt = testRing.getCoordinateN(0);
644
                                LinearRing tryRing = null;
645

    
646
                                for (int j = 0; j < shells.size(); j++) {
647
                                        tryRing = (LinearRing) shells.get(j);
648

    
649
                                        Envelope tryEnv = tryRing.getEnvelopeInternal();
650

    
651
                                        if (minShell != null) {
652
                                                minEnv = minShell.getEnvelopeInternal();
653
                                        }
654

    
655
                                        boolean isContained = false;
656
                                        Coordinate[] coordList = tryRing.getCoordinates();
657

    
658
                                        if (tryEnv.contains(testEnv)
659
                                                        && (CGAlgorithms.isPointInRing(testPt, coordList) || (pointInList(
660
                                                                        testPt, coordList)))) {
661
                                                isContained = true;
662
                                        }
663

    
664
                                        // check if this new containing ring is smaller than the
665
                                        // current minimum ring
666
                                        if (isContained) {
667
                                                if ((minShell == null) || minEnv.contains(tryEnv)) {
668
                                                        minShell = tryRing;
669
                                                }
670
                                        }
671
                                }
672

    
673
                                if (minShell == null) {
674
                                        // System.out.println(
675
                                        // "polygon found with a hole thats not inside a shell");
676
                                        // azabala: we do the assumption that this hole is really a
677
                                        // shell (polygon)
678
                                        // whose point werent digitized in the right order
679
                                        Coordinate[] cs = testRing.getCoordinates();
680
                                        Coordinate[] reversed = new Coordinate[cs.length];
681
                                        int pointIndex = 0;
682
                                        for (int z = cs.length - 1; z >= 0; z--) {
683
                                                reversed[pointIndex] = cs[z];
684
                                                pointIndex++;
685
                                        }
686
                                        LinearRing newRing = geomFactory.createLinearRing(reversed);
687
                                        shells.add(newRing);
688
                                        holesForShells.add(new ArrayList());
689
                                } else {
690
                                        ((ArrayList) holesForShells.get(shells.indexOf(minShell)))
691
                                                        .add(testRing);
692
                                }
693
                        }
694

    
695
                        Polygon[] polygons = new Polygon[shells.size()];
696

    
697
                        for (int i = 0; i < shells.size(); i++) {
698
                                polygons[i] = geomFactory.createPolygon((LinearRing) shells
699
                                                .get(i), (LinearRing[]) ((ArrayList) holesForShells
700
                                                .get(i)).toArray(new LinearRing[0]));
701
                                polygons[i].setSRID(srid);
702
                        }
703
                        // CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
704
                        // LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
705
                        // O CON GEOTOOLS
706
                        // if (numParts > 1) // Generamos una MultiLineString
707

    
708
                        /*
709
                         * if (polygons.length == 1) { return polygons[0]; }
710
                         */
711

    
712
                        // FIN CAMBIO
713

    
714
                        holesForShells = null;
715
                        shells = null;
716
                        holes = null;
717

    
718
                        if (polygons.length == 1) {
719
                                geoJTS = polygons[0];
720
                        } else {
721
                                // its a multi part
722
                                geoJTS = geomFactory.createMultiPolygon(polygons);
723
                        }
724
                        geoJTS.setSRID(srid);
725

    
726
                        /*
727
                         * if (numParts > 1) // Generamos un Polygon con agujeros {
728
                         * arrayLines.add(linRing); // geoJTS = new
729
                         * GeometryFactory().createPolygon(linRingExt, //
730
                         * GeometryFactory.toLinearRingArray(arrayLines)); geoJTS = new
731
                         * GeometryFactory().buildGeometry(arrayLines);
732
                         * 
733
                         * // geoJTS = Polygonizer.class. } else { geoJTS = new
734
                         * GeometryFactory().createPolygon(linRing,null); }
735
                         */
736
                        break;
737
                }
738

    
739
                geoJTS.setSRID(srid);
740
                return geoJTS;
741
        }
742

    
743
        /**
744
         * Converts JTS Geometry objects into Java 2D Shape objects
745
         * 
746
         * @param geo
747
         *            Geometry de JTS.
748
         * 
749
         * @return FShape.
750
         */
751
        // public static FShape jts_to_java2d(com.vividsolutions.jts.geom.Geometry
752
        // geo) {
753
        // FShape shpNew = null;
754
        //
755
        // try {
756
        // if (geo instanceof Point) {
757
        // shpNew = new org.gvsig.fmap.geom.primitive.Point2D(null, null, ((Point)
758
        // geo).getX(), ((Point) geo).getY());
759
        // }
760
        //
761
        // if (geo.isEmpty()) {
762
        // shpNew = null;
763
        // }
764
        //
765
        // if (geo instanceof Polygon) {
766
        // shpNew = new Surface2D(null, null, toShape((Polygon) geo));
767
        // }
768
        //
769
        // if (geo instanceof MultiPolygon) {
770
        // shpNew = new Surface2D(null, null, toShape((MultiPolygon) geo));
771
        // }
772
        //
773
        // if (geo instanceof LineString) {
774
        // shpNew = new Curve2D(null, null, toShape((LineString) geo));
775
        // }
776
        //
777
        // if (geo instanceof MultiLineString) {
778
        // shpNew = new Curve2D(null, null, toShape((MultiLineString) geo));
779
        // }
780
        //
781
        // /* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN
782
        // GeometryCollection
783
        // * No sabremos si queremos una l?nea o un pol?gono.....
784
        // * if (geometry instanceof GeometryCollection) {
785
        // return toShape((GeometryCollection) geometry);
786
        // } */
787
        // return shpNew;
788
        // } catch (NoninvertibleTransformException e) {
789
        // // TODO Auto-generated catch block
790
        // e.printStackTrace();
791
        // }
792
        //
793
        // return null;
794
        // }
795

    
796
        /**
797
         * DOCUMENT ME!
798
         * 
799
         * @param p
800
         *            DOCUMENT ME!
801
         * 
802
         * @return DOCUMENT ME!
803
         */
804
        private static GeneralPathX toShape(Polygon p) {
805
                GeneralPathX resul = new GeneralPathX();
806
                Coordinate coord;
807

    
808
                for (int i = 0; i < p.getExteriorRing().getNumPoints(); i++) {
809
                        coord = p.getExteriorRing().getCoordinateN(i);
810

    
811
                        if (i == 0) {
812
                                resul.moveTo(coord.x, coord.y);
813
                        } else {
814
                                resul.lineTo(coord.x, coord.y);
815
                        }
816
                }
817

    
818
                for (int j = 0; j < p.getNumInteriorRing(); j++) {
819
                        LineString hole = p.getInteriorRingN(j);
820

    
821
                        for (int k = 0; k < hole.getNumPoints(); k++) {
822
                                coord = hole.getCoordinateN(k);
823

    
824
                                if (k == 0) {
825
                                        resul.moveTo(coord.x, coord.y);
826
                                } else {
827
                                        resul.lineTo(coord.x, coord.y);
828
                                }
829
                        }
830
                }
831

    
832
                return resul;
833
        }
834

    
835
        /**
836
         * DOCUMENT ME!
837
         * 
838
         * @param modelCoordinates
839
         *            DOCUMENT ME!
840
         * 
841
         * @return DOCUMENT ME!
842
         * 
843
         * @throws NoninvertibleTransformException
844
         *             DOCUMENT ME!
845
         * 
846
         *             private Coordinate[] toViewCoordinates(Coordinate[]
847
         *             modelCoordinates) throws NoninvertibleTransformException {
848
         *             Coordinate[] viewCoordinates = new
849
         *             Coordinate[modelCoordinates.length];
850
         * 
851
         *             for (int i = 0; i < modelCoordinates.length; i++) { FPoint2D
852
         *             point2D = coordinate2FPoint2D(modelCoordinates[i]);
853
         *             viewCoordinates[i] = new Coordinate(point2D.getX(),
854
         *             point2D.getY()); }
855
         * 
856
         *             return viewCoordinates; }
857
         * @throws CreateGeometryException
858
         */
859

    
860
        /*
861
         * private Shape toShape(GeometryCollection gc) throws
862
         * NoninvertibleTransformException { GeometryCollectionShape shape = new
863
         * GeometryCollectionShape(); for (int i = 0; i < gc.getNumGeometries();
864
         * i++) { Geometry g = (Geometry) gc.getGeometryN(i); shape.add(toShape(g));
865
         * } return shape; }
866
         */
867
        private static GeneralPathX toShape(MultiLineString mls)
868
                        throws NoninvertibleTransformException, CreateGeometryException {
869
                GeneralPathX path = new GeneralPathX();
870

    
871
                for (int i = 0; i < mls.getNumGeometries(); i++) {
872
                        LineString lineString = (LineString) mls.getGeometryN(i);
873
                        path.append(toShape(lineString).getPathIterator(null), false);
874
                }
875

    
876
                // BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
877
                // converted to GeneralPathXs. [Jon Aquino]
878
                return path;
879
        }
880

    
881
        /**
882
         * DOCUMENT ME!
883
         * 
884
         * @param lineString
885
         *            DOCUMENT ME!
886
         * 
887
         * @return DOCUMENT ME!
888
         * 
889
         * @throws NoninvertibleTransformException
890
         *             DOCUMENT ME!
891
         * @throws CreateGeometryException
892
         */
893
        private static GeneralPathX toShape(LineString lineString)
894
                        throws NoninvertibleTransformException, CreateGeometryException {
895
                GeneralPathX shape = new GeneralPathX();
896
                org.gvsig.fmap.geom.primitive.Point viewPoint = coordinate2FPoint2D(lineString
897
                                .getCoordinateN(0));
898
                shape.moveTo(viewPoint.getX(), viewPoint.getY());
899

    
900
                for (int i = 1; i < lineString.getNumPoints(); i++) {
901
                        viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(i));
902
                        shape.lineTo(viewPoint.getX(), viewPoint.getY());
903
                }
904

    
905
                // BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
906
                // converted to GeneralPathXs. [Jon Aquino]
907
                return shape;
908
        }
909

    
910
        /*
911
         * TODO No se usa DOCUMENT ME!
912
         * 
913
         * @param point DOCUMENT ME!
914
         * 
915
         * @return DOCUMENT ME!
916
         * 
917
         * @throws NoninvertibleTransformException DOCUMENT ME!
918
         * 
919
         * private static Point2D toShape(Point point) throws
920
         * NoninvertibleTransformException { Point2D viewPoint =
921
         * coordinate2FPoint2D(point.getCoordinate());
922
         * 
923
         * return viewPoint; }
924
         */
925

    
926
        /**
927
         *
928
         */
929
        private static GeneralPathX toShape(MultiPolygon mp)
930
                        throws NoninvertibleTransformException {
931
                GeneralPathX path = new GeneralPathX();
932

    
933
                for (int i = 0; i < mp.getNumGeometries(); i++) {
934
                        Polygon polygon = (Polygon) mp.getGeometryN(i);
935
                        path.append(toShape(polygon).getPathIterator(null), false);
936
                }
937

    
938
                // BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
939
                // converted to GeneralPathXs. [Jon Aquino]
940
                return path;
941
        }
942

    
943
        /**
944
         * DOCUMENT ME!
945
         * 
946
         * @param coord
947
         *            DOCUMENT ME!
948
         * 
949
         * @return DOCUMENT ME!
950
         * @throws CreateGeometryException
951
         */
952
        public static org.gvsig.fmap.geom.primitive.Point coordinate2FPoint2D(
953
                        Coordinate coord) throws CreateGeometryException {
954
                return geomManager.createPoint(coord.x, coord.y, SUBTYPES.GEOM2D); // ,coord.z);
955
        }
956

    
957
        /**
958
         * Convierte una Geometry de JTS a GeneralPathX.
959
         * 
960
         * @param geometry
961
         *            Geometry a convertir.
962
         * 
963
         * @return GeneralPathX.
964
         * 
965
         * @throws NoninvertibleTransformException
966
         * @throws CreateGeometryException
967
         * @throws IllegalArgumentException
968
         */
969
        public static GeneralPathX toShape(
970
                        com.vividsolutions.jts.geom.Geometry geometry)
971
                        throws NoninvertibleTransformException, CreateGeometryException {
972
                if (geometry.isEmpty()) {
973
                        return new GeneralPathX();
974
                }
975

    
976
                if (geometry instanceof Polygon) {
977
                        return toShape((Polygon) geometry);
978
                }
979

    
980
                if (geometry instanceof MultiPolygon) {
981
                        return toShape((MultiPolygon) geometry);
982
                }
983

    
984
                if (geometry instanceof LineString) {
985
                        return toShape((LineString) geometry);
986
                }
987

    
988
                if (geometry instanceof MultiLineString) {
989
                        return toShape((MultiLineString) geometry);
990
                }
991

    
992
                if (geometry instanceof GeometryCollection) {
993
                        return toShape(geometry);
994
                }
995

    
996
                throw new IllegalArgumentException("Unrecognized Geometry class: "
997
                                + geometry.getClass());
998
        }
999

    
1000
        public static GeneralPathX transformToInts(GeneralPathX gp,
1001
                        AffineTransform at) {
1002
                GeneralPathX newGp = new GeneralPathX();
1003
                PathIterator theIterator;
1004
                int theType;
1005
                int numParts = 0;
1006
                double[] theData = new double[6];
1007
                java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
1008
                java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
1009
                boolean bFirst = true;
1010
                int xInt, yInt, antX = -1, antY = -1;
1011

    
1012
                theIterator = gp.getPathIterator(null); // , flatness);
1013

    
1014
                while (!theIterator.isDone()) {
1015
                        theType = theIterator.currentSegment(theData);
1016
                        switch (theType) {
1017
                        case PathIterator.SEG_MOVETO:
1018
                                numParts++;
1019
                                ptSrc.setLocation(theData[0], theData[1]);
1020
                                at.transform(ptSrc, ptDst);
1021
                                antX = (int) ptDst.getX();
1022
                                antY = (int) ptDst.getY();
1023
                                newGp.moveTo(antX, antY);
1024
                                bFirst = true;
1025
                                break;
1026

    
1027
                        case PathIterator.SEG_LINETO:
1028
                                ptSrc.setLocation(theData[0], theData[1]);
1029
                                at.transform(ptSrc, ptDst);
1030
                                xInt = (int) ptDst.getX();
1031
                                yInt = (int) ptDst.getY();
1032
                                if ((bFirst) || ((xInt != antX) || (yInt != antY))) {
1033
                                        newGp.lineTo(xInt, yInt);
1034
                                        antX = xInt;
1035
                                        antY = yInt;
1036
                                        bFirst = false;
1037
                                }
1038
                                break;
1039

    
1040
                        case PathIterator.SEG_QUADTO:
1041
                                System.out.println("Not supported here");
1042

    
1043
                                break;
1044

    
1045
                        case PathIterator.SEG_CUBICTO:
1046
                                System.out.println("Not supported here");
1047

    
1048
                                break;
1049

    
1050
                        case PathIterator.SEG_CLOSE:
1051
                                newGp.closePath();
1052

    
1053
                                break;
1054
                        } // end switch
1055

    
1056
                        theIterator.next();
1057
                } // end while loop
1058

    
1059
                return newGp;
1060
        }
1061

    
1062
        public static Geometry transformToInts(Geometry gp, AffineTransform at)
1063
                        throws CreateGeometryException {
1064
                GeneralPathX newGp = new GeneralPathX();
1065
                double[] theData = new double[6];
1066
                double[] aux = new double[6];
1067

    
1068
                // newGp.reset();
1069
                PathIterator theIterator;
1070
                int theType;
1071
                int numParts = 0;
1072

    
1073
                java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
1074
                java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
1075
                boolean bFirst = true;
1076
                int xInt, yInt, antX = -1, antY = -1;
1077

    
1078
                theIterator = gp.getPathIterator(null); // , flatness);
1079
                int numSegmentsAdded = 0;
1080
                while (!theIterator.isDone()) {
1081
                        theType = theIterator.currentSegment(theData);
1082

    
1083
                        switch (theType) {
1084
                        case PathIterator.SEG_MOVETO:
1085
                                numParts++;
1086
                                ptSrc.setLocation(theData[0], theData[1]);
1087
                                at.transform(ptSrc, ptDst);
1088
                                antX = (int) ptDst.getX();
1089
                                antY = (int) ptDst.getY();
1090
                                newGp.moveTo(antX, antY);
1091
                                numSegmentsAdded++;
1092
                                bFirst = true;
1093
                                break;
1094

    
1095
                        case PathIterator.SEG_LINETO:
1096
                                ptSrc.setLocation(theData[0], theData[1]);
1097
                                at.transform(ptSrc, ptDst);
1098
                                xInt = (int) ptDst.getX();
1099
                                yInt = (int) ptDst.getY();
1100
                                if ((bFirst) || ((xInt != antX) || (yInt != antY))) {
1101
                                        newGp.lineTo(xInt, yInt);
1102
                                        antX = xInt;
1103
                                        antY = yInt;
1104
                                        bFirst = false;
1105
                                        numSegmentsAdded++;
1106
                                }
1107
                                break;
1108

    
1109
                        case PathIterator.SEG_QUADTO:
1110
                                at.transform(theData, 0, aux, 0, 2);
1111
                                newGp.quadTo(aux[0], aux[1], aux[2], aux[3]);
1112
                                numSegmentsAdded++;
1113
                                break;
1114

    
1115
                        case PathIterator.SEG_CUBICTO:
1116
                                at.transform(theData, 0, aux, 0, 3);
1117
                                newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]);
1118
                                numSegmentsAdded++;
1119
                                break;
1120

    
1121
                        case PathIterator.SEG_CLOSE:
1122
                                if (numSegmentsAdded < 3) {
1123
                                        newGp.lineTo(antX, antY);
1124
                                }
1125
                                newGp.closePath();
1126

    
1127
                                break;
1128
                        } // end switch
1129

    
1130
                        theIterator.next();
1131
                } // end while loop
1132

    
1133
                Geometry shp = null;
1134
                switch (gp.getType()) {
1135
                case Geometry.TYPES.POINT:
1136
                        shp = geomManager.createPoint(ptDst.getX(), ptDst.getY(),
1137
                                        SUBTYPES.GEOM2D);
1138
                        break;
1139

    
1140
                case Geometry.TYPES.CURVE:
1141
                case Geometry.TYPES.ARC:
1142
                        try {
1143
                                shp = geomManager.createCurve(newGp, SUBTYPES.GEOM2D);
1144
                        } catch (CreateGeometryException e1) {
1145
                                logger.error("Error creating a curve", e1);
1146
                        }
1147
                        break;
1148

    
1149
                case Geometry.TYPES.SURFACE:
1150
                case Geometry.TYPES.CIRCLE:
1151
                case Geometry.TYPES.ELLIPSE:
1152

    
1153
                        try {
1154
                                shp = geomManager.createSurface(newGp, SUBTYPES.GEOM2D);
1155
                        } catch (CreateGeometryException e) {
1156
                                logger.error("Error creating a surface", e);
1157
                        }
1158
                        break;
1159
                }
1160
                return shp;
1161
        }
1162

    
1163
        public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR) {
1164
                Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(),
1165
                                jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight());
1166
                return r;
1167
        }
1168

    
1169
        public static Envelope convertEnvelopeToJTS(
1170
                        org.gvsig.fmap.geom.primitive.Envelope r) {
1171
                Envelope e = new Envelope(r.getMinimum(0), r.getMaximum(0),
1172
                                r.getMinimum(1), r.getMaximum(1));
1173
                return e;
1174
        }
1175

    
1176
        /**
1177
         * Return a correct polygon (no hole)
1178
         * 
1179
         * @param coordinates
1180
         * @return
1181
         */
1182
        public static Geometry getExteriorPolygon(Coordinate[] coordinates) {
1183
                // isCCW = true => it's a hole
1184
                Coordinate[] vs = new Coordinate[coordinates.length];
1185
                if (CGAlgorithms.isCCW(coordinates)) {
1186
                        for (int i = vs.length - 1; i >= 0; i--) {
1187
                                vs[i] = coordinates[i];
1188
                        }
1189
                } else {
1190
                        vs = coordinates;
1191
                }
1192
                LinearRing ring = geomFactory.createLinearRing(vs);
1193

    
1194
                try {
1195
                        Surface surface = (Surface) manager.create(TYPES.SURFACE,
1196
                                        SUBTYPES.GEOM2D);
1197
                        surface.setGeneralPath(toShape(ring));
1198
                        return surface;
1199
                } catch (NoninvertibleTransformException e) {
1200
                        e.printStackTrace();
1201
                } catch (CreateGeometryException e) {
1202
                        e.printStackTrace();
1203
                }
1204
                return null;
1205
        }
1206

    
1207
        public static boolean isCCW(Point[] points) {
1208
                int length = points.length;
1209
                Coordinate[] vs;
1210
                // CGAlgorithms.isCCW asume que la lista de puntos tienen el primer
1211
                // y el ultimo puntos iguales.... y que este algoritmo est? solo
1212
                // garantizado con anillos v?lidos.
1213
                if (points[0].getX() != points[length - 1].getX()
1214
                                || points[0].getY() != points[length - 1].getY()) {
1215
                        vs = new Coordinate[length + 1];
1216
                        vs[points.length] = new Coordinate(points[0].getX(),
1217
                                        points[0].getY());
1218
                } else {
1219
                        vs = new Coordinate[length];
1220
                }
1221
                for (int i = 0; i < length; i++) {
1222
                        vs[i] = new Coordinate(points[i].getX(), points[i].getY());
1223
                }
1224

    
1225
                return CGAlgorithms.isCCW(vs);
1226
        }
1227

    
1228
        public static boolean isCCW(Surface pol) {
1229
                com.vividsolutions.jts.geom.Geometry jtsGeom = Converter
1230
                                .geometryToJts(pol);
1231
                if (jtsGeom.getNumGeometries() == 1) {
1232
                        Coordinate[] coords = jtsGeom.getCoordinates();
1233
                        return CGAlgorithms.isCCW(coords);
1234
                }
1235
                return false;
1236

    
1237
        }
1238

    
1239
        /**
1240
         * Return a hole (CCW ordered points)
1241
         * 
1242
         * @param coordinates
1243
         * @return
1244
         */
1245
        public static Geometry getHole(Coordinate[] coordinates) {
1246
                // isCCW = true => it's a hole
1247
                Coordinate[] vs = new Coordinate[coordinates.length];
1248
                if (CGAlgorithms.isCCW(coordinates)) {
1249
                        vs = coordinates;
1250

    
1251
                } else {
1252
                        for (int i = vs.length - 1; i >= 0; i--) {
1253
                                vs[i] = coordinates[i];
1254
                        }
1255
                }
1256
                LinearRing ring = geomFactory.createLinearRing(vs);
1257

    
1258
                try {
1259
                        Surface surface = (Surface) manager.create(TYPES.SURFACE,
1260
                                        SUBTYPES.GEOM2D);
1261
                        surface.setGeneralPath(toShape(ring));
1262
                        return surface;
1263
                } catch (NoninvertibleTransformException e) {
1264
                        e.printStackTrace();
1265
                } catch (CreateGeometryException e) {
1266
                        e.printStackTrace();
1267
                }
1268
                return null;
1269
        }
1270

    
1271
        public static Shape getExteriorPolygon(GeneralPathX gp) {
1272
                Area area = new Area(gp);
1273
                area.isSingular();
1274
                return area;
1275

    
1276
        }
1277

    
1278
        /**
1279
         * Use it ONLY for NOT multipart polygons.
1280
         * 
1281
         * @param pol
1282
         * @return
1283
         */
1284
        public static Geometry getNotHolePolygon(Surface pol) {
1285
                // isCCW == true => hole
1286
                Coordinate[] coords;
1287
                ArrayList arrayCoords = null;
1288
                int theType;
1289
                int numParts = 0;
1290

    
1291
                // Use this array to store segment coordinate data
1292
                double[] theData = new double[6];
1293
                PathIterator theIterator;
1294

    
1295
                ArrayList shells = new ArrayList();
1296
                ArrayList holes = new ArrayList();
1297
                Coordinate[] points = null;
1298

    
1299
                theIterator = pol.getPathIterator(null, manager.getFlatness());
1300

    
1301
                while (!theIterator.isDone()) {
1302
                        // while not done
1303
                        theType = theIterator.currentSegment(theData);
1304

    
1305
                        // Populate a segment of the new
1306
                        // GeneralPathX object.
1307
                        // Process the current segment to populate a new
1308
                        // segment of the new GeneralPathX object.
1309
                        switch (theType) {
1310
                        case PathIterator.SEG_MOVETO:
1311

    
1312
                                // System.out.println("SEG_MOVETO");
1313
                                if (arrayCoords == null) {
1314
                                        arrayCoords = new ArrayList();
1315
                                } else {
1316
                                        points = CoordinateArrays.toCoordinateArray(arrayCoords);
1317

    
1318
                                        try {
1319
                                                LinearRing ring = geomFactory.createLinearRing(points);
1320

    
1321
                                                if (CGAlgorithms.isCCW(points)) {
1322
                                                        holes.add(ring);
1323
                                                } else {
1324
                                                        shells.add(ring);
1325
                                                }
1326
                                        } catch (Exception e) {
1327
                                                System.err
1328
                                                                .println("Caught Topology exception in GMLLinearRingHandler");
1329

    
1330
                                                return null;
1331
                                        }
1332

    
1333
                                        /*
1334
                                         * if (numParts == 1) { linRingExt = new
1335
                                         * GeometryFactory().createLinearRing(
1336
                                         * CoordinateArrays.toCoordinateArray(arrayCoords)); } else
1337
                                         * { linRing = new GeometryFactory().createLinearRing(
1338
                                         * CoordinateArrays.toCoordinateArray(arrayCoords));
1339
                                         * arrayLines.add(linRing); }
1340
                                         */
1341
                                        arrayCoords = new ArrayList();
1342
                                }
1343

    
1344
                                numParts++;
1345
                                arrayCoords.add(new Coordinate(theData[0], theData[1]));
1346

    
1347
                                break;
1348

    
1349
                        case PathIterator.SEG_LINETO:
1350

    
1351
                                // System.out.println("SEG_LINETO");
1352
                                arrayCoords.add(new Coordinate(theData[0], theData[1]));
1353

    
1354
                                break;
1355

    
1356
                        case PathIterator.SEG_QUADTO:
1357
                                System.out.println("SEG_QUADTO Not supported here");
1358

    
1359
                                break;
1360

    
1361
                        case PathIterator.SEG_CUBICTO:
1362
                                System.out.println("SEG_CUBICTO Not supported here");
1363

    
1364
                                break;
1365

    
1366
                        case PathIterator.SEG_CLOSE:
1367

    
1368
                                // A?adimos el primer punto para cerrar.
1369
                                Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
1370
                                arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y));
1371

    
1372
                                break;
1373
                        } // end switch
1374

    
1375
                        // System.out.println("theData[0] = " + theData[0] + " theData[1]="
1376
                        // + theData[1]);
1377
                        theIterator.next();
1378
                } // end while loop
1379

    
1380
                arrayCoords.add(arrayCoords.get(0));
1381
                coords = CoordinateArrays.toCoordinateArray(arrayCoords);
1382

    
1383
                if (numParts == 1) {
1384
                        return getExteriorPolygon(coords);
1385
                }
1386
                return pol;
1387

    
1388
        }
1389

    
1390
        /*
1391
         * public static GeometryCollection
1392
         * convertFGeometryCollection(FGeometryCollection fGeomC) {
1393
         * 
1394
         * geomFactory.createGeometryCollection(theGeoms); }
1395
         */
1396
}