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.postgis;
028:
029: import java.sql.SQLException;
030:
031: import org.postgis.PGgeometry;
032:
033: import fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiCurve;
034: import fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiPoint;
035: import fr.ign.cogit.geoxygene.spatial.geomaggr.GM_MultiSurface;
036: import fr.ign.cogit.geoxygene.spatial.geomroot.GM_Object;
037: import fr.ign.cogit.geoxygene.util.conversion.ParseException;
038: import fr.ign.cogit.geoxygene.util.conversion.WktGeOxygene;
039:
040: /**
041: * Conversion des geometries PostGIS dans le format GeOxygene, et reciproquement.
042: *
043: * @author Thierry Badard & Arnaud Braun
044: * @version 1.1
045: *
046: */
047:
048: public class GeomGeOxygene2Postgis {
049:
050: public static Object sqlToJava(Object geom) {
051: PGgeometry pgGeom = (PGgeometry) geom;
052:
053: try {
054: /* In version 1.0.x of PostGIS, SRID is added to the beginning of the pgGeom string */
055:
056: //System.out.println(pgGeom.toString().substring(pgGeom.toString().indexOf(";")+1));
057: GM_Object geOxyGeom = WktGeOxygene.makeGeOxygene(pgGeom
058: .toString().substring(
059: pgGeom.toString().indexOf(";") + 1));
060: //GM_Object geOxyGeom = WktGeOxygene.makeGeOxygene(pgGeom.toString());
061:
062: if (geOxyGeom instanceof GM_MultiPoint) {
063: GM_MultiPoint aggr = (GM_MultiPoint) geOxyGeom;
064: if (aggr.size() == 1)
065: return aggr.get(0);
066: }
067:
068: if (geOxyGeom instanceof GM_MultiCurve) {
069: GM_MultiCurve aggr = (GM_MultiCurve) geOxyGeom;
070: if (aggr.size() == 1)
071: return aggr.get(0);
072: }
073:
074: if (geOxyGeom instanceof GM_MultiSurface) {
075: GM_MultiSurface aggr = (GM_MultiSurface) geOxyGeom;
076: if (aggr.size() == 1)
077: return aggr.get(0);
078: }
079:
080: return geOxyGeom;
081:
082: } catch (ParseException e) {
083: System.out
084: .println("## WARNING ## Postgis to GeOxygene returns NULL ");
085: e.printStackTrace();
086: return null;
087: }
088: }
089:
090: public static Object javaToSql(Object geom) {
091: try {
092: if (geom == null)
093: return null;
094: PGgeometry pgGeom = new PGgeometry(((GM_Object) geom)
095: .toString());
096: return pgGeom;
097: } catch (SQLException e) {
098: System.out
099: .println("## WARNING ## GeOxygene to Postgis returns NULL ");
100: e.printStackTrace();
101: return null;
102: }
103: }
104:
105: }
|