001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
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: package org.geotools.data.postgis.referencing;
017:
018: import java.io.IOException;
019: import java.sql.Connection;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Statement;
023:
024: import javax.sql.DataSource;
025:
026: import org.geotools.data.DataSourceException;
027: import org.geotools.data.Transaction;
028: import org.geotools.data.jdbc.ConnectionPool;
029: import org.geotools.data.jdbc.JDBCUtils;
030: import org.geotools.data.jdbc.referencing.JDBCAuthorityFactory;
031: import org.geotools.referencing.CRS;
032: import org.opengis.referencing.FactoryException;
033: import org.opengis.referencing.crs.CoordinateReferenceSystem;
034:
035: /**
036: * Access CRS informationj from the PostGIS SPATIAL_REF_SYS table.
037: *
038: * @author Jesse Eichar
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/postgis/src/main/java/org/geotools/data/postgis/referencing/PostgisAuthorityFactory.java $
040: */
041: public class PostgisAuthorityFactory extends JDBCAuthorityFactory {
042:
043: private String TABLE_NAME = "SPATIAL_REF_SYS";
044: private String WKT_COLUMN = "SRTEXT";
045: private String SRID_COLUMN = "SRID";
046: private String AUTH_NAME = "AUTH_NAME";
047: private String AUTH_SRID = "AUTH_SRID";
048:
049: /**
050: // * JD: Added this contstructor because the META_INF/services plugin
051: // * mechanism requires it.
052: // */
053: // public PostgisAuthorityFactory( ) {
054: // super(null);
055: // }
056: /**
057: * Construct <code>PostgisAuthorityFactory</code>.
058: *
059: * @param pool
060: */
061: public PostgisAuthorityFactory(DataSource pool) {
062: super (pool);
063: }
064:
065: public CoordinateReferenceSystem createCRS(int srid)
066: throws FactoryException, IOException {
067: Connection dbConnection = null;
068:
069: try {
070: String sqlStatement = "SELECT * FROM " + TABLE_NAME
071: + " WHERE " + SRID_COLUMN + " = " + srid;
072: dbConnection = dataSource.getConnection();
073:
074: Statement statement = dbConnection.createStatement();
075: ResultSet result = statement.executeQuery(sqlStatement);
076:
077: if (result.next()) {
078: CoordinateReferenceSystem crs = null;
079: try {
080: crs = createFromAuthority(result);
081: } catch (Exception e) {
082: // do nothing
083: }
084: if (crs == null) {
085: crs = createFromWKT(result);
086: }
087: JDBCUtils.close(statement);
088:
089: return crs;
090: } else {
091: String mesg = "No row found for " + srid
092: + " in table: " + TABLE_NAME;
093: throw new FactoryException(mesg);
094: }
095: } catch (SQLException sqle) {
096: String message = sqle.getMessage();
097:
098: throw new DataSourceException(message, sqle);
099: } finally {
100: JDBCUtils
101: .close(dbConnection, Transaction.AUTO_COMMIT, null);
102: }
103: }
104:
105: protected CoordinateReferenceSystem createFromWKT(ResultSet result)
106: throws DataSourceException, FactoryException {
107: try {
108: String wkt = result.getString(WKT_COLUMN);
109:
110: return factory.createFromWKT(wkt);
111:
112: } catch (SQLException sqle) {
113: String message = sqle.getMessage();
114:
115: throw new DataSourceException(message, sqle);
116: }
117: }
118:
119: protected CoordinateReferenceSystem createFromAuthority(
120: ResultSet result) throws DataSourceException,
121: FactoryException {
122: try {
123: String name = result.getString(AUTH_NAME);
124: int id = result.getInt(AUTH_SRID);
125:
126: return CRS.decode(name + ":" + id);
127: } catch (SQLException sqle) {
128: String message = sqle.getMessage();
129:
130: throw new DataSourceException(message, sqle);
131: }
132: }
133:
134: }
|