Statistics
| Revision:

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

History | View | Annotate | Download (37.9 KB)

1 21308 jiyarza
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2 20761 jmvivo
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23 21308 jiyarza
 *   Av. Blasco Ib??ez, 50
24 20761 jmvivo
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.geom.util;
42
43
import java.awt.Shape;
44
import java.awt.geom.AffineTransform;
45
import java.awt.geom.Area;
46
import java.awt.geom.NoninvertibleTransformException;
47
import java.awt.geom.PathIterator;
48
import java.awt.geom.Rectangle2D;
49
import java.lang.reflect.Array;
50
import java.util.ArrayList;
51
52 38388 nbrodin
import org.gvsig.fmap.geom.Geometry;
53
import org.gvsig.fmap.geom.GeometryLocator;
54
import org.gvsig.fmap.geom.GeometryManager;
55
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
56
import org.gvsig.fmap.geom.Geometry.TYPES;
57
import org.gvsig.fmap.geom.aggregate.MultiCurve;
58
import org.gvsig.fmap.geom.aggregate.MultiPoint;
59
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
60
import org.gvsig.fmap.geom.aggregate.MultiSurface;
61
import org.gvsig.fmap.geom.exception.CreateGeometryException;
62
import org.gvsig.fmap.geom.primitive.GeneralPathX;
63
import org.gvsig.fmap.geom.primitive.Surface;
64
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
66
67 20761 jmvivo
import com.vividsolutions.jts.algorithm.CGAlgorithms;
68
import com.vividsolutions.jts.algorithm.RobustCGAlgorithms;
69
import com.vividsolutions.jts.geom.Coordinate;
70
import com.vividsolutions.jts.geom.CoordinateArrays;
71
import com.vividsolutions.jts.geom.Envelope;
72
import com.vividsolutions.jts.geom.GeometryCollection;
73
import com.vividsolutions.jts.geom.LineString;
74
import com.vividsolutions.jts.geom.LinearRing;
75
import com.vividsolutions.jts.geom.MultiLineString;
76
import com.vividsolutions.jts.geom.MultiPolygon;
77
import com.vividsolutions.jts.geom.Point;
78
import com.vividsolutions.jts.geom.Polygon;
79
80
/**
81 33111 cordinyana
 * Clase con varios m?todos est?ticos utilizados para pasar de java2d a jts
82
 * y viceversa.
83
 *
84 20761 jmvivo
 * @author fjp
85 33111 cordinyana
 * @deprecated to be removed or moved from API to implementation in gvSIG 2.1.0
86 20761 jmvivo
 */
87 20860 jiyarza
public class Converter {
88 38185 jjdelcerro
        private static final GeometryManager geomManager = GeometryLocator
89
                        .getGeometryManager();
90
        private static final Logger logger = LoggerFactory
91
                        .getLogger(Converter.class);
92 28396 jmvivo
93 38185 jjdelcerro
        // private static Logger logger = Logger.getLogger(Converter.class);
94 22287 vcaballero
95 20761 jmvivo
        /**
96 38185 jjdelcerro
         * ?QU? PODEMOS HACER CON LOS MULTIPOINT??? => DEBER?AMOS TRABAJAR CON
97
         * UN ARRAY DE PUNTOS EN FShape.....Pensarlo bien.
98 20761 jmvivo
         */
99 20869 jiyarza
        public final static com.vividsolutions.jts.geom.GeometryFactory geomFactory = new com.vividsolutions.jts.geom.GeometryFactory();
100 20761 jmvivo
        public static CGAlgorithms cga = new RobustCGAlgorithms();
101
        // private final static AffineTransform at = new AffineTransform();
102 38185 jjdelcerro
        // private static double POINT_MARKER_SIZE = 3.0;
103 20761 jmvivo
104 38185 jjdelcerro
        private static GeometryManager manager = GeometryLocator
105
                        .getGeometryManager();
106 20761 jmvivo
107 38185 jjdelcerro
        // returns true if testPoint is a point in the pointList list.
108 20761 jmvivo
        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 38185 jjdelcerro
                        if ((testPoint.x == p.x)
119
                                        && (testPoint.y == p.y)
120
                                        && ((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) // nan
121
                                                                                                                                                                        // test;
122
                                                                                                                                                                        // x!=x
123
                                                                                                                                                                        // iff
124
                                                                                                                                                                        // x
125
                                                                                                                                                                        // is
126
                                                                                                                                                                        // nan
127 20761 jmvivo
                        ) {
128
                                return true;
129
                        }
130
                }
131
132
                return false;
133
        }
134
135
        /**
136
         * Receives a JTS Geometry and returns a fmap IGeometry
137 38185 jjdelcerro
         *
138
         * @param jtsGeometry
139
         *            jts Geometry
140 20761 jmvivo
         * @return IGeometry of FMap
141
         * @author azabala
142 38185 jjdelcerro
         * @throws CreateGeometryException
143 20761 jmvivo
         */
144 38185 jjdelcerro
        public static Geometry jtsToGeometry(
145
                        com.vividsolutions.jts.geom.Geometry geo)
146
                        throws CreateGeometryException {
147 22783 vcaballero
                Geometry shpNew = null;
148
149
                try {
150
                        if (geo instanceof Point) {
151 38185 jjdelcerro
                                shpNew = geomManager.createPoint(((Point) geo).getX(),
152
                                                ((Point) geo).getY(), SUBTYPES.GEOM2D);
153 22783 vcaballero
                        }
154 38388 nbrodin
155
                        if (geo instanceof com.vividsolutions.jts.geom.MultiPoint) {
156
                                shpNew = (MultiPoint)geomManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D);
157
                                Coordinate[] coord = ((com.vividsolutions.jts.geom.MultiPoint)geo).getCoordinates();
158
                                for (int i = 0; i < coord.length; i++) {
159
                                        ((MultiPoint)shpNew).addPoint(geomManager.createPoint(coord[i].x, coord[i].y, SUBTYPES.GEOM2D));
160
                                }
161
                        }
162 22783 vcaballero
163
                        if (geo.isEmpty()) {
164
                                shpNew = null;
165
                        }
166
167 38185 jjdelcerro
                        try {
168
                                if (geo instanceof Polygon) {
169
                                        shpNew = geomManager.createSurface(toShape((Polygon) geo),
170
                                                        SUBTYPES.GEOM2D);
171
                                }
172 22783 vcaballero
173 38185 jjdelcerro
                                if (geo instanceof MultiPolygon) {
174
                                        shpNew = geomManager.createSurface(
175
                                                        toShape((MultiPolygon) geo), SUBTYPES.GEOM2D);
176
                                }
177 22783 vcaballero
178 38185 jjdelcerro
                                if (geo instanceof LineString) {
179
                                        shpNew = geomManager.createCurve(toShape((LineString) geo),
180
                                                        SUBTYPES.GEOM2D);
181
                                }
182 22783 vcaballero
183 38185 jjdelcerro
                                if (geo instanceof MultiLineString) {
184
                                        shpNew = geomManager.createCurve(
185
                                                        toShape((MultiLineString) geo), SUBTYPES.GEOM2D);
186
                                }
187
                        } catch (CreateGeometryException e) {
188 27397 jpiera
                                logger.error("Error creating a geometry", e);
189
                        }
190 22783 vcaballero
191 38185 jjdelcerro
                        /*
192
                         * OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN
193
                         * GeometryCollection No sabremos si queremos una l?nea o un
194
                         * pol?gono..... if (geometry instanceof GeometryCollection) {
195
                         * return toShape((GeometryCollection) geometry); }
196
                         */
197 22783 vcaballero
                        return shpNew;
198
                } catch (NoninvertibleTransformException e) {
199
                        // TODO Auto-generated catch block
200
                        e.printStackTrace();
201
                }
202
203
                return null;
204
205 38185 jjdelcerro
                //
206
                // FShape shape = Converter.jts_to_java2d(jtsGeometry);
207
                // return factory.createGeometry(shape);
208 20761 jmvivo
        }
209
210 20953 jiyarza
        /**
211
         * Convierte un MultiPoint2D a un MultiPoint de JTS
212 38185 jjdelcerro
         *
213 20953 jiyarza
         * @param geom
214
         * @return
215 22287 vcaballero
         */
216 27032 jpiera
        public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPoint geom) {
217 20953 jiyarza
                Coordinate[] theGeoms = new Coordinate[geom.getPrimitivesNumber()];
218
                for (int i = 0; i < theGeoms.length; i++) {
219 38185 jjdelcerro
                        java.awt.geom.Point2D p = geom.getPrimitiveAt(i).getHandlers(
220
                                        Geometry.SELECTHANDLER)[0].getPoint();
221 20953 jiyarza
                        Coordinate c = new Coordinate(p.getX(), p.getY());
222
                        theGeoms[i] = c;
223
                }
224
                com.vividsolutions.jts.geom.MultiPoint geomCol = new com.vividsolutions.jts.geom.GeometryFactory()
225
                                .createMultiPoint(theGeoms);
226
                return geomCol;
227
        }
228 22287 vcaballero
229 20953 jiyarza
        /**
230
         * Convierte una MultiCurve2D en una MultiLineString de JTS
231 38185 jjdelcerro
         *
232 20953 jiyarza
         * @param geom
233
         * @return
234
         */
235 38185 jjdelcerro
        public static com.vividsolutions.jts.geom.Geometry geometryToJts(
236
                        MultiCurve geom) {
237 20953 jiyarza
                LineString[] lines = new LineString[geom.getPrimitivesNumber()];
238 38185 jjdelcerro
                for (int i = 0; i < lines.length; i++) {
239
                        lines[i] = (LineString) geometryToJts((geom.getPrimitiveAt(i)));
240
                }
241
                return new com.vividsolutions.jts.geom.GeometryFactory()
242
                                .createMultiLineString(lines);
243 22287 vcaballero
        }
244
245 20953 jiyarza
        /**
246
         * Convierte una MultiSurface2D en un MultiPolygon de JTS
247 38185 jjdelcerro
         *
248 20953 jiyarza
         * @return
249
         */
250 27030 jpiera
        public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiSurface geom) {
251 20953 jiyarza
                Polygon[] polygons = new Polygon[geom.getPrimitivesNumber()];
252 38185 jjdelcerro
                for (int i = 0; i < polygons.length; i++) {
253
                        polygons[i] = (Polygon) geometryToJts((geom.getPrimitiveAt(i)));
254
                }
255
                return new com.vividsolutions.jts.geom.GeometryFactory()
256
                                .createMultiPolygon(polygons);
257 22287 vcaballero
        }
258 20953 jiyarza
259
        /**
260
         * Convierte una BaseMultiPrimitive en una GeometryCollection de JTS
261 38185 jjdelcerro
         *
262 20953 jiyarza
         * @return
263
         */
264 38185 jjdelcerro
        public com.vividsolutions.jts.geom.Geometry geometryToJts(
265
                        MultiPrimitive geom) {
266
                com.vividsolutions.jts.geom.Geometry[] geometriesAux = new LineString[geom
267
                                .getPrimitivesNumber()];
268 20953 jiyarza
                for (int i = 0; i < geometriesAux.length; i++) {
269 28396 jmvivo
                        geometriesAux[i] = geometryToJts((geom.getPrimitiveAt(i)));
270 20953 jiyarza
                }
271 38185 jjdelcerro
                return new com.vividsolutions.jts.geom.GeometryFactory()
272
                                .createGeometryCollection(geometriesAux);
273 20953 jiyarza
        }
274 22287 vcaballero
275 28396 jmvivo
        public static com.vividsolutions.jts.geom.Geometry geometryToJtsWithSRID(
276
                        Geometry geom, int srid) {
277
                // logger.debug(geom.getClass());
278
                // logger.debug(new Integer(geom.getShapeType()));
279
                return geometryToJts(geom, geom.getType(), srid);
280
        }
281 22287 vcaballero
282 38185 jjdelcerro
        public static com.vividsolutions.jts.geom.Geometry geometryToJts(
283
                        Geometry geom) {
284
                // logger.debug(geom.getClass());
285
                // logger.debug(new Integer(geom.getShapeType()));
286 28396 jmvivo
                return geometryToJts(geom, geom.getType(), -1);
287 20918 jiyarza
        }
288 22287 vcaballero
289 38185 jjdelcerro
        // public static com.vividsolutions.jts.geom.Geometry java2d_to_jts(FShape
290
        // shp) {
291
        // return java2d_to_jts(shp, shp.getShapeType());
292
        // }
293 28627 jmvivo
294 38185 jjdelcerro
        private static boolean isClosed(Coordinate firstCoordinate,
295
                        Coordinate lastCoordinate) {
296 28627 jmvivo
                double diff = Math.abs(lastCoordinate.x - firstCoordinate.x);
297 38185 jjdelcerro
                if (diff > 0.000001) {
298 28627 jmvivo
                        return false;
299
                }
300
                diff = Math.abs(lastCoordinate.y - firstCoordinate.y);
301
                if (diff > 0.000001) {
302
                        return false;
303
                }
304
                return true;
305
        }
306
307 20761 jmvivo
        /**
308
         * Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
309
         * "flattened PathIterator". El flattened indica que las curvas las pasa a
310
         * segmentos de l?nea recta AUTOMATICAMENTE!!!.
311 38185 jjdelcerro
         *
312
         * @param shp
313
         *            FShape que se quiere convertir.
314
         *
315 20761 jmvivo
         * @return Geometry de JTS.
316
         */
317 28396 jmvivo
        private static com.vividsolutions.jts.geom.Geometry geometryToJts(
318
                        Geometry shp, int shapeType, int srid) {
319 20761 jmvivo
320
                com.vividsolutions.jts.geom.Geometry geoJTS = null;
321
                Coordinate coord;
322 38185 jjdelcerro
                // Coordinate[] coords;
323 20761 jmvivo
                ArrayList arrayCoords = null;
324
                ArrayList arrayLines;
325
                LineString lin;
326 38185 jjdelcerro
                // LinearRing linRing;
327
                // LinearRing linRingExt = null;
328 20761 jmvivo
                int theType;
329
                int numParts = 0;
330
331 38185 jjdelcerro
                // Use this array to store segment coordinate data
332 20761 jmvivo
                double[] theData = new double[6];
333
                PathIterator theIterator;
334
335 38185 jjdelcerro
                // logger.debug(shp.toString());
336 22287 vcaballero
337 20918 jiyarza
                switch (shapeType) {
338 28396 jmvivo
                case Geometry.TYPES.POINT:
339 27029 jpiera
                        org.gvsig.fmap.geom.primitive.impl.Point2D p = (org.gvsig.fmap.geom.primitive.impl.Point2D) shp;
340 22783 vcaballero
                        coord = new Coordinate(p.getX(), p.getY());
341
                        geoJTS = geomFactory.createPoint(coord);
342 28396 jmvivo
                        geoJTS.setSRID(srid);
343 20761 jmvivo
344 22783 vcaballero
                        break;
345 20761 jmvivo
346 38185 jjdelcerro
                case Geometry.TYPES.MULTIPOINT:
347
                        org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D mp = (org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D) shp;
348
                        int numPoints = mp.getPrimitivesNumber();
349
                        Coordinate[] coordinates = new Coordinate[numPoints];
350
                        for (int i = 0; i < numPoints; i++) {
351
                                p = mp.getPoint(i);
352
                                coordinates[i] = new Coordinate(p.getX(), p.getY());
353
                        }
354
                        geoJTS = geomFactory.createMultiPoint(coordinates);
355
                        geoJTS.setSRID(srid);
356 33681 fdiaz
357 38185 jjdelcerro
                        break;
358 33681 fdiaz
359 22783 vcaballero
                case Geometry.TYPES.CURVE:
360 28396 jmvivo
                case Geometry.TYPES.ARC:
361 35615 jpiera
                case Geometry.TYPES.SPLINE:
362 22783 vcaballero
                        arrayLines = new ArrayList();
363 30323 jpiera
                        theIterator = shp.getPathIterator(null, manager.getFlatness());
364 20761 jmvivo
365 22783 vcaballero
                        while (!theIterator.isDone()) {
366 38185 jjdelcerro
                                // while not done
367 22783 vcaballero
                                theType = theIterator.currentSegment(theData);
368 20761 jmvivo
369 38185 jjdelcerro
                                // Populate a segment of the new
370 22783 vcaballero
                                // GeneralPathX object.
371 38185 jjdelcerro
                                // Process the current segment to populate a new
372 22783 vcaballero
                                // segment of the new GeneralPathX object.
373
                                switch (theType) {
374
                                case PathIterator.SEG_MOVETO:
375 20761 jmvivo
376 22783 vcaballero
                                        // System.out.println("SEG_MOVETO");
377
                                        if (arrayCoords == null) {
378
                                                arrayCoords = new ArrayList();
379
                                        } else {
380 38185 jjdelcerro
                                                lin = geomFactory.createLineString(CoordinateArrays
381
                                                                .toCoordinateArray(arrayCoords));
382 28396 jmvivo
                                                lin.setSRID(srid);
383 22783 vcaballero
                                                arrayLines.add(lin);
384
                                                arrayCoords = new ArrayList();
385
                                        }
386 20761 jmvivo
387 22783 vcaballero
                                        numParts++;
388 28396 jmvivo
                                        coord = new Coordinate(theData[0], theData[1]);
389 20761 jmvivo
390 28396 jmvivo
                                        arrayCoords.add(coord);
391
392 22783 vcaballero
                                        break;
393 20761 jmvivo
394 22783 vcaballero
                                case PathIterator.SEG_LINETO:
395 20761 jmvivo
396 22783 vcaballero
                                        // System.out.println("SEG_LINETO");
397 38185 jjdelcerro
                                        arrayCoords.add(new Coordinate(theData[0], theData[1]));
398 20761 jmvivo
399 22783 vcaballero
                                        break;
400 20761 jmvivo
401 22783 vcaballero
                                case PathIterator.SEG_QUADTO:
402
                                        System.out.println("Not supported here");
403 20761 jmvivo
404 22783 vcaballero
                                        break;
405 20761 jmvivo
406 22783 vcaballero
                                case PathIterator.SEG_CUBICTO:
407
                                        System.out.println("Not supported here");
408 20761 jmvivo
409 22783 vcaballero
                                        break;
410 20761 jmvivo
411 22783 vcaballero
                                case PathIterator.SEG_CLOSE:
412
                                        // A?adimos el primer punto para cerrar.
413
                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
414 38185 jjdelcerro
                                        // Solo anyadimos cuando no esta ya cerrado
415
                                        arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y));
416 20761 jmvivo
417 22783 vcaballero
                                        break;
418 38185 jjdelcerro
                                } // end switch
419 20761 jmvivo
420 22783 vcaballero
                                theIterator.next();
421 38185 jjdelcerro
                        } // end while loop
422 20761 jmvivo
423 38185 jjdelcerro
                        if (arrayCoords.size() < 2) {
424 22783 vcaballero
                                break;
425 28396 jmvivo
                        }
426 38185 jjdelcerro
                        lin = new com.vividsolutions.jts.geom.GeometryFactory()
427
                                        .createLineString(CoordinateArrays
428
                                                        .toCoordinateArray(arrayCoords));
429 20761 jmvivo
430 28396 jmvivo
                        lin.setSRID(srid);
431 22783 vcaballero
                        // 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 38185 jjdelcerro
                        // {
436 22783 vcaballero
                        arrayLines.add(lin);
437 38185 jjdelcerro
                        geoJTS = geomFactory
438
                                        .createMultiLineString(com.vividsolutions.jts.geom.GeometryFactory
439
                                                        .toLineStringArray(arrayLines));
440 28396 jmvivo
                        geoJTS.setSRID(srid);
441 38185 jjdelcerro
                        /*
442
                         * } else { geoJTS = lin; }
443
                         */
444 20761 jmvivo
445 22783 vcaballero
                        break;
446 20761 jmvivo
447 22783 vcaballero
                case Geometry.TYPES.SURFACE:
448
                case Geometry.TYPES.CIRCLE:
449 28396 jmvivo
                case Geometry.TYPES.ELLIPSE:
450 22783 vcaballero
                        arrayLines = new ArrayList();
451 20761 jmvivo
452 22783 vcaballero
                        ArrayList shells = new ArrayList();
453
                        ArrayList holes = new ArrayList();
454
                        Coordinate[] points = null;
455 20761 jmvivo
456 30323 jpiera
                        theIterator = shp.getPathIterator(null, manager.getFlatness());
457 20761 jmvivo
458 22783 vcaballero
                        while (!theIterator.isDone()) {
459 38185 jjdelcerro
                                // while not done
460 22783 vcaballero
                                theType = theIterator.currentSegment(theData);
461 20761 jmvivo
462 38185 jjdelcerro
                                // Populate a segment of the new
463 22783 vcaballero
                                // GeneralPathX object.
464 38185 jjdelcerro
                                // Process the current segment to populate a new
465 22783 vcaballero
                                // segment of the new GeneralPathX object.
466
                                switch (theType) {
467
                                case PathIterator.SEG_MOVETO:
468 20761 jmvivo
469 22783 vcaballero
                                        // System.out.println("SEG_MOVETO");
470
                                        if (arrayCoords == null) {
471
                                                arrayCoords = new ArrayList();
472
                                        } else {
473 38185 jjdelcerro
                                                points = CoordinateArrays
474
                                                                .toCoordinateArray(arrayCoords);
475 20761 jmvivo
476 22783 vcaballero
                                                try {
477 38185 jjdelcerro
                                                        LinearRing ring = geomFactory
478
                                                                        .createLinearRing(points);
479 20761 jmvivo
480 22783 vcaballero
                                                        if (CGAlgorithms.isCCW(points)) {
481
                                                                holes.add(ring);
482
                                                        } else {
483
                                                                shells.add(ring);
484
                                                        }
485
                                                } catch (Exception e) {
486 38185 jjdelcerro
                                                        /*
487
                                                         * (jaume) caso cuando todos los puntos son iguales
488 22783 vcaballero
                                                         * devuelvo el propio punto
489
                                                         */
490
                                                        boolean same = true;
491 38185 jjdelcerro
                                                        for (int i = 0; i < points.length - 1 && same; i++) {
492
                                                                if (points[i].x != points[i + 1].x
493
                                                                                || points[i].y != points[i + 1].y /*
494
                                                                                                                                                 * ||
495
                                                                                                                                                 * points
496
                                                                                                                                                 * [i].z
497
                                                                                                                                                 * !=
498
                                                                                                                                                 * points
499
                                                                                                                                                 * [
500
                                                                                                                                                 * i+1].
501
                                                                                                                                                 * z
502
                                                                                                                                                 */
503 28396 jmvivo
                                                                ) {
504
                                                                        same = false;
505
                                                                }
506 22783 vcaballero
                                                        }
507 28396 jmvivo
                                                        if (same) {
508 22783 vcaballero
                                                                return geomFactory.createPoint(points[0]);
509 28396 jmvivo
                                                        }
510 22783 vcaballero
                                                        /*
511 38185 jjdelcerro
                                                         * caso cuando es una l?nea de 3 puntos, no creo
512
                                                         * un LinearRing, sino una linea
513 22783 vcaballero
                                                         */
514 38185 jjdelcerro
                                                        if (points.length > 1 && points.length <= 3) {
515 22783 vcaballero
                                                                // return geomFactory.createLineString(points);
516 38185 jjdelcerro
                                                                return geomFactory
517
                                                                                .createMultiLineString(new LineString[] { geomFactory
518
                                                                                                .createLineString(points) });
519 28396 jmvivo
                                                        }
520 20761 jmvivo
521 38185 jjdelcerro
                                                        System.err
522
                                                                        .println("Caught Topology exception in GMLLinearRingHandler");
523 20761 jmvivo
524 22783 vcaballero
                                                        return null;
525
                                                }
526 20761 jmvivo
527 38185 jjdelcerro
                                                /*
528
                                                 * if (numParts == 1) { linRingExt = new
529
                                                 * GeometryFactory().createLinearRing(
530
                                                 * CoordinateArrays.toCoordinateArray(arrayCoords)); }
531
                                                 * else { linRing = new
532
                                                 * GeometryFactory().createLinearRing(
533
                                                 * CoordinateArrays.toCoordinateArray(arrayCoords));
534
                                                 * arrayLines.add(linRing); }
535
                                                 */
536 22783 vcaballero
                                                arrayCoords = new ArrayList();
537
                                        }
538 20761 jmvivo
539 22783 vcaballero
                                        numParts++;
540 38185 jjdelcerro
                                        arrayCoords.add(new Coordinate(theData[0], theData[1]));
541 20761 jmvivo
542 22783 vcaballero
                                        break;
543 20761 jmvivo
544 22783 vcaballero
                                case PathIterator.SEG_LINETO:
545 20761 jmvivo
546 22783 vcaballero
                                        // System.out.println("SEG_LINETO");
547 38185 jjdelcerro
                                        arrayCoords.add(new Coordinate(theData[0], theData[1]));
548 20761 jmvivo
549 22783 vcaballero
                                        break;
550 20761 jmvivo
551 22783 vcaballero
                                case PathIterator.SEG_QUADTO:
552
                                        System.out.println("SEG_QUADTO Not supported here");
553 20761 jmvivo
554 22783 vcaballero
                                        break;
555 20761 jmvivo
556 22783 vcaballero
                                case PathIterator.SEG_CUBICTO:
557
                                        System.out.println("SEG_CUBICTO Not supported here");
558 20761 jmvivo
559 22783 vcaballero
                                        break;
560 20761 jmvivo
561 22783 vcaballero
                                case PathIterator.SEG_CLOSE:
562 20761 jmvivo
563 22783 vcaballero
                                        // A?adimos el primer punto para cerrar.
564
                                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
565 38185 jjdelcerro
                                        arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y));
566 20761 jmvivo
567 22783 vcaballero
                                        break;
568 38185 jjdelcerro
                                } // end switch
569 20761 jmvivo
570 38185 jjdelcerro
                                // System.out.println("theData[0] = " + theData[0] +
571
                                // " theData[1]=" + theData[1]);
572 22783 vcaballero
                                theIterator.next();
573 38185 jjdelcerro
                        } // end while loop
574 20761 jmvivo
575 28627 jmvivo
                        Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
576
                        Coordinate lastCoord = (Coordinate) arrayCoords.get(arrayCoords
577
                                        .size() - 1);
578
                        if (!isClosed(firstCoord, lastCoord)) {
579
                                arrayCoords.add(firstCoord);
580
                        }
581 22783 vcaballero
                        points = CoordinateArrays.toCoordinateArray(arrayCoords);
582 20761 jmvivo
583 22783 vcaballero
                        try {
584
                                LinearRing ring = geomFactory.createLinearRing(points);
585 20761 jmvivo
586 22783 vcaballero
                                if (CGAlgorithms.isCCW(points)) {
587
                                        holes.add(ring);
588
                                } else {
589
                                        shells.add(ring);
590 20761 jmvivo
                                }
591 28396 jmvivo
                                ring.setSRID(srid);
592 22783 vcaballero
                        } catch (Exception e) {
593 38185 jjdelcerro
                                /*
594
                                 * (jaume) caso cuando todos los puntos son iguales devuelvo el
595
                                 * propio punto
596 22783 vcaballero
                                 */
597
                                boolean same = true;
598 38185 jjdelcerro
                                for (int i = 0; i < points.length - 1 && same; i++) {
599
                                        if (points[i].x != points[i + 1].x
600
                                                        || points[i].y != points[i + 1].y /*
601
                                                                                                                         * || points[i].z !=
602
                                                                                                                         * points[i+1].z
603
                                                                                                                         */
604 28396 jmvivo
                                        ) {
605
                                                same = false;
606
                                        }
607 22783 vcaballero
                                }
608 28396 jmvivo
                                if (same) {
609
                                        geoJTS = geomFactory.createPoint(points[0]);
610
                                        geoJTS.setSRID(srid);
611
                                        return geoJTS;
612
                                }
613 22783 vcaballero
                                /*
614 38185 jjdelcerro
                                 * caso cuando es una l?nea de 3 puntos, no creo un
615
                                 * LinearRing, sino una linea
616 22783 vcaballero
                                 */
617 38185 jjdelcerro
                                if (points.length > 1 && points.length <= 3) {
618 22783 vcaballero
                                        // return geomFactory.createLineString(points);
619 28396 jmvivo
                                        geoJTS = geomFactory
620
                                                        .createMultiLineString(new LineString[] { geomFactory
621
                                                                        .createLineString(points) });
622
                                        geoJTS.setSRID(srid);
623
                                        return geoJTS;
624
                                }
625 38185 jjdelcerro
                                System.err
626
                                                .println("Caught Topology exception in GMLLinearRingHandler");
627 20761 jmvivo
628 22783 vcaballero
                                return null;
629
                        }
630 20761 jmvivo
631 38185 jjdelcerro
                        /*
632
                         * linRing = new GeometryFactory().createLinearRing(
633
                         * CoordinateArrays.toCoordinateArray(arrayCoords));
634
                         */
635 20761 jmvivo
636 22783 vcaballero
                        // System.out.println("NumParts = " + numParts);
637 38185 jjdelcerro
                        // now we have a list of all shells and all holes
638 22783 vcaballero
                        ArrayList holesForShells = new ArrayList(shells.size());
639 20761 jmvivo
640 22783 vcaballero
                        for (int i = 0; i < shells.size(); i++) {
641
                                holesForShells.add(new ArrayList());
642
                        }
643 20761 jmvivo
644 38185 jjdelcerro
                        // find homes
645 22783 vcaballero
                        for (int i = 0; i < holes.size(); i++) {
646
                                LinearRing testRing = (LinearRing) holes.get(i);
647
                                LinearRing minShell = null;
648
                                Envelope minEnv = null;
649
                                Envelope testEnv = testRing.getEnvelopeInternal();
650
                                Coordinate testPt = testRing.getCoordinateN(0);
651
                                LinearRing tryRing = null;
652 20761 jmvivo
653 22783 vcaballero
                                for (int j = 0; j < shells.size(); j++) {
654
                                        tryRing = (LinearRing) shells.get(j);
655 20761 jmvivo
656 22783 vcaballero
                                        Envelope tryEnv = tryRing.getEnvelopeInternal();
657 20761 jmvivo
658 22783 vcaballero
                                        if (minShell != null) {
659
                                                minEnv = minShell.getEnvelopeInternal();
660
                                        }
661 20761 jmvivo
662 22783 vcaballero
                                        boolean isContained = false;
663
                                        Coordinate[] coordList = tryRing.getCoordinates();
664 20761 jmvivo
665 38185 jjdelcerro
                                        if (tryEnv.contains(testEnv)
666
                                                        && (CGAlgorithms.isPointInRing(testPt, coordList) || (pointInList(
667
                                                                        testPt, coordList)))) {
668 22783 vcaballero
                                                isContained = true;
669
                                        }
670
671 38185 jjdelcerro
                                        // check if this new containing ring is smaller than the
672
                                        // current minimum ring
673 22783 vcaballero
                                        if (isContained) {
674
                                                if ((minShell == null) || minEnv.contains(tryEnv)) {
675
                                                        minShell = tryRing;
676 20761 jmvivo
                                                }
677
                                        }
678 22783 vcaballero
                                }
679 20761 jmvivo
680 22783 vcaballero
                                if (minShell == null) {
681 38185 jjdelcerro
                                        // System.out.println(
682
                                        // "polygon found with a hole thats not inside a shell");
683
                                        // azabala: we do the assumption that this hole is really a
684
                                        // shell (polygon)
685
                                        // whose point werent digitized in the right order
686 22783 vcaballero
                                        Coordinate[] cs = testRing.getCoordinates();
687
                                        Coordinate[] reversed = new Coordinate[cs.length];
688
                                        int pointIndex = 0;
689 38185 jjdelcerro
                                        for (int z = cs.length - 1; z >= 0; z--) {
690 22783 vcaballero
                                                reversed[pointIndex] = cs[z];
691
                                                pointIndex++;
692 20761 jmvivo
                                        }
693 22783 vcaballero
                                        LinearRing newRing = geomFactory.createLinearRing(reversed);
694
                                        shells.add(newRing);
695
                                        holesForShells.add(new ArrayList());
696
                                } else {
697 38185 jjdelcerro
                                        ((ArrayList) holesForShells.get(shells.indexOf(minShell)))
698
                                                        .add(testRing);
699 20761 jmvivo
                                }
700 22783 vcaballero
                        }
701 20761 jmvivo
702 22783 vcaballero
                        Polygon[] polygons = new Polygon[shells.size()];
703 20761 jmvivo
704 22783 vcaballero
                        for (int i = 0; i < shells.size(); i++) {
705 38185 jjdelcerro
                                polygons[i] = geomFactory.createPolygon((LinearRing) shells
706
                                                .get(i), (LinearRing[]) ((ArrayList) holesForShells
707
                                                .get(i)).toArray(new LinearRing[0]));
708 28396 jmvivo
                                polygons[i].setSRID(srid);
709 22783 vcaballero
                        }
710
                        // CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
711
                        // LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
712
                        // O CON GEOTOOLS
713
                        // if (numParts > 1) // Generamos una MultiLineString
714 20761 jmvivo
715 38185 jjdelcerro
                        /*
716
                         * if (polygons.length == 1) { return polygons[0]; }
717
                         */
718 20761 jmvivo
719 22783 vcaballero
                        // FIN CAMBIO
720 20761 jmvivo
721 22783 vcaballero
                        holesForShells = null;
722
                        shells = null;
723
                        holes = null;
724 20761 jmvivo
725 34137 cordinyana
                        if (polygons.length == 1) {
726
                                geoJTS = polygons[0];
727
                        } else {
728
                                // its a multi part
729
                                geoJTS = geomFactory.createMultiPolygon(polygons);
730
                        }
731 28396 jmvivo
                        geoJTS.setSRID(srid);
732 20761 jmvivo
733 38185 jjdelcerro
                        /*
734
                         * if (numParts > 1) // Generamos un Polygon con agujeros {
735
                         * arrayLines.add(linRing); // geoJTS = new
736
                         * GeometryFactory().createPolygon(linRingExt, //
737
                         * GeometryFactory.toLinearRingArray(arrayLines)); geoJTS = new
738
                         * GeometryFactory().buildGeometry(arrayLines);
739
                         *
740
                         * // geoJTS = Polygonizer.class. } else { geoJTS = new
741
                         * GeometryFactory().createPolygon(linRing,null); }
742
                         */
743 22783 vcaballero
                        break;
744 20761 jmvivo
                }
745
746 28396 jmvivo
                geoJTS.setSRID(srid);
747 20761 jmvivo
                return geoJTS;
748
        }
749
750
        /**
751
         * Converts JTS Geometry objects into Java 2D Shape objects
752 38185 jjdelcerro
         *
753
         * @param geo
754
         *            Geometry de JTS.
755
         *
756 20761 jmvivo
         * @return FShape.
757
         */
758 38185 jjdelcerro
        // public static FShape jts_to_java2d(com.vividsolutions.jts.geom.Geometry
759
        // geo) {
760
        // FShape shpNew = null;
761
        //
762
        // try {
763
        // if (geo instanceof Point) {
764
        // shpNew = new org.gvsig.fmap.geom.primitive.Point2D(null, null, ((Point)
765
        // geo).getX(), ((Point) geo).getY());
766
        // }
767
        //
768
        // if (geo.isEmpty()) {
769
        // shpNew = null;
770
        // }
771
        //
772
        // if (geo instanceof Polygon) {
773
        // shpNew = new Surface2D(null, null, toShape((Polygon) geo));
774
        // }
775
        //
776
        // if (geo instanceof MultiPolygon) {
777
        // shpNew = new Surface2D(null, null, toShape((MultiPolygon) geo));
778
        // }
779
        //
780
        // if (geo instanceof LineString) {
781
        // shpNew = new Curve2D(null, null, toShape((LineString) geo));
782
        // }
783
        //
784
        // if (geo instanceof MultiLineString) {
785
        // shpNew = new Curve2D(null, null, toShape((MultiLineString) geo));
786
        // }
787
        //
788
        // /* OJO: CON ALGO COMO FSHAPE NO S? C?MO PODEMOS IMPLEMENTAR UN
789
        // GeometryCollection
790
        // * No sabremos si queremos una l?nea o un pol?gono.....
791
        // * if (geometry instanceof GeometryCollection) {
792
        // return toShape((GeometryCollection) geometry);
793
        // } */
794
        // return shpNew;
795
        // } catch (NoninvertibleTransformException e) {
796
        // // TODO Auto-generated catch block
797
        // e.printStackTrace();
798
        // }
799
        //
800
        // return null;
801
        // }
802 20761 jmvivo
803
        /**
804
         * DOCUMENT ME!
805 38185 jjdelcerro
         *
806
         * @param p
807
         *            DOCUMENT ME!
808
         *
809 20761 jmvivo
         * @return DOCUMENT ME!
810
         */
811
        private static GeneralPathX toShape(Polygon p) {
812
                GeneralPathX resul = new GeneralPathX();
813
                Coordinate coord;
814
815
                for (int i = 0; i < p.getExteriorRing().getNumPoints(); i++) {
816
                        coord = p.getExteriorRing().getCoordinateN(i);
817
818
                        if (i == 0) {
819 38185 jjdelcerro
                                resul.moveTo(coord.x, coord.y);
820 20761 jmvivo
                        } else {
821 38185 jjdelcerro
                                resul.lineTo(coord.x, coord.y);
822 20761 jmvivo
                        }
823
                }
824
825
                for (int j = 0; j < p.getNumInteriorRing(); j++) {
826
                        LineString hole = p.getInteriorRingN(j);
827
828
                        for (int k = 0; k < hole.getNumPoints(); k++) {
829
                                coord = hole.getCoordinateN(k);
830
831
                                if (k == 0) {
832
                                        resul.moveTo(coord.x, coord.y);
833
                                } else {
834
                                        resul.lineTo(coord.x, coord.y);
835
                                }
836
                        }
837
                }
838
839
                return resul;
840
        }
841
842
        /**
843
         * DOCUMENT ME!
844 38185 jjdelcerro
         *
845
         * @param modelCoordinates
846
         *            DOCUMENT ME!
847
         *
848 20761 jmvivo
         * @return DOCUMENT ME!
849 38185 jjdelcerro
         *
850
         * @throws NoninvertibleTransformException
851
         *             DOCUMENT ME!
852
         *
853
         *             private Coordinate[] toViewCoordinates(Coordinate[]
854
         *             modelCoordinates) throws NoninvertibleTransformException {
855
         *             Coordinate[] viewCoordinates = new
856
         *             Coordinate[modelCoordinates.length];
857
         *
858
         *             for (int i = 0; i < modelCoordinates.length; i++) { FPoint2D
859
         *             point2D = coordinate2FPoint2D(modelCoordinates[i]);
860
         *             viewCoordinates[i] = new Coordinate(point2D.getX(),
861
         *             point2D.getY()); }
862
         *
863
         *             return viewCoordinates; }
864
         * @throws CreateGeometryException
865
         */
866 20761 jmvivo
867 38185 jjdelcerro
        /*
868
         * private Shape toShape(GeometryCollection gc) throws
869
         * NoninvertibleTransformException { GeometryCollectionShape shape = new
870
         * GeometryCollectionShape(); for (int i = 0; i < gc.getNumGeometries();
871
         * i++) { Geometry g = (Geometry) gc.getGeometryN(i); shape.add(toShape(g));
872
         * } return shape; }
873
         */
874 20761 jmvivo
        private static GeneralPathX toShape(MultiLineString mls)
875 38185 jjdelcerro
                        throws NoninvertibleTransformException, CreateGeometryException {
876 20761 jmvivo
                GeneralPathX path = new GeneralPathX();
877
878
                for (int i = 0; i < mls.getNumGeometries(); i++) {
879
                        LineString lineString = (LineString) mls.getGeometryN(i);
880 29972 jldominguez
                        path.append(toShape(lineString).getPathIterator(null), false);
881 20761 jmvivo
                }
882
883 38185 jjdelcerro
                // BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
884
                // converted to GeneralPathXs. [Jon Aquino]
885 20761 jmvivo
                return path;
886
        }
887
888
        /**
889
         * DOCUMENT ME!
890 38185 jjdelcerro
         *
891
         * @param lineString
892
         *            DOCUMENT ME!
893
         *
894 20761 jmvivo
         * @return DOCUMENT ME!
895 38185 jjdelcerro
         *
896
         * @throws NoninvertibleTransformException
897
         *             DOCUMENT ME!
898
         * @throws CreateGeometryException
899 20761 jmvivo
         */
900
        private static GeneralPathX toShape(LineString lineString)
901 38185 jjdelcerro
                        throws NoninvertibleTransformException, CreateGeometryException {
902 20761 jmvivo
                GeneralPathX shape = new GeneralPathX();
903 38185 jjdelcerro
                org.gvsig.fmap.geom.primitive.Point viewPoint = coordinate2FPoint2D(lineString
904
                                .getCoordinateN(0));
905 20761 jmvivo
                shape.moveTo(viewPoint.getX(), viewPoint.getY());
906
907
                for (int i = 1; i < lineString.getNumPoints(); i++) {
908
                        viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(i));
909
                        shape.lineTo(viewPoint.getX(), viewPoint.getY());
910
                }
911
912 38185 jjdelcerro
                // BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
913
                // converted to GeneralPathXs. [Jon Aquino]
914 20761 jmvivo
                return shape;
915
        }
916
917 38185 jjdelcerro
        /*
918
         * TODO No se usa DOCUMENT ME!
919
         *
920 20761 jmvivo
         * @param point DOCUMENT ME!
921 38185 jjdelcerro
         *
922 20761 jmvivo
         * @return DOCUMENT ME!
923 38185 jjdelcerro
         *
924 20761 jmvivo
         * @throws NoninvertibleTransformException DOCUMENT ME!
925 38185 jjdelcerro
         *
926
         * private static Point2D toShape(Point point) throws
927
         * NoninvertibleTransformException { Point2D viewPoint =
928
         * coordinate2FPoint2D(point.getCoordinate());
929
         *
930
         * return viewPoint; }
931
         */
932 20761 jmvivo
933
        /**
934 22287 vcaballero
         *
935 20761 jmvivo
         */
936
        private static GeneralPathX toShape(MultiPolygon mp)
937 38185 jjdelcerro
                        throws NoninvertibleTransformException {
938
                GeneralPathX path = new GeneralPathX();
939 20761 jmvivo
940 38185 jjdelcerro
                for (int i = 0; i < mp.getNumGeometries(); i++) {
941
                        Polygon polygon = (Polygon) mp.getGeometryN(i);
942
                        path.append(toShape(polygon).getPathIterator(null), false);
943
                }
944
945
                // BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
946
                // converted to GeneralPathXs. [Jon Aquino]
947
                return path;
948 20761 jmvivo
        }
949
950
        /**
951
         * DOCUMENT ME!
952 38185 jjdelcerro
         *
953
         * @param coord
954
         *            DOCUMENT ME!
955
         *
956 20761 jmvivo
         * @return DOCUMENT ME!
957 38185 jjdelcerro
         * @throws CreateGeometryException
958 20761 jmvivo
         */
959 38185 jjdelcerro
        public static org.gvsig.fmap.geom.primitive.Point coordinate2FPoint2D(
960
                        Coordinate coord) throws CreateGeometryException {
961
                return geomManager.createPoint(coord.x, coord.y, SUBTYPES.GEOM2D); // ,coord.z);
962 20761 jmvivo
        }
963
964
        /**
965
         * Convierte una Geometry de JTS a GeneralPathX.
966 38185 jjdelcerro
         *
967
         * @param geometry
968
         *            Geometry a convertir.
969
         *
970 20761 jmvivo
         * @return GeneralPathX.
971 38185 jjdelcerro
         *
972 20761 jmvivo
         * @throws NoninvertibleTransformException
973 38185 jjdelcerro
         * @throws CreateGeometryException
974 20761 jmvivo
         * @throws IllegalArgumentException
975
         */
976 38185 jjdelcerro
        public static GeneralPathX toShape(
977
                        com.vividsolutions.jts.geom.Geometry geometry)
978
                        throws NoninvertibleTransformException, CreateGeometryException {
979 20761 jmvivo
                if (geometry.isEmpty()) {
980
                        return new GeneralPathX();
981
                }
982
983
                if (geometry instanceof Polygon) {
984
                        return toShape((Polygon) geometry);
985
                }
986
987
                if (geometry instanceof MultiPolygon) {
988
                        return toShape((MultiPolygon) geometry);
989
                }
990
991
                if (geometry instanceof LineString) {
992
                        return toShape((LineString) geometry);
993
                }
994
995
                if (geometry instanceof MultiLineString) {
996
                        return toShape((MultiLineString) geometry);
997
                }
998
999
                if (geometry instanceof GeometryCollection) {
1000 28396 jmvivo
                        return toShape(geometry);
1001 20761 jmvivo
                }
1002
1003 38185 jjdelcerro
                throw new IllegalArgumentException("Unrecognized Geometry class: "
1004
                                + geometry.getClass());
1005 20761 jmvivo
        }
1006
1007 38185 jjdelcerro
        public static GeneralPathX transformToInts(GeneralPathX gp,
1008
                        AffineTransform at) {
1009
                GeneralPathX newGp = new GeneralPathX();
1010
                PathIterator theIterator;
1011
                int theType;
1012
                int numParts = 0;
1013
                double[] theData = new double[6];
1014
                java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
1015
                java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
1016
                boolean bFirst = true;
1017
                int xInt, yInt, antX = -1, antY = -1;
1018 20761 jmvivo
1019 38185 jjdelcerro
                theIterator = gp.getPathIterator(null); // , flatness);
1020 20761 jmvivo
1021 38185 jjdelcerro
                while (!theIterator.isDone()) {
1022
                        theType = theIterator.currentSegment(theData);
1023
                        switch (theType) {
1024
                        case PathIterator.SEG_MOVETO:
1025
                                numParts++;
1026
                                ptSrc.setLocation(theData[0], theData[1]);
1027
                                at.transform(ptSrc, ptDst);
1028
                                antX = (int) ptDst.getX();
1029
                                antY = (int) ptDst.getY();
1030
                                newGp.moveTo(antX, antY);
1031
                                bFirst = true;
1032
                                break;
1033 20761 jmvivo
1034 38185 jjdelcerro
                        case PathIterator.SEG_LINETO:
1035
                                ptSrc.setLocation(theData[0], theData[1]);
1036
                                at.transform(ptSrc, ptDst);
1037
                                xInt = (int) ptDst.getX();
1038
                                yInt = (int) ptDst.getY();
1039
                                if ((bFirst) || ((xInt != antX) || (yInt != antY))) {
1040
                                        newGp.lineTo(xInt, yInt);
1041
                                        antX = xInt;
1042
                                        antY = yInt;
1043
                                        bFirst = false;
1044
                                }
1045
                                break;
1046 20761 jmvivo
1047 38185 jjdelcerro
                        case PathIterator.SEG_QUADTO:
1048
                                System.out.println("Not supported here");
1049 20761 jmvivo
1050 38185 jjdelcerro
                                break;
1051 20761 jmvivo
1052 38185 jjdelcerro
                        case PathIterator.SEG_CUBICTO:
1053
                                System.out.println("Not supported here");
1054 20761 jmvivo
1055 38185 jjdelcerro
                                break;
1056 20761 jmvivo
1057 38185 jjdelcerro
                        case PathIterator.SEG_CLOSE:
1058
                                newGp.closePath();
1059 20761 jmvivo
1060 38185 jjdelcerro
                                break;
1061
                        } // end switch
1062 20761 jmvivo
1063 38185 jjdelcerro
                        theIterator.next();
1064
                } // end while loop
1065 20761 jmvivo
1066 38185 jjdelcerro
                return newGp;
1067
        }
1068 20761 jmvivo
1069 38185 jjdelcerro
        public static Geometry transformToInts(Geometry gp, AffineTransform at)
1070
                        throws CreateGeometryException {
1071
                GeneralPathX newGp = new GeneralPathX();
1072
                double[] theData = new double[6];
1073
                double[] aux = new double[6];
1074 20761 jmvivo
1075 38185 jjdelcerro
                // newGp.reset();
1076
                PathIterator theIterator;
1077
                int theType;
1078
                int numParts = 0;
1079 20761 jmvivo
1080 38185 jjdelcerro
                java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
1081
                java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
1082
                boolean bFirst = true;
1083
                int xInt, yInt, antX = -1, antY = -1;
1084 20761 jmvivo
1085 38185 jjdelcerro
                theIterator = gp.getPathIterator(null); // , flatness);
1086
                int numSegmentsAdded = 0;
1087
                while (!theIterator.isDone()) {
1088
                        theType = theIterator.currentSegment(theData);
1089 20761 jmvivo
1090 38185 jjdelcerro
                        switch (theType) {
1091
                        case PathIterator.SEG_MOVETO:
1092
                                numParts++;
1093
                                ptSrc.setLocation(theData[0], theData[1]);
1094
                                at.transform(ptSrc, ptDst);
1095
                                antX = (int) ptDst.getX();
1096
                                antY = (int) ptDst.getY();
1097
                                newGp.moveTo(antX, antY);
1098
                                numSegmentsAdded++;
1099
                                bFirst = true;
1100
                                break;
1101 20761 jmvivo
1102 38185 jjdelcerro
                        case PathIterator.SEG_LINETO:
1103
                                ptSrc.setLocation(theData[0], theData[1]);
1104
                                at.transform(ptSrc, ptDst);
1105
                                xInt = (int) ptDst.getX();
1106
                                yInt = (int) ptDst.getY();
1107
                                if ((bFirst) || ((xInt != antX) || (yInt != antY))) {
1108
                                        newGp.lineTo(xInt, yInt);
1109
                                        antX = xInt;
1110
                                        antY = yInt;
1111
                                        bFirst = false;
1112
                                        numSegmentsAdded++;
1113
                                }
1114
                                break;
1115 20761 jmvivo
1116 38185 jjdelcerro
                        case PathIterator.SEG_QUADTO:
1117
                                at.transform(theData, 0, aux, 0, 2);
1118
                                newGp.quadTo(aux[0], aux[1], aux[2], aux[3]);
1119
                                numSegmentsAdded++;
1120
                                break;
1121 20761 jmvivo
1122 38185 jjdelcerro
                        case PathIterator.SEG_CUBICTO:
1123
                                at.transform(theData, 0, aux, 0, 3);
1124
                                newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]);
1125
                                numSegmentsAdded++;
1126
                                break;
1127 20761 jmvivo
1128 38185 jjdelcerro
                        case PathIterator.SEG_CLOSE:
1129
                                if (numSegmentsAdded < 3) {
1130
                                        newGp.lineTo(antX, antY);
1131
                                }
1132
                                newGp.closePath();
1133 20761 jmvivo
1134 38185 jjdelcerro
                                break;
1135
                        } // end switch
1136 20761 jmvivo
1137 38185 jjdelcerro
                        theIterator.next();
1138
                } // end while loop
1139 20761 jmvivo
1140 38185 jjdelcerro
                Geometry shp = null;
1141
                switch (gp.getType()) {
1142
                case Geometry.TYPES.POINT:
1143
                        shp = geomManager.createPoint(ptDst.getX(), ptDst.getY(),
1144
                                        SUBTYPES.GEOM2D);
1145
                        break;
1146 20761 jmvivo
1147 38185 jjdelcerro
                case Geometry.TYPES.CURVE:
1148
                case Geometry.TYPES.ARC:
1149
                        try {
1150
                                shp = geomManager.createCurve(newGp, SUBTYPES.GEOM2D);
1151
                        } catch (CreateGeometryException e1) {
1152
                                logger.error("Error creating a curve", e1);
1153
                        }
1154
                        break;
1155 20761 jmvivo
1156 38185 jjdelcerro
                case Geometry.TYPES.SURFACE:
1157
                case Geometry.TYPES.CIRCLE:
1158
                case Geometry.TYPES.ELLIPSE:
1159 20761 jmvivo
1160 38185 jjdelcerro
                        try {
1161
                                shp = geomManager.createSurface(newGp, SUBTYPES.GEOM2D);
1162
                        } catch (CreateGeometryException e) {
1163
                                logger.error("Error creating a surface", e);
1164
                        }
1165
                        break;
1166
                }
1167
                return shp;
1168
        }
1169 20761 jmvivo
1170 38185 jjdelcerro
        public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR) {
1171
                Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(),
1172
                                jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight());
1173
                return r;
1174
        }
1175 20761 jmvivo
1176 38185 jjdelcerro
        public static Envelope convertEnvelopeToJTS(
1177
                        org.gvsig.fmap.geom.primitive.Envelope r) {
1178
                Envelope e = new Envelope(r.getMinimum(0), r.getMaximum(0),
1179
                                r.getMinimum(1), r.getMaximum(1));
1180
                return e;
1181
        }
1182 20761 jmvivo
1183 38185 jjdelcerro
        /**
1184
         * Return a correct polygon (no hole)
1185
         *
1186
         * @param coordinates
1187
         * @return
1188
         */
1189
        public static Geometry getExteriorPolygon(Coordinate[] coordinates) {
1190
                // isCCW = true => it's a hole
1191
                Coordinate[] vs = new Coordinate[coordinates.length];
1192
                if (CGAlgorithms.isCCW(coordinates)) {
1193
                        for (int i = vs.length - 1; i >= 0; i--) {
1194
                                vs[i] = coordinates[i];
1195
                        }
1196
                } else {
1197
                        vs = coordinates;
1198
                }
1199
                LinearRing ring = geomFactory.createLinearRing(vs);
1200 20761 jmvivo
1201 38185 jjdelcerro
                try {
1202
                        Surface surface = (Surface) manager.create(TYPES.SURFACE,
1203
                                        SUBTYPES.GEOM2D);
1204 26867 jpiera
                        surface.setGeneralPath(toShape(ring));
1205
                        return surface;
1206 20761 jmvivo
                } catch (NoninvertibleTransformException e) {
1207
                        e.printStackTrace();
1208 27397 jpiera
                } catch (CreateGeometryException e) {
1209 26867 jpiera
                        e.printStackTrace();
1210 20761 jmvivo
                }
1211
                return null;
1212 38185 jjdelcerro
        }
1213 20761 jmvivo
1214 38185 jjdelcerro
        public static boolean isCCW(Point[] points) {
1215
                int length = points.length;
1216
                Coordinate[] vs;
1217
                // CGAlgorithms.isCCW asume que la lista de puntos tienen el primer
1218
                // y el ultimo puntos iguales.... y que este algoritmo est? solo
1219
                // garantizado con anillos v?lidos.
1220
                if (points[0].getX() != points[length - 1].getX()
1221
                                || points[0].getY() != points[length - 1].getY()) {
1222
                        vs = new Coordinate[length + 1];
1223
                        vs[points.length] = new Coordinate(points[0].getX(),
1224
                                        points[0].getY());
1225
                } else {
1226
                        vs = new Coordinate[length];
1227
                }
1228
                for (int i = 0; i < length; i++) {
1229
                        vs[i] = new Coordinate(points[i].getX(), points[i].getY());
1230
                }
1231 20761 jmvivo
1232 38185 jjdelcerro
                return CGAlgorithms.isCCW(vs);
1233
        }
1234 20761 jmvivo
1235 38185 jjdelcerro
        public static boolean isCCW(Surface pol) {
1236
                com.vividsolutions.jts.geom.Geometry jtsGeom = Converter
1237
                                .geometryToJts(pol);
1238
                if (jtsGeom.getNumGeometries() == 1) {
1239
                        Coordinate[] coords = jtsGeom.getCoordinates();
1240
                        return CGAlgorithms.isCCW(coords);
1241
                }
1242
                return false;
1243 20761 jmvivo
1244 38185 jjdelcerro
        }
1245 20761 jmvivo
1246 38185 jjdelcerro
        /**
1247
         * Return a hole (CCW ordered points)
1248
         *
1249
         * @param coordinates
1250
         * @return
1251
         */
1252
        public static Geometry getHole(Coordinate[] coordinates) {
1253
                // isCCW = true => it's a hole
1254
                Coordinate[] vs = new Coordinate[coordinates.length];
1255
                if (CGAlgorithms.isCCW(coordinates)) {
1256
                        vs = coordinates;
1257 20761 jmvivo
1258 38185 jjdelcerro
                } else {
1259
                        for (int i = vs.length - 1; i >= 0; i--) {
1260
                                vs[i] = coordinates[i];
1261
                        }
1262
                }
1263
                LinearRing ring = geomFactory.createLinearRing(vs);
1264 20761 jmvivo
1265 38185 jjdelcerro
                try {
1266
                        Surface surface = (Surface) manager.create(TYPES.SURFACE,
1267
                                        SUBTYPES.GEOM2D);
1268
                        surface.setGeneralPath(toShape(ring));
1269 26872 jpiera
                        return surface;
1270 20761 jmvivo
                } catch (NoninvertibleTransformException e) {
1271
                        e.printStackTrace();
1272 27397 jpiera
                } catch (CreateGeometryException e) {
1273 26872 jpiera
                        e.printStackTrace();
1274 20761 jmvivo
                }
1275
                return null;
1276 38185 jjdelcerro
        }
1277 20761 jmvivo
1278
        public static Shape getExteriorPolygon(GeneralPathX gp) {
1279
                Area area = new Area(gp);
1280
                area.isSingular();
1281
                return area;
1282
1283 38185 jjdelcerro
        }
1284 20761 jmvivo
1285
        /**
1286
         * Use it ONLY for NOT multipart polygons.
1287 38185 jjdelcerro
         *
1288 20761 jmvivo
         * @param pol
1289
         * @return
1290
         */
1291 27030 jpiera
        public static Geometry getNotHolePolygon(Surface pol) {
1292 20761 jmvivo
                // isCCW == true => hole
1293
                Coordinate[] coords;
1294
                ArrayList arrayCoords = null;
1295
                int theType;
1296
                int numParts = 0;
1297
1298 38185 jjdelcerro
                // Use this array to store segment coordinate data
1299 20761 jmvivo
                double[] theData = new double[6];
1300
                PathIterator theIterator;
1301
1302 38185 jjdelcerro
                ArrayList shells = new ArrayList();
1303
                ArrayList holes = new ArrayList();
1304
                Coordinate[] points = null;
1305 20761 jmvivo
1306 38185 jjdelcerro
                theIterator = pol.getPathIterator(null, manager.getFlatness());
1307 20761 jmvivo
1308 38185 jjdelcerro
                while (!theIterator.isDone()) {
1309
                        // while not done
1310
                        theType = theIterator.currentSegment(theData);
1311 20761 jmvivo
1312 38185 jjdelcerro
                        // Populate a segment of the new
1313
                        // GeneralPathX object.
1314
                        // Process the current segment to populate a new
1315
                        // segment of the new GeneralPathX object.
1316
                        switch (theType) {
1317
                        case PathIterator.SEG_MOVETO:
1318 20761 jmvivo
1319 38185 jjdelcerro
                                // System.out.println("SEG_MOVETO");
1320
                                if (arrayCoords == null) {
1321
                                        arrayCoords = new ArrayList();
1322
                                } else {
1323
                                        points = CoordinateArrays.toCoordinateArray(arrayCoords);
1324 20761 jmvivo
1325 38185 jjdelcerro
                                        try {
1326
                                                LinearRing ring = geomFactory.createLinearRing(points);
1327 20761 jmvivo
1328 38185 jjdelcerro
                                                if (CGAlgorithms.isCCW(points)) {
1329
                                                        holes.add(ring);
1330
                                                } else {
1331
                                                        shells.add(ring);
1332
                                                }
1333
                                        } catch (Exception e) {
1334
                                                System.err
1335
                                                                .println("Caught Topology exception in GMLLinearRingHandler");
1336 20761 jmvivo
1337 38185 jjdelcerro
                                                return null;
1338
                                        }
1339 20761 jmvivo
1340 38185 jjdelcerro
                                        /*
1341
                                         * if (numParts == 1) { linRingExt = new
1342
                                         * GeometryFactory().createLinearRing(
1343
                                         * CoordinateArrays.toCoordinateArray(arrayCoords)); } else
1344
                                         * { linRing = new GeometryFactory().createLinearRing(
1345
                                         * CoordinateArrays.toCoordinateArray(arrayCoords));
1346
                                         * arrayLines.add(linRing); }
1347
                                         */
1348
                                        arrayCoords = new ArrayList();
1349
                                }
1350 20761 jmvivo
1351 38185 jjdelcerro
                                numParts++;
1352
                                arrayCoords.add(new Coordinate(theData[0], theData[1]));
1353 20761 jmvivo
1354 38185 jjdelcerro
                                break;
1355 20761 jmvivo
1356 38185 jjdelcerro
                        case PathIterator.SEG_LINETO:
1357 20761 jmvivo
1358 38185 jjdelcerro
                                // System.out.println("SEG_LINETO");
1359
                                arrayCoords.add(new Coordinate(theData[0], theData[1]));
1360 20761 jmvivo
1361 38185 jjdelcerro
                                break;
1362 20761 jmvivo
1363 38185 jjdelcerro
                        case PathIterator.SEG_QUADTO:
1364
                                System.out.println("SEG_QUADTO Not supported here");
1365 20761 jmvivo
1366 38185 jjdelcerro
                                break;
1367 20761 jmvivo
1368 38185 jjdelcerro
                        case PathIterator.SEG_CUBICTO:
1369
                                System.out.println("SEG_CUBICTO Not supported here");
1370 20761 jmvivo
1371 38185 jjdelcerro
                                break;
1372 20761 jmvivo
1373 38185 jjdelcerro
                        case PathIterator.SEG_CLOSE:
1374 20761 jmvivo
1375 38185 jjdelcerro
                                // A?adimos el primer punto para cerrar.
1376
                                Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
1377
                                arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y));
1378 20761 jmvivo
1379 38185 jjdelcerro
                                break;
1380
                        } // end switch
1381 20761 jmvivo
1382 38185 jjdelcerro
                        // System.out.println("theData[0] = " + theData[0] + " theData[1]="
1383
                        // + theData[1]);
1384
                        theIterator.next();
1385
                } // end while loop
1386 20761 jmvivo
1387 38185 jjdelcerro
                arrayCoords.add(arrayCoords.get(0));
1388
                coords = CoordinateArrays.toCoordinateArray(arrayCoords);
1389 20761 jmvivo
1390 38185 jjdelcerro
                if (numParts == 1) {
1391 20761 jmvivo
                        return getExteriorPolygon(coords);
1392
                }
1393 22783 vcaballero
                return pol;
1394 20761 jmvivo
1395
        }
1396
1397 38185 jjdelcerro
        /*
1398
         * public static GeometryCollection
1399
         * convertFGeometryCollection(FGeometryCollection fGeomC) {
1400
         *
1401
         * geomFactory.createGeometryCollection(theGeoms); }
1402
         */
1403 20761 jmvivo
}