Revision 28396

View differences:

branches/v2_0_0_prep/libraries/libFMap_geometries/src-test/org/gvsig/fmap/geom/GeometryManagerTest.java
34 34
	}
35 35

  
36 36
	private GeometryManager manager;
37
	
37

  
38 38
	protected void setUp() throws Exception {
39 39
		super.setUp();
40 40

  
41
		DefaultGeometryLibrary lib = new DefaultGeometryLibrary();		
41
		DefaultGeometryLibrary lib = new DefaultGeometryLibrary();
42 42
		lib.initialize();
43 43
		lib.postInitialize();
44
		
44

  
45 45
		DefaultGeometryOperationLibrary opLib = new DefaultGeometryOperationLibrary();
46 46
		opLib.initialize();
47
		opLib.postInitialize();		
48
		
47
		opLib.postInitialize();
48

  
49 49
		manager = GeometryLocator.getGeometryManager();
50 50
	}
51 51

  
......
57 57
		Point p2d = (Point) manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
58 58
		p2d.setCoordinateAt(0, 0);
59 59
		p2d.setCoordinateAt(1, 0);
60
		
60

  
61 61
		java.awt.geom.Point2D pt1 = new java.awt.geom.Point2D.Float(0,1);
62 62
		java.awt.geom.Point2D pt2 = new java.awt.geom.Point2D.Float(1,1);
63 63
		java.awt.geom.Point2D pt3 = new java.awt.geom.Point2D.Float(1,0);
......
94 94

  
95 95
		//Point p = (Point) factory.createPoint2D(1, 2);
96 96
		Point p = (Point) manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
97
									
97

  
98 98
		GeometryOperationContext ctx = new GeometryOperationContext();
99 99
		ctx.setAttribute("dummy", "DummyValue");
100 100

  
......
129 129
//		java.awt.geom.Point2D p1 = new java.awt.geom.Point2D.Double(1,2);
130 130
//		java.awt.geom.Point2D p2 = new java.awt.geom.Point2D.Double(2,1);
131 131
//		java.awt.geom.Point2D p3 = new java.awt.geom.Point2D.Double(3,3);
132
		
132

  
133 133
		Point p1 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
134 134
		p1.setCoordinateAt(0, 1);
135
		p1.setCoordinateAt(1, 2);	
136
		
135
		p1.setCoordinateAt(1, 2);
136

  
137 137
		Point p2 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
138 138
		p2.setCoordinateAt(0, 2);
139 139
		p2.setCoordinateAt(1, 1);
140
		
140

  
141 141
		Point p3 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
142 142
		p3.setCoordinateAt(0, 3);
143
		p3.setCoordinateAt(1, 3);		
144
		
143
		p3.setCoordinateAt(1, 3);
144

  
145 145
		//Build a curve to get the operation invoked with the registered operation.
146 146
		//Geometry curve = (Curve) factory.createArc(p1, p2, p3);
147 147
		Arc arc = (Arc) manager.create(TYPES.ARC, SUBTYPES.GEOM2D);
......
195 195
		indirectInvoke(ToJTS.CODE, ctx, size);
196 196
		directInvoke(ToJTS.CODE, ctx, size);
197 197
		logger.debug("ToWKB.-");
198
		indirectInvoke(ToWKB.CODE, ctx, size);
199
		directInvoke(ToWKB.CODE, ctx, size);
198
		indirectInvoke(ToWKB.CODE, null, size);
199
		directInvoke(ToWKB.CODE, null, size);
200 200
		logger.debug("Flip.-");
201 201
		indirectInvoke(Flip.CODE, ctx, size);
202 202
		directInvoke(Flip.CODE, ctx, size);
......
259 259
		// Here is the main loop where you call the operation
260 260
		try {
261 261
			for (int i = 0; i < geoms.length; i++) {
262
				Object result = ((Geometry) geoms[i]).invokeOperation(opCode,
262
				Object result = (geoms[i]).invokeOperation(opCode,
263 263
						ctx);
264 264
				// if(i%100 == 99) logger.debug(result);
265 265
			}
branches/v2_0_0_prep/libraries/libFMap_geometries/src/org/gvsig/fmap/geom/util/Converter.java
61 61
import org.gvsig.fmap.geom.exception.CreateGeometryException;
62 62
import org.gvsig.fmap.geom.primitive.GeneralPathX;
63 63
import org.gvsig.fmap.geom.primitive.Surface;
64
import org.gvsig.fmap.geom.primitive.impl.Curve2D;
65 64
import org.slf4j.Logger;
66 65
import org.slf4j.LoggerFactory;
67 66

  
......
88 87
public class Converter {
89 88
	private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
90 89
	private static final Logger logger = LoggerFactory.getLogger(Converter.class);
91
	
90

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

  
94 93
	/**
......
194 193
	public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPoint geom) {
195 194
		Coordinate[] theGeoms = new Coordinate[geom.getPrimitivesNumber()];
196 195
		for (int i = 0; i < theGeoms.length; i++) {
197
			java.awt.geom.Point2D p = (java.awt.geom.Point2D) geom.getPrimitiveAt(i)
196
			java.awt.geom.Point2D p = geom.getPrimitiveAt(i)
198 197
					.getHandlers(Geometry.SELECTHANDLER)[0].getPoint();
199 198
			Coordinate c = new Coordinate(p.getX(), p.getY());
200 199
			theGeoms[i] = c;
......
212 211
	public static com.vividsolutions.jts.geom.Geometry geometryToJts(MultiCurve geom) {
213 212
		LineString[] lines = new LineString[geom.getPrimitivesNumber()];
214 213
        for (int i = 0; i < lines.length; i++){
215
        	lines[i] = (LineString) geometryToJts(((Curve2D) geom.getPrimitiveAt(i)));
214
        	lines[i] = (LineString) geometryToJts((geom.getPrimitiveAt(i)));
216 215
        }
217 216
        return new com.vividsolutions.jts.geom.GeometryFactory().createMultiLineString(lines);
218 217
	}
......
224 223
	public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiSurface geom) {
225 224
		Polygon[] polygons = new Polygon[geom.getPrimitivesNumber()];
226 225
        for (int i = 0; i < polygons.length; i++){
227
        	polygons[i] = (Polygon) geometryToJts(((Surface)geom.getPrimitiveAt(i)));
226
        	polygons[i] = (Polygon) geometryToJts((geom.getPrimitiveAt(i)));
228 227
        }
229 228
        return new com.vividsolutions.jts.geom.GeometryFactory().createMultiPolygon(polygons);
230 229
	}
......
236 235
	public com.vividsolutions.jts.geom.Geometry geometryToJts(MultiPrimitive geom) {
237 236
		com.vividsolutions.jts.geom.Geometry[] geometriesAux = new LineString[geom.getPrimitivesNumber()];
238 237
		for (int i = 0; i < geometriesAux.length; i++) {
239
			geometriesAux[i] = geometryToJts(((Geometry) geom.getPrimitiveAt(i)));
238
			geometriesAux[i] = geometryToJts((geom.getPrimitiveAt(i)));
240 239
		}
241 240
		return new com.vividsolutions.jts.geom.GeometryFactory().createGeometryCollection(geometriesAux);
242 241
	}
243 242

  
243
	public static com.vividsolutions.jts.geom.Geometry geometryToJtsWithSRID(
244
			Geometry geom, int srid) {
245
		// logger.debug(geom.getClass());
246
		// logger.debug(new Integer(geom.getShapeType()));
247
		return geometryToJts(geom, geom.getType(), srid);
248
	}
244 249

  
250

  
245 251
	public static com.vividsolutions.jts.geom.Geometry geometryToJts(Geometry geom) {
246 252
		//logger.debug(geom.getClass());
247 253
		//logger.debug(new Integer(geom.getShapeType()));
248
	 return geometryToJts(geom, geom.getType());
254
		return geometryToJts(geom, geom.getType(), -1);
249 255
	}
250 256

  
251 257
//	public static com.vividsolutions.jts.geom.Geometry java2d_to_jts(FShape shp) {
......
260 266
	 *
261 267
	 * @return Geometry de JTS.
262 268
	 */
263
	private static com.vividsolutions.jts.geom.Geometry geometryToJts(Geometry shp, int shapeType) {
269
	private static com.vividsolutions.jts.geom.Geometry geometryToJts(
270
			Geometry shp, int shapeType, int srid) {
264 271

  
265 272

  
266 273
		com.vividsolutions.jts.geom.Geometry geoJTS = null;
......
280 287

  
281 288
		//logger.debug(shp.toString());
282 289

  
290

  
283 291
		switch (shapeType) {
284
		case Geometry.TYPES.POINT:		
292
		case Geometry.TYPES.POINT:
285 293
			org.gvsig.fmap.geom.primitive.impl.Point2D p = (org.gvsig.fmap.geom.primitive.impl.Point2D) shp;
286 294
			coord = new Coordinate(p.getX(), p.getY());
287 295
			geoJTS = geomFactory.createPoint(coord);
296
			geoJTS.setSRID(srid);
288 297

  
289 298
			break;
290 299

  
291 300
		case Geometry.TYPES.CURVE:
292
		case Geometry.TYPES.ARC:		
301
		case Geometry.TYPES.ARC:
293 302
			arrayLines = new ArrayList();
294 303
			theIterator = shp.getPathIterator(null, FLATNESS);
295 304

  
......
310 319
					} else {
311 320
						lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray(
312 321
								arrayCoords));
322
						lin.setSRID(srid);
313 323
						arrayLines.add(lin);
314 324
						arrayCoords = new ArrayList();
315 325
					}
316 326

  
317 327
					numParts++;
318
					arrayCoords.add(new Coordinate(theData[0],
319
							theData[1]));
328
					coord = new Coordinate(theData[0], theData[1]);
320 329

  
330
					arrayCoords.add(coord);
331

  
321 332
					break;
322 333

  
323 334
				case PathIterator.SEG_LINETO:
......
350 361
				theIterator.next();
351 362
			} //end while loop
352 363

  
353
			if (arrayCoords.size()<2)
364
			if (arrayCoords.size()<2) {
354 365
				break;
366
			}
355 367
			lin = new com.vividsolutions.jts.geom.GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
356 368
					arrayCoords));
357 369

  
370
			lin.setSRID(srid);
358 371
			// CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
359 372
			// LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
360 373
			// O CON GEOTOOLS
......
363 376
			arrayLines.add(lin);
364 377
			geoJTS = geomFactory.createMultiLineString(com.vividsolutions.jts.geom.GeometryFactory.toLineStringArray(
365 378
					arrayLines));
379
			geoJTS.setSRID(srid);
366 380
			/* } else {
367 381
			 geoJTS = lin;
368 382
			 } */
......
371 385

  
372 386
		case Geometry.TYPES.SURFACE:
373 387
		case Geometry.TYPES.CIRCLE:
374
		case Geometry.TYPES.ELLIPSE:		
388
		case Geometry.TYPES.ELLIPSE:
375 389
			arrayLines = new ArrayList();
376 390

  
377 391
			ArrayList shells = new ArrayList();
......
414 428
								if (points[i].x != points[i+1].x ||
415 429
										points[i].y != points[i+1].y /*||
416 430
										points[i].z != points[i+1].z*/
417
								) same = false;
431
								) {
432
									same = false;
433
								}
418 434
							}
419
							if (same)
435
							if (same) {
420 436
								return geomFactory.createPoint(points[0]);
437
							}
421 438
							/*
422 439
							 * caso cuando es una l�nea de 3 puntos, no creo un LinearRing, sino
423 440
							 * una linea
424 441
							 */
425
							if (points.length>1 && points.length<=3)
442
							if (points.length>1 && points.length<=3) {
426 443
								// return geomFactory.createLineString(points);
427 444
								return geomFactory.createMultiLineString(new LineString[] {geomFactory.createLineString(points)});
445
							}
428 446

  
429 447
							System.err.println(
430 448
							"Caught Topology exception in GMLLinearRingHandler");
......
495 513
				} else {
496 514
					shells.add(ring);
497 515
				}
516
				ring.setSRID(srid);
498 517
			} catch (Exception e) {
499 518
				/* (jaume) caso cuando todos los puntos son iguales
500 519
				 * devuelvo el propio punto
......
504 523
					if (points[i].x != points[i+1].x ||
505 524
							points[i].y != points[i+1].y /*||
506 525
							points[i].z != points[i+1].z*/
507
					) same = false;
526
					) {
527
						same = false;
528
					}
508 529
				}
509
				if (same)
510
					return geomFactory.createPoint(points[0]);
530
				if (same) {
531
					geoJTS = geomFactory.createPoint(points[0]);
532
					geoJTS.setSRID(srid);
533
					return geoJTS;
534
				}
511 535
				/*
512 536
				 * caso cuando es una l�nea de 3 puntos, no creo un LinearRing, sino
513 537
				 * una linea
514 538
				 */
515
				if (points.length>1 && points.length<=3)
539
				if (points.length>1 && points.length<=3) {
516 540
					// return geomFactory.createLineString(points);
517
					return geomFactory.createMultiLineString(new LineString[] {geomFactory.createLineString(points)});
541
					geoJTS = geomFactory
542
							.createMultiLineString(new LineString[] { geomFactory
543
									.createLineString(points) });
544
					geoJTS.setSRID(srid);
545
					return geoJTS;
546
				}
518 547
				System.err.println(
519 548
				"Caught Topology exception in GMLLinearRingHandler");
520 549

  
......
594 623
						i),
595 624
						(LinearRing[]) ((ArrayList) holesForShells.get(i)).toArray(
596 625
								new LinearRing[0]));
626
				polygons[i].setSRID(srid);
597 627
			}
598 628
			// CAMBIO: ENTREGAMOS SIEMPRE MULTILINESTRING, QUE ES
599 629
			// LO QUE HACE TODO EL MUNDO CUANDO ESCRIBE EN POSTGIS
......
612 642

  
613 643
			//its a multi part
614 644
			geoJTS = geomFactory.createMultiPolygon(polygons);
645
			geoJTS.setSRID(srid);
615 646

  
616 647
			/* if (numParts > 1) // Generamos un Polygon con agujeros
617 648
			 {
......
629 660
			break;
630 661
		}
631 662

  
663
		geoJTS.setSRID(srid);
632 664
		return geoJTS;
633 665
	}
634 666

  
......
865 897
		}
866 898

  
867 899
		if (geometry instanceof GeometryCollection) {
868
			return toShape((GeometryCollection) geometry);
900
			return toShape(geometry);
869 901
		}
870 902

  
871 903
		throw new IllegalArgumentException("Unrecognized Geometry class: " +
......
995 1027
                    break;
996 1028

  
997 1029
                case PathIterator.SEG_CLOSE:
998
                    if (numSegmentsAdded < 3)
999
                        newGp.lineTo(antX, antY);
1030
                    if (numSegmentsAdded < 3) {
1031
						newGp.lineTo(antX, antY);
1032
					}
1000 1033
                    newGp.closePath();
1001 1034

  
1002 1035
                    break;
......
1008 1041
        Geometry shp = null;
1009 1042
        switch (gp.getType())
1010 1043
        {
1011
            case Geometry.TYPES.POINT: 
1044
            case Geometry.TYPES.POINT:
1012 1045
                shp = new org.gvsig.fmap.geom.primitive.impl.Point2D(null, null, ptDst.getX(), ptDst.getY());
1013 1046
                break;
1014 1047

  
branches/v2_0_0_prep/libraries/libFMap_geometries/src/org/gvsig/fmap/geom/operation/towkb/ToWKB.java
16 16
			.registerGeometryOperation("toWKB", new ToWKB());
17 17

  
18 18
	public Object invoke(Geometry geom, GeometryOperationContext ctx) {
19
		return writer.write(Converter.geometryToJts(geom));
19
		if (ctx == null){
20
			return writer.write(Converter.geometryToJts(geom));
21
		}
22
		return writer.write(Converter.geometryToJtsWithSRID(geom,
23
				((ToWKBOperationContext) ctx).getSrID()));
20 24
	}
21 25

  
22 26
	public int getOperationIndex() {
branches/v2_0_0_prep/libraries/libFMap_geometries/src/org/gvsig/fmap/geom/operation/towkb/ToWKBOperationContext.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2009 IVER T.I   {{Task}}
26
*/
27

  
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.geom.operation.towkb;
32

  
33
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
34

  
35
/**
36
 * @author jmvivo
37
 *
38
 */
39
public class ToWKBOperationContext extends GeometryOperationContext {
40
	private int srID = -1;
41

  
42
	/**
43
	 * Set the srID to use in the WKB geometry
44
	 * 
45
	 * @param srID
46
	 */
47
	public void setSrID(int srID) {
48
		this.srID = srID;
49
	}
50

  
51
	/**
52
	 * Get the srId to use in the WKB geometry
53
	 * 
54
	 * @return
55
	 */
56
	public int getSrID() {
57
		return srID;
58
	}
59

  
60

  
61
}
0 62

  

Also available in: Unified diff