Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FConverter.java @ 3059

History | View | Annotate | Download (24.3 KB)

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

    
49
import com.iver.cit.gvsig.fmap.core.FGeometry;
50
import com.iver.cit.gvsig.fmap.core.FPoint2D;
51
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
52
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
53
import com.iver.cit.gvsig.fmap.core.FShape;
54
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
55
import com.iver.cit.gvsig.fmap.core.GeneralPathXIterator;
56
import com.iver.cit.gvsig.fmap.core.IGeometry;
57
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
58

    
59
import com.vividsolutions.jts.algorithm.CGAlgorithms;
60
import com.vividsolutions.jts.algorithm.RobustCGAlgorithms;
61
import com.vividsolutions.jts.geom.Coordinate;
62
import com.vividsolutions.jts.geom.CoordinateArrays;
63
import com.vividsolutions.jts.geom.Envelope;
64
import com.vividsolutions.jts.geom.Geometry;
65
import com.vividsolutions.jts.geom.GeometryCollection;
66
import com.vividsolutions.jts.geom.GeometryFactory;
67
import com.vividsolutions.jts.geom.LineString;
68
import com.vividsolutions.jts.geom.LinearRing;
69
import com.vividsolutions.jts.geom.MultiLineString;
70
import com.vividsolutions.jts.geom.MultiPolygon;
71
import com.vividsolutions.jts.geom.Point;
72
import com.vividsolutions.jts.geom.Polygon;
73

    
74
import java.awt.geom.AffineTransform;
75
import java.awt.geom.NoninvertibleTransformException;
76
import java.awt.geom.PathIterator;
77
import java.awt.geom.Point2D;
78
import java.awt.geom.Rectangle2D;
79

    
80
import java.lang.reflect.Array;
81

    
82
import java.util.ArrayList;
83

    
84

    
85
/**
86
 * Clase con varios m?todos est?ticos utilizados para pasar de java2d a jts y
87
 * viceversa.
88
 *
89
 * @author fjp
90
 */
91
public class FConverter {
92
        /**
93
         * ?QU? PODEMOS HACER CON LOS MULTIPOINT??? => DEBER?AMOS TRABAJAR CON UN
94
         * ARRAY DE PUNTOS EN FShape.....Pensarlo bien.
95
         */
96
        private final static GeometryFactory geomFactory = new GeometryFactory();
97
        protected static CGAlgorithms cga = new RobustCGAlgorithms();
98
        private final static AffineTransform at = new AffineTransform();
99
        private static double POINT_MARKER_SIZE = 3.0;
100
    
101
    
102
        private static PointConverter pointConverter = new PointConverter() {
103
                        /* (non-Javadoc)
104
                         * @see com.iver.cit.opensig.fmap.FConverter.PointConverter#toViewPoint(com.vividsolutions.jts.geom.Coordinate)
105
                         */
106
                        public Point2D toViewPoint(Coordinate modelCoordinate)
107
                                throws NoninvertibleTransformException {
108
                                // TODO Auto-generated method stub
109
                                return null;
110
                        }
111
                };
112

    
113
        //returns true if testPoint is a point in the pointList list.
114
        static boolean pointInList(Coordinate testPoint, Coordinate[] pointList) {
115
                int t;
116
                int numpoints;
117
                Coordinate p;
118

    
119
                numpoints = Array.getLength(pointList);
120

    
121
                for (t = 0; t < numpoints; t++) {
122
                        p = pointList[t];
123

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

    
131
                return false;
132
        }
133

    
134
        /**
135
         * Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
136
         * "flattened PathIterator". El flattened indica que las curvas las pasa a
137
         * segmentos de l?nea recta AUTOMATICAMENTE!!!.
138
         *
139
         * @param shp FShape que se quiere convertir.
140
         *
141
         * @return Geometry de JTS.
142
         */
143
        public static Geometry java2d_to_jts(FShape shp) {
144
                double flatness = 0.8; // Por ejemplo. Cuanto m?s peque?o, m?s segmentos necesitar? la curva
145

    
146
                // Es la m?xima distancia que permitimos que el trazo aproximado
147
                // difiera del trazo real.
148
                Geometry geoJTS = null;
149
                Coordinate coord;
150
                Coordinate[] coords;
151
                ArrayList arrayCoords = null;
152
                ArrayList arrayLines;
153
                LineString lin;
154
                LinearRing linRing;
155
                LinearRing linRingExt = null;
156
                int theType;
157
                int numParts = 0;
158

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

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

    
170
                                break;
171

    
172
                        case FShape.LINE:
173
            case FShape.LINE + FShape.Z:                
174
                                arrayLines = new ArrayList();
175
                                theIterator = shp.getPathIterator(null); //, flatness);
176

    
177
                                while (!theIterator.isDone()) {
178
                                        //while not done
179
                                        theType = theIterator.currentSegment(theData);
180

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

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

    
198
                                                        numParts++;
199
                                                        arrayCoords.add(new Coordinate(theData[0],
200
                                                                        theData[1]));
201

    
202
                                                        break;
203

    
204
                                                case PathIterator.SEG_LINETO:
205

    
206
                                                        // System.out.println("SEG_LINETO");
207
                                                        arrayCoords.add(new Coordinate(theData[0],
208
                                                                        theData[1]));
209

    
210
                                                        break;
211

    
212
                                                case PathIterator.SEG_QUADTO:
213
                                                        System.out.println("Not supported here");
214

    
215
                                                        break;
216

    
217
                                                case PathIterator.SEG_CUBICTO:
218
                                                        System.out.println("Not supported here");
219

    
220
                                                        break;
221

    
222
                                                case PathIterator.SEG_CLOSE:
223
                                                        System.out.println("SEG_CLOSE");
224

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

    
230
                                                        break;
231
                                        } //end switch
232

    
233
                                        theIterator.next();
234
                                } //end while loop
235

    
236
                                lin = new GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
237
                                                        arrayCoords));
238

    
239
                                if (numParts > 1) // Generamos una MultiLineString
240
                                 {
241
                                        arrayLines.add(lin);
242
                                        geoJTS = new GeometryFactory().createMultiLineString(GeometryFactory.toLineStringArray(
243
                                                                arrayLines));
244
                                } else {
245
                                        geoJTS = lin;
246
                                }
247

    
248
                                break;
249

    
250
                        case FShape.POLYGON:
251
            case FShape.POLYGON + FShape.Z:                
252
                                arrayLines = new ArrayList();
253

    
254
                                ArrayList shells = new ArrayList();
255
                                ArrayList holes = new ArrayList();
256
                                Coordinate[] points = null;
257

    
258
                                theIterator = shp.getPathIterator(at, flatness);
259

    
260
                                while (!theIterator.isDone()) {
261
                                        //while not done
262
                                        theType = theIterator.currentSegment(theData);
263

    
264
                                        //Populate a segment of the new
265
                                        // GeneralPathX object.
266
                                        //Process the current segment to populate a new
267
                                        // segment of the new GeneralPathX object.
268
                                        switch (theType) {
269
                                                case PathIterator.SEG_MOVETO:
270

    
271
                                                        // System.out.println("SEG_MOVETO");
272
                                                        if (arrayCoords == null) {
273
                                                                arrayCoords = new ArrayList();
274
                                                        } else {
275
                                                                points = CoordinateArrays.toCoordinateArray(arrayCoords);
276

    
277
                                                                try {
278
                                                                        LinearRing ring = geomFactory.createLinearRing(points);
279

    
280
                                                                        if (CGAlgorithms.isCCW(points)) {
281
                                                                                holes.add(ring);
282
                                                                        } else {
283
                                                                                shells.add(ring);
284
                                                                        }
285
                                                                } catch (Exception e) {
286
                                                                        System.err.println(
287
                                                                                "Caught Topology exception in GMLLinearRingHandler");
288

    
289
                                                                        return null;
290
                                                                }
291

    
292
                                                                /* if (numParts == 1)
293
                                                                   {
294
                                                                           linRingExt = new GeometryFactory().createLinearRing(
295
                                                                                  CoordinateArrays.toCoordinateArray(arrayCoords));
296
                                                                   }
297
                                                                   else
298
                                                                   {
299
                                                                           linRing = new GeometryFactory().createLinearRing(
300
                                                                                          CoordinateArrays.toCoordinateArray(arrayCoords));
301
                                                                           arrayLines.add(linRing);
302
                                                                   } */
303
                                                                arrayCoords = new ArrayList();
304
                                                        }
305

    
306
                                                        numParts++;
307
                                                        arrayCoords.add(new Coordinate(theData[0],
308
                                                                        theData[1]));
309

    
310
                                                        break;
311

    
312
                                                case PathIterator.SEG_LINETO:
313

    
314
                                                        // System.out.println("SEG_LINETO");
315
                                                        arrayCoords.add(new Coordinate(theData[0],
316
                                                                        theData[1]));
317

    
318
                                                        break;
319

    
320
                                                case PathIterator.SEG_QUADTO:
321
                                                        System.out.println("SEG_QUADTO Not supported here");
322

    
323
                                                        break;
324

    
325
                                                case PathIterator.SEG_CUBICTO:
326
                                                        System.out.println("SEG_CUBICTO Not supported here");
327

    
328
                                                        break;
329

    
330
                                                case PathIterator.SEG_CLOSE:
331

    
332
                                                        // A?adimos el primer punto para cerrar.
333
                                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
334
                                                        arrayCoords.add(new Coordinate(firstCoord.x,
335
                                                                        firstCoord.y));
336

    
337
                                                        break;
338
                                        } //end switch
339

    
340
                                        // System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
341
                                        theIterator.next();
342
                                } //end while loop
343

    
344
                                // arrayCoords.add(arrayCoords.get(0));
345
                                points = CoordinateArrays.toCoordinateArray(arrayCoords);
346

    
347
                                try {
348
                                        LinearRing ring = geomFactory.createLinearRing(points);
349

    
350
                                        if (CGAlgorithms.isCCW(points)) {
351
                                                holes.add(ring);
352
                                        } else {
353
                                                shells.add(ring);
354
                                        }
355
                                } catch (Exception e) {
356
                                        System.err.println(
357
                                                "Caught Topology exception in GMLLinearRingHandler");
358

    
359
                                        return null;
360
                                }
361

    
362
                                /* linRing = new GeometryFactory().createLinearRing(
363
                                   CoordinateArrays.toCoordinateArray(arrayCoords)); */
364

    
365
                                // System.out.println("NumParts = " + numParts);
366
                                //now we have a list of all shells and all holes
367
                                ArrayList holesForShells = new ArrayList(shells.size());
368

    
369
                                for (int i = 0; i < shells.size(); i++) {
370
                                        holesForShells.add(new ArrayList());
371
                                }
372

    
373
                                //find homes
374
                                for (int i = 0; i < holes.size(); i++) {
375
                                        LinearRing testRing = (LinearRing) holes.get(i);
376
                                        LinearRing minShell = null;
377
                                        Envelope minEnv = null;
378
                                        Envelope testEnv = testRing.getEnvelopeInternal();
379
                                        Coordinate testPt = testRing.getCoordinateN(0);
380
                                        LinearRing tryRing;
381

    
382
                                        for (int j = 0; j < shells.size(); j++) {
383
                                                tryRing = (LinearRing) shells.get(j);
384

    
385
                                                Envelope tryEnv = tryRing.getEnvelopeInternal();
386

    
387
                                                if (minShell != null) {
388
                                                        minEnv = minShell.getEnvelopeInternal();
389
                                                }
390

    
391
                                                boolean isContained = false;
392
                                                Coordinate[] coordList = tryRing.getCoordinates();
393

    
394
                                                if (tryEnv.contains(testEnv) &&
395
                                                                (CGAlgorithms.isPointInRing(testPt, coordList) ||
396
                                                                (pointInList(testPt, coordList)))) {
397
                                                        isContained = true;
398
                                                }
399

    
400
                                                // check if this new containing ring is smaller than the current minimum ring
401
                                                if (isContained) {
402
                                                        if ((minShell == null) || minEnv.contains(tryEnv)) {
403
                                                                minShell = tryRing;
404
                                                        }
405
                                                }
406
                                        }
407

    
408
                                        if (minShell == null) {
409
                                                System.out.println(
410
                                                        "polygon found with a hole thats not inside a shell");
411
                                        } else {
412
                                                ((ArrayList) holesForShells.get(shells.indexOf(minShell))).add(testRing);
413
                                        }
414
                                }
415

    
416
                                Polygon[] polygons = new Polygon[shells.size()];
417

    
418
                                for (int i = 0; i < shells.size(); i++) {
419
                                        polygons[i] = geomFactory.createPolygon((LinearRing) shells.get(
420
                                                                i),
421
                                                        (LinearRing[]) ((ArrayList) holesForShells.get(i)).toArray(
422
                                                                new LinearRing[0]));
423
                                }
424

    
425
                                if (polygons.length == 1) {
426
                                        return polygons[0];
427
                                }
428

    
429
                                holesForShells = null;
430
                                shells = null;
431
                                holes = null;
432

    
433
                                //its a multi part
434
                                geoJTS = geomFactory.createMultiPolygon(polygons);
435

    
436
                                /* if (numParts > 1) // Generamos un Polygon con agujeros
437
                                   {
438
                                    arrayLines.add(linRing);
439
                                           // geoJTS = new GeometryFactory().createPolygon(linRingExt,
440
                                                           // GeometryFactory.toLinearRingArray(arrayLines));
441
                                    geoJTS = new GeometryFactory().buildGeometry(arrayLines);
442
                                
443
                                    // geoJTS = Polygonizer.class.
444
                                   }
445
                                   else
446
                                   {
447
                                           geoJTS = new GeometryFactory().createPolygon(linRing,null);
448
                                   } */
449
                                break;
450
                }
451

    
452
                return geoJTS;
453
        }
454

    
455
        /**
456
         * Converts JTS Geometry objects into Java 2D Shape objects
457
         *
458
         * @param geo Geometry de JTS.
459
         *
460
         * @return FShape.
461
         */
462
        public static FShape jts_to_java2d(Geometry geo) {
463
                FShape shpNew = null;
464

    
465
                try {
466
                        if (geo instanceof Point) {
467
                                shpNew = new FPoint2D(((Point) geo).getX(), ((Point) geo).getY());
468
                        }
469

    
470
                        if (geo.isEmpty()) {
471
                                shpNew = null;
472
                        }
473

    
474
                        if (geo instanceof Polygon) {
475
                                shpNew = new FPolygon2D(toShape((Polygon) geo));
476
                        }
477

    
478
                        if (geo instanceof MultiPolygon) {
479
                                shpNew = new FPolygon2D(toShape((MultiPolygon) geo));
480
                        }
481

    
482
                        if (geo instanceof LineString) {
483
                                shpNew = new FPolyline2D(toShape((LineString) geo));
484
                        }
485

    
486
                        if (geo instanceof MultiLineString) {
487
                                shpNew = new FPolyline2D(toShape((MultiLineString) geo));
488
                        }
489

    
490
                        /* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN GeometryCollection
491
                         * No sabremos si queremos una l?nea o un pol?gono.....
492
                         *  if (geometry instanceof GeometryCollection) {
493
                                  return toShape((GeometryCollection) geometry);
494
                           } */
495
                        return shpNew;
496
                } catch (NoninvertibleTransformException e) {
497
                        // TODO Auto-generated catch block
498
                        e.printStackTrace();
499
                }
500

    
501
                return null;
502
        }
503

    
504
        /**
505
         * DOCUMENT ME!
506
         *
507
         * @param p DOCUMENT ME!
508
         *
509
         * @return DOCUMENT ME!
510
         */
511
        private static GeneralPathX toShape(Polygon p) {
512
                GeneralPathX resul = new GeneralPathX();
513
                Coordinate coord;
514

    
515
                for (int i = 0; i < p.getExteriorRing().getNumPoints(); i++) {
516
                        coord = p.getExteriorRing().getCoordinateN(i);
517

    
518
                        if (i == 0) {
519
                                resul.moveTo(coord.x,coord.y);
520
                        } else {
521
                                resul.lineTo(coord.x,coord.y);
522
                        }
523
                }
524

    
525
                for (int j = 0; j < p.getNumInteriorRing(); j++) {
526
                        LineString hole = p.getInteriorRingN(j);
527

    
528
                        for (int k = 0; k < hole.getNumPoints(); k++) {
529
                                coord = hole.getCoordinateN(k);
530

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

    
539
                return resul;
540
        }
541

    
542
        /**
543
         * DOCUMENT ME!
544
         *
545
         * @param modelCoordinates DOCUMENT ME!
546
         *
547
         * @return DOCUMENT ME!
548
         *
549
         * @throws NoninvertibleTransformException DOCUMENT ME!
550
         */
551
        private Coordinate[] toViewCoordinates(Coordinate[] modelCoordinates)
552
                throws NoninvertibleTransformException {
553
                Coordinate[] viewCoordinates = new Coordinate[modelCoordinates.length];
554

    
555
                for (int i = 0; i < modelCoordinates.length; i++) {
556
                        FPoint2D point2D = toViewPoint(modelCoordinates[i]);
557
                        viewCoordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
558
                }
559

    
560
                return viewCoordinates;
561
        }
562

    
563
        /* private Shape toShape(GeometryCollection gc)
564
           throws NoninvertibleTransformException {
565
           GeometryCollectionShape shape = new GeometryCollectionShape();
566
           for (int i = 0; i < gc.getNumGeometries(); i++) {
567
                   Geometry g = (Geometry) gc.getGeometryN(i);
568
                   shape.add(toShape(g));
569
           }
570
           return shape;
571
           } */
572
        private static GeneralPathX toShape(MultiLineString mls)
573
                throws NoninvertibleTransformException {
574
                GeneralPathX path = new GeneralPathX();
575

    
576
                for (int i = 0; i < mls.getNumGeometries(); i++) {
577
                        LineString lineString = (LineString) mls.getGeometryN(i);
578
                        path.append(toShape(lineString), false);
579
                }
580

    
581
                //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
582
                //converted to GeneralPathXs. [Jon Aquino]
583
                return path;
584
        }
585

    
586
        /**
587
         * DOCUMENT ME!
588
         *
589
         * @param lineString DOCUMENT ME!
590
         *
591
         * @return DOCUMENT ME!
592
         *
593
         * @throws NoninvertibleTransformException DOCUMENT ME!
594
         */
595
        private static GeneralPathX toShape(LineString lineString)
596
                throws NoninvertibleTransformException {
597
                GeneralPathX shape = new GeneralPathX();
598
                FPoint2D viewPoint = toViewPoint(lineString.getCoordinateN(0));
599
                shape.moveTo(viewPoint.getX(), viewPoint.getY());
600

    
601
                for (int i = 1; i < lineString.getNumPoints(); i++) {
602
                        viewPoint = toViewPoint(lineString.getCoordinateN(i));
603
                        shape.lineTo(viewPoint.getX(), viewPoint.getY());
604
                }
605

    
606
                //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
607
                //converted to GeneralPathXs. [Jon Aquino]
608
                return shape;
609
        }
610

    
611
        /**
612
         * DOCUMENT ME!
613
         *
614
         * @param point DOCUMENT ME!
615
         *
616
         * @return DOCUMENT ME!
617
         *
618
         * @throws NoninvertibleTransformException DOCUMENT ME!
619
         */
620
        private static FPoint2D toShape(Point point)
621
                throws NoninvertibleTransformException {
622
                FPoint2D viewPoint = toViewPoint(point.getCoordinate());
623

    
624
                return viewPoint;
625
        }
626
        
627
        private static GeneralPathX toShape(MultiPolygon mp)
628
        throws NoninvertibleTransformException {
629
        GeneralPathX path = new GeneralPathX();
630

    
631
        for (int i = 0; i < mp.getNumGeometries(); i++) {
632
                Polygon polygon = (Polygon) mp.getGeometryN(i);
633
                path.append(toShape(polygon), false);
634
        }
635

    
636
        //BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
637
        //converted to GeneralPathXs. [Jon Aquino]
638
        return path;
639
}
640
        /**
641
         * DOCUMENT ME!
642
         *
643
         * @param coord DOCUMENT ME!
644
         *
645
         * @return DOCUMENT ME!
646
         */
647
        private static FPoint2D toViewPoint(Coordinate coord) {
648
                return new FPoint2D(coord.x, coord.y); //,coord.z);
649
        }
650

    
651
        /**
652
         * Convierte una Geometry de JTS a GeneralPathX.
653
         *
654
         * @param geometry Geometry a convertir.
655
         *
656
         * @return GeneralPathX.
657
         *
658
         * @throws NoninvertibleTransformException 
659
         * @throws IllegalArgumentException
660
         */
661
        public static GeneralPathX toShape(Geometry geometry)
662
                throws NoninvertibleTransformException {
663
                if (geometry.isEmpty()) {
664
                        return new GeneralPathX();
665
                }
666

    
667
                if (geometry instanceof Polygon) {
668
                        return toShape((Polygon) geometry);
669
                }
670

    
671
                if (geometry instanceof MultiPolygon) {
672
                        return toShape((MultiPolygon) geometry);
673
                }
674

    
675
                if (geometry instanceof LineString) {
676
                        return toShape((LineString) geometry);
677
                }
678

    
679
                if (geometry instanceof MultiLineString) {
680
                        return toShape((MultiLineString) geometry);
681
                }
682

    
683
                if (geometry instanceof GeometryCollection) {
684
                        return toShape((GeometryCollection) geometry);
685
                }
686

    
687
                throw new IllegalArgumentException("Unrecognized Geometry class: " +
688
                        geometry.getClass());
689
        }
690

    
691
        /**
692
         * Interfazaz para convertir una Coordinate de JTS a Point2D.
693
         *
694
         */
695
        public static interface PointConverter {
696
                /**
697
                 * DOCUMENT ME!
698
                 *
699
                 * @param modelCoordinate DOCUMENT ME!
700
                 *
701
                 * @return DOCUMENT ME!
702
                 *
703
                 * @throws NoninvertibleTransformException DOCUMENT ME!
704
                 */
705
                public Point2D toViewPoint(Coordinate modelCoordinate)
706
                        throws NoninvertibleTransformException;
707
        }
708
    public static GeneralPathX transformToInts(GeneralPathX gp, AffineTransform at) {
709
        GeneralPathX newGp = new GeneralPathX();
710
        PathIterator theIterator;
711
        int theType;
712
        int numParts = 0;
713
        double[] theData = new double[6];
714
        Point2D ptDst = new Point2D.Double();
715
        Point2D ptSrc = new Point2D.Double();
716
        boolean bFirst = true;
717
        int xInt, yInt, antX = -1, antY = -1;
718
        
719
        theIterator = gp.getPathIterator(null); //, flatness);
720

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

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

    
748
                case PathIterator.SEG_QUADTO:
749
                    System.out.println("Not supported here");
750

    
751
                    break;
752

    
753
                case PathIterator.SEG_CUBICTO:
754
                    System.out.println("Not supported here");
755

    
756
                    break;
757

    
758
                case PathIterator.SEG_CLOSE:
759
                    newGp.closePath();
760

    
761
                    break;
762
            } //end switch
763

    
764
            theIterator.next();
765
        } //end while loop
766
                
767
        return newGp;
768
    }
769
    public static FShape transformToInts(IGeometry gp, AffineTransform at) {
770
        GeneralPathX newGp = new GeneralPathX(); 
771
        double[] theData = new double[6];
772
        double[] aux = new double[6];
773

    
774
        // newGp.reset();
775
        GeneralPathXIterator theIterator;
776
        int theType;
777
        int numParts = 0;
778
        
779
        Point2D ptDst = new Point2D.Double();
780
        Point2D ptSrc = new Point2D.Double();
781
        boolean bFirst = true;
782
        int xInt, yInt, antX = -1, antY = -1;
783
        
784
        
785
        theIterator = gp.getGeneralPathXIterator(); //, flatness);
786
        int numSegmentsAdded = 0;
787
        while (!theIterator.isDone()) {
788
            theType = theIterator.currentSegment(theData);
789
            
790
            switch (theType) {
791
                case PathIterator.SEG_MOVETO:
792
                    numParts++;
793
                    ptSrc.setLocation(theData[0], theData[1]);
794
                    at.transform(ptSrc, ptDst);
795
                    antX = (int) ptDst.getX();
796
                    antY = (int) ptDst.getY();
797
                    newGp.moveTo(antX, antY);
798
                    numSegmentsAdded++;
799
                    bFirst = true;
800
                    break;
801

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

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

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

    
829
                case PathIterator.SEG_CLOSE:
830
                    if (numSegmentsAdded < 3)
831
                        newGp.lineTo(antX, antY);
832
                    newGp.closePath();
833

    
834
                    break;
835
            } //end switch
836

    
837
            theIterator.next();
838
        } //end while loop
839
        FShape shp = null;
840
        switch (gp.getGeometryType())
841
        {
842
            case FShape.POINT: //Tipo punto
843
            case FShape.POINT + FShape.Z:
844
                shp = new FPoint2D(ptDst.getX(), ptDst.getY());
845
                break;
846
    
847
            case FShape.LINE:
848
            case FShape.LINE + FShape.Z: 
849
                shp = new FPolyline2D(newGp);
850
                break;
851
            case FShape.POLYGON:
852
            case FShape.POLYGON + FShape.Z:
853
                shp = new FPolygon2D(newGp);
854
                break;
855
        } 
856
        return shp;
857
    }
858
    
859
    public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR)
860
    {
861
        Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(), 
862
                jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight());
863
        return r;
864
    }
865
}