001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) Copyright IBM Corporation, 2005. All rights reserved.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.data.db2;
018:
019: import org.geotools.factory.FactoryRegistryException;
020: import org.geotools.referencing.ReferencingFactoryFinder;
021: import org.opengis.referencing.FactoryException;
022: import org.opengis.referencing.crs.CoordinateReferenceSystem;
023: import java.sql.Connection;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.sql.Statement;
027:
028: /**
029: * Represent the coordinate system information maintained in the DB2 Spatial
030: * Extender catalog tables.
031: *
032: * @author David Adler - IBM Corporation
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/db2/src/main/java/org/geotools/data/db2/DB2CoordinateSystem.java $
034: */
035: public class DB2CoordinateSystem {
036: private int srsId;
037: private String coordsysName;
038: private CoordinateReferenceSystem crs = null;
039: private String organization;
040: private int coordsysId;
041: private String definition;
042:
043: /**
044: * Constructs a DB2CoordinateSystem.
045: *
046: * @param conn an open database connection.
047: * @param srsId the DB2 spatial reference system identifier.
048: *
049: * @throws SQLException if there was a database error attempting to load
050: * the coordinate system information.
051: */
052: public DB2CoordinateSystem(Connection conn, int srsId)
053: throws SQLException {
054: super ();
055: this .srsId = srsId;
056: this .loadFromDB(conn);
057: }
058:
059: /**
060: * Selects the coordinate system information from the DB2 spatial catalog
061: * table db2gse.ST_Spatial_Reference_Systems.
062: *
063: * <p></p>
064: *
065: * @param conn an open database connection
066: *
067: * @throws SQLException if there was a database error attempting to load
068: * the coordinate system information.
069: */
070: private void loadFromDB(Connection conn) throws SQLException {
071: String querySRS = "SELECT coordsys_name, organization, organization_coordsys_id, "
072: + " definition"
073: + " FROM db2gse.st_spatial_reference_systems"
074: + " WHERE srs_id = " + this .srsId;
075: Statement stmt = conn.createStatement();
076: ResultSet rs = stmt.executeQuery(querySRS);
077:
078: if (rs.next()) {
079: this .coordsysName = rs.getString(1);
080: this .organization = rs.getString(2);
081: this .coordsysId = rs.getInt(3);
082: this .definition = rs.getString(4);
083: } else {
084: throw new SQLException("Unrecognized srid '" + this .srsId
085: + "'");
086: }
087:
088: rs.close();
089: stmt.close();
090: }
091:
092: /**
093: * Returns the coordinate system name, organization and coordinate system
094: * identifier.
095: *
096: * @return the coordinate system name, organization and coordinate system
097: * identifier as a String.
098: */
099: public String toString() {
100: return this .coordsysName + "=" + this .organization + ":"
101: + this .coordsysId;
102: }
103:
104: /**
105: * Gets the coordinate system identifier, usually an EPSG or similar value.
106: *
107: * @return coordinate system identifier.
108: */
109: int getCsId() {
110: return this .coordsysId;
111: }
112:
113: /**
114: * Gets the OpenGIS CoordinateReferenceSystem for this DB2CoordinateSystem.
115: *
116: * @return a CoordinateReferenceSystem
117: *
118: * @throws FactoryRegistryException
119: * @throws FactoryException
120: */
121: CoordinateReferenceSystem getCRS() throws FactoryRegistryException,
122: FactoryException {
123: if (this.crs == null) {
124: this.crs = ReferencingFactoryFinder.getCRSFactory(null)
125: .createFromWKT(this.definition);
126: }
127:
128: return this.crs;
129: }
130: }
|