Revision 29932 branches/v2_0_0_prep/extensions/org.gvsig.oracle/src/org/gvsig/fmap/dal/store/oracle/OracleUtils.java

View differences:

OracleUtils.java
58 58
import org.gvsig.fmap.geom.GeometryLocator;
59 59
import org.gvsig.fmap.geom.GeometryManager;
60 60
import org.gvsig.fmap.geom.aggregate.Aggregate;
61
import org.gvsig.fmap.geom.aggregate.MultiCurve;
62
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
63
import org.gvsig.fmap.geom.aggregate.MultiSolid;
61 64
import org.gvsig.fmap.geom.aggregate.MultiSurface;
62 65
import org.gvsig.fmap.geom.aggregate.impl.MultiPoint2D;
63 66
import org.gvsig.fmap.geom.aggregate.impl.MultiPoint2DZ;
64 67
import org.gvsig.fmap.geom.aggregate.impl.MultiSurface2D;
65 68
import org.gvsig.fmap.geom.exception.CreateGeometryException;
69
import org.gvsig.fmap.geom.primitive.Curve;
66 70
import org.gvsig.fmap.geom.primitive.GeneralPathX;
67 71
import org.gvsig.fmap.geom.primitive.Point;
72
import org.gvsig.fmap.geom.primitive.Primitive;
73
import org.gvsig.fmap.geom.primitive.Surface;
68 74
import org.gvsig.fmap.geom.primitive.impl.Circle2D;
69 75
import org.gvsig.fmap.geom.primitive.impl.Curve2D;
70 76
import org.gvsig.fmap.geom.primitive.impl.Curve2DZ;
......
82 88
import com.vividsolutions.jts.geom.GeometryFactory;
83 89
import com.vividsolutions.jts.geom.LineString;
84 90
import com.vividsolutions.jts.geom.LinearRing;
91
import com.vividsolutions.jts.geom.MultiPoint;
85 92
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
86 93

  
87

  
88

  
89 94
/**
90 95
 * Utility class with static methods.
91 96
 * 
......
93 98
 * 
94 99
 */
95 100
public class OracleUtils {
96
	
97
	private static Logger logger = LoggerFactory
98
			.getLogger(OracleUtils.class.getName());
99
	
100
	private static double FLATNESS = 0.8;
101
	private static GeometryFactory geomFactory = new GeometryFactory();
102
	private static final double IRRELEVANT_DISTANCE = 0.00000001;
101

  
102
	private static Logger logger = LoggerFactory.getLogger(OracleUtils.class
103
			.getName());
104

  
103 105
	private static Random rnd = new Random();
104 106
	private static DecimalFormat df = new DecimalFormat();
105 107
	private static DecimalFormatSymbols dfs = new DecimalFormatSymbols();
106
	public static final int ORACLE_GTYPE_UNKNOWN = 0;
107
	public static final int ORACLE_GTYPE_POINT = 1;
108
	public static final int ORACLE_GTYPE_LINE = 2;
109
	public static final int ORACLE_GTYPE_POLYGON = 3;
110
	public static final int ORACLE_GTYPE_COLLECTION = 4;
111
	public static final int ORACLE_GTYPE_MULTIPOINT = 5;
112
	public static final int ORACLE_GTYPE_MULTILINE = 6;
113
	public static final int ORACLE_GTYPE_MULTIPOLYGON = 7;
114 108

  
115
	public static final int ORACLE_GTYPE_COMPLEX_VOIDED_POLYON = 3;
116
	public static final int ORACLE_GTYPE_COMPLEX_COMPOUND_LINE = 4;
117
	public static final int ORACLE_GTYPE_COMPLEX_COMPOUND_POLYON = 5;
118

  
119 109
	/**
120
	 * COnstructs a geometry from a file that contains a vertex per line:
110
	 * Constructs a geometry from a file that contains a vertex per line:
121 111
	 * 
122 112
	 * x1 y1 z1 x2 y2 z2 ...
123 113
	 * 
......
131 121

  
132 122
		GeometryManager gManager = GeometryLocator.getGeometryManager();
133 123

  
134
		GeneralPathX resp = new GeneralPathX();
124
		GeneralPathX gpath = new GeneralPathX();
135 125
		File file = new File(filepath.getFile());
136 126
		List<Double> z = new ArrayList<Double>();
137 127

  
......
151 141
					move = true;
152 142
				} else {
153 143
					if (move) {
154
						resp.moveTo(coords[0], coords[1]);
144
						gpath.moveTo(coords[0], coords[1]);
155 145
						z.add(new Double(coords[2]));
156 146
					} else {
157
						resp.lineTo(coords[0], coords[1]);
147
						gpath.lineTo(coords[0], coords[1]);
158 148
						z.add(new Double(coords[2]));
159 149
					}
160 150

  
......
177 167

  
178 168
		if (polygon) {
179 169
			try {
180
				return gManager.createSurface(resp, Geometry.SUBTYPES.GEOM3D);
170
				return gManager.createSurface(gpath, Geometry.SUBTYPES.GEOM3D);
181 171
			} catch (CreateGeometryException e) {
182 172
				e.printStackTrace();
183 173
				return null;
184 174
			}
185 175
		} else {
186 176
			try {
187
				return gManager
188
						.createMultiCurve(resp, Geometry.SUBTYPES.GEOM3D);
177
				return gManager.createCurve(gpath, Geometry.SUBTYPES.GEOM3D);
189 178
			} catch (CreateGeometryException e) {
190 179
				e.printStackTrace();
191 180
				return null;
......
193 182
		}
194 183
	}
195 184

  
185
	/**
186
	 * Parse line
187
	 * 
188
	 * @param line
189
	 * @return
190
	 */
196 191
	private static double[] parseLine(String line) {
197 192
		String[] sep = line.split(" ");
198 193
		double[] resp = new double[3];
......
225 220
	/**
226 221
	 * Utility method to convert a gvSIG FShape into a oracle struct
227 222
	 * 
228
	 * @param fshp
223
	 * @param geom
229 224
	 *            the FShape object
230 225
	 * @param c
231 226
	 *            the connection
......
239 234
	 * 
240 235
	 * @throws SQLException
241 236
	 */
242
	public static STRUCT fShapeToSTRUCT(Object fshp, Connection c, int srid,
243
			boolean agu_b, boolean hasSrid) throws SQLException {
244
		boolean three = false;
237
	public static STRUCT GeometryToSTRUCT(Geometry geom, Connection c,
238
			int srid, boolean agu_b, boolean hasSrid) throws SQLException {
245 239

  
246
		GeometryManager gManager = GeometryLocator.getGeometryManager();
247

  
248
		if (fshp instanceof Surface2DZ) {
249
			three = true;
250
		}
251

  
240
		GeometryManager geomManager = GeometryLocator.getGeometryManager();
241
		
252 242
		STRUCT resp = null;
253

  
254
		if (fshp instanceof MultiPoint2D) {
255
			resp = multiPoint2DToStruct((MultiPoint2D) fshp, c, srid, hasSrid);
256

  
257
			return resp;
243
		
244
		// PRIMITIVES
245
		if (geom instanceof Primitive ) {
246
			//POINT
247
			if (geom instanceof Point ) {
248
				// POINT 2D
249
				if (geom instanceof Point2D) {
250
					Coordinate p = getSingleCoordinate2D((Point2D) geom);
251
					resp = getMultiPointAsStruct(p, srid, false, c, hasSrid);
252
					return resp;
253
				}
254
				// POINT 2DZ
255
				if (geom instanceof Point2DZ) {
256
					Coordinate p = getSingleCoordinate3D((Point2DZ) geom);
257
					resp = getMultiPointAsStruct(p, srid, true, c, hasSrid);
258
					return resp;
259
				}
260
			}
261
			// CURVE
262
			if (geom instanceof Curve ) {
263
				
264
			}
265
			// SURFACE
266
			if (geom instanceof Surface ) {
267
				
268
			}
258 269
		}
270
		
271
		// AGGREGATE
272
		if (geom instanceof Aggregate ) {
273
			// MULTIPRIMITIVE
274
			if (geom instanceof MultiPrimitive ) {
275
				
276
			}
277
			// MULTIPOINT
278
			if (geom instanceof MultiPoint ) {
279
				// MULTIPOINT 2D
280
				if (geom instanceof MultiPoint2D) {
281
					resp = multiPoint2DToStruct((MultiPoint2D) geom, c, srid, hasSrid);
282
					return resp;
283
				}
284
				// MULTIPOINT 2DZ
285
				if (geom instanceof MultiPoint2DZ) {
286
					resp = multiPoint2DZToStruct((MultiPoint2DZ) geom, c, srid, hasSrid);
287
					return resp;
288
				}
289
			}
290
			// MULTICURVE
291
			if (geom instanceof MultiCurve ) {
292
				
293
			}
294
			// MULTISURFACE
295
			if (geom instanceof MultiSurface ) {
296
				
297
			}
298
			// MULTISOLID
299
			if (geom instanceof MultiSolid ) {
300
				return null;
301
			}
302
		}
303
		
304
		
305
		
259 306

  
260
		if (!(fshp instanceof Shape)) {
261
			logger.error("Unknown geometry: " + fshp.toString());
307
		
262 308

  
263
			return null;
264
		}
309
	
265 310

  
266
		if (fshp instanceof Point2D) { // point 2/3d
311
		
312
		// POINT
313
		if (geom instanceof Point2D) { 
267 314

  
268
			Coordinate p = getSingleCoordinate((Point2D) fshp);
315
			Coordinate p = getSingleCoordinate((Point2D) geom);
269 316
			resp = getMultiPointAsStruct(p, srid, three, c, hasSrid);
270 317
		} else {
271
			if (fshp instanceof Surface2D) { // polygon 2/3d
318
			if (geom instanceof Surface2D) { // polygon 2/3d
272 319

  
273
				if (fshp instanceof Circle2D) {
274
					resp = getCircleAsStruct((Circle2D) fshp, srid, c, hasSrid);
320
				if (geom instanceof Circle2D) {
321
					resp = getCircleAsStruct((Circle2D) geom, srid, c, hasSrid);
275 322
				} else {
276 323
					// also FEllipse2D
277
					resp = getMultiPolygonAsStruct((MultiSurface) fshp, srid,
324
					resp = getMultiPolygonAsStruct((MultiSurface) geom, srid,
278 325
							three, c, agu_b, hasSrid);
279 326

  
280
				
281 327
				}
282
			} else { // line 2/3d
328
			} 
329
			//LINE
330
			else { // line 2/3d
283 331

  
284
				List<LineString3D> _lines = getLineStrings((Shape) fshp);
332
				List<LineString3D> _lines = getLineStrings((Shape) geom);
285 333
				resp = getMultiLineAsStruct(_lines, srid, three, c, hasSrid);
286 334
			}
287 335
		}
......
339 387
		return resp;
340 388
	}
341 389

  
390
	/**
391
	 * Get struct from circle
392
	 * 
393
	 * @param fcirc
394
	 * @param srid
395
	 * @param _conn
396
	 * @param hasSrid
397
	 * @return
398
	 * @throws SQLException
399
	 */
342 400
	private static STRUCT getCircleAsStruct(Circle2D fcirc, int srid,
343 401
			Connection _conn, boolean hasSrid) throws SQLException {
344 402
		int geotype = 2003;
......
393 451
		return resp;
394 452
	}
395 453

  
396
	private static Coordinate getSingleCoordinate(Point2D p2d) {
397
		// TODO Auto-generated method stub
454
	/**
455
	 * Get coordinate 2d
456
	 * 
457
	 * @param p2d
458
	 * @return
459
	 */
460
	private static Coordinate getSingleCoordinate2D(Point2D p2d) {
398 461
		Coordinate resp = new Coordinate();
399 462
		resp.x = p2d.getX();
400 463
		resp.y = p2d.getY();
401 464
		return resp;
402 465
	}
466
	
467
	/**
468
	 * Get coordinate 3d
469
	 * 
470
	 * @param p2dz
471
	 * @return
472
	 */
473
	private static Coordinate getSingleCoordinate3D(Point2DZ p2dz) {
474
		Coordinate resp = new Coordinate();
475
		resp.x = p2dz.getX();
476
		resp.y = p2dz.getY();
477
		resp.z = p2dz.getCoordinateAt(3);
478
		return resp;
479
	}
403 480

  
481
	/**
482
	 * tolerance
483
	 * 
484
	 * @param cc
485
	 * @return
486
	 */
404 487
	private static List<Coordinate> ensureSensibleLineString(List<Coordinate> cc) {
405 488
		if (cc.size() == 2) {
406 489
			if (sameCoordinate((Coordinate) cc.get(0), (Coordinate) cc.get(cc
......
409 492
				resp.add(cc.get(0));
410 493

  
411 494
				Coordinate newc = new Coordinate((Coordinate) cc.get(0));
412
				newc.x = newc.x + IRRELEVANT_DISTANCE;
495
				newc.x = newc.x + OracleValues.IRRELEVANT_DISTANCE;
413 496
				resp.add(newc);
414 497

  
415 498
				return resp;
......
419 502
		return cc;
420 503
	}
421 504

  
505
	/**
506
	 * Validate if two coordinates are equal
507
	 * 
508
	 * @param c1
509
	 * @param c2
510
	 * @return
511
	 */
422 512
	private static boolean sameCoordinate(Coordinate c1, Coordinate c2) {
423 513
		if (c1.x != c2.x) {
424 514
			return false;
......
467 557
		resp.add(c);
468 558

  
469 559
		Coordinate nc = new Coordinate(c);
470
		nc.x = nc.x + IRRELEVANT_DISTANCE;
560
		nc.x = nc.x + OracleValues.IRRELEVANT_DISTANCE;
471 561
		resp.add(nc);
472 562

  
473 563
		Coordinate nc2 = new Coordinate(nc);
474
		nc2.y = nc2.y + IRRELEVANT_DISTANCE;
564
		nc2.y = nc2.y + OracleValues.IRRELEVANT_DISTANCE;
475 565
		resp.add(nc2);
476 566

  
477 567
		resp.add(new Coordinate(c));
......
484 574
		p[0] = c;
485 575

  
486 576
		Coordinate nc = new Coordinate(c);
487
		nc.x = nc.x + IRRELEVANT_DISTANCE;
577
		nc.x = nc.x + OracleValues.IRRELEVANT_DISTANCE;
488 578

  
489 579
		Coordinate nc2 = new Coordinate(nc);
490
		nc2.y = nc2.y - IRRELEVANT_DISTANCE;
580
		nc2.y = nc2.y - OracleValues.IRRELEVANT_DISTANCE;
491 581
		p[1] = nc;
492 582
		p[2] = nc2;
493 583
		p[3] = new Coordinate(c);
......
520 610

  
521 611
			if ((testPoint.x == p.x)
522 612
					&& (testPoint.y == p.y)
523
					&& ((testPoint.z == p.z) || (!(testPoint.z == testPoint.z))) 			
524
			) {
613
					&& ((testPoint.z == p.z) || (!(testPoint.z == testPoint.z)))) {
525 614
				return true;
526 615
			}
527 616
		}
......
548 637
		Coordinate onlyCoord = null;
549 638
		int numParts = 0;
550 639

  
551
		PathIterator theIterator = mpolygon.getPathIterator(null, FLATNESS);
640
		PathIterator theIterator = mpolygon.getPathIterator(null, OracleValues.FLATNESS);
552 641

  
553 642
		while (!theIterator.isDone()) {
554 643
			// while not done
......
671 760
				double[] z = null;
672 761

  
673 762
				if (threed) {
674
					z = getZ(mpolygon, start_ind, end_ind,
675
							new_size);
763
					z = getZ(mpolygon, start_ind, end_ind, new_size);
676 764
				}
677 765

  
678 766
				LineString3D ring = new LineString3D(aux, z);
......
771 859
		LineString3D lin;
772 860

  
773 861
		List<LineString3D> arrayLines = new ArrayList<LineString3D>();
774
		PathIterator theIterator = mlines.getPathIterator(null, FLATNESS);
862
		PathIterator theIterator = mlines.getPathIterator(null, OracleValues.FLATNESS);
775 863
		int theType = -99;
776 864
		double[] theData = new double[6];
777 865
		List<Coordinate> arrayCoords = null;
......
882 970
		return resp;
883 971
	}
884 972

  
885
	private static String multiLineStringToWKT(List<LineString3D> ml, boolean threed) {
973
	private static String multiLineStringToWKT(List<LineString3D> ml,
974
			boolean threed) {
886 975
		String resp = "MULTILINESTRING(";
887 976

  
888 977
		for (int i = 0; i < ml.size(); i++) {
......
926 1015
		return resp;
927 1016
	}
928 1017

  
929
	private static String multiPolygonToWKT(List<LineString3D> shells, List<List<LineString3D>> hFs,
930
			boolean threed) {
1018
	private static String multiPolygonToWKT(List<LineString3D> shells,
1019
			List<List<LineString3D>> hFs, boolean threed) {
931 1020
		String resp = "MULTIPOLYGON(";
932 1021
		LineString3D ls = null;
933 1022
		List<LineString3D> holes;
......
1021 1110
		}
1022 1111
	}
1023 1112

  
1024
	private static STRUCT getMultiPolygonAsStruct(List<LineString3D> pols, int srid,
1025
			boolean threed, Connection _conn, boolean agu_bien, boolean hasSrid)
1026
			throws SQLException {
1113
	private static STRUCT getMultiPolygonAsStruct(List<LineString3D> pols,
1114
			int srid, boolean threed, Connection _conn, boolean agu_bien,
1115
			boolean hasSrid) throws SQLException {
1027 1116
		int size = pols.size();
1028 1117
		int geotype = 2007;
1029 1118
		int dim = 2;
......
1084 1173
		return resp;
1085 1174
	}
1086 1175

  
1087
	private static STRUCT getMultiLineAsStruct(List<LineString3D> lines, int srid,
1088
			boolean threed, Connection _conn, boolean hasSrid)
1176
	private static STRUCT getMultiLineAsStruct(List<LineString3D> lines,
1177
			int srid, boolean threed, Connection _conn, boolean hasSrid)
1089 1178
			throws SQLException {
1090 1179
		/*
1091 1180
		 * if (lines.size() == 1) { return
......
1152 1241
		return resp;
1153 1242
	}
1154 1243

  
1244
	/**
1245
	 * Get multipoint as STRUCT
1246
	 * 
1247
	 * @param pnt
1248
	 * @param srid
1249
	 * @param threed
1250
	 * @param _conn
1251
	 * @param hasSrid
1252
	 * @return
1253
	 * @throws SQLException
1254
	 */
1155 1255
	private static STRUCT getMultiPointAsStruct(Coordinate pnt, int srid,
1156 1256
			boolean threed, Connection _conn, boolean hasSrid)
1157 1257
			throws SQLException {
......
1166 1266
		Object[] ords = new Object[3];
1167 1267
		ords[0] = new NUMBER(pnt.x);
1168 1268
		ords[1] = new NUMBER(pnt.y);
1169
		ords[2] = (dim == 3) ? new NUMBER(pnt.z) : null; // ole ole y ole
1269
		ords[2] = (dim == 3) ? new NUMBER(pnt.z) : null; 
1170 1270

  
1171 1271
		StructDescriptor ord_dsc = StructDescriptor.createDescriptor(
1172 1272
				"MDSYS.SDO_POINT_TYPE", _conn);
......
1430 1530
		return _resp;
1431 1531
	}
1432 1532

  
1433
	private static List getHolesOf(int ind, int[] final_contn,
1434
			List all) {
1533
	private static List getHolesOf(int ind, int[] final_contn, List all) {
1435 1534
		List<Integer> resp_ind = new ArrayList<Integer>();
1436 1535

  
1437 1536
		for (int i = 0; i < final_contn.length; i++) {
......
1529 1628

  
1530 1629
	// private static STRUCT // private static ArrayList
1531 1630
	// getPolygonsEasily(FShape mpolygon) {
1532
	private static STRUCT getMultiPolygonAsStruct(MultiSurface2D mpol, int srid,
1533
			boolean threed, Connection _conn, boolean agu_bien, boolean hasSrid)
1534
			throws SQLException {
1631
	private static STRUCT getMultiPolygonAsStruct(MultiSurface2D mpol,
1632
			int srid, boolean threed, Connection _conn, boolean agu_bien,
1633
			boolean hasSrid) throws SQLException {
1535 1634
		List<LineString3D> all_ls = getPolygonsEasily(mpol);
1536 1635
		Object[] hs = getHolesForShells(all_ls);
1537 1636
		List sh = (List) hs[0];
......
1561 1660
	}
1562 1661

  
1563 1662
	private static STRUCT getMultiPolygonAsStruct(List<LineString3D> shells,
1564
			List<List<LineString3D>> holes, int srid, boolean threed, Connection _conn,
1565
			boolean explicito, boolean hasSrid) throws SQLException {
1663
			List<List<LineString3D>> holes, int srid, boolean threed,
1664
			Connection _conn, boolean explicito, boolean hasSrid)
1665
			throws SQLException {
1566 1666
		int t = 1003;
1567 1667

  
1568 1668
		if (explicito) {
......
1726 1826
	public static STRUCT appendGeometriesInStruct(Aggregate co,
1727 1827
			int _forced_type, Connection _conn, String _o_srid,
1728 1828
			boolean withSrid, boolean agu_bien, boolean _isGeoCS) {
1729
		
1829

  
1730 1830
		Geometry[] geoms = new Geometry[co.getPrimitivesNumber()];
1731 1831
		for (int i = 0; i < co.getPrimitivesNumber(); i++) {
1732
			geoms[i]= co.getPrimitiveAt(i);
1832
			geoms[i] = co.getPrimitiveAt(i);
1733 1833
		}
1734
		
1735
		
1736
		
1834

  
1737 1835
		STRUCT[] sts = new STRUCT[co.getPrimitivesNumber()];
1738 1836

  
1739 1837
		for (int i = 0; i < co.getPrimitivesNumber(); i++) {
1740
			sts[i] = geometryToSTRUCT(geoms[i],
1741
					_forced_type, _conn, _o_srid, withSrid, agu_bien, _isGeoCS);
1838
			sts[i] = geometryToSTRUCT(geoms[i], _forced_type, _conn, _o_srid,
1839
					withSrid, agu_bien, _isGeoCS);
1742 1840
		}
1743 1841

  
1744 1842
		if (co.getPrimitivesNumber() == 1) {
......
3149 3247
		return resp.toArray();
3150 3248
	}
3151 3249

  
3152
	/**
3153
	 * 
3154
	 * @param shp
3155
	 * @return
3156
	 */
3157
	public static Geometry shapeToGeometry(Shape shp) {
3158
		
3159
		GeometryManager geomManager = GeometryLocator.getGeometryManager();
3160
		
3161
		if (shp == null) {
3162
			return null;
3163
		}
3164
		
3165
		Geometry geom = null;
3250
	
3166 3251

  
3167
		int type = Geometry.TYPES.SURFACE;
3168

  
3169
		if ((shp instanceof Curve2D) && (!(shp instanceof Surface2D))) {
3170
			type = Geometry.TYPES.CURVE;
3171
			geom = geomManager.create(type, Geometry.SUBTYPES.GEOM2D);
3172
			GeneralPathX gpx = new GeneralPathX(shp);
3173
			((Curve2D)geom).setGeneralPath(gpx);
3174
			return geom;
3175
		}
3176

  
3177
		if (shp instanceof Point2D) {
3178
			type = Geometry.TYPES.POINT;
3179
			geom = geomManager.create(type, Geometry.SUBTYPES.GEOM2D);
3180
			((Point2D)geom).setGeneralPath(gpx.);
3181
		}
3182

  
3183
		if (shp instanceof MultiPoint2D) {
3184
			type = Geometry.TYPES.MULTIPOINT;
3185
		}		
3186

  
3187
		return geom;
3188
	}
3189

  
3190 3252
	/**
3191 3253
	 * 
3192 3254
	 * @param r
......
3551 3613
		}
3552 3614

  
3553 3615
		if (shp instanceof Rectangle2D) {
3554
			return OracleUtils.rectangleToStruct((Rectangle2D) shp,
3555
					hasSrid, isView, _isGeoCS, o_srid, _conn);
3616
			return OracleUtils.rectangleToStruct((Rectangle2D) shp, hasSrid,
3617
					isView, _isGeoCS, o_srid, _conn);
3556 3618
		}
3557 3619

  
3558 3620
		try {
3559
			STRUCT the_struct = OracleUtils.fShapeToSTRUCT(shp, _conn,
3560
					_srid, agu_bien, hasSrid);
3621
			STRUCT the_struct = GeometryToSTRUCT(shp, _conn, _srid,
3622
					agu_bien, hasSrid);
3561 3623

  
3562 3624
			return the_struct;
3563 3625
		} catch (SQLException ex) {
......
3566 3628
			return null;
3567 3629
		}
3568 3630
	}
3569
	
3570
	 /**
3571
     * Utility method to transform a struct into a IGeometry.
3572
     *
3573
     * @param st the struct to be converted
3574
     * @param complex comes from a complex sdo geometry
3575
     * @return the IGeometry
3576
     */
3577
    private Geometry getFMapGeometry(STRUCT st, boolean complex) {
3578 3631

  
3579
    	GeometryManager geomManager = GeometryLocator.getGeometryManager();
3580
    	
3581
    	if (st == null) {
3582
    		return geomManager.createNullGeometry(Geometry.SUBTYPES.UNKNOWN);
3583
    	}
3584
    	
3632
	/**
3633
	 * Utility method to transform a struct into a IGeometry.
3634
	 * 
3635
	 * @param st
3636
	 *            the struct to be converted
3637
	 * @param complex
3638
	 *            comes from a complex sdo geometry
3639
	 * @return the IGeometry
3640
	 */
3641
	private Geometry getFMapGeometry(STRUCT st, boolean complex) {
3585 3642

  
3586
        Datum[] the_data = null;
3643
		GeometryManager geomManager = GeometryLocator.getGeometryManager();
3587 3644

  
3588
        try {
3589
            the_data = st.getOracleAttributes();
3645
		if (st == null) {
3646
			return geomManager.createNullGeometry(Geometry.SUBTYPES.UNKNOWN);
3647
		}
3590 3648

  
3591
            int jgtype = ((NUMBER) the_data[0]).intValue() % 1000;
3592
            jgtype = OracleUtils.oracleGTypeToFShapeType(jgtype, complex);
3649
		Datum[] the_data = null;
3593 3650

  
3594
            int dim = ((NUMBER) the_data[0]).intValue() / 1000;
3651
		try {
3652
			the_data = st.getOracleAttributes();
3595 3653

  
3596
            if (dim < 2) {
3597
                dim = 2;
3598
            }
3654
			int jgtype = ((NUMBER) the_data[0]).intValue() % 1000;
3655
			jgtype = OracleUtils.oracleGTypeToFShapeType(jgtype, complex);
3599 3656

  
3600
            Geometry ig = null;
3657
			int dim = ((NUMBER) the_data[0]).intValue() / 1000;
3601 3658

  
3602
            if (OracleUtils.isActuallyACollection(the_data)) {
3603
                jgtype = Geometry.TYPES.AGGREGATE;
3604
            }
3659
			if (dim < 2) {
3660
				dim = 2;
3661
			}
3605 3662

  
3606
            switch (jgtype) {
3607
            case Geometry.TYPES.AGGREGATE:
3663
			Geometry ig = null;
3608 3664

  
3609
                // int srid = ((NUMBER) the_data[1]).intValue();
3610
                ig = getFMapGeometryCollection(the_data, dim);
3665
			if (OracleUtils.isActuallyACollection(the_data)) {
3666
				jgtype = Geometry.TYPES.AGGREGATE;
3667
			}
3611 3668

  
3612
                break;
3669
			switch (jgtype) {
3670
			case Geometry.TYPES.AGGREGATE:
3613 3671

  
3614
            case Geometry.TYPES.POINT:
3615
                ig = OracleUtils.getFMapGeometryPoint(the_data, dim);
3672
				// int srid = ((NUMBER) the_data[1]).intValue();
3673
				ig = getFMapGeometryCollection(the_data, dim);
3616 3674

  
3617
                break;
3675
				break;
3618 3676

  
3619
            case Geometry.TYPES.CURVE:
3620
                ig = OracleUtils.getFMapGeometryMultiLineString(the_data, dim);
3677
			case Geometry.TYPES.POINT:
3678
				ig = OracleUtils.getFMapGeometryPoint(the_data, dim);
3621 3679

  
3622
                break;
3680
				break;
3623 3681

  
3624
            case Geometry.TYPES.SURFACE:
3625
                ig = OracleUtils.getFMapGeometryMultipolygon(the_data, dim);
3682
			case Geometry.TYPES.CURVE:
3683
				ig = OracleUtils.getFMapGeometryMultiLineString(the_data, dim);
3626 3684

  
3627
                break;
3628
            }
3685
				break;
3629 3686

  
3630
            return ig;
3631
        }
3632
        catch (SQLException e) {
3633
            logger.error(e);
3634
        }
3687
			case Geometry.TYPES.SURFACE:
3688
				ig = OracleUtils.getFMapGeometryMultipolygon(the_data, dim);
3635 3689

  
3636
        return null;
3637
    }
3638
    
3639
    private Geometry getFMapGeometryCollection(Datum[] the_data, int dim) {
3640
        // int __srid) {
3690
				break;
3691
			}
3641 3692

  
3642
    	NUMBER _srid = new NUMBER(0);
3643
        NUMBER main_type = new NUMBER((dim * 1000) +
3644
        		OracleUtils.getStructType(the_data));
3693
			return ig;
3694
		} catch (SQLException e) {
3695
			logger.error(e);
3696
		}
3645 3697

  
3698
		return null;
3699
	}
3646 3700

  
3647
        Datum[] all_info_array = null;
3648
        Object[] elems_info_aray = null;
3649
        Datum[] all_ords = null;
3701
	private Geometry getFMapGeometryCollection(Datum[] the_data, int dim) {
3702
		// int __srid) {
3650 3703

  
3651
        Object[] ords_of_groups = null;
3652
        Object[] _elems_info_aray = null;
3653
        try {
3654
            all_info_array = ((ARRAY) the_data[3]).getOracleArray();
3655
            elems_info_aray = OracleUtils.groupByElement(all_info_array);
3656
            all_ords = ((ARRAY) the_data[4]).getOracleArray();
3704
		NUMBER _srid = new NUMBER(0);
3705
		NUMBER main_type = new NUMBER((dim * 1000)
3706
				+ OracleUtils.getStructType(the_data));
3657 3707

  
3658
            ords_of_groups = OracleUtils.getOrdOfGroups(all_ords, elems_info_aray);
3659
            _elems_info_aray = new Object[elems_info_aray.length];
3660
        }
3661
        catch (SQLException e) {
3662
            logger.error("Unexpected error: " + e.getMessage());
3663
        }
3708
		Datum[] all_info_array = null;
3709
		Object[] elems_info_aray = null;
3710
		Datum[] all_ords = null;
3664 3711

  
3712
		Object[] ords_of_groups = null;
3713
		Object[] _elems_info_aray = null;
3714
		try {
3715
			all_info_array = ((ARRAY) the_data[3]).getOracleArray();
3716
			elems_info_aray = OracleUtils.groupByElement(all_info_array);
3717
			all_ords = ((ARRAY) the_data[4]).getOracleArray();
3665 3718

  
3666
        for (int i = 0; i < elems_info_aray.length; i++) {
3667
            _elems_info_aray[i] = OracleUtils.updateIndexes((Datum[]) elems_info_aray[i]);
3668
        }
3719
			ords_of_groups = OracleUtils.getOrdOfGroups(all_ords,
3720
					elems_info_aray);
3721
			_elems_info_aray = new Object[elems_info_aray.length];
3722
		} catch (SQLException e) {
3723
			logger.error("Unexpected error: " + e.getMessage());
3724
		}
3669 3725

  
3670
        // _elems_info_aray, ords_of_groups
3671
        int no_of_elems = ords_of_groups.length;
3672
        Geometry[] geoms = new Geometry[no_of_elems];
3726
		for (int i = 0; i < elems_info_aray.length; i++) {
3727
			_elems_info_aray[i] = OracleUtils
3728
					.updateIndexes((Datum[]) elems_info_aray[i]);
3729
		}
3673 3730

  
3674
        for (int i = 0; i < no_of_elems; i++) {
3675
            Datum[] item_info_array = null;
3676
            Datum[] item_ords = null;
3677
            NUMBER gtype = null;
3731
		// _elems_info_aray, ords_of_groups
3732
		int no_of_elems = ords_of_groups.length;
3733
		Geometry[] geoms = new Geometry[no_of_elems];
3678 3734

  
3679
            try {
3680
                item_info_array = (Datum[]) _elems_info_aray[i];
3681
                item_ords = (Datum[]) ords_of_groups[i];
3735
		for (int i = 0; i < no_of_elems; i++) {
3736
			Datum[] item_info_array = null;
3737
			Datum[] item_ords = null;
3738
			NUMBER gtype = null;
3682 3739

  
3683
                gtype = new NUMBER((dim * 1000) +
3684
                        (item_info_array[1].intValue() % 1000));
3740
			try {
3741
				item_info_array = (Datum[]) _elems_info_aray[i];
3742
				item_ords = (Datum[]) ords_of_groups[i];
3685 3743

  
3686
                if (tableHasSrid) {
3687
                	_srid = new NUMBER(Integer.parseInt(oracleSRID));
3688
                }
3689
            }
3690
            catch (SQLException se) {
3691
                logger.error("Unexpected error: " + se.getMessage());
3692
            }
3744
				gtype = new NUMBER((dim * 1000)
3745
						+ (item_info_array[1].intValue() % 1000));
3693 3746

  
3694
            // if it's the first geometry, the type is the collection's main type (no?) - no
3695
            // if (i == 0) gtype = main_type;
3747
				if (tableHasSrid) {
3748
					_srid = new NUMBER(Integer.parseInt(oracleSRID));
3749
				}
3750
			} catch (SQLException se) {
3751
				logger.error("Unexpected error: " + se.getMessage());
3752
			}
3696 3753

  
3697
            STRUCT itemst = null;
3754
			// if it's the first geometry, the type is the collection's main
3755
			// type (no?) - no
3756
			// if (i == 0) gtype = main_type;
3698 3757

  
3699
            if (tableHasSrid) {
3758
			STRUCT itemst = null;
3700 3759

  
3701
                itemst = OracleSpatialUtils.createStruct(gtype, _srid,
3702
                        item_info_array, item_ords, conn);
3703
            }
3704
            else {
3705
                itemst = OracleSpatialUtils.createStruct(gtype, null,
3706
                        item_info_array, item_ords, conn);
3707
            }
3760
			if (tableHasSrid) {
3708 3761

  
3709
            geoms[i] = getFMapGeometry(itemst, true);
3710
        }
3762
				itemst = OracleUtils.createStruct(gtype, _srid,
3763
						item_info_array, item_ords, conn);
3764
			} else {
3765
				itemst = OracleUtils.createStruct(gtype, null, item_info_array,
3766
						item_ords, conn);
3767
			}
3711 3768

  
3712
        return new FGeometryCollection(geoms);
3713
    }
3769
			geoms[i] = getFMapGeometry(itemst, true);
3770
		}
3714 3771

  
3772
		return new FGeometryCollection(geoms);
3773
	}
3774

  
3775
	/**
3776
	 * 
3777
	 * @param tname
3778
	 * @param suffix
3779
	 * @return
3780
	 */
3781
	public static String getDerivedName(String tname, String suffix) {
3782

  
3783
		int ind = tname.lastIndexOf(".");
3784
		if (ind == -1) {
3785

  
3786
			int l = Math.min(28, tname.length());
3787
			return tname.substring(0, l) + "_" + suffix;
3788

  
3789
		} else {
3790

  
3791
			String pre = tname.substring(0, ind);
3792
			String post = tname.substring(ind + 1, tname.length());
3793
			int lpost = Math.min(24, post.length());
3794
			int lpre = Math.min(3, pre.length());
3795
			return pre.substring(0, lpre) + "_" + post.substring(0, lpost)
3796
					+ "_" + suffix;
3797
		}
3798

  
3799
	}
3800

  
3715 3801
}

Also available in: Unified diff