Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / util / Converter.java @ 33111

History | View | Annotate | Download (38.1 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 org.gvsig.fmap.geom.Geometry;
53
import org.gvsig.fmap.geom.GeometryLocator;
54
import org.gvsig.fmap.geom.GeometryManager;
55
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
56
import org.gvsig.fmap.geom.Geometry.TYPES;
57
import org.gvsig.fmap.geom.aggregate.MultiCurve;
58
import org.gvsig.fmap.geom.aggregate.MultiPoint;
59
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
60
import org.gvsig.fmap.geom.aggregate.MultiSurface;
61
import org.gvsig.fmap.geom.exception.CreateGeometryException;
62
import org.gvsig.fmap.geom.primitive.GeneralPathX;
63
import org.gvsig.fmap.geom.primitive.Surface;
64
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
66

    
67
import com.vividsolutions.jts.algorithm.CGAlgorithms;
68
import com.vividsolutions.jts.algorithm.RobustCGAlgorithms;
69
import com.vividsolutions.jts.geom.Coordinate;
70
import com.vividsolutions.jts.geom.CoordinateArrays;
71
import com.vividsolutions.jts.geom.Envelope;
72
import com.vividsolutions.jts.geom.GeometryCollection;
73
import com.vividsolutions.jts.geom.LineString;
74
import com.vividsolutions.jts.geom.LinearRing;
75
import com.vividsolutions.jts.geom.MultiLineString;
76
import com.vividsolutions.jts.geom.MultiPolygon;
77
import com.vividsolutions.jts.geom.Point;
78
import com.vividsolutions.jts.geom.Polygon;
79

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

    
91
        //private static Logger logger = Logger.getLogger(Converter.class);
92

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

    
102
        private static GeometryManager manager = GeometryLocator.getGeometryManager();
103

    
104
        //returns true if testPoint is a point in the pointList list.
105
        static boolean pointInList(Coordinate testPoint, Coordinate[] pointList) {
106
                int t;
107
                int numpoints;
108
                Coordinate p;
109

    
110
                numpoints = Array.getLength(pointList);
111

    
112
                for (t = 0; t < numpoints; t++) {
113
                        p = pointList[t];
114

    
115
                        if ((testPoint.x == p.x) && (testPoint.y == p.y) &&
116
                                        ((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) //nan test; x!=x iff x is nan
117
                        ) {
118
                                return true;
119
                        }
120
                }
121

    
122
                return false;
123
        }
124

    
125
        /**
126
         * Receives a JTS Geometry and returns a fmap IGeometry
127
         * @param jtsGeometry jts Geometry
128
         * @return IGeometry of FMap
129
         * @author azabala
130
         * @throws CreateGeometryException 
131
         */
132
        public static Geometry jtsToGeometry(com.vividsolutions.jts.geom.Geometry geo) throws CreateGeometryException{
133
                Geometry shpNew = null;
134

    
135
                try {
136
                        if (geo instanceof Point) {
137
                                shpNew = geomManager.createPoint(((Point) geo).getX(),((Point) geo).getY(), SUBTYPES.GEOM2D);
138
                        }
139

    
140
                        if (geo.isEmpty()) {
141
                                shpNew = null;
142
                        }
143

    
144
                        try{
145
                        if (geo instanceof Polygon) {
146
                                shpNew = geomManager.createSurface(toShape((Polygon) geo), SUBTYPES.GEOM2D);
147
                        }
148

    
149
                        if (geo instanceof MultiPolygon) {
150
                                shpNew = geomManager.createSurface(toShape((MultiPolygon) geo), SUBTYPES.GEOM2D);
151
                        }
152

    
153
                        if (geo instanceof LineString) {
154
                                shpNew = geomManager.createCurve(toShape((LineString) geo), SUBTYPES.GEOM2D);
155
                        }
156

    
157
                        if (geo instanceof MultiLineString) {
158
                                shpNew = geomManager.createCurve(toShape((MultiLineString) geo), SUBTYPES.GEOM2D);
159
                        }
160
                        }catch(CreateGeometryException e){
161
                                logger.error("Error creating a geometry", e);
162
                        }
163

    
164
                        /* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN GeometryCollection
165
                         * No sabremos si queremos una l?nea o un pol?gono.....
166
                         *  if (geometry instanceof GeometryCollection) {
167
                                  return toShape((GeometryCollection) geometry);
168
                           } */
169
                        return shpNew;
170
                } catch (NoninvertibleTransformException e) {
171
                        // TODO Auto-generated catch block
172
                        e.printStackTrace();
173
                }
174

    
175
                return null;
176

    
177
//
178
//                FShape shape = Converter.jts_to_java2d(jtsGeometry);
179
//                return factory.createGeometry(shape);
180
        }
181

    
182
        /**
183
         * Convierte un MultiPoint2D a un MultiPoint de JTS
184
         * @param geom
185
         * @return
186
         */
187
        public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPoint geom) {
188
                Coordinate[] theGeoms = new Coordinate[geom.getPrimitivesNumber()];
189
                for (int i = 0; i < theGeoms.length; i++) {
190
                        java.awt.geom.Point2D p = geom.getPrimitiveAt(i)
191
                                        .getHandlers(Geometry.SELECTHANDLER)[0].getPoint();
192
                        Coordinate c = new Coordinate(p.getX(), p.getY());
193
                        theGeoms[i] = c;
194
                }
195
                com.vividsolutions.jts.geom.MultiPoint geomCol = new com.vividsolutions.jts.geom.GeometryFactory()
196
                                .createMultiPoint(theGeoms);
197
                return geomCol;
198
        }
199

    
200
        /**
201
         * Convierte una MultiCurve2D en una MultiLineString de JTS
202
         * @param geom
203
         * @return
204
         */
205
        public static com.vividsolutions.jts.geom.Geometry geometryToJts(MultiCurve geom) {
206
                LineString[] lines = new LineString[geom.getPrimitivesNumber()];
207
        for (int i = 0; i < lines.length; i++){
208
                lines[i] = (LineString) geometryToJts((geom.getPrimitiveAt(i)));
209
        }
210
        return new com.vividsolutions.jts.geom.GeometryFactory().createMultiLineString(lines);
211
        }
212

    
213
        /**
214
         * Convierte una MultiSurface2D en un MultiPolygon de JTS
215
         * @return
216
         */
217
        public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiSurface geom) {
218
                Polygon[] polygons = new Polygon[geom.getPrimitivesNumber()];
219
        for (int i = 0; i < polygons.length; i++){
220
                polygons[i] = (Polygon) geometryToJts((geom.getPrimitiveAt(i)));
221
        }
222
        return new com.vividsolutions.jts.geom.GeometryFactory().createMultiPolygon(polygons);
223
        }
224

    
225
        /**
226
         * Convierte una BaseMultiPrimitive en una GeometryCollection de JTS
227
         * @return
228
         */
229
        public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPrimitive geom) {
230
                com.vividsolutions.jts.geom.Geometry[] geometriesAux = new LineString[geom.getPrimitivesNumber()];
231
                for (int i = 0; i < geometriesAux.length; i++) {
232
                        geometriesAux[i] = geometryToJts((geom.getPrimitiveAt(i)));
233
                }
234
                return new com.vividsolutions.jts.geom.GeometryFactory().createGeometryCollection(geometriesAux);
235
        }
236

    
237
        public static com.vividsolutions.jts.geom.Geometry geometryToJtsWithSRID(
238
                        Geometry geom, int srid) {
239
                // logger.debug(geom.getClass());
240
                // logger.debug(new Integer(geom.getShapeType()));
241
                return geometryToJts(geom, geom.getType(), srid);
242
        }
243

    
244

    
245
        public static com.vividsolutions.jts.geom.Geometry geometryToJts(Geometry geom) {
246
                //logger.debug(geom.getClass());
247
                //logger.debug(new Integer(geom.getShapeType()));
248
                return geometryToJts(geom, geom.getType(), -1);
249
        }
250

    
251
//        public static com.vividsolutions.jts.geom.Geometry java2d_to_jts(FShape shp) {
252
//                return java2d_to_jts(shp, shp.getShapeType());
253
//        }
254

    
255
        private static boolean isClosed(Coordinate firstCoordinate, Coordinate lastCoordinate){
256
                double diff = Math.abs(lastCoordinate.x - firstCoordinate.x);
257
                if (diff > 0.000001){
258
                        return false;
259
                }
260
                diff = Math.abs(lastCoordinate.y - firstCoordinate.y);
261
                if (diff > 0.000001) {
262
                        return false;
263
                }
264
                return true;
265
        }
266

    
267
        /**
268
         * Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
269
         * "flattened PathIterator". El flattened indica que las curvas las pasa a
270
         * segmentos de l?nea recta AUTOMATICAMENTE!!!.
271
         *
272
         * @param shp FShape que se quiere convertir.
273
         *
274
         * @return Geometry de JTS.
275
         */
276
        private static com.vividsolutions.jts.geom.Geometry geometryToJts(
277
                        Geometry shp, int shapeType, int srid) {
278

    
279

    
280
                com.vividsolutions.jts.geom.Geometry geoJTS = null;
281
                Coordinate coord;
282
                //Coordinate[] coords;
283
                ArrayList arrayCoords = null;
284
                ArrayList arrayLines;
285
                LineString lin;
286
                //LinearRing linRing;
287
                //LinearRing linRingExt = null;
288
                int theType;
289
                int numParts = 0;
290

    
291
                //                 Use this array to store segment coordinate data
292
                double[] theData = new double[6];
293
                PathIterator theIterator;
294

    
295
                //logger.debug(shp.toString());
296

    
297

    
298
                switch (shapeType) {
299
                case Geometry.TYPES.POINT:
300
                        org.gvsig.fmap.geom.primitive.impl.Point2D p = (org.gvsig.fmap.geom.primitive.impl.Point2D) shp;
301
                        coord = new Coordinate(p.getX(), p.getY());
302
                        geoJTS = geomFactory.createPoint(coord);
303
                        geoJTS.setSRID(srid);
304

    
305
                        break;
306

    
307
                case Geometry.TYPES.CURVE:
308
                case Geometry.TYPES.ARC:
309
                        arrayLines = new ArrayList();
310
                        theIterator = shp.getPathIterator(null, manager.getFlatness());
311

    
312
                        while (!theIterator.isDone()) {
313
                                //while not done
314
                                theType = theIterator.currentSegment(theData);
315

    
316
                                //Populate a segment of the new
317
                                // GeneralPathX object.
318
                                //Process the current segment to populate a new
319
                                // segment of the new GeneralPathX object.
320
                                switch (theType) {
321
                                case PathIterator.SEG_MOVETO:
322

    
323
                                        // System.out.println("SEG_MOVETO");
324
                                        if (arrayCoords == null) {
325
                                                arrayCoords = new ArrayList();
326
                                        } else {
327
                                                lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray(
328
                                                                arrayCoords));
329
                                                lin.setSRID(srid);
330
                                                arrayLines.add(lin);
331
                                                arrayCoords = new ArrayList();
332
                                        }
333

    
334
                                        numParts++;
335
                                        coord = new Coordinate(theData[0], theData[1]);
336

    
337
                                        arrayCoords.add(coord);
338

    
339
                                        break;
340

    
341
                                case PathIterator.SEG_LINETO:
342

    
343
                                        // System.out.println("SEG_LINETO");
344
                                        arrayCoords.add(new Coordinate(theData[0],
345
                                                        theData[1]));
346

    
347
                                        break;
348

    
349
                                case PathIterator.SEG_QUADTO:
350
                                        System.out.println("Not supported here");
351

    
352
                                        break;
353

    
354
                                case PathIterator.SEG_CUBICTO:
355
                                        System.out.println("Not supported here");
356

    
357
                                        break;
358

    
359
                                case PathIterator.SEG_CLOSE:
360
                                        // A?adimos el primer punto para cerrar.
361
                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
362
                                                // Solo anyadimos cuando no esta ya cerrado
363
                                        arrayCoords.add(new Coordinate(firstCoord.x,
364
                                                        firstCoord.y));
365

    
366
                                        break;
367
                                } //end switch
368

    
369
                                theIterator.next();
370
                        } //end while loop
371

    
372
                        if (arrayCoords.size()<2) {
373
                                break;
374
                        }
375
                        lin = new com.vividsolutions.jts.geom.GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
376
                                        arrayCoords));
377

    
378
                        lin.setSRID(srid);
379
                        // CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
380
                        // LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
381
                        // O CON GEOTOOLS
382
                        // if (numParts > 1) // Generamos una MultiLineString
383
                        //  {
384
                        arrayLines.add(lin);
385
                        geoJTS = geomFactory.createMultiLineString(com.vividsolutions.jts.geom.GeometryFactory.toLineStringArray(
386
                                        arrayLines));
387
                        geoJTS.setSRID(srid);
388
                        /* } else {
389
                         geoJTS = lin;
390
                         } */
391

    
392
                        break;
393

    
394
                case Geometry.TYPES.SURFACE:
395
                case Geometry.TYPES.CIRCLE:
396
                case Geometry.TYPES.ELLIPSE:
397
                        arrayLines = new ArrayList();
398

    
399
                        ArrayList shells = new ArrayList();
400
                        ArrayList holes = new ArrayList();
401
                        Coordinate[] points = null;
402

    
403
                        theIterator = shp.getPathIterator(null, manager.getFlatness());
404

    
405
                        while (!theIterator.isDone()) {
406
                                //while not done
407
                                theType = theIterator.currentSegment(theData);
408

    
409
                                //Populate a segment of the new
410
                                // GeneralPathX object.
411
                                //Process the current segment to populate a new
412
                                // segment of the new GeneralPathX object.
413
                                switch (theType) {
414
                                case PathIterator.SEG_MOVETO:
415

    
416
                                        // System.out.println("SEG_MOVETO");
417
                                        if (arrayCoords == null) {
418
                                                arrayCoords = new ArrayList();
419
                                        } else {
420
                                                points = CoordinateArrays.toCoordinateArray(arrayCoords);
421

    
422
                                                try {
423
                                                        LinearRing ring = geomFactory.createLinearRing(points);
424

    
425
                                                        if (CGAlgorithms.isCCW(points)) {
426
                                                                holes.add(ring);
427
                                                        } else {
428
                                                                shells.add(ring);
429
                                                        }
430
                                                } catch (Exception e) {
431
                                                        /* (jaume) caso cuando todos los puntos son iguales
432
                                                         * devuelvo el propio punto
433
                                                         */
434
                                                        boolean same = true;
435
                                                        for (int i = 0; i < points.length-1 && same; i++) {
436
                                                                if (points[i].x != points[i+1].x ||
437
                                                                                points[i].y != points[i+1].y /*||
438
                                                                                points[i].z != points[i+1].z*/
439
                                                                ) {
440
                                                                        same = false;
441
                                                                }
442
                                                        }
443
                                                        if (same) {
444
                                                                return geomFactory.createPoint(points[0]);
445
                                                        }
446
                                                        /*
447
                                                         * caso cuando es una l?nea de 3 puntos, no creo un LinearRing, sino
448
                                                         * una linea
449
                                                         */
450
                                                        if (points.length>1 && points.length<=3) {
451
                                                                // return geomFactory.createLineString(points);
452
                                                                return geomFactory.createMultiLineString(new LineString[] {geomFactory.createLineString(points)});
453
                                                        }
454

    
455
                                                        System.err.println(
456
                                                        "Caught Topology exception in GMLLinearRingHandler");
457

    
458
                                                        return null;
459
                                                }
460

    
461
                                                /* if (numParts == 1)
462
                                                 {
463
                                                 linRingExt = new GeometryFactory().createLinearRing(
464
                                                 CoordinateArrays.toCoordinateArray(arrayCoords));
465
                                                 }
466
                                                 else
467
                                                 {
468
                                                 linRing = new GeometryFactory().createLinearRing(
469
                                                 CoordinateArrays.toCoordinateArray(arrayCoords));
470
                                                 arrayLines.add(linRing);
471
                                                 } */
472
                                                arrayCoords = new ArrayList();
473
                                        }
474

    
475
                                        numParts++;
476
                                        arrayCoords.add(new Coordinate(theData[0],
477
                                                        theData[1]));
478

    
479
                                        break;
480

    
481
                                case PathIterator.SEG_LINETO:
482

    
483
                                        // System.out.println("SEG_LINETO");
484
                                        arrayCoords.add(new Coordinate(theData[0],
485
                                                        theData[1]));
486

    
487
                                        break;
488

    
489
                                case PathIterator.SEG_QUADTO:
490
                                        System.out.println("SEG_QUADTO Not supported here");
491

    
492
                                        break;
493

    
494
                                case PathIterator.SEG_CUBICTO:
495
                                        System.out.println("SEG_CUBICTO Not supported here");
496

    
497
                                        break;
498

    
499
                                case PathIterator.SEG_CLOSE:
500

    
501
                                        // A?adimos el primer punto para cerrar.
502
                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
503
                                        arrayCoords.add(new Coordinate(firstCoord.x,
504
                                                        firstCoord.y));
505

    
506
                                        break;
507
                                } //end switch
508

    
509
                                // System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
510
                                theIterator.next();
511
                        } //end while loop
512

    
513

    
514
                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
515
                        Coordinate lastCoord = (Coordinate) arrayCoords.get(arrayCoords
516
                                        .size() - 1);
517
                        if (!isClosed(firstCoord, lastCoord)) {
518
                                arrayCoords.add(firstCoord);
519
                        }
520
                        points = CoordinateArrays.toCoordinateArray(arrayCoords);
521

    
522
                        try {
523
                                LinearRing ring = geomFactory.createLinearRing(points);
524

    
525
                                if (CGAlgorithms.isCCW(points)) {
526
                                        holes.add(ring);
527
                                } else {
528
                                        shells.add(ring);
529
                                }
530
                                ring.setSRID(srid);
531
                        } catch (Exception e) {
532
                                /* (jaume) caso cuando todos los puntos son iguales
533
                                 * devuelvo el propio punto
534
                                 */
535
                                boolean same = true;
536
                                for (int i = 0; i < points.length-1 && same; i++) {
537
                                        if (points[i].x != points[i+1].x ||
538
                                                        points[i].y != points[i+1].y /*||
539
                                                        points[i].z != points[i+1].z*/
540
                                        ) {
541
                                                same = false;
542
                                        }
543
                                }
544
                                if (same) {
545
                                        geoJTS = geomFactory.createPoint(points[0]);
546
                                        geoJTS.setSRID(srid);
547
                                        return geoJTS;
548
                                }
549
                                /*
550
                                 * caso cuando es una l?nea de 3 puntos, no creo un LinearRing, sino
551
                                 * una linea
552
                                 */
553
                                if (points.length>1 && points.length<=3) {
554
                                        // return geomFactory.createLineString(points);
555
                                        geoJTS = geomFactory
556
                                                        .createMultiLineString(new LineString[] { geomFactory
557
                                                                        .createLineString(points) });
558
                                        geoJTS.setSRID(srid);
559
                                        return geoJTS;
560
                                }
561
                                System.err.println(
562
                                "Caught Topology exception in GMLLinearRingHandler");
563

    
564
                                return null;
565
                        }
566

    
567
                        /* linRing = new GeometryFactory().createLinearRing(
568
                         CoordinateArrays.toCoordinateArray(arrayCoords)); */
569

    
570
                        // System.out.println("NumParts = " + numParts);
571
                        //now we have a list of all shells and all holes
572
                        ArrayList holesForShells = new ArrayList(shells.size());
573

    
574
                        for (int i = 0; i < shells.size(); i++) {
575
                                holesForShells.add(new ArrayList());
576
                        }
577

    
578
                        //find homes
579
                        for (int i = 0; i < holes.size(); i++) {
580
                                LinearRing testRing = (LinearRing) holes.get(i);
581
                                LinearRing minShell = null;
582
                                Envelope minEnv = null;
583
                                Envelope testEnv = testRing.getEnvelopeInternal();
584
                                Coordinate testPt = testRing.getCoordinateN(0);
585
                                LinearRing tryRing = null;
586

    
587
                                for (int j = 0; j < shells.size(); j++) {
588
                                        tryRing = (LinearRing) shells.get(j);
589

    
590
                                        Envelope tryEnv = tryRing.getEnvelopeInternal();
591

    
592
                                        if (minShell != null) {
593
                                                minEnv = minShell.getEnvelopeInternal();
594
                                        }
595

    
596
                                        boolean isContained = false;
597
                                        Coordinate[] coordList = tryRing.getCoordinates();
598

    
599
                                        if (tryEnv.contains(testEnv) &&
600
                                                        (CGAlgorithms.isPointInRing(testPt, coordList) ||
601
                                                                        (pointInList(testPt, coordList)))) {
602
                                                isContained = true;
603
                                        }
604

    
605
                                        // check if this new containing ring is smaller than the current minimum ring
606
                                        if (isContained) {
607
                                                if ((minShell == null) || minEnv.contains(tryEnv)) {
608
                                                        minShell = tryRing;
609
                                                }
610
                                        }
611
                                }
612

    
613
                                if (minShell == null) {
614
//                                        System.out.println(
615
//                                        "polygon found with a hole thats not inside a shell");
616
//                                        azabala: we do the assumption that this hole is really a shell (polygon)
617
//                                        whose point werent digitized in the right order
618
                                        Coordinate[] cs = testRing.getCoordinates();
619
                                        Coordinate[] reversed = new Coordinate[cs.length];
620
                                        int pointIndex = 0;
621
                                        for(int z = cs.length-1; z >= 0; z--){
622
                                                reversed[pointIndex] = cs[z];
623
                                                pointIndex++;
624
                                        }
625
                                        LinearRing newRing = geomFactory.createLinearRing(reversed);
626
                                        shells.add(newRing);
627
                                        holesForShells.add(new ArrayList());
628
                                } else {
629
                                        ((ArrayList) holesForShells.get(shells.indexOf(minShell))).add(testRing);
630
                                }
631
                        }
632

    
633
                        Polygon[] polygons = new Polygon[shells.size()];
634

    
635
                        for (int i = 0; i < shells.size(); i++) {
636
                                polygons[i] = geomFactory.createPolygon((LinearRing) shells.get(
637
                                                i),
638
                                                (LinearRing[]) ((ArrayList) holesForShells.get(i)).toArray(
639
                                                                new LinearRing[0]));
640
                                polygons[i].setSRID(srid);
641
                        }
642
                        // CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
643
                        // LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
644
                        // O CON GEOTOOLS
645
                        // if (numParts > 1) // Generamos una MultiLineString
646

    
647
                        /* if (polygons.length == 1) {
648
                         return polygons[0];
649
                         } */
650

    
651
                        // FIN CAMBIO
652

    
653
                        holesForShells = null;
654
                        shells = null;
655
                        holes = null;
656

    
657
                        //its a multi part
658
                        geoJTS = geomFactory.createMultiPolygon(polygons);
659
                        geoJTS.setSRID(srid);
660

    
661
                        /* if (numParts > 1) // Generamos un Polygon con agujeros
662
                         {
663
                         arrayLines.add(linRing);
664
                         // geoJTS = new GeometryFactory().createPolygon(linRingExt,
665
                          // GeometryFactory.toLinearRingArray(arrayLines));
666
                           geoJTS = new GeometryFactory().buildGeometry(arrayLines);
667

668
                           // geoJTS = Polygonizer.class.
669
                            }
670
                            else
671
                            {
672
                            geoJTS = new GeometryFactory().createPolygon(linRing,null);
673
                            } */
674
                        break;
675
                }
676

    
677
                geoJTS.setSRID(srid);
678
                return geoJTS;
679
        }
680

    
681
        /**
682
         * Converts JTS Geometry objects into Java 2D Shape objects
683
         *
684
         * @param geo Geometry de JTS.
685
         *
686
         * @return FShape.
687
         */
688
//        public static FShape jts_to_java2d(com.vividsolutions.jts.geom.Geometry geo) {
689
//                FShape shpNew = null;
690
//
691
//                try {
692
//                        if (geo instanceof Point) {
693
//                                shpNew = new org.gvsig.fmap.geom.primitive.Point2D(null, null, ((Point) geo).getX(), ((Point) geo).getY());
694
//                        }
695
//
696
//                        if (geo.isEmpty()) {
697
//                                shpNew = null;
698
//                        }
699
//
700
//                        if (geo instanceof Polygon) {
701
//                                shpNew = new Surface2D(null, null, toShape((Polygon) geo));
702
//                        }
703
//
704
//                        if (geo instanceof MultiPolygon) {
705
//                                shpNew = new Surface2D(null, null, toShape((MultiPolygon) geo));
706
//                        }
707
//
708
//                        if (geo instanceof LineString) {
709
//                                shpNew = new Curve2D(null, null, toShape((LineString) geo));
710
//                        }
711
//
712
//                        if (geo instanceof MultiLineString) {
713
//                                shpNew = new Curve2D(null, null, toShape((MultiLineString) geo));
714
//                        }
715
//
716
//                        /* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN GeometryCollection
717
//                         * No sabremos si queremos una l?nea o un pol?gono.....
718
//                         *  if (geometry instanceof GeometryCollection) {
719
//                                  return toShape((GeometryCollection) geometry);
720
//                           } */
721
//                        return shpNew;
722
//                } catch (NoninvertibleTransformException e) {
723
//                        // TODO Auto-generated catch block
724
//                        e.printStackTrace();
725
//                }
726
//
727
//                return null;
728
//        }
729

    
730
        /**
731
         * DOCUMENT ME!
732
         *
733
         * @param p DOCUMENT ME!
734
         *
735
         * @return DOCUMENT ME!
736
         */
737
        private static GeneralPathX toShape(Polygon p) {
738
                GeneralPathX resul = new GeneralPathX();
739
                Coordinate coord;
740

    
741
                for (int i = 0; i < p.getExteriorRing().getNumPoints(); i++) {
742
                        coord = p.getExteriorRing().getCoordinateN(i);
743

    
744
                        if (i == 0) {
745
                                resul.moveTo(coord.x,coord.y);
746
                        } else {
747
                                resul.lineTo(coord.x,coord.y);
748
                        }
749
                }
750

    
751
                for (int j = 0; j < p.getNumInteriorRing(); j++) {
752
                        LineString hole = p.getInteriorRingN(j);
753

    
754
                        for (int k = 0; k < hole.getNumPoints(); k++) {
755
                                coord = hole.getCoordinateN(k);
756

    
757
                                if (k == 0) {
758
                                        resul.moveTo(coord.x, coord.y);
759
                                } else {
760
                                        resul.lineTo(coord.x, coord.y);
761
                                }
762
                        }
763
                }
764

    
765
                return resul;
766
        }
767

    
768
        /**
769
         * DOCUMENT ME!
770
         *
771
         * @param modelCoordinates DOCUMENT ME!
772
         *
773
         * @return DOCUMENT ME!
774
         *
775
         * @throws NoninvertibleTransformException DOCUMENT ME!
776
         *
777
        private Coordinate[] toViewCoordinates(Coordinate[] modelCoordinates)
778
                throws NoninvertibleTransformException {
779
                Coordinate[] viewCoordinates = new Coordinate[modelCoordinates.length];
780

781
                for (int i = 0; i < modelCoordinates.length; i++) {
782
                        FPoint2D point2D = coordinate2FPoint2D(modelCoordinates[i]);
783
                        viewCoordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
784
                }
785

786
                return viewCoordinates;
787
        } 
788
         * @throws CreateGeometryException */
789

    
790
        /* private Shape toShape(GeometryCollection gc)
791
           throws NoninvertibleTransformException {
792
           GeometryCollectionShape shape = new GeometryCollectionShape();
793
           for (int i = 0; i < gc.getNumGeometries(); i++) {
794
                   Geometry g = (Geometry) gc.getGeometryN(i);
795
                   shape.add(toShape(g));
796
           }
797
           return shape;
798
           } */
799
        private static GeneralPathX toShape(MultiLineString mls)
800
                throws NoninvertibleTransformException, CreateGeometryException {
801
                GeneralPathX path = new GeneralPathX();
802

    
803
                for (int i = 0; i < mls.getNumGeometries(); i++) {
804
                        LineString lineString = (LineString) mls.getGeometryN(i);
805
                        path.append(toShape(lineString).getPathIterator(null), false);
806
                }
807

    
808
                //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
809
                //converted to GeneralPathXs. [Jon Aquino]
810
                return path;
811
        }
812

    
813
        /**
814
         * DOCUMENT ME!
815
         *
816
         * @param lineString DOCUMENT ME!
817
         *
818
         * @return DOCUMENT ME!
819
         *
820
         * @throws NoninvertibleTransformException DOCUMENT ME!
821
         * @throws CreateGeometryException 
822
         */
823
        private static GeneralPathX toShape(LineString lineString)
824
                throws NoninvertibleTransformException, CreateGeometryException {
825
                GeneralPathX shape = new GeneralPathX();
826
                org.gvsig.fmap.geom.primitive.Point viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(0));
827
                shape.moveTo(viewPoint.getX(), viewPoint.getY());
828

    
829
                for (int i = 1; i < lineString.getNumPoints(); i++) {
830
                        viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(i));
831
                        shape.lineTo(viewPoint.getX(), viewPoint.getY());
832
                }
833

    
834
                //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
835
                //converted to GeneralPathXs. [Jon Aquino]
836
                return shape;
837
        }
838

    
839
        /* TODO No se usa
840
         * DOCUMENT ME!
841
         *
842
         * @param point DOCUMENT ME!
843
         *
844
         * @return DOCUMENT ME!
845
         *
846
         * @throws NoninvertibleTransformException DOCUMENT ME!
847
         *
848
        private static Point2D toShape(Point point)
849
                throws NoninvertibleTransformException {
850
                Point2D viewPoint = coordinate2FPoint2D(point.getCoordinate());
851

852
                return viewPoint;
853
        }
854
*/
855

    
856
        /**
857
         *
858
         */
859
        private static GeneralPathX toShape(MultiPolygon mp)
860
        throws NoninvertibleTransformException {
861
        GeneralPathX path = new GeneralPathX();
862

    
863
        for (int i = 0; i < mp.getNumGeometries(); i++) {
864
                Polygon polygon = (Polygon) mp.getGeometryN(i);
865
                path.append(toShape(polygon).getPathIterator(null), false);
866
        }
867

    
868
        //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
869
        //converted to GeneralPathXs. [Jon Aquino]
870
        return path;
871
}
872
        /**
873
         * DOCUMENT ME!
874
         *
875
         * @param coord DOCUMENT ME!
876
         *
877
         * @return DOCUMENT ME!
878
         * @throws CreateGeometryException 
879
         */
880
        public static org.gvsig.fmap.geom.primitive.Point coordinate2FPoint2D(Coordinate coord) throws CreateGeometryException {
881
                return geomManager.createPoint(coord.x, coord.y, SUBTYPES.GEOM2D); //,coord.z);
882
        }
883

    
884
        /**
885
         * Convierte una Geometry de JTS a GeneralPathX.
886
         *
887
         * @param geometry Geometry a convertir.
888
         *
889
         * @return GeneralPathX.
890
         *
891
         * @throws NoninvertibleTransformException
892
         * @throws CreateGeometryException 
893
         * @throws IllegalArgumentException
894
         */
895
        public static GeneralPathX toShape(com.vividsolutions.jts.geom.Geometry geometry)
896
                throws NoninvertibleTransformException, CreateGeometryException {
897
                if (geometry.isEmpty()) {
898
                        return new GeneralPathX();
899
                }
900

    
901
                if (geometry instanceof Polygon) {
902
                        return toShape((Polygon) geometry);
903
                }
904

    
905
                if (geometry instanceof MultiPolygon) {
906
                        return toShape((MultiPolygon) geometry);
907
                }
908

    
909
                if (geometry instanceof LineString) {
910
                        return toShape((LineString) geometry);
911
                }
912

    
913
                if (geometry instanceof MultiLineString) {
914
                        return toShape((MultiLineString) geometry);
915
                }
916

    
917
                if (geometry instanceof GeometryCollection) {
918
                        return toShape(geometry);
919
                }
920

    
921
                throw new IllegalArgumentException("Unrecognized Geometry class: " +
922
                        geometry.getClass());
923
        }
924

    
925

    
926
    public static GeneralPathX transformToInts(GeneralPathX gp, AffineTransform at) {
927
        GeneralPathX newGp = new GeneralPathX();
928
        PathIterator theIterator;
929
        int theType;
930
        int numParts = 0;
931
        double[] theData = new double[6];
932
        java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
933
        java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
934
        boolean bFirst = true;
935
        int xInt, yInt, antX = -1, antY = -1;
936

    
937
        theIterator = gp.getPathIterator(null); //, flatness);
938

    
939
        while (!theIterator.isDone()) {
940
            theType = theIterator.currentSegment(theData);
941
            switch (theType) {
942
                case PathIterator.SEG_MOVETO:
943
                    numParts++;
944
                    ptSrc.setLocation(theData[0], theData[1]);
945
                    at.transform(ptSrc, ptDst);
946
                    antX = (int) ptDst.getX();
947
                    antY = (int) ptDst.getY();
948
                    newGp.moveTo(antX, antY);
949
                    bFirst = true;
950
                    break;
951

    
952
                case PathIterator.SEG_LINETO:
953
                    ptSrc.setLocation(theData[0], theData[1]);
954
                    at.transform(ptSrc, ptDst);
955
                    xInt = (int) ptDst.getX();
956
                    yInt = (int) ptDst.getY();
957
                    if ((bFirst) || ((xInt != antX) || (yInt != antY)))
958
                    {
959
                        newGp.lineTo(xInt, yInt);
960
                        antX = xInt;
961
                        antY = yInt;
962
                        bFirst = false;
963
                    }
964
                    break;
965

    
966
                case PathIterator.SEG_QUADTO:
967
                    System.out.println("Not supported here");
968

    
969
                    break;
970

    
971
                case PathIterator.SEG_CUBICTO:
972
                    System.out.println("Not supported here");
973

    
974
                    break;
975

    
976
                case PathIterator.SEG_CLOSE:
977
                    newGp.closePath();
978

    
979
                    break;
980
            } //end switch
981

    
982
            theIterator.next();
983
        } //end while loop
984

    
985
        return newGp;
986
    }
987
    public static Geometry transformToInts(Geometry gp, AffineTransform at) throws CreateGeometryException {
988
        GeneralPathX newGp = new GeneralPathX();
989
        double[] theData = new double[6];
990
        double[] aux = new double[6];
991

    
992
        // newGp.reset();
993
        PathIterator theIterator;
994
        int theType;
995
        int numParts = 0;
996

    
997
        java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
998
        java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
999
        boolean bFirst = true;
1000
        int xInt, yInt, antX = -1, antY = -1;
1001

    
1002

    
1003
        theIterator = gp.getPathIterator(null); //, flatness);
1004
        int numSegmentsAdded = 0;
1005
        while (!theIterator.isDone()) {
1006
            theType = theIterator.currentSegment(theData);
1007

    
1008
            switch (theType) {
1009
                case PathIterator.SEG_MOVETO:
1010
                    numParts++;
1011
                    ptSrc.setLocation(theData[0], theData[1]);
1012
                    at.transform(ptSrc, ptDst);
1013
                    antX = (int) ptDst.getX();
1014
                    antY = (int) ptDst.getY();
1015
                    newGp.moveTo(antX, antY);
1016
                    numSegmentsAdded++;
1017
                    bFirst = true;
1018
                    break;
1019

    
1020
                case PathIterator.SEG_LINETO:
1021
                    ptSrc.setLocation(theData[0], theData[1]);
1022
                    at.transform(ptSrc, ptDst);
1023
                    xInt = (int) ptDst.getX();
1024
                    yInt = (int) ptDst.getY();
1025
                    if ((bFirst) || ((xInt != antX) || (yInt != antY)))
1026
                    {
1027
                        newGp.lineTo(xInt, yInt);
1028
                        antX = xInt;
1029
                        antY = yInt;
1030
                        bFirst = false;
1031
                        numSegmentsAdded++;
1032
                    }
1033
                    break;
1034

    
1035
                case PathIterator.SEG_QUADTO:
1036
                    at.transform(theData,0,aux,0,2);
1037
                    newGp.quadTo(aux[0], aux[1], aux[2], aux[3]);
1038
                    numSegmentsAdded++;
1039
                    break;
1040

    
1041
                case PathIterator.SEG_CUBICTO:
1042
                    at.transform(theData,0,aux,0,3);
1043
                    newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]);
1044
                    numSegmentsAdded++;
1045
                    break;
1046

    
1047
                case PathIterator.SEG_CLOSE:
1048
                    if (numSegmentsAdded < 3) {
1049
                                                newGp.lineTo(antX, antY);
1050
                                        }
1051
                    newGp.closePath();
1052

    
1053
                    break;
1054
            } //end switch
1055

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

    
1059
        Geometry shp = null;
1060
        switch (gp.getType())
1061
        {
1062
            case Geometry.TYPES.POINT:
1063
                shp = geomManager.createPoint(ptDst.getX(), ptDst.getY(), SUBTYPES.GEOM2D); 
1064
                break;
1065

    
1066
            case Geometry.TYPES.CURVE:
1067
            case Geometry.TYPES.ARC:
1068
                    try {
1069
                                        shp = geomManager.createCurve(newGp, SUBTYPES.GEOM2D);
1070
                                } catch (CreateGeometryException e1) {
1071
                                        logger.error("Error creating a curve", e1);
1072
                                }
1073
                break;
1074

    
1075
            case Geometry.TYPES.SURFACE:
1076
            case Geometry.TYPES.CIRCLE:
1077
            case Geometry.TYPES.ELLIPSE:
1078

    
1079
                try {
1080
                                        shp = geomManager.createSurface(newGp, SUBTYPES.GEOM2D);
1081
                                } catch (CreateGeometryException e) {
1082
                                        logger.error("Error creating a surface", e);
1083
                                }
1084
                break;
1085
        }
1086
        return shp;
1087
    }
1088

    
1089
    public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR)
1090
    {
1091
        Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(),
1092
                jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight());
1093
        return r;
1094
    }
1095

    
1096
    public static Envelope convertEnvelopeToJTS(org.gvsig.fmap.geom.primitive.Envelope r)
1097
    {
1098
            Envelope e = new Envelope(r.getMinimum(0), r.getMaximum(0), r.getMinimum(1),
1099
                        r.getMaximum(1));
1100
            return e;
1101
    }
1102

    
1103
    /**
1104
     * Return a correct polygon (no hole)
1105
     * @param coordinates
1106
     * @return
1107
     */
1108
    public static Geometry getExteriorPolygon(Coordinate[] coordinates)
1109
    {
1110
            // isCCW = true => it's a hole
1111
            Coordinate[] vs=new Coordinate[coordinates.length];
1112
        if (CGAlgorithms.isCCW(coordinates)){
1113
                for (int i=vs.length-1;i>=0;i--){
1114
                        vs[i]=coordinates[i];
1115
                }
1116
        }else{
1117
                vs=coordinates;
1118
        }
1119
        LinearRing ring = geomFactory.createLinearRing(vs);
1120

    
1121
        try {
1122
                        Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
1123
                        surface.setGeneralPath(toShape(ring));
1124
                        return surface;
1125
                } catch (NoninvertibleTransformException e) {
1126
                        e.printStackTrace();
1127
                } catch (CreateGeometryException e) {
1128
                        e.printStackTrace();
1129
                }
1130
                return null;
1131
    }
1132

    
1133
    public static boolean isCCW(Point[] points)
1134
    {
1135
            int length = points.length;
1136
            Coordinate[] vs;
1137
            // CGAlgorithms.isCCW asume que la lista de puntos tienen el primer
1138
            // y el ultimo puntos iguales.... y que este algoritmo est? solo
1139
            // garantizado con anillos v?lidos.
1140
            if (points[0].getX() != points[length-1].getX() || points[0].getY() != points[length-1].getY()) {
1141
                vs=new Coordinate[length+1];
1142
                vs[points.length] = new Coordinate(points[0].getX(), points[0].getY());
1143
            } else {
1144
                vs=new Coordinate[length];
1145
            }
1146
               for (int i=0; i<length; i++){
1147
                    vs[i] = new Coordinate(points[i].getX(), points[i].getY());
1148
            }
1149

    
1150
        return CGAlgorithms.isCCW(vs);
1151
    }
1152

    
1153
    public static boolean isCCW(Surface pol)
1154
    {
1155
            com.vividsolutions.jts.geom.Geometry jtsGeom = Converter.geometryToJts(pol);
1156
            if (jtsGeom.getNumGeometries() == 1)
1157
            {
1158
                    Coordinate[] coords = jtsGeom.getCoordinates();
1159
                    return CGAlgorithms.isCCW(coords);
1160
            }
1161
            return false;
1162

    
1163
    }
1164

    
1165

    
1166
    /**
1167
     * Return a hole (CCW ordered points)
1168
     * @param coordinates
1169
     * @return
1170
     */
1171
    public static Geometry getHole(Coordinate[] coordinates)
1172
    {
1173
            // isCCW = true => it's a hole
1174
            Coordinate[] vs=new Coordinate[coordinates.length];
1175
        if (CGAlgorithms.isCCW(coordinates)){
1176
                vs=coordinates;
1177

    
1178
        }else{
1179
                for (int i=vs.length-1;i>=0;i--){
1180
                        vs[i]=coordinates[i];
1181
                }
1182
        }
1183
        LinearRing ring = geomFactory.createLinearRing(vs);
1184

    
1185
        try {
1186
                Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
1187
                surface.setGeneralPath(toShape(ring));
1188
                        return surface;
1189
                } catch (NoninvertibleTransformException e) {
1190
                        e.printStackTrace();
1191
                } catch (CreateGeometryException e) {
1192
                        e.printStackTrace();
1193
                }
1194
                return null;
1195
    }
1196

    
1197
        public static Shape getExteriorPolygon(GeneralPathX gp) {
1198
                Area area = new Area(gp);
1199
                area.isSingular();
1200
                return area;
1201

    
1202

    
1203

    
1204
        }
1205
        /**
1206
         * Use it ONLY for NOT multipart polygons.
1207
         * @param pol
1208
         * @return
1209
         */
1210
        public static Geometry getNotHolePolygon(Surface pol) {
1211
                // isCCW == true => hole
1212
                Coordinate[] coords;
1213
                ArrayList arrayCoords = null;
1214
                int theType;
1215
                int numParts = 0;
1216

    
1217
                //                 Use this array to store segment coordinate data
1218
                double[] theData = new double[6];
1219
                PathIterator theIterator;
1220

    
1221
                                ArrayList shells = new ArrayList();
1222
                                ArrayList holes = new ArrayList();
1223
                                Coordinate[] points = null;
1224

    
1225
                                theIterator = pol.getPathIterator(null, manager.getFlatness());
1226

    
1227
                                while (!theIterator.isDone()) {
1228
                                        //while not done
1229
                                        theType = theIterator.currentSegment(theData);
1230

    
1231
                                        //Populate a segment of the new
1232
                                        // GeneralPathX object.
1233
                                        //Process the current segment to populate a new
1234
                                        // segment of the new GeneralPathX object.
1235
                                        switch (theType) {
1236
                                                case PathIterator.SEG_MOVETO:
1237

    
1238
                                                        // System.out.println("SEG_MOVETO");
1239
                                                        if (arrayCoords == null) {
1240
                                                                arrayCoords = new ArrayList();
1241
                                                        } else {
1242
                                                                points = CoordinateArrays.toCoordinateArray(arrayCoords);
1243

    
1244
                                                                try {
1245
                                                                        LinearRing ring = geomFactory.createLinearRing(points);
1246

    
1247
                                                                        if (CGAlgorithms.isCCW(points)) {
1248
                                                                                holes.add(ring);
1249
                                                                        } else {
1250
                                                                                shells.add(ring);
1251
                                                                        }
1252
                                                                } catch (Exception e) {
1253
                                                                        System.err.println(
1254
                                                                                "Caught Topology exception in GMLLinearRingHandler");
1255

    
1256
                                                                        return null;
1257
                                                                }
1258

    
1259
                                                                /* if (numParts == 1)
1260
                                                                   {
1261
                                                                           linRingExt = new GeometryFactory().createLinearRing(
1262
                                                                                  CoordinateArrays.toCoordinateArray(arrayCoords));
1263
                                                                   }
1264
                                                                   else
1265
                                                                   {
1266
                                                                           linRing = new GeometryFactory().createLinearRing(
1267
                                                                                          CoordinateArrays.toCoordinateArray(arrayCoords));
1268
                                                                           arrayLines.add(linRing);
1269
                                                                   } */
1270
                                                                arrayCoords = new ArrayList();
1271
                                                        }
1272

    
1273
                                                        numParts++;
1274
                                                        arrayCoords.add(new Coordinate(theData[0],
1275
                                                                        theData[1]));
1276

    
1277
                                                        break;
1278

    
1279
                                                case PathIterator.SEG_LINETO:
1280

    
1281
                                                        // System.out.println("SEG_LINETO");
1282
                                                        arrayCoords.add(new Coordinate(theData[0],
1283
                                                                        theData[1]));
1284

    
1285
                                                        break;
1286

    
1287
                                                case PathIterator.SEG_QUADTO:
1288
                                                        System.out.println("SEG_QUADTO Not supported here");
1289

    
1290
                                                        break;
1291

    
1292
                                                case PathIterator.SEG_CUBICTO:
1293
                                                        System.out.println("SEG_CUBICTO Not supported here");
1294

    
1295
                                                        break;
1296

    
1297
                                                case PathIterator.SEG_CLOSE:
1298

    
1299
                                                        // A?adimos el primer punto para cerrar.
1300
                                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
1301
                                                        arrayCoords.add(new Coordinate(firstCoord.x,
1302
                                                                        firstCoord.y));
1303

    
1304
                                                        break;
1305
                                        } //end switch
1306

    
1307
                                        // System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
1308
                                        theIterator.next();
1309
                                } //end while loop
1310

    
1311
                                arrayCoords.add(arrayCoords.get(0));
1312
                                coords = CoordinateArrays.toCoordinateArray(arrayCoords);
1313

    
1314

    
1315
                if (numParts == 1)
1316
                {
1317
                        return getExteriorPolygon(coords);
1318
                }
1319
                return pol;
1320

    
1321
        }
1322

    
1323

    
1324
    /* public static GeometryCollection convertFGeometryCollection(FGeometryCollection fGeomC)
1325
    {
1326

1327
        geomFactory.createGeometryCollection(theGeoms);
1328
    } */
1329
}