Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FConverter.java @ 6025

History | View | Annotate | Download (29.6 KB)

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

    
49
import java.awt.Shape;
50
import java.awt.geom.AffineTransform;
51
import java.awt.geom.Area;
52
import java.awt.geom.NoninvertibleTransformException;
53
import java.awt.geom.PathIterator;
54
import java.awt.geom.Point2D;
55
import java.awt.geom.Rectangle2D;
56
import java.lang.reflect.Array;
57
import java.util.ArrayList;
58

    
59
import com.iver.cit.gvsig.fmap.core.FPoint2D;
60
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
61
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
62
import com.iver.cit.gvsig.fmap.core.FShape;
63
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
64
import com.iver.cit.gvsig.fmap.core.IGeometry;
65
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
66
import com.vividsolutions.jts.algorithm.CGAlgorithms;
67
import com.vividsolutions.jts.algorithm.RobustCGAlgorithms;
68
import com.vividsolutions.jts.geom.Coordinate;
69
import com.vividsolutions.jts.geom.CoordinateArrays;
70
import com.vividsolutions.jts.geom.Envelope;
71
import com.vividsolutions.jts.geom.Geometry;
72
import com.vividsolutions.jts.geom.GeometryCollection;
73
import com.vividsolutions.jts.geom.GeometryFactory;
74
import com.vividsolutions.jts.geom.LineString;
75
import com.vividsolutions.jts.geom.LinearRing;
76
import com.vividsolutions.jts.geom.MultiLineString;
77
import com.vividsolutions.jts.geom.MultiPolygon;
78
import com.vividsolutions.jts.geom.Point;
79
import com.vividsolutions.jts.geom.Polygon;
80

    
81

    
82
/**
83
 * Clase con varios m?todos est?ticos utilizados para pasar de java2d a jts y
84
 * viceversa.
85
 *
86
 * @author fjp
87
 */
88
public class FConverter {
89
        /**
90
         * ?QU? PODEMOS HACER CON LOS MULTIPOINT??? => DEBER?AMOS TRABAJAR CON UN
91
         * ARRAY DE PUNTOS EN FShape.....Pensarlo bien.
92
         */
93
        public final static GeometryFactory geomFactory = new GeometryFactory();
94
        public static CGAlgorithms cga = new RobustCGAlgorithms();
95
        // private final static AffineTransform at = new AffineTransform();
96
        private static double POINT_MARKER_SIZE = 3.0;
97

    
98
        /**
99
         * Es la m?xima distancia que permitimos que el trazo aproximado
100
         * difiera del trazo real. 
101
         */
102
        public static double flatness = 0.8; // Por ejemplo. Cuanto m?s peque?o, m?s segmentos necesitar? la curva
103
        
104

    
105

    
106

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

    
113
                numpoints = Array.getLength(pointList);
114

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

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

    
125
                return false;
126
        }
127

    
128
        /**
129
         * Receives a JTS Geometry and returns a fmap IGeometry
130
         * @param jtsGeometry jts Geometry
131
         * @return IGeometry of FMap
132
         * @author azabala
133
         */
134
        public static IGeometry jts_to_igeometry(Geometry jtsGeometry){
135
                FShape shape = FConverter.jts_to_java2d(jtsGeometry);
136
                return ShapeFactory.createGeometry(shape);
137
        }
138
        /**
139
         * Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
140
         * "flattened PathIterator". El flattened indica que las curvas las pasa a
141
         * segmentos de l?nea recta AUTOMATICAMENTE!!!.
142
         *
143
         * @param shp FShape que se quiere convertir.
144
         *
145
         * @return Geometry de JTS.
146
         */
147
        public static Geometry java2d_to_jts(FShape shp) {
148

    
149

    
150
                Geometry geoJTS = null;
151
                Coordinate coord;
152
                Coordinate[] coords;
153
                ArrayList arrayCoords = null;
154
                ArrayList arrayLines;
155
                LineString lin;
156
                LinearRing linRing;
157
                LinearRing linRingExt = null;
158
                int theType;
159
                int numParts = 0;
160

    
161
                //                 Use this array to store segment coordinate data
162
                double[] theData = new double[6];
163
                PathIterator theIterator;
164

    
165
                switch (shp.getShapeType()) {
166
                        case FShape.POINT:
167
            case FShape.POINT + FShape.Z:
168
                                FPoint2D p = (FPoint2D) shp;
169
                                coord = new Coordinate(p.getX(), p.getY());
170
                                geoJTS = geomFactory.createPoint(coord);
171

    
172
                                break;
173

    
174
                        case FShape.LINE:
175
                        case FShape.ARC:
176
            case FShape.LINE + FShape.Z:
177
                                arrayLines = new ArrayList();
178
                                theIterator = shp.getPathIterator(null, flatness);
179

    
180
                                while (!theIterator.isDone()) {
181
                                        //while not done
182
                                        theType = theIterator.currentSegment(theData);
183

    
184
                                        //Populate a segment of the new
185
                                        // GeneralPathX object.
186
                                        //Process the current segment to populate a new
187
                                        // segment of the new GeneralPathX object.
188
                                        switch (theType) {
189
                                                case PathIterator.SEG_MOVETO:
190

    
191
                                                        // System.out.println("SEG_MOVETO");
192
                                                        if (arrayCoords == null) {
193
                                                                arrayCoords = new ArrayList();
194
                                                        } else {
195
                                                                lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray(
196
                                                                                        arrayCoords));
197
                                                                arrayLines.add(lin);
198
                                                                arrayCoords = new ArrayList();
199
                                                        }
200

    
201
                                                        numParts++;
202
                                                        arrayCoords.add(new Coordinate(theData[0],
203
                                                                        theData[1]));
204

    
205
                                                        break;
206

    
207
                                                case PathIterator.SEG_LINETO:
208

    
209
                                                        // System.out.println("SEG_LINETO");
210
                                                        arrayCoords.add(new Coordinate(theData[0],
211
                                                                        theData[1]));
212

    
213
                                                        break;
214

    
215
                                                case PathIterator.SEG_QUADTO:
216
                                                        System.out.println("Not supported here");
217

    
218
                                                        break;
219

    
220
                                                case PathIterator.SEG_CUBICTO:
221
                                                        System.out.println("Not supported here");
222

    
223
                                                        break;
224

    
225
                                                case PathIterator.SEG_CLOSE:
226
                                                        System.out.println("SEG_CLOSE");
227

    
228
                                                        // A?adimos el primer punto para cerrar.
229
                                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
230
                                                        arrayCoords.add(new Coordinate(firstCoord.x,
231
                                                                        firstCoord.y));
232

    
233
                                                        break;
234
                                        } //end switch
235

    
236
                                        theIterator.next();
237
                                } //end while loop
238

    
239
                                lin = new GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
240
                                                        arrayCoords));
241

    
242
                                // CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
243
                                // LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
244
                                // O CON GEOTOOLS
245
                                // if (numParts > 1) // Generamos una MultiLineString
246
                                //  {
247
                                        arrayLines.add(lin);
248
                                        geoJTS = geomFactory.createMultiLineString(GeometryFactory.toLineStringArray(
249
                                                                arrayLines));
250
                                /* } else {
251
                                        geoJTS = lin;
252
                                } */
253

    
254
                                break;
255

    
256
                        case FShape.POLYGON:
257
                        case FShape.CIRCLE:
258
            case FShape.POLYGON + FShape.Z:
259
                                arrayLines = new ArrayList();
260

    
261
                                ArrayList shells = new ArrayList();
262
                                ArrayList holes = new ArrayList();
263
                                Coordinate[] points = null;
264

    
265
                                theIterator = shp.getPathIterator(null, flatness);
266

    
267
                                while (!theIterator.isDone()) {
268
                                        //while not done
269
                                        theType = theIterator.currentSegment(theData);
270

    
271
                                        //Populate a segment of the new
272
                                        // GeneralPathX object.
273
                                        //Process the current segment to populate a new
274
                                        // segment of the new GeneralPathX object.
275
                                        switch (theType) {
276
                                                case PathIterator.SEG_MOVETO:
277

    
278
                                                        // System.out.println("SEG_MOVETO");
279
                                                        if (arrayCoords == null) {
280
                                                                arrayCoords = new ArrayList();
281
                                                        } else {
282
                                                                points = CoordinateArrays.toCoordinateArray(arrayCoords);
283

    
284
                                                                try {
285
                                                                        LinearRing ring = geomFactory.createLinearRing(points);
286

    
287
                                                                        if (CGAlgorithms.isCCW(points)) {
288
                                                                                holes.add(ring);
289
                                                                        } else {
290
                                                                                shells.add(ring);
291
                                                                        }
292
                                                                } catch (Exception e) {
293
                                                                        System.err.println(
294
                                                                                "Caught Topology exception in GMLLinearRingHandler");
295

    
296
                                                                        return null;
297
                                                                }
298

    
299
                                                                /* if (numParts == 1)
300
                                                                   {
301
                                                                           linRingExt = new GeometryFactory().createLinearRing(
302
                                                                                  CoordinateArrays.toCoordinateArray(arrayCoords));
303
                                                                   }
304
                                                                   else
305
                                                                   {
306
                                                                           linRing = new GeometryFactory().createLinearRing(
307
                                                                                          CoordinateArrays.toCoordinateArray(arrayCoords));
308
                                                                           arrayLines.add(linRing);
309
                                                                   } */
310
                                                                arrayCoords = new ArrayList();
311
                                                        }
312

    
313
                                                        numParts++;
314
                                                        arrayCoords.add(new Coordinate(theData[0],
315
                                                                        theData[1]));
316

    
317
                                                        break;
318

    
319
                                                case PathIterator.SEG_LINETO:
320

    
321
                                                        // System.out.println("SEG_LINETO");
322
                                                        arrayCoords.add(new Coordinate(theData[0],
323
                                                                        theData[1]));
324

    
325
                                                        break;
326

    
327
                                                case PathIterator.SEG_QUADTO:
328
                                                        System.out.println("SEG_QUADTO Not supported here");
329

    
330
                                                        break;
331

    
332
                                                case PathIterator.SEG_CUBICTO:
333
                                                        System.out.println("SEG_CUBICTO Not supported here");
334

    
335
                                                        break;
336

    
337
                                                case PathIterator.SEG_CLOSE:
338

    
339
                                                        // A?adimos el primer punto para cerrar.
340
                                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
341
                                                        arrayCoords.add(new Coordinate(firstCoord.x,
342
                                                                        firstCoord.y));
343

    
344
                                                        break;
345
                                        } //end switch
346

    
347
                                        // System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
348
                                        theIterator.next();
349
                                } //end while loop
350

    
351
                                arrayCoords.add(arrayCoords.get(0));
352
                                points = CoordinateArrays.toCoordinateArray(arrayCoords);
353

    
354
                                try {
355
                                        LinearRing ring = geomFactory.createLinearRing(points);
356

    
357
                                        if (CGAlgorithms.isCCW(points)) {
358
                                                holes.add(ring);
359
                                        } else {
360
                                                shells.add(ring);
361
                                        }
362
                                } catch (Exception e) {
363
                                        System.err.println(
364
                                                "Caught Topology exception in GMLLinearRingHandler");
365

    
366
                                        return null;
367
                                }
368

    
369
                                /* linRing = new GeometryFactory().createLinearRing(
370
                                   CoordinateArrays.toCoordinateArray(arrayCoords)); */
371

    
372
                                // System.out.println("NumParts = " + numParts);
373
                                //now we have a list of all shells and all holes
374
                                ArrayList holesForShells = new ArrayList(shells.size());
375

    
376
                                for (int i = 0; i < shells.size(); i++) {
377
                                        holesForShells.add(new ArrayList());
378
                                }
379

    
380
                                //find homes
381
                                for (int i = 0; i < holes.size(); i++) {
382
                                        LinearRing testRing = (LinearRing) holes.get(i);
383
                                        LinearRing minShell = null;
384
                                        Envelope minEnv = null;
385
                                        Envelope testEnv = testRing.getEnvelopeInternal();
386
                                        Coordinate testPt = testRing.getCoordinateN(0);
387
                                        LinearRing tryRing;
388

    
389
                                        for (int j = 0; j < shells.size(); j++) {
390
                                                tryRing = (LinearRing) shells.get(j);
391

    
392
                                                Envelope tryEnv = tryRing.getEnvelopeInternal();
393

    
394
                                                if (minShell != null) {
395
                                                        minEnv = minShell.getEnvelopeInternal();
396
                                                }
397

    
398
                                                boolean isContained = false;
399
                                                Coordinate[] coordList = tryRing.getCoordinates();
400

    
401
                                                if (tryEnv.contains(testEnv) &&
402
                                                                (CGAlgorithms.isPointInRing(testPt, coordList) ||
403
                                                                (pointInList(testPt, coordList)))) {
404
                                                        isContained = true;
405
                                                }
406

    
407
                                                // check if this new containing ring is smaller than the current minimum ring
408
                                                if (isContained) {
409
                                                        if ((minShell == null) || minEnv.contains(tryEnv)) {
410
                                                                minShell = tryRing;
411
                                                        }
412
                                                }
413
                                        }
414

    
415
                                        if (minShell == null) {
416
                                                System.out.println(
417
                                                        "polygon found with a hole thats not inside a shell");
418
                                        } else {
419
                                                ((ArrayList) holesForShells.get(shells.indexOf(minShell))).add(testRing);
420
                                        }
421
                                }
422

    
423
                                Polygon[] polygons = new Polygon[shells.size()];
424

    
425
                                for (int i = 0; i < shells.size(); i++) {
426
                                        polygons[i] = geomFactory.createPolygon((LinearRing) shells.get(
427
                                                                i),
428
                                                        (LinearRing[]) ((ArrayList) holesForShells.get(i)).toArray(
429
                                                                new LinearRing[0]));
430
                                }
431
                                // CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
432
                                // LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
433
                                // O CON GEOTOOLS
434
                                // if (numParts > 1) // Generamos una MultiLineString
435

    
436
                                /* if (polygons.length == 1) {
437
                                        return polygons[0];
438
                                } */
439

    
440
                                // FIN CAMBIO
441

    
442
                                holesForShells = null;
443
                                shells = null;
444
                                holes = null;
445

    
446
                                //its a multi part
447
                                geoJTS = geomFactory.createMultiPolygon(polygons);
448

    
449
                                /* if (numParts > 1) // Generamos un Polygon con agujeros
450
                                   {
451
                                    arrayLines.add(linRing);
452
                                           // geoJTS = new GeometryFactory().createPolygon(linRingExt,
453
                                                           // GeometryFactory.toLinearRingArray(arrayLines));
454
                                    geoJTS = new GeometryFactory().buildGeometry(arrayLines);
455

456
                                    // geoJTS = Polygonizer.class.
457
                                   }
458
                                   else
459
                                   {
460
                                           geoJTS = new GeometryFactory().createPolygon(linRing,null);
461
                                   } */
462
                                break;
463
                }
464

    
465
                return geoJTS;
466
        }
467

    
468
        /**
469
         * Converts JTS Geometry objects into Java 2D Shape objects
470
         *
471
         * @param geo Geometry de JTS.
472
         *
473
         * @return FShape.
474
         */
475
        public static FShape jts_to_java2d(Geometry geo) {
476
                FShape shpNew = null;
477

    
478
                try {
479
                        if (geo instanceof Point) {
480
                                shpNew = new FPoint2D(((Point) geo).getX(), ((Point) geo).getY());
481
                        }
482

    
483
                        if (geo.isEmpty()) {
484
                                shpNew = null;
485
                        }
486

    
487
                        if (geo instanceof Polygon) {
488
                                shpNew = new FPolygon2D(toShape((Polygon) geo));
489
                        }
490

    
491
                        if (geo instanceof MultiPolygon) {
492
                                shpNew = new FPolygon2D(toShape((MultiPolygon) geo));
493
                        }
494

    
495
                        if (geo instanceof LineString) {
496
                                shpNew = new FPolyline2D(toShape((LineString) geo));
497
                        }
498

    
499
                        if (geo instanceof MultiLineString) {
500
                                shpNew = new FPolyline2D(toShape((MultiLineString) geo));
501
                        }
502

    
503
                        /* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN GeometryCollection
504
                         * No sabremos si queremos una l?nea o un pol?gono.....
505
                         *  if (geometry instanceof GeometryCollection) {
506
                                  return toShape((GeometryCollection) geometry);
507
                           } */
508
                        return shpNew;
509
                } catch (NoninvertibleTransformException e) {
510
                        // TODO Auto-generated catch block
511
                        e.printStackTrace();
512
                }
513

    
514
                return null;
515
        }
516

    
517
        /**
518
         * DOCUMENT ME!
519
         *
520
         * @param p DOCUMENT ME!
521
         *
522
         * @return DOCUMENT ME!
523
         */
524
        private static GeneralPathX toShape(Polygon p) {
525
                GeneralPathX resul = new GeneralPathX();
526
                Coordinate coord;
527

    
528
                for (int i = 0; i < p.getExteriorRing().getNumPoints(); i++) {
529
                        coord = p.getExteriorRing().getCoordinateN(i);
530

    
531
                        if (i == 0) {
532
                                resul.moveTo(coord.x,coord.y);
533
                        } else {
534
                                resul.lineTo(coord.x,coord.y);
535
                        }
536
                }
537

    
538
                for (int j = 0; j < p.getNumInteriorRing(); j++) {
539
                        LineString hole = p.getInteriorRingN(j);
540

    
541
                        for (int k = 0; k < hole.getNumPoints(); k++) {
542
                                coord = hole.getCoordinateN(k);
543

    
544
                                if (k == 0) {
545
                                        resul.moveTo(coord.x, coord.y);
546
                                } else {
547
                                        resul.lineTo(coord.x, coord.y);
548
                                }
549
                        }
550
                }
551

    
552
                return resul;
553
        }
554

    
555
        /**
556
         * DOCUMENT ME!
557
         *
558
         * @param modelCoordinates DOCUMENT ME!
559
         *
560
         * @return DOCUMENT ME!
561
         *
562
         * @throws NoninvertibleTransformException DOCUMENT ME!
563
         *
564
        private Coordinate[] toViewCoordinates(Coordinate[] modelCoordinates)
565
                throws NoninvertibleTransformException {
566
                Coordinate[] viewCoordinates = new Coordinate[modelCoordinates.length];
567

568
                for (int i = 0; i < modelCoordinates.length; i++) {
569
                        FPoint2D point2D = coordinate2FPoint2D(modelCoordinates[i]);
570
                        viewCoordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
571
                }
572

573
                return viewCoordinates;
574
        } */
575

    
576
        /* private Shape toShape(GeometryCollection gc)
577
           throws NoninvertibleTransformException {
578
           GeometryCollectionShape shape = new GeometryCollectionShape();
579
           for (int i = 0; i < gc.getNumGeometries(); i++) {
580
                   Geometry g = (Geometry) gc.getGeometryN(i);
581
                   shape.add(toShape(g));
582
           }
583
           return shape;
584
           } */
585
        private static GeneralPathX toShape(MultiLineString mls)
586
                throws NoninvertibleTransformException {
587
                GeneralPathX path = new GeneralPathX();
588

    
589
                for (int i = 0; i < mls.getNumGeometries(); i++) {
590
                        LineString lineString = (LineString) mls.getGeometryN(i);
591
                        path.append(toShape(lineString), false);
592
                }
593

    
594
                //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
595
                //converted to GeneralPathXs. [Jon Aquino]
596
                return path;
597
        }
598

    
599
        /**
600
         * DOCUMENT ME!
601
         *
602
         * @param lineString DOCUMENT ME!
603
         *
604
         * @return DOCUMENT ME!
605
         *
606
         * @throws NoninvertibleTransformException DOCUMENT ME!
607
         */
608
        private static GeneralPathX toShape(LineString lineString)
609
                throws NoninvertibleTransformException {
610
                GeneralPathX shape = new GeneralPathX();
611
                FPoint2D viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(0));
612
                shape.moveTo(viewPoint.getX(), viewPoint.getY());
613

    
614
                for (int i = 1; i < lineString.getNumPoints(); i++) {
615
                        viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(i));
616
                        shape.lineTo(viewPoint.getX(), viewPoint.getY());
617
                }
618

    
619
                //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
620
                //converted to GeneralPathXs. [Jon Aquino]
621
                return shape;
622
        }
623

    
624
        /**
625
         * DOCUMENT ME!
626
         *
627
         * @param point DOCUMENT ME!
628
         *
629
         * @return DOCUMENT ME!
630
         *
631
         * @throws NoninvertibleTransformException DOCUMENT ME!
632
         */
633
        private static FPoint2D toShape(Point point)
634
                throws NoninvertibleTransformException {
635
                FPoint2D viewPoint = coordinate2FPoint2D(point.getCoordinate());
636

    
637
                return viewPoint;
638
        }
639

    
640
        private static GeneralPathX toShape(MultiPolygon mp)
641
        throws NoninvertibleTransformException {
642
        GeneralPathX path = new GeneralPathX();
643

    
644
        for (int i = 0; i < mp.getNumGeometries(); i++) {
645
                Polygon polygon = (Polygon) mp.getGeometryN(i);
646
                path.append(toShape(polygon), false);
647
        }
648

    
649
        //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
650
        //converted to GeneralPathXs. [Jon Aquino]
651
        return path;
652
}
653
        /**
654
         * DOCUMENT ME!
655
         *
656
         * @param coord DOCUMENT ME!
657
         *
658
         * @return DOCUMENT ME!
659
         */
660
        public static FPoint2D coordinate2FPoint2D(Coordinate coord) {
661
                return new FPoint2D(coord.x, coord.y); //,coord.z);
662
        }
663

    
664
        /**
665
         * Convierte una Geometry de JTS a GeneralPathX.
666
         *
667
         * @param geometry Geometry a convertir.
668
         *
669
         * @return GeneralPathX.
670
         *
671
         * @throws NoninvertibleTransformException
672
         * @throws IllegalArgumentException
673
         */
674
        public static GeneralPathX toShape(Geometry geometry)
675
                throws NoninvertibleTransformException {
676
                if (geometry.isEmpty()) {
677
                        return new GeneralPathX();
678
                }
679

    
680
                if (geometry instanceof Polygon) {
681
                        return toShape((Polygon) geometry);
682
                }
683

    
684
                if (geometry instanceof MultiPolygon) {
685
                        return toShape((MultiPolygon) geometry);
686
                }
687

    
688
                if (geometry instanceof LineString) {
689
                        return toShape((LineString) geometry);
690
                }
691

    
692
                if (geometry instanceof MultiLineString) {
693
                        return toShape((MultiLineString) geometry);
694
                }
695

    
696
                if (geometry instanceof GeometryCollection) {
697
                        return toShape((GeometryCollection) geometry);
698
                }
699

    
700
                throw new IllegalArgumentException("Unrecognized Geometry class: " +
701
                        geometry.getClass());
702
        }
703

    
704
        
705
    public static GeneralPathX transformToInts(GeneralPathX gp, AffineTransform at) {
706
        GeneralPathX newGp = new GeneralPathX();
707
        PathIterator theIterator;
708
        int theType;
709
        int numParts = 0;
710
        double[] theData = new double[6];
711
        Point2D ptDst = new Point2D.Double();
712
        Point2D ptSrc = new Point2D.Double();
713
        boolean bFirst = true;
714
        int xInt, yInt, antX = -1, antY = -1;
715

    
716
        theIterator = gp.getPathIterator(null); //, flatness);
717

    
718
        while (!theIterator.isDone()) {
719
            theType = theIterator.currentSegment(theData);
720
            switch (theType) {
721
                case PathIterator.SEG_MOVETO:
722
                    numParts++;
723
                    ptSrc.setLocation(theData[0], theData[1]);
724
                    at.transform(ptSrc, ptDst);
725
                    antX = (int) ptDst.getX();
726
                    antY = (int) ptDst.getY();
727
                    newGp.moveTo(antX, antY);
728
                    bFirst = true;
729
                    break;
730

    
731
                case PathIterator.SEG_LINETO:
732
                    ptSrc.setLocation(theData[0], theData[1]);
733
                    at.transform(ptSrc, ptDst);
734
                    xInt = (int) ptDst.getX();
735
                    yInt = (int) ptDst.getY();
736
                    if ((bFirst) || ((xInt != antX) || (yInt != antY)))
737
                    {
738
                        newGp.lineTo(xInt, yInt);
739
                        antX = xInt;
740
                        antY = yInt;
741
                        bFirst = false;
742
                    }
743
                    break;
744

    
745
                case PathIterator.SEG_QUADTO:
746
                    System.out.println("Not supported here");
747

    
748
                    break;
749

    
750
                case PathIterator.SEG_CUBICTO:
751
                    System.out.println("Not supported here");
752

    
753
                    break;
754

    
755
                case PathIterator.SEG_CLOSE:
756
                    newGp.closePath();
757

    
758
                    break;
759
            } //end switch
760

    
761
            theIterator.next();
762
        } //end while loop
763

    
764
        return newGp;
765
    }
766
    public static FShape transformToInts(IGeometry gp, AffineTransform at) {
767
        GeneralPathX newGp = new GeneralPathX();
768
        double[] theData = new double[6];
769
        double[] aux = new double[6];
770

    
771
        // newGp.reset();
772
        PathIterator theIterator;
773
        int theType;
774
        int numParts = 0;
775

    
776
        Point2D ptDst = new Point2D.Double();
777
        Point2D ptSrc = new Point2D.Double();
778
        boolean bFirst = true;
779
        int xInt, yInt, antX = -1, antY = -1;
780

    
781

    
782
        theIterator = gp.getPathIterator(null); //, flatness);
783
        int numSegmentsAdded = 0;
784
        while (!theIterator.isDone()) {
785
            theType = theIterator.currentSegment(theData);
786

    
787
            switch (theType) {
788
                case PathIterator.SEG_MOVETO:
789
                    numParts++;
790
                    ptSrc.setLocation(theData[0], theData[1]);
791
                    at.transform(ptSrc, ptDst);
792
                    antX = (int) ptDst.getX();
793
                    antY = (int) ptDst.getY();
794
                    newGp.moveTo(antX, antY);
795
                    numSegmentsAdded++;
796
                    bFirst = true;
797
                    break;
798

    
799
                case PathIterator.SEG_LINETO:
800
                    ptSrc.setLocation(theData[0], theData[1]);
801
                    at.transform(ptSrc, ptDst);
802
                    xInt = (int) ptDst.getX();
803
                    yInt = (int) ptDst.getY();
804
                    if ((bFirst) || ((xInt != antX) || (yInt != antY)))
805
                    {
806
                        newGp.lineTo(xInt, yInt);
807
                        antX = xInt;
808
                        antY = yInt;
809
                        bFirst = false;
810
                        numSegmentsAdded++;
811
                    }
812
                    break;
813

    
814
                case PathIterator.SEG_QUADTO:
815
                    at.transform(theData,0,aux,0,2);
816
                    newGp.quadTo(aux[0], aux[1], aux[2], aux[3]);
817
                    numSegmentsAdded++;
818
                    break;
819

    
820
                case PathIterator.SEG_CUBICTO:
821
                    at.transform(theData,0,aux,0,3);
822
                    newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]);
823
                    numSegmentsAdded++;
824
                    break;
825

    
826
                case PathIterator.SEG_CLOSE:
827
                    if (numSegmentsAdded < 3)
828
                        newGp.lineTo(antX, antY);
829
                    newGp.closePath();
830

    
831
                    break;
832
            } //end switch
833

    
834
            theIterator.next();
835
        } //end while loop
836
        FShape shp = null;
837
        switch (gp.getGeometryType())
838
        {
839
            case FShape.POINT: //Tipo punto
840
            case FShape.POINT + FShape.Z:
841
                shp = new FPoint2D(ptDst.getX(), ptDst.getY());
842
                break;
843

    
844
            case FShape.LINE:
845
            case FShape.LINE + FShape.Z:
846
            case FShape.ARC:
847
                    shp = new FPolyline2D(newGp);
848
                break;
849
                
850
            case FShape.POLYGON:
851
            case FShape.POLYGON + FShape.Z:
852
            case FShape.CIRCLE:
853
            case FShape.ELLIPSE:
854

    
855
                shp = new FPolygon2D(newGp);
856
                break;
857
        }
858
        return shp;
859
    }
860

    
861
    public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR)
862
    {
863
        Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(),
864
                jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight());
865
        return r;
866
    }
867

    
868
    public static Envelope convertRectangle2DtoEnvelope(Rectangle2D r)
869
    {
870
            Envelope e = new Envelope(r.getX(), r.getX() + r.getWidth(), r.getY(),
871
                        r.getY() + r.getHeight());
872
            return e;
873
    }
874

    
875
    /**
876
     * Return a correct polygon (no hole)
877
     * @param coordinates
878
     * @return
879
     */
880
    public static IGeometry getExteriorPolygon(Coordinate[] coordinates)
881
    {
882
            // isCCW = true => it's a hole
883
            Coordinate[] vs=new Coordinate[coordinates.length];
884
        if (CGAlgorithms.isCCW(coordinates)){
885
                for (int i=vs.length-1;i>=0;i--){
886
                        vs[i]=coordinates[i];
887
                }
888
        }else{
889
                vs=coordinates;
890
        }
891
        LinearRing ring = geomFactory.createLinearRing(vs);
892

    
893
        try {
894
                        return ShapeFactory.createPolygon2D(toShape(ring));
895
                } catch (NoninvertibleTransformException e) {
896
                        e.printStackTrace();
897
                }
898
                return null;
899
    }
900
    
901
    public static boolean isCCW(Point2D[] points)
902
    {
903
            Coordinate[] vs=new Coordinate[points.length];
904
            for (int i=points.length-1;i>=0;i--){
905
                    vs[i] = new Coordinate(points[i].getX(), points[i].getY());
906
            }
907

    
908
        return CGAlgorithms.isCCW(vs);
909
    }
910

    
911
    public static boolean isCCW(FPolygon2D pol)
912
    {
913
            Geometry jtsGeom = FConverter.java2d_to_jts(pol);
914
            if (jtsGeom.getNumGeometries() == 1)
915
            {
916
                    Coordinate[] coords = jtsGeom.getCoordinates();
917
                    return CGAlgorithms.isCCW(coords);
918
            }
919
            return false;
920

    
921
    }
922
    
923
    
924
    /**
925
     * Return a hole (CCW ordered points)
926
     * @param coordinates
927
     * @return
928
     */
929
    public static IGeometry getHole(Coordinate[] coordinates)
930
    {
931
            // isCCW = true => it's a hole
932
            Coordinate[] vs=new Coordinate[coordinates.length];
933
        if (CGAlgorithms.isCCW(coordinates)){
934
                vs=coordinates;
935

    
936
        }else{
937
                for (int i=vs.length-1;i>=0;i--){
938
                        vs[i]=coordinates[i];
939
                }
940
        }
941
        LinearRing ring = geomFactory.createLinearRing(vs);
942

    
943
        try {
944
                        return ShapeFactory.createPolygon2D(toShape(ring));
945
                } catch (NoninvertibleTransformException e) {
946
                        e.printStackTrace();
947
                }
948
                return null;
949
    }
950

    
951
        public static Shape getExteriorPolygon(GeneralPathX gp) {
952
                Area area = new Area(gp);
953
                area.isSingular();
954
                return area; 
955
                
956

    
957

    
958
        }
959
        /**
960
         * Use it ONLY for NOT multipart polygons.
961
         * @param pol
962
         * @return
963
         */
964
        public static IGeometry getNotHolePolygon(FPolygon2D pol) {
965
                // isCCW == true => hole
966
                Coordinate[] coords;
967
                ArrayList arrayCoords = null;
968
                int theType;
969
                int numParts = 0;
970

    
971
                //                 Use this array to store segment coordinate data
972
                double[] theData = new double[6];
973
                PathIterator theIterator;
974

    
975
                                ArrayList shells = new ArrayList();
976
                                ArrayList holes = new ArrayList();
977
                                Coordinate[] points = null;
978

    
979
                                theIterator = pol.getPathIterator(null, flatness);
980

    
981
                                while (!theIterator.isDone()) {
982
                                        //while not done
983
                                        theType = theIterator.currentSegment(theData);
984

    
985
                                        //Populate a segment of the new
986
                                        // GeneralPathX object.
987
                                        //Process the current segment to populate a new
988
                                        // segment of the new GeneralPathX object.
989
                                        switch (theType) {
990
                                                case PathIterator.SEG_MOVETO:
991

    
992
                                                        // System.out.println("SEG_MOVETO");
993
                                                        if (arrayCoords == null) {
994
                                                                arrayCoords = new ArrayList();
995
                                                        } else {
996
                                                                points = CoordinateArrays.toCoordinateArray(arrayCoords);
997

    
998
                                                                try {
999
                                                                        LinearRing ring = geomFactory.createLinearRing(points);
1000

    
1001
                                                                        if (CGAlgorithms.isCCW(points)) {
1002
                                                                                holes.add(ring);
1003
                                                                        } else {
1004
                                                                                shells.add(ring);
1005
                                                                        }
1006
                                                                } catch (Exception e) {
1007
                                                                        System.err.println(
1008
                                                                                "Caught Topology exception in GMLLinearRingHandler");
1009

    
1010
                                                                        return null;
1011
                                                                }
1012

    
1013
                                                                /* if (numParts == 1)
1014
                                                                   {
1015
                                                                           linRingExt = new GeometryFactory().createLinearRing(
1016
                                                                                  CoordinateArrays.toCoordinateArray(arrayCoords));
1017
                                                                   }
1018
                                                                   else
1019
                                                                   {
1020
                                                                           linRing = new GeometryFactory().createLinearRing(
1021
                                                                                          CoordinateArrays.toCoordinateArray(arrayCoords));
1022
                                                                           arrayLines.add(linRing);
1023
                                                                   } */
1024
                                                                arrayCoords = new ArrayList();
1025
                                                        }
1026

    
1027
                                                        numParts++;
1028
                                                        arrayCoords.add(new Coordinate(theData[0],
1029
                                                                        theData[1]));
1030

    
1031
                                                        break;
1032

    
1033
                                                case PathIterator.SEG_LINETO:
1034

    
1035
                                                        // System.out.println("SEG_LINETO");
1036
                                                        arrayCoords.add(new Coordinate(theData[0],
1037
                                                                        theData[1]));
1038

    
1039
                                                        break;
1040

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

    
1044
                                                        break;
1045

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

    
1049
                                                        break;
1050

    
1051
                                                case PathIterator.SEG_CLOSE:
1052

    
1053
                                                        // A?adimos el primer punto para cerrar.
1054
                                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
1055
                                                        arrayCoords.add(new Coordinate(firstCoord.x,
1056
                                                                        firstCoord.y));
1057

    
1058
                                                        break;
1059
                                        } //end switch
1060

    
1061
                                        // System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
1062
                                        theIterator.next();
1063
                                } //end while loop
1064

    
1065
                                arrayCoords.add(arrayCoords.get(0));
1066
                                coords = CoordinateArrays.toCoordinateArray(arrayCoords);
1067

    
1068

    
1069
                if (numParts == 1)
1070
                {
1071
                        return getExteriorPolygon(coords);
1072
                }
1073
                return ShapeFactory.createGeometry(pol);
1074

    
1075
        }
1076
        
1077

    
1078

    
1079
    /* public static GeometryCollection convertFGeometryCollection(FGeometryCollection fGeomC)
1080
    {
1081

1082
        geomFactory.createGeometryCollection(theGeoms);
1083
    } */
1084
}