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