001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut Géographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut Géographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: package fr.ign.cogit.geoxygene.datatools.oracle;
028:
029: import oracle.sdoapi.geom.CoordPoint;
030: import oracle.sdoapi.geom.CoordPointImpl;
031: import oracle.sdoapi.geom.CurvePolygon;
032: import oracle.sdoapi.geom.CurveString;
033: import oracle.sdoapi.geom.Geometry;
034: import oracle.sdoapi.geom.GeometryCollection;
035: import oracle.sdoapi.geom.GeometryFactory;
036: import oracle.sdoapi.geom.LineString;
037: import oracle.sdoapi.geom.MultiCurvePolygon;
038: import oracle.sdoapi.geom.MultiCurveString;
039: import oracle.sdoapi.geom.MultiLineString;
040: import oracle.sdoapi.geom.MultiPoint;
041: import oracle.sdoapi.geom.MultiPolygon;
042: import oracle.sdoapi.geom.Point;
043: import oracle.sdoapi.geom.Polygon;
044: import oracle.sdoapi.geom.Segment;
045: import fr.ign.cogit.geoxygene.spatial.coordgeom.DirectPosition;
046: import fr.ign.cogit.geoxygene.spatial.coordgeom.DirectPositionList;
047: import fr.ign.cogit.geoxygene.spatial.coordgeom.GM_CurveSegment;
048: import fr.ign.cogit.geoxygene.spatial.coordgeom.GM_LineString;
049: import fr.ign.cogit.geoxygene.spatial.coordgeom.GM_Polygon;
050: import fr.ign.cogit.geoxygene.spatial.geomaggr.GM_Aggregate;
051: import fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiCurve;
052: import fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiPoint;
053: import fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiSurface;
054: import fr.ign.cogit.geoxygene.spatial.geomprim.GM_Curve;
055: import fr.ign.cogit.geoxygene.spatial.geomprim.GM_Point;
056: import fr.ign.cogit.geoxygene.spatial.geomprim.GM_Ring;
057: import fr.ign.cogit.geoxygene.spatial.geomprim.GM_Surface;
058: import fr.ign.cogit.geoxygene.spatial.geomroot.GM_Object;
059:
060: /**
061: * Méthodes de conversion du format SDOAPI vers le format ISO, et réciproquement.
062: * Le format SDOAPI permet ensuite d'importer et d'exporter facilement dans Oracle.
063: *
064: * <P> A priori achevé, MAIS
065: * ne fonctionne pas pour les arcs de cercles et
066: * ne fonctionne pas pour les GM_Surface composées de plusieurs patch.
067: *
068: * @author Thierry Badard & Arnaud Braun
069: * @version 1.1
070: */
071:
072: public class IsoAndSdo {
073:
074: ///////////////////////////////////////////////////////////////////////////////////////////////////////
075: ///////////////////////////////////////////////////////////////////////////////////////////////////////
076: // sdoapi2iso /////////////////////////////////////////////////////////////////////////////////////////
077: ///////////////////////////////////////////////////////////////////////////////////////////////////////
078: ///////////////////////////////////////////////////////////////////////////////////////////////////////
079:
080: /** Convertit du format SDOAPI vers notre format ISO. Si on a null en entrée, renvoie null.
081: * Cette méthode publique appelle la méthode privée "sdoapi2iso_xxxxx" adéquate, selon le type de géométrie rencontrée.*/
082: public static GM_Object sdoapi2iso(Geometry geom) throws Exception {
083: GM_Object gm_o = null;
084: int n = 3; // dimension par defaut (avant on la passait en parametre)
085: if (geom != null) {
086: java.lang.Class geomClass = geom.getGeometryType();
087: if (geomClass == java.lang.Class
088: .forName("oracle.sdoapi.geom.Point"))
089: gm_o = sdoapi2iso_point((Point) geom, n);
090: else if (geomClass == java.lang.Class
091: .forName("oracle.sdoapi.geom.LineString"))
092: gm_o = sdoapi2iso_linestring((LineString) geom, n);
093: else if (geomClass == java.lang.Class
094: .forName("oracle.sdoapi.geom.Polygon"))
095: gm_o = sdoapi2iso_polygon((Polygon) geom, n);
096: else if (geomClass == java.lang.Class
097: .forName("oracle.sdoapi.geom.CurveString"))
098: gm_o = sdoapi2iso_curvestring((CurveString) geom, n);
099: else if (geomClass == java.lang.Class
100: .forName("oracle.sdoapi.geom.CurvePolygon"))
101: gm_o = sdoapi2iso_curvepolygon((CurvePolygon) geom, n);
102: else if (geomClass == java.lang.Class
103: .forName("oracle.sdoapi.geom.GeometryCollection"))
104: gm_o = sdoapi2iso_geometrycollection(
105: (GeometryCollection) geom, n);
106: else if (geomClass == java.lang.Class
107: .forName("oracle.sdoapi.geom.MultiPoint"))
108: gm_o = sdoapi2iso_multipoint((MultiPoint) geom, n);
109: else if (geomClass == java.lang.Class
110: .forName("oracle.sdoapi.geom.MultiLineString"))
111: gm_o = sdoapi2iso_multilinestring(
112: (MultiLineString) geom, n);
113: else if (geomClass == java.lang.Class
114: .forName("oracle.sdoapi.geom.MultiPolygon"))
115: gm_o = sdoapi2iso_multipolygon((MultiPolygon) geom, n);
116: else if (geomClass == java.lang.Class
117: .forName("oracle.sdoapi.geom.MultiCurveString"))
118: gm_o = sdoapi2iso_multicurvestring(
119: (MultiCurveString) geom, n);
120: else if (geomClass == java.lang.Class
121: .forName("oracle.sdoapi.geom.MultiCurvePolygon"))
122: gm_o = sdoapi2iso_multicurvepolygon(
123: (MultiCurvePolygon) geom, n);
124: else
125: throw new Exception("Format sdoapi non reconnu : "
126: + geomClass.getName());
127: }
128: return gm_o;
129: }
130:
131: /** Convertit un Point SDOAPI en un GM_Point ISO */
132: private static GM_Point sdoapi2iso_point(Point sdoPoint, int n)
133: throws Exception {
134: double[] tab = new double[n];
135: for (int j = 0; j < n; j++)
136: tab[j] = sdoPoint.getOrd(j);
137: DirectPosition direct_pt = new DirectPosition(tab);
138: GM_Point thePoint = new GM_Point(direct_pt);
139: return thePoint;
140: }
141:
142: // VERSION AVANT HERITAGE DE GM_LINESTRING SUR GM_CURVE
143: /** Convertit une LineString SDOAPI en une GM_Curve ISO, composée d'une et d'une seule GM_LineString. */
144: /* private static GM_Curve sdoapi2iso_linestring (LineString sdoLineString) throws Exception {
145: GM_LineString isoLineString = new GM_LineString();
146: int n = sdoLineString.getCoordinateDimension();
147: int nbpts = sdoLineString.getNumPoints();
148: for (int i=0; i<nbpts; i++) {
149: CoordPoint coord_pt = sdoLineString.getPointAt(i);
150: double[] tab = new double[n];
151: for (int j=0; j<n; j++) tab[j] = coord_pt.getOrd(j);
152: DirectPosition direct_pt = new DirectPosition(tab);
153: isoLineString.appendControlPoint(direct_pt);
154: }
155: GM_Curve isoCurve = new GM_Curve();
156: isoCurve.appendSegment(isoLineString);
157: return isoCurve;
158: }*/
159:
160: /** Convertit une LineString SDOAPI en une GM_LineString ISO. */
161: private static GM_LineString sdoapi2iso_linestring(
162: LineString sdoLineString, int n) throws Exception {
163: GM_LineString isoLineString = new GM_LineString();
164: int nbpts = sdoLineString.getNumPoints();
165: double[] tab = new double[n];
166: for (int i = 0; i < nbpts; i++) {
167: CoordPoint coord_pt = sdoLineString.getPointAt(i);
168: for (int j = 0; j < n; j++)
169: tab[j] = coord_pt.getOrd(j);
170: DirectPosition direct_pt = new DirectPosition(tab);
171: isoLineString.getControlPoint().add(direct_pt);
172: }
173: return isoLineString;
174: }
175:
176: // A REPRENDRE quand on aura implementé GM_ArcString
177: /** Convertit une CurveString SDOAPI en une GM_Curve ISO.
178: * A TERMINER : pour l'instant je genere une GM_Curve composee uniquement de GM_LineString.
179: * Les segments éventuellement circulaires sont linéarisés (pas de 10, en dur).
180: * Il faudra generer une vraie GM_Curve composee de segments éventuellement circulaires. */
181: private static GM_Curve sdoapi2iso_curvestring(
182: CurveString sdoCurveString, int n) throws Exception {
183: GM_Curve isoCurve = new GM_Curve();
184: int nbseg = sdoCurveString.getNumSegments();
185: Segment SegmentArray[] = sdoCurveString.getSegmentArray();
186: for (int j = 0; j < nbseg; j++) {
187: Segment segment = SegmentArray[j];
188: if (segment.getSegmentType() == java.lang.Class
189: .forName("oracle.sdoapi.geom.CircularArc")) { // c'est ca qu'il faudra changer
190: Segment segment_arc = (Segment) segment.clone();
191: segment = segment_arc.linearizeSegment(10);
192: } else if (segment.getSegmentType() != java.lang.Class
193: .forName("oracle.sdoapi.geom.LinearSegment"))
194: throw new Exception(
195: "Cas non traité dans la conversion de SDOAPI en ISO (ni linéaire, ni arc de cercle)");
196: GM_LineString isoLineString = new GM_LineString();
197: int nbpts = segment.getNumPoints();
198: for (int i = 0; i < nbpts; i++) {
199: CoordPoint coord_pt = segment.getPointAt(i);
200: double tab[] = new double[n];
201: for (int k = 0; k < n; k++)
202: tab[k] = coord_pt.getOrd(k);
203: DirectPosition direct_pt = new DirectPosition(tab);
204: isoLineString.getControlPoint().add(direct_pt);
205: }
206: isoCurve.addSegment(isoLineString);
207: }
208: return isoCurve;
209: }
210:
211: // VERSION AVANT HERITAGE DE GM_POLYGON SUR GM_SURFACE
212: /** Convertit un Polygon SDOAPI en une GM_Surface ISO, composée d'un et d'un seul GM_Polygon. */
213: /* private static GM_Surface sdoapi2iso_polygon (Polygon sdoPolygon) throws Exception {
214: LineString sdoExteriorRing = (LineString)sdoPolygon.getExteriorRing();
215: GM_Curve isoCurve = IsoAndSdo.sdoapi2iso_linestring(sdoExteriorRing);
216: GM_Ring isoRing = new GM_Ring (isoCurve);
217: GM_SurfaceBoundary isoBoundary = new GM_SurfaceBoundary(isoRing);
218: if (sdoPolygon.getNumRings() > 1) {
219: LineString[] sdoInteriorRing = (LineString[])sdoPolygon.getInteriorRingArray();
220: for (int i=0; i<sdoInteriorRing.length; i++) {
221: isoCurve = IsoAndSdo.sdoapi2iso_linestring(sdoInteriorRing[i]);
222: isoRing = new GM_Ring (isoCurve);
223: isoBoundary.appendInterior(isoRing);
224: }
225: }
226: GM_Polygon isoPoly = new GM_Polygon(isoBoundary);
227: GM_Surface isoSurface = new GM_Surface(isoPoly);
228: return isoSurface;
229: }
230:
231:
232: // VERSION AVANT HERITAGE DE GM_POLYGON SUR GM_SURFACE
233: /** Convertit un CurvePolygon SDOAPI en une GM_Surface ISO, composée d'un et d'un seul GM_Polygon. */
234: /* private static GM_Surface sdoapi2iso_curvepolygon (CurvePolygon sdoCurvePolygon) throws Exception {
235: CurveString sdoExteriorRing = sdoCurvePolygon.getExteriorRing();
236: GM_Curve isoCurve = IsoAndSdo.sdoapi2iso_curvestring(sdoExteriorRing);
237: GM_Ring isoRing = new GM_Ring (isoCurve);
238: GM_SurfaceBoundary isoBoundary = new GM_SurfaceBoundary(isoRing);
239: if (sdoCurvePolygon.getNumRings() > 1) {
240: CurveString[] sdoInteriorRing = sdoCurvePolygon.getInteriorRingArray();
241: for (int i=0; i<sdoInteriorRing.length; i++) {
242: isoCurve = IsoAndSdo.sdoapi2iso_curvestring(sdoInteriorRing[i]);
243: isoRing = new GM_Ring (isoCurve);
244: isoBoundary.appendInterior(isoRing);
245: }
246: }
247: GM_Polygon isoPoly = new GM_Polygon(isoBoundary);
248: GM_Surface isoSurface = new GM_Surface(isoPoly);
249: return isoSurface;
250: }
251:
252:
253: /** Convertit un Polygon SDOAPI en un GM_Polygon ISO. */
254: private static GM_Polygon sdoapi2iso_polygon(Polygon sdoPolygon,
255: int n) throws Exception {
256: LineString sdoExteriorRing = (LineString) sdoPolygon
257: .getExteriorRing();
258: GM_Curve isoCurve = IsoAndSdo.sdoapi2iso_linestring(
259: sdoExteriorRing, n);
260: GM_Ring isoRing = new GM_Ring(isoCurve);
261: GM_Polygon isoPoly = new GM_Polygon(isoRing);
262: if (sdoPolygon.getNumRings() > 1) {
263: LineString[] sdoInteriorRing = (LineString[]) sdoPolygon
264: .getInteriorRingArray();
265: for (int i = 0; i < sdoInteriorRing.length; i++) {
266: isoCurve = IsoAndSdo.sdoapi2iso_linestring(
267: sdoInteriorRing[i], n);
268: isoRing = new GM_Ring(isoCurve);
269: isoPoly.addInterior(isoRing);
270: }
271: }
272: return isoPoly;
273: }
274:
275: /** Convertit un CurvePolygon SDOAPI en un GM_Polygon ISO. */
276: private static GM_Polygon sdoapi2iso_curvepolygon(
277: CurvePolygon sdoCurvePolygon, int n) throws Exception {
278: CurveString sdoExteriorRing = sdoCurvePolygon.getExteriorRing();
279: GM_Curve isoCurve = IsoAndSdo.sdoapi2iso_curvestring(
280: sdoExteriorRing, n);
281: GM_Ring isoRing = new GM_Ring(isoCurve);
282: GM_Polygon isoPoly = new GM_Polygon(isoRing);
283: if (sdoCurvePolygon.getNumRings() > 1) {
284: CurveString[] sdoInteriorRing = sdoCurvePolygon
285: .getInteriorRingArray();
286: for (int i = 0; i < sdoInteriorRing.length; i++) {
287: isoCurve = IsoAndSdo.sdoapi2iso_curvestring(
288: sdoInteriorRing[i], n);
289: isoRing = new GM_Ring(isoCurve);
290: isoPoly.addInterior(isoRing);
291: }
292: }
293: return isoPoly;
294: }
295:
296: /** Convertit une GeometryCollection SDOAPI en un GM_Aggregate ISO */
297: private static GM_Aggregate sdoapi2iso_geometrycollection(
298: GeometryCollection sdoCollection, int m) throws Exception {
299: GM_Aggregate isoAggregate = new GM_Aggregate();
300: int n = sdoCollection.getNumGeometries();
301: for (int i = 0; i < n; i++) {
302: Geometry sdoGeom = sdoCollection.getGeometryAt(i);
303: GM_Object isoGeom = IsoAndSdo.sdoapi2iso(sdoGeom/*,m*/);
304: isoAggregate.add(isoGeom);
305: }
306: return isoAggregate;
307: }
308:
309: /** Convertit un MultiPoint SDOAPI en un GM_MultiPoint ISO */
310: private static GM_MultiPoint sdoapi2iso_multipoint(
311: MultiPoint sdoCollection, int m) throws Exception {
312: GM_MultiPoint isoAggregate = new GM_MultiPoint();
313: int n = sdoCollection.getNumGeometries();
314: for (int i = 0; i < n; i++) {
315: Geometry sdoGeom = sdoCollection.getGeometryAt(i);
316: GM_Object isoGeom = IsoAndSdo.sdoapi2iso(sdoGeom/*,m*/);
317: isoAggregate.add(isoGeom);
318: }
319: return isoAggregate;
320: }
321:
322: /** Convertit un MultiLineString SDOAPI en un GM_MultiCurve ISO */
323: private static GM_MultiCurve sdoapi2iso_multilinestring(
324: MultiLineString sdoCollection, int m) throws Exception {
325: GM_MultiCurve isoAggregate = new GM_MultiCurve();
326: int n = sdoCollection.getNumGeometries();
327: for (int i = 0; i < n; i++) {
328: Geometry sdoGeom = sdoCollection.getGeometryAt(i);
329: GM_Object isoGeom = IsoAndSdo.sdoapi2iso(sdoGeom/*,m*/);
330: isoAggregate.add(isoGeom);
331: }
332: return isoAggregate;
333: }
334:
335: /** Convertit un MultiCurveString SDOAPI en un GM_MultiCurve ISO */
336: private static GM_MultiCurve sdoapi2iso_multicurvestring(
337: MultiCurveString sdoCollection, int m) throws Exception {
338: GM_MultiCurve isoAggregate = new GM_MultiCurve();
339: int n = sdoCollection.getNumGeometries();
340: for (int i = 0; i < n; i++) {
341: Geometry sdoGeom = sdoCollection.getGeometryAt(i);
342: GM_Object isoGeom = IsoAndSdo.sdoapi2iso(sdoGeom/*,m*/);
343: isoAggregate.add(isoGeom);
344: }
345: return isoAggregate;
346: }
347:
348: /** Convertit un MultiPolygon SDOAPI en un GM_MultiPolygon ISO */
349: private static GM_MultiSurface sdoapi2iso_multipolygon(
350: MultiPolygon sdoCollection, int m) throws Exception {
351: GM_MultiSurface isoAggregate = new GM_MultiSurface();
352: int n = sdoCollection.getNumGeometries();
353: for (int i = 0; i < n; i++) {
354: Geometry sdoGeom = sdoCollection.getGeometryAt(i);
355: GM_Object isoGeom = IsoAndSdo.sdoapi2iso(sdoGeom/*,m*/);
356: isoAggregate.add(isoGeom);
357: }
358: return isoAggregate;
359: }
360:
361: /** Convertit un MultiCurvePolygon SDOAPI en un GM_MultiPolygon ISO */
362: private static GM_MultiSurface sdoapi2iso_multicurvepolygon(
363: MultiCurvePolygon sdoCollection, int m) throws Exception {
364: GM_MultiSurface isoAggregate = new GM_MultiSurface();
365: int n = sdoCollection.getNumGeometries();
366: for (int i = 0; i < n; i++) {
367: Geometry sdoGeom = sdoCollection.getGeometryAt(i);
368: GM_Object isoGeom = IsoAndSdo.sdoapi2iso(sdoGeom/*,m*/);
369: isoAggregate.add(isoGeom);
370: }
371: return isoAggregate;
372: }
373:
374: ///////////////////////////////////////////////////////////////////////////////////////////////////////
375: ///////////////////////////////////////////////////////////////////////////////////////////////////////
376: // iso2sdoapi /////////////////////////////////////////////////////////////////////////////////////////
377: ///////////////////////////////////////////////////////////////////////////////////////////////////////
378: ///////////////////////////////////////////////////////////////////////////////////////////////////////
379:
380: /** Convertit de notre format ISO vers le format SDOAPI. Si on a null en entrée, renvoie null.
381: * Cette méthode publique appelle la méthode privée "iso2sdoapi_xxxxx" adéquate, selon le type de géométrie rencontrée.*/
382: public static Geometry iso2sdoapi(GeometryFactory gf, GM_Object gm_o)
383: throws Exception {
384: Geometry geom = null;
385: if (gm_o != null) {
386: java.lang.Class gmoClass = gm_o.getClass();
387: if (gmoClass == java.lang.Class
388: .forName("fr.ign.cogit.geoxygene.spatial.geomprim.GM_Point"))
389: geom = iso2sdoapi_point(gf, (GM_Point) gm_o); // a faire
390: else if (gmoClass == java.lang.Class
391: .forName("fr.ign.cogit.geoxygene.spatial.geomprim.GM_Curve"))
392: geom = iso2sdoapi_curve(gf, (GM_Curve) gm_o);
393: else if (gmoClass == java.lang.Class
394: .forName("fr.ign.cogit.geoxygene.spatial.coordgeom.GM_LineString"))
395: geom = iso2sdoapi_linestring(gf, (GM_LineString) gm_o);
396: else if (gmoClass == java.lang.Class
397: .forName("fr.ign.cogit.geoxygene.spatial.geomprim.GM_Surface"))
398: geom = iso2sdoapi_surface(gf, (GM_Surface) gm_o);
399: else if (gmoClass == java.lang.Class
400: .forName("fr.ign.cogit.geoxygene.spatial.coordgeom.GM_Polygon"))
401: geom = iso2sdoapi_polygon(gf, (GM_Polygon) gm_o);
402: else if (gmoClass == java.lang.Class
403: .forName("fr.ign.cogit.geoxygene.spatial.geomaggr.GM_Aggregate"))
404: geom = iso2sdoapi_aggregate(gf, (GM_Aggregate) gm_o);
405: else if (gmoClass == java.lang.Class
406: .forName("fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiPoint"))
407: geom = iso2sdoapi_multipoint(gf, (GM_MultiPoint) gm_o);
408: else if (gmoClass == java.lang.Class
409: .forName("fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiCurve"))
410: geom = iso2sdoapi_multicurve(gf, (GM_MultiCurve) gm_o);
411: else if (gmoClass == java.lang.Class
412: .forName("fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiSurface"))
413: geom = iso2sdoapi_multisurface(gf,
414: (GM_MultiSurface) gm_o);
415: else
416: throw new Exception(
417: "Impossible de rendre ce type de géométrie ISO persistante : "
418: + gmoClass.getName());
419: }
420: return geom;
421: }
422:
423: /** Convertit un GM_Point ISO en un Point SDOAPI. */
424: private static Geometry iso2sdoapi_point(GeometryFactory gf,
425: GM_Point isoPoint) throws Exception {
426: CoordPoint coordPoint = new CoordPointImpl();
427: for (int k = 0; k < isoPoint.getPosition().getDimension(); k++) {
428: try {
429: double x = isoPoint.getPosition().getCoordinate(k);
430: if (!Double.isNaN(x))
431: coordPoint.setOrd(x, k);
432: } catch (Exception e) {
433: throw new Exception(e.getMessage());
434: }
435: }
436: Geometry geom = gf.createPoint(coordPoint);
437: return geom;
438: }
439:
440: /** Convertit une GM_Curve ISO en un type de courbe SDOAPI.
441: * Si la courbe est compose d'un seul segment qui est une polyligne, on appelle iso2sdoapi_linestring, qui génère une LineString.
442: * Sinon on appelle iso2sdoapi_curvestring, qui génère une CurveString. */
443: private static Geometry iso2sdoapi_curve(GeometryFactory gf,
444: GM_Curve isoCurve) throws Exception {
445: Geometry geom = null;
446: if ((isoCurve.sizeSegment() == 1)
447: && (isoCurve.getSegment(0).getInterpolation() == "linear"))
448: geom = iso2sdoapi_linestring(gf, (GM_LineString) (isoCurve
449: .getSegment(0)));
450: else
451: geom = iso2sdoapi_curvestring(gf, isoCurve);
452: return geom;
453: }
454:
455: /** Génère une LineString SDOAPI à partir d'une GM_Curve ISO constituée d'une et d'une seule GM_LineString. */
456: private static Geometry iso2sdoapi_linestring(GeometryFactory gf,
457: GM_LineString isoCurve) throws Exception {
458: Geometry geom = null;
459: DirectPositionList listOfPoints = isoCurve.coord();
460: CoordPoint[] sdoPointArray = new CoordPoint[listOfPoints.size()];
461: listOfPoints.initIterator();
462: int j = 0;
463: while (listOfPoints.hasNext()) {
464: DirectPosition isoPoint = listOfPoints.next();
465: CoordPoint sdoPoint = new CoordPointImpl();
466: for (int k = 0; k < isoPoint.getDimension(); k++) {
467: try {
468: double x = isoPoint.getCoordinate(k);
469: // sdoPoint.setOrd(x,k);
470: if (!Double.isNaN(x))
471: sdoPoint.setOrd(x, k);
472: } catch (Exception e) {
473: throw new Exception(e.getMessage());
474: }
475: }
476: sdoPointArray[j] = sdoPoint;
477: j++;
478: }
479: geom = gf.createLineString(sdoPointArray);
480: return geom;
481: }
482:
483: // A REPRENDRE quand on aura implementé GM_ArcString
484: /** Génère une CurveString SDOAPI à partir d'une GM_Curve ISO. Ne fonctionne que pour des segments linéaires.
485: * A TERMINER : pour l'instant je genere une CurveString composee uniquement de segments linéaires.
486: * Il faudra generer une vraie CurveString composée de segments éventuellement circulaires. */
487: private static Geometry iso2sdoapi_curvestring(GeometryFactory gf,
488: GM_Curve isoCurve) throws Exception {
489: Geometry geom = null;
490: String sdoClassName = "";
491: Segment[] sdoSegmentArray = new Segment[isoCurve.sizeSegment()];
492: for (int i = 0; i < isoCurve.sizeSegment(); i++) {
493: GM_CurveSegment isoSegment = isoCurve.getSegment(i);
494: // prévoir les cas : Bezier, Circular
495: if (isoSegment.getInterpolation() == "linear")
496: sdoClassName = "oracle.sdoapi.geom.LinearSegment";
497: else
498: throw new Exception(
499: "Cas non traité dans la conversion de ISO en SDOAPI (géométrie non linéaire)");
500: DirectPositionList listOfPoints = isoSegment.coord();
501: CoordPoint[] sdoPointArray = new CoordPoint[listOfPoints
502: .size()];
503: listOfPoints.initIterator();
504: int j = 0;
505: while (listOfPoints.hasNext()) {
506: DirectPosition isoPoint = listOfPoints.next();
507: CoordPoint sdoPoint = new CoordPointImpl();
508: for (int k = 0; k < isoPoint.getDimension(); k++) {
509: try {
510: double x = isoPoint.getCoordinate(k);
511: if (!Double.isNaN(x))
512: sdoPoint.setOrd(x, k);
513: } catch (Exception e) {
514: throw new Exception(e.getMessage());
515: }
516:
517: }
518: sdoPointArray[j] = sdoPoint;
519: j++;
520: }
521: Segment sdoSegment = gf.createSegment(java.lang.Class
522: .forName(sdoClassName), sdoPointArray);
523: sdoSegmentArray[i] = sdoSegment;
524: }
525: geom = gf.createCurveString(sdoSegmentArray);
526: return geom;
527: }
528:
529: /** Convertit une GM_Surface ISO en un type de surface SDOAPI.
530: * Si la surface est compose d'un seul morceau qui est un polygone, on appelle iso2sdoapi_polygon, qui génère un Polygon ou un CurvePolygon.
531: * Sinon : A FAIRE. */
532: private static Geometry iso2sdoapi_surface(GeometryFactory gf,
533: GM_Surface isoSurface) throws Exception {
534: Geometry geom = null;
535: if ((isoSurface.sizePatch() == 1)
536: && (isoSurface.getPatch(0).getInterpolation() == "planar"))
537: geom = iso2sdoapi_polygon(gf, (GM_Polygon) isoSurface);
538: else
539: throw new Exception(
540: "On ne peut rendre persistante qu'une surface composée d'un et d'un seul patch qui est un GM_Polygon");
541: return geom;
542: }
543:
544: /** Génère un Polygon ou un CurvePolygon SDOAPI à partir d'un GM_Polygon ISO. */
545: private static Geometry iso2sdoapi_polygon(GeometryFactory gf,
546: GM_Polygon isoSurface) throws Exception {
547: Geometry sdoGeom = null;
548: if (isoSurface.sizeExterior() == 1) {
549: GM_Ring isoExtRing = isoSurface.getExterior();
550: int nInt = isoSurface.sizeInterior();
551: if (nInt == 0) {
552: GM_Curve isoExtCurve = isoExtRing.getPrimitive();
553: Geometry sdoExtCurve = IsoAndSdo.iso2sdoapi_curve(gf,
554: isoExtCurve);
555: if (sdoExtCurve.getGeometryType().getName() == "oracle.sdoapi.geom.LineString") {
556: LineString[] sdoInterior = null;
557: sdoGeom = gf.createPolygon(
558: (LineString) sdoExtCurve, sdoInterior);
559: }
560: if (sdoExtCurve.getGeometryType().getName() == "oracle.sdoapi.geom.CurveString") {
561: CurveString[] sdoInterior = null;
562: sdoGeom = gf.createCurvePolygon(
563: (CurveString) sdoExtCurve, sdoInterior);
564: }
565:
566: } else { // il y a des anneaux interieurs
567: GM_Curve isoExtCurve = isoExtRing.getPrimitive();
568: Geometry sdoExtCurve = IsoAndSdo.iso2sdoapi_curve(gf,
569: isoExtCurve);
570: Geometry[] sdoIntCurve = new Geometry[nInt];
571: for (int i = 0; i < nInt; i++) {
572: GM_Ring isoIntRing = isoSurface.getInterior(i);
573: GM_Curve theIsoIntCurve = isoIntRing.getPrimitive();
574: sdoIntCurve[i] = IsoAndSdo.iso2sdoapi_curve(gf,
575: theIsoIntCurve);
576: }
577:
578: boolean flagLineString = true;
579: if (sdoExtCurve.getGeometryType().getName() == "oracle.sdoapi.geom.CurveString")
580: flagLineString = false;
581: else
582: for (int i = 0; i < nInt; i++)
583: if (sdoIntCurve[i].getGeometryType().getName() == "oracle.sdoapi.geom.CurveString") {
584: flagLineString = false;
585: break;
586: }
587:
588: if (flagLineString == true) {
589: LineString[] sdoIntLineString = new LineString[nInt];
590: for (int i = 0; i < nInt; i++)
591: sdoIntLineString[i] = (LineString) sdoIntCurve[i];
592: sdoGeom = gf.createPolygon(
593: (LineString) sdoExtCurve, sdoIntLineString);
594: } else {
595: CurveString[] sdoIntCurveString = new CurveString[nInt];
596: for (int i = 0; i < nInt; i++)
597: sdoIntCurveString[i] = (CurveString) sdoIntCurve[i];
598: sdoGeom = gf.createCurvePolygon(
599: (CurveString) sdoExtCurve,
600: sdoIntCurveString);
601: }
602: }
603: } else
604: throw new Exception(
605: "Cas non géré : cardExterior != 1 pour un polygone");
606:
607: return sdoGeom;
608: }
609:
610: /** Génère une GeometryCollection SDOAPI à partir d'un GM_Aggregate ISO */
611: private static Geometry iso2sdoapi_aggregate(GeometryFactory gf,
612: GM_Aggregate isoCollection) throws Exception {
613: Geometry sdoCollection = null;
614: int n = isoCollection.size();
615: Geometry[] sdoArray = new Geometry[n];
616: for (int i = 0; i < n; i++) {
617: GM_Object isoGeom = isoCollection.get(i);
618: Geometry sdoGeom = IsoAndSdo.iso2sdoapi(gf, isoGeom);
619: sdoArray[i] = sdoGeom;
620: }
621: sdoCollection = gf.createGeometryCollection(sdoArray);
622: return sdoCollection;
623: }
624:
625: /** Génère un MultiPoint SDOAPI à partir d'un GM_MultiPoint ISO */
626: private static Geometry iso2sdoapi_multipoint(GeometryFactory gf,
627: GM_MultiPoint isoCollection) throws Exception {
628: Geometry sdoMultiPoint = null;
629: int n = isoCollection.size();
630: Point[] sdoArray = new Point[n];
631: for (int i = 0; i < n; i++) {
632: GM_Point isoGeom = (GM_Point) isoCollection.get(i);
633: Point sdoGeom = (Point) IsoAndSdo.iso2sdoapi(gf, isoGeom);
634: sdoArray[i] = sdoGeom;
635: }
636: sdoMultiPoint = gf.createGeometryCollection(sdoArray);
637: return sdoMultiPoint;
638: }
639:
640: /** Génère un MultiCurveString SDOAPI à partir d'un GM_MultiCurve ISO */
641: private static Geometry iso2sdoapi_multicurve(GeometryFactory gf,
642: GM_MultiCurve isoCollection) throws Exception {
643: Geometry sdoMultiCurveString = null;
644: int n = isoCollection.size();
645: CurveString[] sdoArray = new CurveString[n];
646: for (int i = 0; i < n; i++) {
647: GM_Curve isoGeom = (GM_Curve) isoCollection.get(i);
648: CurveString sdoGeom = (CurveString) IsoAndSdo.iso2sdoapi(
649: gf, isoGeom);
650: sdoArray[i] = sdoGeom;
651: }
652: sdoMultiCurveString = gf.createGeometryCollection(sdoArray);
653: return sdoMultiCurveString;
654: }
655:
656: /** Génère un MultiCurvePolygon SDOAPI à partir d'un GM_MultiSurface ISO */
657: private static Geometry iso2sdoapi_multisurface(GeometryFactory gf,
658: GM_MultiSurface isoCollection) throws Exception {
659: Geometry sdoMultiCurvePolygon = null;
660: int n = isoCollection.size();
661: CurvePolygon[] sdoArray = new CurvePolygon[n];
662: for (int i = 0; i < n; i++) {
663: GM_Surface isoGeom = (GM_Surface) isoCollection.get(i);
664: CurvePolygon sdoGeom = (CurvePolygon) IsoAndSdo.iso2sdoapi(
665: gf, isoGeom);
666: sdoArray[i] = sdoGeom;
667: }
668: sdoMultiCurvePolygon = gf.createGeometryCollection(sdoArray);
669: return sdoMultiCurvePolygon;
670: }
671:
672: }
|