Revision 38185 branches/v2_0_0_prep/libraries/libFMap_geometries/src/org/gvsig/fmap/geom/util/Converter.java

View differences:

Converter.java
86 86
 * @deprecated to be removed or moved from API to implementation in gvSIG 2.1.0
87 87
 */
88 88
public class Converter {
89
	private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
90
	private static final Logger logger = LoggerFactory.getLogger(Converter.class);
89
	private static final GeometryManager geomManager = GeometryLocator
90
			.getGeometryManager();
91
	private static final Logger logger = LoggerFactory
92
			.getLogger(Converter.class);
91 93

  
92
	//private static Logger logger = Logger.getLogger(Converter.class);
94
	// private static Logger logger = Logger.getLogger(Converter.class);
93 95

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

  
103
	private static GeometryManager manager = GeometryLocator.getGeometryManager();
105
	private static GeometryManager manager = GeometryLocator
106
			.getGeometryManager();
104 107

  
105
	//returns true if testPoint is a point in the pointList list.
108
	// returns true if testPoint is a point in the pointList list.
106 109
	static boolean pointInList(Coordinate testPoint, Coordinate[] pointList) {
107 110
		int t;
108 111
		int numpoints;
......
113 116
		for (t = 0; t < numpoints; t++) {
114 117
			p = pointList[t];
115 118

  
116
			if ((testPoint.x == p.x) && (testPoint.y == p.y) &&
117
					((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) //nan test; x!=x iff x is nan
119
			if ((testPoint.x == p.x)
120
					&& (testPoint.y == p.y)
121
					&& ((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) // nan
122
																					// test;
123
																					// x!=x
124
																					// iff
125
																					// x
126
																					// is
127
																					// nan
118 128
			) {
119 129
				return true;
120 130
			}
......
125 135

  
126 136
	/**
127 137
	 * Receives a JTS Geometry and returns a fmap IGeometry
128
	 * @param jtsGeometry jts Geometry
138
	 * 
139
	 * @param jtsGeometry
140
	 *            jts Geometry
129 141
	 * @return IGeometry of FMap
130 142
	 * @author azabala
131
	 * @throws CreateGeometryException 
143
	 * @throws CreateGeometryException
132 144
	 */
133
	public static Geometry jtsToGeometry(com.vividsolutions.jts.geom.Geometry geo) throws CreateGeometryException{
145
	public static Geometry jtsToGeometry(
146
			com.vividsolutions.jts.geom.Geometry geo)
147
			throws CreateGeometryException {
134 148
		Geometry shpNew = null;
135 149

  
136 150
		try {
137 151
			if (geo instanceof Point) {
138
				shpNew = geomManager.createPoint(((Point) geo).getX(),((Point) geo).getY(), SUBTYPES.GEOM2D);
152
				shpNew = geomManager.createPoint(((Point) geo).getX(),
153
						((Point) geo).getY(), SUBTYPES.GEOM2D);
139 154
			}
140 155

  
141 156
			if (geo.isEmpty()) {
142 157
				shpNew = null;
143 158
			}
144 159

  
145
			try{
146
			if (geo instanceof Polygon) {
147
				shpNew = geomManager.createSurface(toShape((Polygon) geo), SUBTYPES.GEOM2D);
148
			}
160
			try {
161
				if (geo instanceof Polygon) {
162
					shpNew = geomManager.createSurface(toShape((Polygon) geo),
163
							SUBTYPES.GEOM2D);
164
				}
149 165

  
150
			if (geo instanceof MultiPolygon) {
151
				shpNew = geomManager.createSurface(toShape((MultiPolygon) geo), SUBTYPES.GEOM2D);
152
			}
166
				if (geo instanceof MultiPolygon) {
167
					shpNew = geomManager.createSurface(
168
							toShape((MultiPolygon) geo), SUBTYPES.GEOM2D);
169
				}
153 170

  
154
			if (geo instanceof LineString) {
155
				shpNew = geomManager.createCurve(toShape((LineString) geo), SUBTYPES.GEOM2D);
156
			}
171
				if (geo instanceof LineString) {
172
					shpNew = geomManager.createCurve(toShape((LineString) geo),
173
							SUBTYPES.GEOM2D);
174
				}
157 175

  
158
			if (geo instanceof MultiLineString) {
159
				shpNew = geomManager.createCurve(toShape((MultiLineString) geo), SUBTYPES.GEOM2D);
160
			}
161
			}catch(CreateGeometryException e){
176
				if (geo instanceof MultiLineString) {
177
					shpNew = geomManager.createCurve(
178
							toShape((MultiLineString) geo), SUBTYPES.GEOM2D);
179
				}
180
			} catch (CreateGeometryException e) {
162 181
				logger.error("Error creating a geometry", e);
163 182
			}
164 183

  
165
			/* OJO: CON ALGO COMO FSHAPE NO S� C�MO PODEMOS IMPLEMENTAR UN GeometryCollection
166
			 * No sabremos si queremos una l�nea o un pol�gono.....
167
			 *  if (geometry instanceof GeometryCollection) {
168
			          return toShape((GeometryCollection) geometry);
169
			   } */
184
			/*
185
			 * OJO: CON ALGO COMO FSHAPE NO S� C�MO PODEMOS IMPLEMENTAR UN
186
			 * GeometryCollection No sabremos si queremos una l�nea o un
187
			 * pol�gono..... if (geometry instanceof GeometryCollection) {
188
			 * return toShape((GeometryCollection) geometry); }
189
			 */
170 190
			return shpNew;
171 191
		} catch (NoninvertibleTransformException e) {
172 192
			// TODO Auto-generated catch block
......
175 195

  
176 196
		return null;
177 197

  
178
//
179
//		FShape shape = Converter.jts_to_java2d(jtsGeometry);
180
//		return factory.createGeometry(shape);
198
		//
199
		// FShape shape = Converter.jts_to_java2d(jtsGeometry);
200
		// return factory.createGeometry(shape);
181 201
	}
182 202

  
183 203
	/**
184 204
	 * Convierte un MultiPoint2D a un MultiPoint de JTS
205
	 * 
185 206
	 * @param geom
186 207
	 * @return
187 208
	 */
188 209
	public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPoint geom) {
189 210
		Coordinate[] theGeoms = new Coordinate[geom.getPrimitivesNumber()];
190 211
		for (int i = 0; i < theGeoms.length; i++) {
191
			java.awt.geom.Point2D p = geom.getPrimitiveAt(i)
192
					.getHandlers(Geometry.SELECTHANDLER)[0].getPoint();
212
			java.awt.geom.Point2D p = geom.getPrimitiveAt(i).getHandlers(
213
					Geometry.SELECTHANDLER)[0].getPoint();
193 214
			Coordinate c = new Coordinate(p.getX(), p.getY());
194 215
			theGeoms[i] = c;
195 216
		}
......
200 221

  
201 222
	/**
202 223
	 * Convierte una MultiCurve2D en una MultiLineString de JTS
224
	 * 
203 225
	 * @param geom
204 226
	 * @return
205 227
	 */
206
	public static com.vividsolutions.jts.geom.Geometry geometryToJts(MultiCurve geom) {
228
	public static com.vividsolutions.jts.geom.Geometry geometryToJts(
229
			MultiCurve geom) {
207 230
		LineString[] lines = new LineString[geom.getPrimitivesNumber()];
208
        for (int i = 0; i < lines.length; i++){
209
        	lines[i] = (LineString) geometryToJts((geom.getPrimitiveAt(i)));
210
        }
211
        return new com.vividsolutions.jts.geom.GeometryFactory().createMultiLineString(lines);
231
		for (int i = 0; i < lines.length; i++) {
232
			lines[i] = (LineString) geometryToJts((geom.getPrimitiveAt(i)));
233
		}
234
		return new com.vividsolutions.jts.geom.GeometryFactory()
235
				.createMultiLineString(lines);
212 236
	}
213 237

  
214 238
	/**
215 239
	 * Convierte una MultiSurface2D en un MultiPolygon de JTS
240
	 * 
216 241
	 * @return
217 242
	 */
218 243
	public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiSurface geom) {
219 244
		Polygon[] polygons = new Polygon[geom.getPrimitivesNumber()];
220
        for (int i = 0; i < polygons.length; i++){
221
        	polygons[i] = (Polygon) geometryToJts((geom.getPrimitiveAt(i)));
222
        }
223
        return new com.vividsolutions.jts.geom.GeometryFactory().createMultiPolygon(polygons);
245
		for (int i = 0; i < polygons.length; i++) {
246
			polygons[i] = (Polygon) geometryToJts((geom.getPrimitiveAt(i)));
247
		}
248
		return new com.vividsolutions.jts.geom.GeometryFactory()
249
				.createMultiPolygon(polygons);
224 250
	}
225 251

  
226 252
	/**
227 253
	 * Convierte una BaseMultiPrimitive en una GeometryCollection de JTS
254
	 * 
228 255
	 * @return
229 256
	 */
230
	public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPrimitive geom) {
231
		com.vividsolutions.jts.geom.Geometry[] geometriesAux = new LineString[geom.getPrimitivesNumber()];
257
	public com.vividsolutions.jts.geom.Geometry geometryToJts(
258
			MultiPrimitive geom) {
259
		com.vividsolutions.jts.geom.Geometry[] geometriesAux = new LineString[geom
260
				.getPrimitivesNumber()];
232 261
		for (int i = 0; i < geometriesAux.length; i++) {
233 262
			geometriesAux[i] = geometryToJts((geom.getPrimitiveAt(i)));
234 263
		}
235
		return new com.vividsolutions.jts.geom.GeometryFactory().createGeometryCollection(geometriesAux);
264
		return new com.vividsolutions.jts.geom.GeometryFactory()
265
				.createGeometryCollection(geometriesAux);
236 266
	}
237 267

  
238 268
	public static com.vividsolutions.jts.geom.Geometry geometryToJtsWithSRID(
......
242 272
		return geometryToJts(geom, geom.getType(), srid);
243 273
	}
244 274

  
245

  
246
	public static com.vividsolutions.jts.geom.Geometry geometryToJts(Geometry geom) {
247
		//logger.debug(geom.getClass());
248
		//logger.debug(new Integer(geom.getShapeType()));
275
	public static com.vividsolutions.jts.geom.Geometry geometryToJts(
276
			Geometry geom) {
277
		// logger.debug(geom.getClass());
278
		// logger.debug(new Integer(geom.getShapeType()));
249 279
		return geometryToJts(geom, geom.getType(), -1);
250 280
	}
251 281

  
252
//	public static com.vividsolutions.jts.geom.Geometry java2d_to_jts(FShape shp) {
253
//		return java2d_to_jts(shp, shp.getShapeType());
254
//	}
282
	// public static com.vividsolutions.jts.geom.Geometry java2d_to_jts(FShape
283
	// shp) {
284
	// return java2d_to_jts(shp, shp.getShapeType());
285
	// }
255 286

  
256
	private static boolean isClosed(Coordinate firstCoordinate, Coordinate lastCoordinate){
287
	private static boolean isClosed(Coordinate firstCoordinate,
288
			Coordinate lastCoordinate) {
257 289
		double diff = Math.abs(lastCoordinate.x - firstCoordinate.x);
258
		if (diff > 0.000001){
290
		if (diff > 0.000001) {
259 291
			return false;
260 292
		}
261 293
		diff = Math.abs(lastCoordinate.y - firstCoordinate.y);
......
269 301
	 * Convierte un FShape a una Geometry del JTS. Para ello, utilizamos un
270 302
	 * "flattened PathIterator". El flattened indica que las curvas las pasa a
271 303
	 * segmentos de l�nea recta AUTOMATICAMENTE!!!.
272
	 *
273
	 * @param shp FShape que se quiere convertir.
274
	 *
304
	 * 
305
	 * @param shp
306
	 *            FShape que se quiere convertir.
307
	 * 
275 308
	 * @return Geometry de JTS.
276 309
	 */
277 310
	private static com.vividsolutions.jts.geom.Geometry geometryToJts(
278 311
			Geometry shp, int shapeType, int srid) {
279 312

  
280

  
281 313
		com.vividsolutions.jts.geom.Geometry geoJTS = null;
282 314
		Coordinate coord;
283
		//Coordinate[] coords;
315
		// Coordinate[] coords;
284 316
		ArrayList arrayCoords = null;
285 317
		ArrayList arrayLines;
286 318
		LineString lin;
287
		//LinearRing linRing;
288
		//LinearRing linRingExt = null;
319
		// LinearRing linRing;
320
		// LinearRing linRingExt = null;
289 321
		int theType;
290 322
		int numParts = 0;
291 323

  
292
		//	 	Use this array to store segment coordinate data
324
		// Use this array to store segment coordinate data
293 325
		double[] theData = new double[6];
294 326
		PathIterator theIterator;
295 327

  
296
		//logger.debug(shp.toString());
328
		// logger.debug(shp.toString());
297 329

  
298

  
299 330
		switch (shapeType) {
300 331
		case Geometry.TYPES.POINT:
301 332
			org.gvsig.fmap.geom.primitive.impl.Point2D p = (org.gvsig.fmap.geom.primitive.impl.Point2D) shp;
......
305 336

  
306 337
			break;
307 338

  
308
	     case Geometry.TYPES.MULTIPOINT:
309
	            org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D mp = (org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D) shp;
310
	            int numPoints = mp.getPrimitivesNumber();
311
	            Coordinate[] coordinates = new Coordinate[numPoints];
312
	            for(int i=0; i<numPoints; i++){
313
	                p = mp.getPoint(i);
314
	                coordinates[i]=new Coordinate(p.getX(), p.getY());
315
	            }
316
	            geoJTS = geomFactory.createMultiPoint(coordinates);
317
	            geoJTS.setSRID(srid);
339
		case Geometry.TYPES.MULTIPOINT:
340
			org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D mp = (org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D) shp;
341
			int numPoints = mp.getPrimitivesNumber();
342
			Coordinate[] coordinates = new Coordinate[numPoints];
343
			for (int i = 0; i < numPoints; i++) {
344
				p = mp.getPoint(i);
345
				coordinates[i] = new Coordinate(p.getX(), p.getY());
346
			}
347
			geoJTS = geomFactory.createMultiPoint(coordinates);
348
			geoJTS.setSRID(srid);
318 349

  
319
	            break;
350
			break;
320 351

  
321 352
		case Geometry.TYPES.CURVE:
322 353
		case Geometry.TYPES.ARC:
......
325 356
			theIterator = shp.getPathIterator(null, manager.getFlatness());
326 357

  
327 358
			while (!theIterator.isDone()) {
328
				//while not done
359
				// while not done
329 360
				theType = theIterator.currentSegment(theData);
330 361

  
331
				//Populate a segment of the new
362
				// Populate a segment of the new
332 363
				// GeneralPathX object.
333
				//Process the current segment to populate a new
364
				// Process the current segment to populate a new
334 365
				// segment of the new GeneralPathX object.
335 366
				switch (theType) {
336 367
				case PathIterator.SEG_MOVETO:
......
339 370
					if (arrayCoords == null) {
340 371
						arrayCoords = new ArrayList();
341 372
					} else {
342
						lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray(
343
								arrayCoords));
373
						lin = geomFactory.createLineString(CoordinateArrays
374
								.toCoordinateArray(arrayCoords));
344 375
						lin.setSRID(srid);
345 376
						arrayLines.add(lin);
346 377
						arrayCoords = new ArrayList();
......
356 387
				case PathIterator.SEG_LINETO:
357 388

  
358 389
					// System.out.println("SEG_LINETO");
359
					arrayCoords.add(new Coordinate(theData[0],
360
							theData[1]));
390
					arrayCoords.add(new Coordinate(theData[0], theData[1]));
361 391

  
362 392
					break;
363 393

  
......
374 404
				case PathIterator.SEG_CLOSE:
375 405
					// A�adimos el primer punto para cerrar.
376 406
					Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
377
						// Solo anyadimos cuando no esta ya cerrado
378
					arrayCoords.add(new Coordinate(firstCoord.x,
379
							firstCoord.y));
407
					// Solo anyadimos cuando no esta ya cerrado
408
					arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y));
380 409

  
381 410
					break;
382
				} //end switch
411
				} // end switch
383 412

  
384 413
				theIterator.next();
385
			} //end while loop
414
			} // end while loop
386 415

  
387
			if (arrayCoords.size()<2) {
416
			if (arrayCoords.size() < 2) {
388 417
				break;
389 418
			}
390
			lin = new com.vividsolutions.jts.geom.GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
391
					arrayCoords));
419
			lin = new com.vividsolutions.jts.geom.GeometryFactory()
420
					.createLineString(CoordinateArrays
421
							.toCoordinateArray(arrayCoords));
392 422

  
393 423
			lin.setSRID(srid);
394 424
			// CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
395 425
			// LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
396 426
			// O CON GEOTOOLS
397 427
			// if (numParts > 1) // Generamos una MultiLineString
398
			//  {
428
			// {
399 429
			arrayLines.add(lin);
400
			geoJTS = geomFactory.createMultiLineString(com.vividsolutions.jts.geom.GeometryFactory.toLineStringArray(
401
					arrayLines));
430
			geoJTS = geomFactory
431
					.createMultiLineString(com.vividsolutions.jts.geom.GeometryFactory
432
							.toLineStringArray(arrayLines));
402 433
			geoJTS.setSRID(srid);
403
			/* } else {
404
			 geoJTS = lin;
405
			 } */
434
			/*
435
			 * } else { geoJTS = lin; }
436
			 */
406 437

  
407 438
			break;
408 439

  
......
418 449
			theIterator = shp.getPathIterator(null, manager.getFlatness());
419 450

  
420 451
			while (!theIterator.isDone()) {
421
				//while not done
452
				// while not done
422 453
				theType = theIterator.currentSegment(theData);
423 454

  
424
				//Populate a segment of the new
455
				// Populate a segment of the new
425 456
				// GeneralPathX object.
426
				//Process the current segment to populate a new
457
				// Process the current segment to populate a new
427 458
				// segment of the new GeneralPathX object.
428 459
				switch (theType) {
429 460
				case PathIterator.SEG_MOVETO:
......
432 463
					if (arrayCoords == null) {
433 464
						arrayCoords = new ArrayList();
434 465
					} else {
435
						points = CoordinateArrays.toCoordinateArray(arrayCoords);
466
						points = CoordinateArrays
467
								.toCoordinateArray(arrayCoords);
436 468

  
437 469
						try {
438
							LinearRing ring = geomFactory.createLinearRing(points);
470
							LinearRing ring = geomFactory
471
									.createLinearRing(points);
439 472

  
440 473
							if (CGAlgorithms.isCCW(points)) {
441 474
								holes.add(ring);
......
443 476
								shells.add(ring);
444 477
							}
445 478
						} catch (Exception e) {
446
							/* (jaume) caso cuando todos los puntos son iguales
479
							/*
480
							 * (jaume) caso cuando todos los puntos son iguales
447 481
							 * devuelvo el propio punto
448 482
							 */
449 483
							boolean same = true;
450
							for (int i = 0; i < points.length-1 && same; i++) {
451
								if (points[i].x != points[i+1].x ||
452
										points[i].y != points[i+1].y /*||
453
										points[i].z != points[i+1].z*/
484
							for (int i = 0; i < points.length - 1 && same; i++) {
485
								if (points[i].x != points[i + 1].x
486
										|| points[i].y != points[i + 1].y /*
487
																		 * ||
488
																		 * points
489
																		 * [i].z
490
																		 * !=
491
																		 * points
492
																		 * [
493
																		 * i+1].
494
																		 * z
495
																		 */
454 496
								) {
455 497
									same = false;
456 498
								}
......
459 501
								return geomFactory.createPoint(points[0]);
460 502
							}
461 503
							/*
462
							 * caso cuando es una l�nea de 3 puntos, no creo un LinearRing, sino
463
							 * una linea
504
							 * caso cuando es una l�nea de 3 puntos, no creo
505
							 * un LinearRing, sino una linea
464 506
							 */
465
							if (points.length>1 && points.length<=3) {
507
							if (points.length > 1 && points.length <= 3) {
466 508
								// return geomFactory.createLineString(points);
467
								return geomFactory.createMultiLineString(new LineString[] {geomFactory.createLineString(points)});
509
								return geomFactory
510
										.createMultiLineString(new LineString[] { geomFactory
511
												.createLineString(points) });
468 512
							}
469 513

  
470
							System.err.println(
471
							"Caught Topology exception in GMLLinearRingHandler");
514
							System.err
515
									.println("Caught Topology exception in GMLLinearRingHandler");
472 516

  
473 517
							return null;
474 518
						}
475 519

  
476
						/* if (numParts == 1)
477
						 {
478
						 linRingExt = new GeometryFactory().createLinearRing(
479
						 CoordinateArrays.toCoordinateArray(arrayCoords));
480
						 }
481
						 else
482
						 {
483
						 linRing = new GeometryFactory().createLinearRing(
484
						 CoordinateArrays.toCoordinateArray(arrayCoords));
485
						 arrayLines.add(linRing);
486
						 } */
520
						/*
521
						 * if (numParts == 1) { linRingExt = new
522
						 * GeometryFactory().createLinearRing(
523
						 * CoordinateArrays.toCoordinateArray(arrayCoords)); }
524
						 * else { linRing = new
525
						 * GeometryFactory().createLinearRing(
526
						 * CoordinateArrays.toCoordinateArray(arrayCoords));
527
						 * arrayLines.add(linRing); }
528
						 */
487 529
						arrayCoords = new ArrayList();
488 530
					}
489 531

  
490 532
					numParts++;
491
					arrayCoords.add(new Coordinate(theData[0],
492
							theData[1]));
533
					arrayCoords.add(new Coordinate(theData[0], theData[1]));
493 534

  
494 535
					break;
495 536

  
496 537
				case PathIterator.SEG_LINETO:
497 538

  
498 539
					// System.out.println("SEG_LINETO");
499
					arrayCoords.add(new Coordinate(theData[0],
500
							theData[1]));
540
					arrayCoords.add(new Coordinate(theData[0], theData[1]));
501 541

  
502 542
					break;
503 543

  
......
515 555

  
516 556
					// A�adimos el primer punto para cerrar.
517 557
					Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
518
					arrayCoords.add(new Coordinate(firstCoord.x,
519
							firstCoord.y));
558
					arrayCoords.add(new Coordinate(firstCoord.x, firstCoord.y));
520 559

  
521 560
					break;
522
				} //end switch
561
				} // end switch
523 562

  
524
				// System.out.println("theData[0] = " + theData[0] + " theData[1]=" + theData[1]);
563
				// System.out.println("theData[0] = " + theData[0] +
564
				// " theData[1]=" + theData[1]);
525 565
				theIterator.next();
526
			} //end while loop
566
			} // end while loop
527 567

  
528

  
529 568
			Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
530 569
			Coordinate lastCoord = (Coordinate) arrayCoords.get(arrayCoords
531 570
					.size() - 1);
......
544 583
				}
545 584
				ring.setSRID(srid);
546 585
			} catch (Exception e) {
547
				/* (jaume) caso cuando todos los puntos son iguales
548
				 * devuelvo el propio punto
586
				/*
587
				 * (jaume) caso cuando todos los puntos son iguales devuelvo el
588
				 * propio punto
549 589
				 */
550 590
				boolean same = true;
551
				for (int i = 0; i < points.length-1 && same; i++) {
552
					if (points[i].x != points[i+1].x ||
553
							points[i].y != points[i+1].y /*||
554
							points[i].z != points[i+1].z*/
591
				for (int i = 0; i < points.length - 1 && same; i++) {
592
					if (points[i].x != points[i + 1].x
593
							|| points[i].y != points[i + 1].y /*
594
															 * || points[i].z !=
595
															 * points[i+1].z
596
															 */
555 597
					) {
556 598
						same = false;
557 599
					}
......
562 604
					return geoJTS;
563 605
				}
564 606
				/*
565
				 * caso cuando es una l�nea de 3 puntos, no creo un LinearRing, sino
566
				 * una linea
607
				 * caso cuando es una l�nea de 3 puntos, no creo un
608
				 * LinearRing, sino una linea
567 609
				 */
568
				if (points.length>1 && points.length<=3) {
610
				if (points.length > 1 && points.length <= 3) {
569 611
					// return geomFactory.createLineString(points);
570 612
					geoJTS = geomFactory
571 613
							.createMultiLineString(new LineString[] { geomFactory
......
573 615
					geoJTS.setSRID(srid);
574 616
					return geoJTS;
575 617
				}
576
				System.err.println(
577
				"Caught Topology exception in GMLLinearRingHandler");
618
				System.err
619
						.println("Caught Topology exception in GMLLinearRingHandler");
578 620

  
579 621
				return null;
580 622
			}
581 623

  
582
			/* linRing = new GeometryFactory().createLinearRing(
583
			 CoordinateArrays.toCoordinateArray(arrayCoords)); */
624
			/*
625
			 * linRing = new GeometryFactory().createLinearRing(
626
			 * CoordinateArrays.toCoordinateArray(arrayCoords));
627
			 */
584 628

  
585 629
			// System.out.println("NumParts = " + numParts);
586
			//now we have a list of all shells and all holes
630
			// now we have a list of all shells and all holes
587 631
			ArrayList holesForShells = new ArrayList(shells.size());
588 632

  
589 633
			for (int i = 0; i < shells.size(); i++) {
590 634
				holesForShells.add(new ArrayList());
591 635
			}
592 636

  
593
			//find homes
637
			// find homes
594 638
			for (int i = 0; i < holes.size(); i++) {
595 639
				LinearRing testRing = (LinearRing) holes.get(i);
596 640
				LinearRing minShell = null;
......
611 655
					boolean isContained = false;
612 656
					Coordinate[] coordList = tryRing.getCoordinates();
613 657

  
614
					if (tryEnv.contains(testEnv) &&
615
							(CGAlgorithms.isPointInRing(testPt, coordList) ||
616
									(pointInList(testPt, coordList)))) {
658
					if (tryEnv.contains(testEnv)
659
							&& (CGAlgorithms.isPointInRing(testPt, coordList) || (pointInList(
660
									testPt, coordList)))) {
617 661
						isContained = true;
618 662
					}
619 663

  
620
					// check if this new containing ring is smaller than the current minimum ring
664
					// check if this new containing ring is smaller than the
665
					// current minimum ring
621 666
					if (isContained) {
622 667
						if ((minShell == null) || minEnv.contains(tryEnv)) {
623 668
							minShell = tryRing;
......
626 671
				}
627 672

  
628 673
				if (minShell == null) {
629
//					System.out.println(
630
//					"polygon found with a hole thats not inside a shell");
631
//					azabala: we do the assumption that this hole is really a shell (polygon)
632
//					whose point werent digitized in the right order
674
					// System.out.println(
675
					// "polygon found with a hole thats not inside a shell");
676
					// azabala: we do the assumption that this hole is really a
677
					// shell (polygon)
678
					// whose point werent digitized in the right order
633 679
					Coordinate[] cs = testRing.getCoordinates();
634 680
					Coordinate[] reversed = new Coordinate[cs.length];
635 681
					int pointIndex = 0;
636
					for(int z = cs.length-1; z >= 0; z--){
682
					for (int z = cs.length - 1; z >= 0; z--) {
637 683
						reversed[pointIndex] = cs[z];
638 684
						pointIndex++;
639 685
					}
......
641 687
					shells.add(newRing);
642 688
					holesForShells.add(new ArrayList());
643 689
				} else {
644
					((ArrayList) holesForShells.get(shells.indexOf(minShell))).add(testRing);
690
					((ArrayList) holesForShells.get(shells.indexOf(minShell)))
691
							.add(testRing);
645 692
				}
646 693
			}
647 694

  
648 695
			Polygon[] polygons = new Polygon[shells.size()];
649 696

  
650 697
			for (int i = 0; i < shells.size(); i++) {
651
				polygons[i] = geomFactory.createPolygon((LinearRing) shells.get(
652
						i),
653
						(LinearRing[]) ((ArrayList) holesForShells.get(i)).toArray(
654
								new LinearRing[0]));
698
				polygons[i] = geomFactory.createPolygon((LinearRing) shells
699
						.get(i), (LinearRing[]) ((ArrayList) holesForShells
700
						.get(i)).toArray(new LinearRing[0]));
655 701
				polygons[i].setSRID(srid);
656 702
			}
657 703
			// CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
......
659 705
			// O CON GEOTOOLS
660 706
			// if (numParts > 1) // Generamos una MultiLineString
661 707

  
662
			/* if (polygons.length == 1) {
663
			 return polygons[0];
664
			 } */
708
			/*
709
			 * if (polygons.length == 1) { return polygons[0]; }
710
			 */
665 711

  
666 712
			// FIN CAMBIO
667 713

  
......
677 723
			}
678 724
			geoJTS.setSRID(srid);
679 725

  
680
			/* if (numParts > 1) // Generamos un Polygon con agujeros
681
			 {
682
			 arrayLines.add(linRing);
683
			 // geoJTS = new GeometryFactory().createPolygon(linRingExt,
684
			  // GeometryFactory.toLinearRingArray(arrayLines));
685
			   geoJTS = new GeometryFactory().buildGeometry(arrayLines);
686

  
687
			   // geoJTS = Polygonizer.class.
688
			    }
689
			    else
690
			    {
691
			    geoJTS = new GeometryFactory().createPolygon(linRing,null);
692
			    } */
726
			/*
727
			 * if (numParts > 1) // Generamos un Polygon con agujeros {
728
			 * arrayLines.add(linRing); // geoJTS = new
729
			 * GeometryFactory().createPolygon(linRingExt, //
730
			 * GeometryFactory.toLinearRingArray(arrayLines)); geoJTS = new
731
			 * GeometryFactory().buildGeometry(arrayLines);
732
			 * 
733
			 * // geoJTS = Polygonizer.class. } else { geoJTS = new
734
			 * GeometryFactory().createPolygon(linRing,null); }
735
			 */
693 736
			break;
694 737
		}
695 738

  
......
699 742

  
700 743
	/**
701 744
	 * Converts JTS Geometry objects into Java 2D Shape objects
702
	 *
703
	 * @param geo Geometry de JTS.
704
	 *
745
	 * 
746
	 * @param geo
747
	 *            Geometry de JTS.
748
	 * 
705 749
	 * @return FShape.
706 750
	 */
707
//	public static FShape jts_to_java2d(com.vividsolutions.jts.geom.Geometry geo) {
708
//		FShape shpNew = null;
709
//
710
//		try {
711
//			if (geo instanceof Point) {
712
//				shpNew = new org.gvsig.fmap.geom.primitive.Point2D(null, null, ((Point) geo).getX(), ((Point) geo).getY());
713
//			}
714
//
715
//			if (geo.isEmpty()) {
716
//				shpNew = null;
717
//			}
718
//
719
//			if (geo instanceof Polygon) {
720
//				shpNew = new Surface2D(null, null, toShape((Polygon) geo));
721
//			}
722
//
723
//			if (geo instanceof MultiPolygon) {
724
//				shpNew = new Surface2D(null, null, toShape((MultiPolygon) geo));
725
//			}
726
//
727
//			if (geo instanceof LineString) {
728
//				shpNew = new Curve2D(null, null, toShape((LineString) geo));
729
//			}
730
//
731
//			if (geo instanceof MultiLineString) {
732
//				shpNew = new Curve2D(null, null, toShape((MultiLineString) geo));
733
//			}
734
//
735
//			/* OJO: CON ALGO COMO FSHAPE NO S� C�MO PODEMOS IMPLEMENTAR UN GeometryCollection
736
//			 * No sabremos si queremos una l�nea o un pol�gono.....
737
//			 *  if (geometry instanceof GeometryCollection) {
738
//			          return toShape((GeometryCollection) geometry);
739
//			   } */
740
//			return shpNew;
741
//		} catch (NoninvertibleTransformException e) {
742
//			// TODO Auto-generated catch block
743
//			e.printStackTrace();
744
//		}
745
//
746
//		return null;
747
//	}
751
	// public static FShape jts_to_java2d(com.vividsolutions.jts.geom.Geometry
752
	// geo) {
753
	// FShape shpNew = null;
754
	//
755
	// try {
756
	// if (geo instanceof Point) {
757
	// shpNew = new org.gvsig.fmap.geom.primitive.Point2D(null, null, ((Point)
758
	// geo).getX(), ((Point) geo).getY());
759
	// }
760
	//
761
	// if (geo.isEmpty()) {
762
	// shpNew = null;
763
	// }
764
	//
765
	// if (geo instanceof Polygon) {
766
	// shpNew = new Surface2D(null, null, toShape((Polygon) geo));
767
	// }
768
	//
769
	// if (geo instanceof MultiPolygon) {
770
	// shpNew = new Surface2D(null, null, toShape((MultiPolygon) geo));
771
	// }
772
	//
773
	// if (geo instanceof LineString) {
774
	// shpNew = new Curve2D(null, null, toShape((LineString) geo));
775
	// }
776
	//
777
	// if (geo instanceof MultiLineString) {
778
	// shpNew = new Curve2D(null, null, toShape((MultiLineString) geo));
779
	// }
780
	//
781
	// /* OJO: CON ALGO COMO FSHAPE NO S� C�MO PODEMOS IMPLEMENTAR UN
782
	// GeometryCollection
783
	// * No sabremos si queremos una l�nea o un pol�gono.....
784
	// * if (geometry instanceof GeometryCollection) {
785
	// return toShape((GeometryCollection) geometry);
786
	// } */
787
	// return shpNew;
788
	// } catch (NoninvertibleTransformException e) {
789
	// // TODO Auto-generated catch block
790
	// e.printStackTrace();
791
	// }
792
	//
793
	// return null;
794
	// }
748 795

  
749 796
	/**
750 797
	 * DOCUMENT ME!
751
	 *
752
	 * @param p DOCUMENT ME!
753
	 *
798
	 * 
799
	 * @param p
800
	 *            DOCUMENT ME!
801
	 * 
754 802
	 * @return DOCUMENT ME!
755 803
	 */
756 804
	private static GeneralPathX toShape(Polygon p) {
......
761 809
			coord = p.getExteriorRing().getCoordinateN(i);
762 810

  
763 811
			if (i == 0) {
764
				resul.moveTo(coord.x,coord.y);
812
				resul.moveTo(coord.x, coord.y);
765 813
			} else {
766
				resul.lineTo(coord.x,coord.y);
814
				resul.lineTo(coord.x, coord.y);
767 815
			}
768 816
		}
769 817

  
......
786 834

  
787 835
	/**
788 836
	 * DOCUMENT ME!
789
	 *
790
	 * @param modelCoordinates DOCUMENT ME!
791
	 *
837
	 * 
838
	 * @param modelCoordinates
839
	 *            DOCUMENT ME!
840
	 * 
792 841
	 * @return DOCUMENT ME!
793
	 *
794
	 * @throws NoninvertibleTransformException DOCUMENT ME!
795
	 *
796
	private Coordinate[] toViewCoordinates(Coordinate[] modelCoordinates)
797
		throws NoninvertibleTransformException {
798
		Coordinate[] viewCoordinates = new Coordinate[modelCoordinates.length];
842
	 * 
843
	 * @throws NoninvertibleTransformException
844
	 *             DOCUMENT ME!
845
	 * 
846
	 *             private Coordinate[] toViewCoordinates(Coordinate[]
847
	 *             modelCoordinates) throws NoninvertibleTransformException {
848
	 *             Coordinate[] viewCoordinates = new
849
	 *             Coordinate[modelCoordinates.length];
850
	 * 
851
	 *             for (int i = 0; i < modelCoordinates.length; i++) { FPoint2D
852
	 *             point2D = coordinate2FPoint2D(modelCoordinates[i]);
853
	 *             viewCoordinates[i] = new Coordinate(point2D.getX(),
854
	 *             point2D.getY()); }
855
	 * 
856
	 *             return viewCoordinates; }
857
	 * @throws CreateGeometryException
858
	 */
799 859

  
800
		for (int i = 0; i < modelCoordinates.length; i++) {
801
			FPoint2D point2D = coordinate2FPoint2D(modelCoordinates[i]);
802
			viewCoordinates[i] = new Coordinate(point2D.getX(), point2D.getY());
803
		}
804

  
805
		return viewCoordinates;
806
	} 
807
	 * @throws CreateGeometryException */
808

  
809
	/* private Shape toShape(GeometryCollection gc)
810
	   throws NoninvertibleTransformException {
811
	   GeometryCollectionShape shape = new GeometryCollectionShape();
812
	   for (int i = 0; i < gc.getNumGeometries(); i++) {
813
	           Geometry g = (Geometry) gc.getGeometryN(i);
814
	           shape.add(toShape(g));
815
	   }
816
	   return shape;
817
	   } */
860
	/*
861
	 * private Shape toShape(GeometryCollection gc) throws
862
	 * NoninvertibleTransformException { GeometryCollectionShape shape = new
863
	 * GeometryCollectionShape(); for (int i = 0; i < gc.getNumGeometries();
864
	 * i++) { Geometry g = (Geometry) gc.getGeometryN(i); shape.add(toShape(g));
865
	 * } return shape; }
866
	 */
818 867
	private static GeneralPathX toShape(MultiLineString mls)
819
		throws NoninvertibleTransformException, CreateGeometryException {
868
			throws NoninvertibleTransformException, CreateGeometryException {
820 869
		GeneralPathX path = new GeneralPathX();
821 870

  
822 871
		for (int i = 0; i < mls.getNumGeometries(); i++) {
......
824 873
			path.append(toShape(lineString).getPathIterator(null), false);
825 874
		}
826 875

  
827
		//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
828
		//converted to GeneralPathXs. [Jon Aquino]
876
		// BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
877
		// converted to GeneralPathXs. [Jon Aquino]
829 878
		return path;
830 879
	}
831 880

  
832 881
	/**
833 882
	 * DOCUMENT ME!
834
	 *
835
	 * @param lineString DOCUMENT ME!
836
	 *
883
	 * 
884
	 * @param lineString
885
	 *            DOCUMENT ME!
886
	 * 
837 887
	 * @return DOCUMENT ME!
838
	 *
839
	 * @throws NoninvertibleTransformException DOCUMENT ME!
840
	 * @throws CreateGeometryException 
888
	 * 
889
	 * @throws NoninvertibleTransformException
890
	 *             DOCUMENT ME!
891
	 * @throws CreateGeometryException
841 892
	 */
842 893
	private static GeneralPathX toShape(LineString lineString)
843
		throws NoninvertibleTransformException, CreateGeometryException {
894
			throws NoninvertibleTransformException, CreateGeometryException {
844 895
		GeneralPathX shape = new GeneralPathX();
845
		org.gvsig.fmap.geom.primitive.Point viewPoint = coordinate2FPoint2D(lineString.getCoordinateN(0));
896
		org.gvsig.fmap.geom.primitive.Point viewPoint = coordinate2FPoint2D(lineString
897
				.getCoordinateN(0));
846 898
		shape.moveTo(viewPoint.getX(), viewPoint.getY());
847 899

  
848 900
		for (int i = 1; i < lineString.getNumPoints(); i++) {
......
850 902
			shape.lineTo(viewPoint.getX(), viewPoint.getY());
851 903
		}
852 904

  
853
		//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
854
		//converted to GeneralPathXs. [Jon Aquino]
905
		// BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
906
		// converted to GeneralPathXs. [Jon Aquino]
855 907
		return shape;
856 908
	}
857 909

  
858
	/* TODO No se usa
859
	 * DOCUMENT ME!
860
	 *
910
	/*
911
	 * TODO No se usa DOCUMENT ME!
912
	 * 
861 913
	 * @param point DOCUMENT ME!
862
	 *
914
	 * 
863 915
	 * @return DOCUMENT ME!
864
	 *
916
	 * 
865 917
	 * @throws NoninvertibleTransformException DOCUMENT ME!
866
	 *
867
	private static Point2D toShape(Point point)
868
		throws NoninvertibleTransformException {
869
		Point2D viewPoint = coordinate2FPoint2D(point.getCoordinate());
918
	 * 
919
	 * private static Point2D toShape(Point point) throws
920
	 * NoninvertibleTransformException { Point2D viewPoint =
921
	 * coordinate2FPoint2D(point.getCoordinate());
922
	 * 
923
	 * return viewPoint; }
924
	 */
870 925

  
871
		return viewPoint;
872
	}
873
*/
874

  
875 926
	/**
876 927
	 *
877 928
	 */
878 929
	private static GeneralPathX toShape(MultiPolygon mp)
879
	throws NoninvertibleTransformException {
880
	GeneralPathX path = new GeneralPathX();
930
			throws NoninvertibleTransformException {
931
		GeneralPathX path = new GeneralPathX();
881 932

  
882
	for (int i = 0; i < mp.getNumGeometries(); i++) {
883
		Polygon polygon = (Polygon) mp.getGeometryN(i);
884
		path.append(toShape(polygon).getPathIterator(null), false);
933
		for (int i = 0; i < mp.getNumGeometries(); i++) {
934
			Polygon polygon = (Polygon) mp.getGeometryN(i);
935
			path.append(toShape(polygon).getPathIterator(null), false);
936
		}
937

  
938
		// BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
939
		// converted to GeneralPathXs. [Jon Aquino]
940
		return path;
885 941
	}
886 942

  
887
	//BasicFeatureRenderer expects LineStrings and MultiLineStrings to be
888
	//converted to GeneralPathXs. [Jon Aquino]
889
	return path;
890
}
891 943
	/**
892 944
	 * DOCUMENT ME!
893
	 *
894
	 * @param coord DOCUMENT ME!
895
	 *
945
	 * 
946
	 * @param coord
947
	 *            DOCUMENT ME!
948
	 * 
896 949
	 * @return DOCUMENT ME!
897
	 * @throws CreateGeometryException 
950
	 * @throws CreateGeometryException
898 951
	 */
899
	public static org.gvsig.fmap.geom.primitive.Point coordinate2FPoint2D(Coordinate coord) throws CreateGeometryException {
900
		return geomManager.createPoint(coord.x, coord.y, SUBTYPES.GEOM2D); //,coord.z);
952
	public static org.gvsig.fmap.geom.primitive.Point coordinate2FPoint2D(
953
			Coordinate coord) throws CreateGeometryException {
954
		return geomManager.createPoint(coord.x, coord.y, SUBTYPES.GEOM2D); // ,coord.z);
901 955
	}
902 956

  
903 957
	/**
904 958
	 * Convierte una Geometry de JTS a GeneralPathX.
905
	 *
906
	 * @param geometry Geometry a convertir.
907
	 *
959
	 * 
960
	 * @param geometry
961
	 *            Geometry a convertir.
962
	 * 
908 963
	 * @return GeneralPathX.
909
	 *
964
	 * 
910 965
	 * @throws NoninvertibleTransformException
911
	 * @throws CreateGeometryException 
966
	 * @throws CreateGeometryException
912 967
	 * @throws IllegalArgumentException
913 968
	 */
914
	public static GeneralPathX toShape(com.vividsolutions.jts.geom.Geometry geometry)
915
		throws NoninvertibleTransformException, CreateGeometryException {
969
	public static GeneralPathX toShape(
970
			com.vividsolutions.jts.geom.Geometry geometry)
971
			throws NoninvertibleTransformException, CreateGeometryException {
916 972
		if (geometry.isEmpty()) {
917 973
			return new GeneralPathX();
918 974
		}
......
937 993
			return toShape(geometry);
938 994
		}
939 995

  
940
		throw new IllegalArgumentException("Unrecognized Geometry class: " +
941
			geometry.getClass());
996
		throw new IllegalArgumentException("Unrecognized Geometry class: "
997
				+ geometry.getClass());
942 998
	}
943 999

  
1000
	public static GeneralPathX transformToInts(GeneralPathX gp,
1001
			AffineTransform at) {
1002
		GeneralPathX newGp = new GeneralPathX();
1003
		PathIterator theIterator;
1004
		int theType;
1005
		int numParts = 0;
1006
		double[] theData = new double[6];
1007
		java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
1008
		java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
1009
		boolean bFirst = true;
1010
		int xInt, yInt, antX = -1, antY = -1;
944 1011

  
945
    public static GeneralPathX transformToInts(GeneralPathX gp, AffineTransform at) {
946
        GeneralPathX newGp = new GeneralPathX();
947
        PathIterator theIterator;
948
        int theType;
949
        int numParts = 0;
950
        double[] theData = new double[6];
951
        java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
952
        java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
953
        boolean bFirst = true;
954
        int xInt, yInt, antX = -1, antY = -1;
1012
		theIterator = gp.getPathIterator(null); // , flatness);
955 1013

  
956
        theIterator = gp.getPathIterator(null); //, flatness);
1014
		while (!theIterator.isDone()) {
1015
			theType = theIterator.currentSegment(theData);
1016
			switch (theType) {
1017
			case PathIterator.SEG_MOVETO:
1018
				numParts++;
1019
				ptSrc.setLocation(theData[0], theData[1]);
1020
				at.transform(ptSrc, ptDst);
1021
				antX = (int) ptDst.getX();
1022
				antY = (int) ptDst.getY();
1023
				newGp.moveTo(antX, antY);
1024
				bFirst = true;
1025
				break;
957 1026

  
958
        while (!theIterator.isDone()) {
959
            theType = theIterator.currentSegment(theData);
960
            switch (theType) {
961
                case PathIterator.SEG_MOVETO:
962
                    numParts++;
963
                    ptSrc.setLocation(theData[0], theData[1]);
964
                    at.transform(ptSrc, ptDst);
965
                    antX = (int) ptDst.getX();
966
                    antY = (int) ptDst.getY();
967
                    newGp.moveTo(antX, antY);
968
                    bFirst = true;
969
                    break;
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
					newGp.lineTo(xInt, yInt);
1034
					antX = xInt;
1035
					antY = yInt;
1036
					bFirst = false;
1037
				}
1038
				break;
970 1039

  
971
                case PathIterator.SEG_LINETO:
972
                    ptSrc.setLocation(theData[0], theData[1]);
973
                    at.transform(ptSrc, ptDst);
974
                    xInt = (int) ptDst.getX();
975
                    yInt = (int) ptDst.getY();
976
                    if ((bFirst) || ((xInt != antX) || (yInt != antY)))
977
                    {
978
                        newGp.lineTo(xInt, yInt);
979
                        antX = xInt;
980
                        antY = yInt;
981
                        bFirst = false;
982
                    }
983
                    break;
1040
			case PathIterator.SEG_QUADTO:
1041
				System.out.println("Not supported here");
984 1042

  
985
                case PathIterator.SEG_QUADTO:
986
                    System.out.println("Not supported here");
1043
				break;
987 1044

  
988
                    break;
1045
			case PathIterator.SEG_CUBICTO:
1046
				System.out.println("Not supported here");
989 1047

  
990
                case PathIterator.SEG_CUBICTO:
991
                    System.out.println("Not supported here");
1048
				break;
992 1049

  
993
                    break;
1050
			case PathIterator.SEG_CLOSE:
1051
				newGp.closePath();
994 1052

  
995
                case PathIterator.SEG_CLOSE:
996
                    newGp.closePath();
1053
				break;
1054
			} // end switch
997 1055

  
998
                    break;
999
            } //end switch
1056
			theIterator.next();
1057
		} // end while loop
1000 1058

  
1001
            theIterator.next();
1002
        } //end while loop
1059
		return newGp;
1060
	}
1003 1061

  
1004
        return newGp;
1005
    }
1006
    public static Geometry transformToInts(Geometry gp, AffineTransform at) throws CreateGeometryException {
1007
        GeneralPathX newGp = new GeneralPathX();
1008
        double[] theData = new double[6];
1009
        double[] aux = new double[6];
1062
	public static Geometry transformToInts(Geometry gp, AffineTransform at)
1063
			throws CreateGeometryException {
1064
		GeneralPathX newGp = new GeneralPathX();
1065
		double[] theData = new double[6];
1066
		double[] aux = new double[6];
1010 1067

  
1011
        // newGp.reset();
1012
        PathIterator theIterator;
1013
        int theType;
1014
        int numParts = 0;
1068
		// newGp.reset();
1069
		PathIterator theIterator;
1070
		int theType;
1071
		int numParts = 0;
1015 1072

  
1016
        java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
1017
        java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
1018
        boolean bFirst = true;
1019
        int xInt, yInt, antX = -1, antY = -1;
1073
		java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
1074
		java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
1075
		boolean bFirst = true;
1076
		int xInt, yInt, antX = -1, antY = -1;
1020 1077

  
1078
		theIterator = gp.getPathIterator(null); // , flatness);
1079
		int numSegmentsAdded = 0;
1080
		while (!theIterator.isDone()) {
1081
			theType = theIterator.currentSegment(theData);
1021 1082

  
1022
        theIterator = gp.getPathIterator(null); //, flatness);
1023
        int numSegmentsAdded = 0;
1024
        while (!theIterator.isDone()) {
1025
            theType = theIterator.currentSegment(theData);
1083
			switch (theType) {
1084
			case PathIterator.SEG_MOVETO:
1085
				numParts++;
1086
				ptSrc.setLocation(theData[0], theData[1]);
1087
				at.transform(ptSrc, ptDst);
1088
				antX = (int) ptDst.getX();
1089
				antY = (int) ptDst.getY();
1090
				newGp.moveTo(antX, antY);
1091
				numSegmentsAdded++;
1092
				bFirst = true;
1093
				break;
1026 1094

  
1027
            switch (theType) {
1028
                case PathIterator.SEG_MOVETO:
1029
                    numParts++;
1030
                    ptSrc.setLocation(theData[0], theData[1]);
1031
                    at.transform(ptSrc, ptDst);
1032
                    antX = (int) ptDst.getX();
1033
                    antY = (int) ptDst.getY();
1034
                    newGp.moveTo(antX, antY);
1035
                    numSegmentsAdded++;
1036
                    bFirst = true;
1037
                    break;
1095
			case PathIterator.SEG_LINETO:
1096
				ptSrc.setLocation(theData[0], theData[1]);
1097
				at.transform(ptSrc, ptDst);
1098
				xInt = (int) ptDst.getX();
1099
				yInt = (int) ptDst.getY();
1100
				if ((bFirst) || ((xInt != antX) || (yInt != antY))) {
1101
					newGp.lineTo(xInt, yInt);
1102
					antX = xInt;
1103
					antY = yInt;
1104
					bFirst = false;
1105
					numSegmentsAdded++;
1106
				}
1107
				break;
1038 1108

  
1039
                case PathIterator.SEG_LINETO:
1040
                    ptSrc.setLocation(theData[0], theData[1]);
1041
                    at.transform(ptSrc, ptDst);
1042
                    xInt = (int) ptDst.getX();
1043
                    yInt = (int) ptDst.getY();
1044
                    if ((bFirst) || ((xInt != antX) || (yInt != antY)))
1045
                    {
1046
                        newGp.lineTo(xInt, yInt);
1047
                        antX = xInt;
1048
                        antY = yInt;
1049
                        bFirst = false;
1050
                        numSegmentsAdded++;
1051
                    }
1052
                    break;
1109
			case PathIterator.SEG_QUADTO:
1110
				at.transform(theData, 0, aux, 0, 2);
1111
				newGp.quadTo(aux[0], aux[1], aux[2], aux[3]);
1112
				numSegmentsAdded++;
1113
				break;
1053 1114

  
1054
                case PathIterator.SEG_QUADTO:
1055
                    at.transform(theData,0,aux,0,2);
1056
                    newGp.quadTo(aux[0], aux[1], aux[2], aux[3]);
1057
                    numSegmentsAdded++;
1058
                    break;
1115
			case PathIterator.SEG_CUBICTO:
1116
				at.transform(theData, 0, aux, 0, 3);
1117
				newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]);
1118
				numSegmentsAdded++;
1119
				break;
1059 1120

  
1060
                case PathIterator.SEG_CUBICTO:
1061
                    at.transform(theData,0,aux,0,3);
1062
                    newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]);
1063
                    numSegmentsAdded++;
1064
                    break;
1121
			case PathIterator.SEG_CLOSE:
1122
				if (numSegmentsAdded < 3) {
1123
					newGp.lineTo(antX, antY);
1124
				}
1125
				newGp.closePath();
1065 1126

  
1066
                case PathIterator.SEG_CLOSE:
1067
                    if (numSegmentsAdded < 3) {
1068
						newGp.lineTo(antX, antY);
1069
					}
1070
                    newGp.closePath();
1127
				break;
1128
			} // end switch
1071 1129

  
1072
                    break;
1073
            } //end switch
1130
			theIterator.next();
1131
		} // end while loop
1074 1132

  
1075
            theIterator.next();
1076
        } //end while loop
1133
		Geometry shp = null;
1134
		switch (gp.getType()) {
1135
		case Geometry.TYPES.POINT:
1136
			shp = geomManager.createPoint(ptDst.getX(), ptDst.getY(),
1137
					SUBTYPES.GEOM2D);
1138
			break;
1077 1139

  
1078
        Geometry shp = null;
1079
        switch (gp.getType())
1080
        {
1081
            case Geometry.TYPES.POINT:
1082
                shp = geomManager.createPoint(ptDst.getX(), ptDst.getY(), SUBTYPES.GEOM2D); 
1083
                break;
1140
		case Geometry.TYPES.CURVE:
1141
		case Geometry.TYPES.ARC:
1142
			try {
1143
				shp = geomManager.createCurve(newGp, SUBTYPES.GEOM2D);
1144
			} catch (CreateGeometryException e1) {
1145
				logger.error("Error creating a curve", e1);
1146
			}
1147
			break;
1084 1148

  
1085
            case Geometry.TYPES.CURVE:
1086
            case Geometry.TYPES.ARC:
1087
            	try {
1088
					shp = geomManager.createCurve(newGp, SUBTYPES.GEOM2D);
1089
				} catch (CreateGeometryException e1) {
1090
					logger.error("Error creating a curve", e1);
1091
				}
1092
                break;
1149
		case Geometry.TYPES.SURFACE:
1150
		case Geometry.TYPES.CIRCLE:
1151
		case Geometry.TYPES.ELLIPSE:
1093 1152

  
1094
            case Geometry.TYPES.SURFACE:
1095
            case Geometry.TYPES.CIRCLE:
1096
            case Geometry.TYPES.ELLIPSE:
1153
			try {
1154
				shp = geomManager.createSurface(newGp, SUBTYPES.GEOM2D);
1155
			} catch (CreateGeometryException e) {
1156
				logger.error("Error creating a surface", e);
1157
			}
1158
			break;
1159
		}
1160
		return shp;
1161
	}
1097 1162

  
1098
                try {
1099
					shp = geomManager.createSurface(newGp, SUBTYPES.GEOM2D);
1100
				} catch (CreateGeometryException e) {
1101
					logger.error("Error creating a surface", e);
1102
				}
1103
                break;
1104
        }
1105
        return shp;
1106
    }
1163
	public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR) {
1164
		Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(),
1165
				jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight());
1166
		return r;
1167
	}
1107 1168

  
1108
    public static Rectangle2D convertEnvelopeToRectangle2D(Envelope jtsR)
1109
    {
1110
        Rectangle2D.Double r = new Rectangle2D.Double(jtsR.getMinX(),
1111
                jtsR.getMinY(), jtsR.getWidth(), jtsR.getHeight());
1112
        return r;
1113
    }
1169
	public static Envelope convertEnvelopeToJTS(
1170
			org.gvsig.fmap.geom.primitive.Envelope r) {
1171
		Envelope e = new Envelope(r.getMinimum(0), r.getMaximum(0),
1172
				r.getMinimum(1), r.getMaximum(1));
1173
		return e;
1174
	}
1114 1175

  
1115
    public static Envelope convertEnvelopeToJTS(org.gvsig.fmap.geom.primitive.Envelope r)
1116
    {
1117
    	Envelope e = new Envelope(r.getMinimum(0), r.getMaximum(0), r.getMinimum(1),
1118
			r.getMaximum(1));
1119
    	return e;
1120
    }
1176
	/**
1177
	 * Return a correct polygon (no hole)
1178
	 * 
1179
	 * @param coordinates
1180
	 * @return
1181
	 */
1182
	public static Geometry getExteriorPolygon(Coordinate[] coordinates) {
1183
		// isCCW = true => it's a hole
1184
		Coordinate[] vs = new Coordinate[coordinates.length];
1185
		if (CGAlgorithms.isCCW(coordinates)) {
1186
			for (int i = vs.length - 1; i >= 0; i--) {
1187
				vs[i] = coordinates[i];
1188
			}
1189
		} else {
1190
			vs = coordinates;
1191
		}
1192
		LinearRing ring = geomFactory.createLinearRing(vs);
1121 1193

  
1122
    /**
1123
     * Return a correct polygon (no hole)
1124
     * @param coordinates
1125
     * @return
1126
     */
1127
    public static Geometry getExteriorPolygon(Coordinate[] coordinates)
1128
    {
1129
    	// isCCW = true => it's a hole
1130
    	Coordinate[] vs=new Coordinate[coordinates.length];
1131
        if (CGAlgorithms.isCCW(coordinates)){
1132
        	for (int i=vs.length-1;i>=0;i--){
1133
        		vs[i]=coordinates[i];
1134
        	}
1135
        }else{
1136
        	vs=coordinates;
1137
        }
1138
        LinearRing ring = geomFactory.createLinearRing(vs);
1139

  
1140
        try {
1141
			Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
1194
		try {
1195
			Surface surface = (Surface) manager.create(TYPES.SURFACE,
1196
					SUBTYPES.GEOM2D);
1142 1197
			surface.setGeneralPath(toShape(ring));
1143 1198
			return surface;
1144 1199
		} catch (NoninvertibleTransformException e) {
......
1147 1202
			e.printStackTrace();
1148 1203
		}
1149 1204
		return null;
1150
    }
1205
	}
1151 1206

  
1152
    public static boolean isCCW(Point[] points)
1153
    {
1154
    	int length = points.length;
1155
    	Coordinate[] vs;
1156
    	// CGAlgorithms.isCCW asume que la lista de puntos tienen el primer
1157
    	// y el ultimo puntos iguales.... y que este algoritmo est? solo
1158
    	// garantizado con anillos v?lidos.
1159
    	if (points[0].getX() != points[length-1].getX() || points[0].getY() != points[length-1].getY()) {
1160
        	vs=new Coordinate[length+1];
1161
        	vs[points.length] = new Coordinate(points[0].getX(), points[0].getY());
1162
    	} else {
1163
        	vs=new Coordinate[length];
1164
    	}
1165
       	for (int i=0; i<length; i++){
1166
    		vs[i] = new Coordinate(points[i].getX(), points[i].getY());
1167
    	}
1207
	public static boolean isCCW(Point[] points) {
1208
		int length = points.length;
1209
		Coordinate[] vs;
1210
		// CGAlgorithms.isCCW asume que la lista de puntos tienen el primer
1211
		// y el ultimo puntos iguales.... y que este algoritmo est? solo
1212
		// garantizado con anillos v?lidos.
1213
		if (points[0].getX() != points[length - 1].getX()
1214
				|| points[0].getY() != points[length - 1].getY()) {
1215
			vs = new Coordinate[length + 1];
1216
			vs[points.length] = new Coordinate(points[0].getX(),
1217
					points[0].getY());
1218
		} else {
1219
			vs = new Coordinate[length];
1220
		}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff