Statistics
| Revision:

root / org.gvsig.geotools.proj / trunk / org.gvsig.geotools.proj / org.gvsig.geotools.proj.esri / src / main / java / org / gvsig / geotools / proj / esri / EsriFactoryUsingHSQL.java @ 875

History | View | Annotate | Download (6.76 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2018 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geotools.proj.esri;
25

    
26
import java.sql.Connection;
27
import java.sql.PreparedStatement;
28
import java.sql.ResultSet;
29
import java.sql.SQLException;
30
import java.sql.Statement;
31
import java.text.ParseException;
32
import java.util.Hashtable;
33
import java.util.LinkedHashSet;
34
import java.util.Map;
35
import java.util.Set;
36
import java.util.regex.Matcher;
37
import java.util.regex.Pattern;
38
import javax.sql.DataSource;
39
import org.geotools.factory.Hints;
40
import org.geotools.metadata.iso.citation.Citations;
41
import org.geotools.referencing.factory.epsg.DirectEpsgFactory;
42
import org.geotools.referencing.factory.epsg.FactoryUsingAnsiSQL;
43
import org.geotools.referencing.wkt.Parser;
44
import org.geotools.resources.i18n.ErrorKeys;
45
import org.geotools.resources.i18n.Errors;
46
import org.opengis.metadata.citation.Citation;
47
import org.opengis.referencing.FactoryException;
48
import org.opengis.referencing.IdentifiedObject;
49
import org.opengis.referencing.NoSuchAuthorityCodeException;
50
import org.opengis.referencing.crs.CRSFactory;
51
import org.opengis.referencing.crs.CompoundCRS;
52
import org.opengis.referencing.crs.CoordinateReferenceSystem;
53
import org.opengis.referencing.crs.GeocentricCRS;
54
import org.opengis.referencing.crs.GeographicCRS;
55
import org.opengis.referencing.cs.CartesianCS;
56
import org.opengis.referencing.cs.CoordinateSystem;
57
import org.opengis.referencing.cs.EllipsoidalCS;
58
import org.opengis.referencing.cs.SphericalCS;
59
import org.opengis.referencing.cs.VerticalCS;
60
import org.opengis.referencing.datum.EngineeringDatum;
61
import org.opengis.referencing.datum.GeodeticDatum;
62
import org.opengis.referencing.datum.VerticalDatum;
63
import org.opengis.referencing.operation.Conversion;
64
import org.opengis.referencing.operation.CoordinateOperation;
65
import org.opengis.referencing.operation.Projection;
66

    
67
/**
68
 * A factory to retrieve and create CRSs from ESRI HSQL database.
69
 *
70
 * @author Cesar Martinez Izquierdo
71
 */
72
class EsriFactoryUsingHSQL extends DirectEpsgFactory {
73
        
74
    /** Cache of parsed CoordinateReferenceSystem WKT by ESRI_NUMBER */
75
    private Hashtable cache = new Hashtable();
76
    
77

    
78
    /** Constructs the factory for the given connection to the HSQL database. */
79
    public EsriFactoryUsingHSQL(final Hints hints, final Connection connection) {
80
        super(hints, connection);
81
    }
82

    
83
    public EsriFactoryUsingHSQL(final Hints hints, final DataSource dataSource) {
84
        super(hints, dataSource);
85
    }
86

    
87
    @Override
88
    public String adaptSQL(String query) {
89
        return query;
90
    }
91

    
92
    /**
93
     * Shutdown the HSQL database engine. This method is invoked automatically at JVM shutdown time
94
     * just before to close the connection.
95
     */
96
    protected void shutdown(final boolean active) throws SQLException {
97
        if (active) {
98
            final Statement statement = getConnection().createStatement();
99
            statement.execute("SHUTDOWN");
100
            statement.close();
101
        }
102
        super.shutdown(active);
103
    }
104
    
105
    @Override
106
    public Citation getAuthority() {
107
            return Citations.ESRI;
108
    }
109
   
110
    @Override
111
        public synchronized CoordinateReferenceSystem createCoordinateReferenceSystem(String code) throws FactoryException {
112
                String wkt = null;
113
                String ESRI_NUMBER = code.substring(code.indexOf(":")+1, code.length());
114
                int cod = Integer.parseInt(ESRI_NUMBER);
115
        
116
                if( cache.contains( ESRI_NUMBER ) ){
117
            Object value = cache.get( ESRI_NUMBER );
118
            if( value instanceof Throwable ){
119
                throw new FactoryException( "WKT for "+code+" could not be parsed", (Throwable) value );
120
            }
121
            if( value instanceof CoordinateReferenceSystem){
122
                return (CoordinateReferenceSystem) value;
123
            }
124
        }
125
                ResultSet result = null;
126
                
127
                try {
128
                 
129
            String sentence = "SELECT ESRI_WKT"
130
            + " FROM ESRI"
131
            + " WHERE ESRI_CODE = ?";
132
            
133
            String codeStr = code.substring(code.indexOf(":")+1, code.length());
134
            int codeInt = Integer.parseInt(codeStr);
135
        
136
                PreparedStatement st = null;
137
                st = getConnection().prepareStatement(sentence);
138
                st.setInt(1, codeInt);
139
                        result = st.executeQuery();
140
                        st.close();
141
                if (result.next()) {
142
                wkt    = result.getString("esri_wkt");
143
                if( wkt.indexOf( cod ) == -1){
144
                        wkt = wkt.trim();
145
                        wkt = wkt.substring(0, wkt.length()-1 );
146
                        wkt += ",AUTHORITY[\"ESRI\",\""+cod+"\"]]";
147
                }
148
                Parser parser = new Parser();
149
                return parser.parseCoordinateReferenceSystem(wkt);
150
            }
151
        } catch (SQLException exception) {
152
                throw new FactoryException(exception);
153
        } catch (ParseException exception) {
154
                throw new FactoryException(exception);
155
        } finally {
156
                if (result!=null) {
157
                        try {
158
                                        result.close();
159
                                } catch (SQLException e) {}
160
                }
161
        };
162
        throw new NoSuchAuthorityCodeException("No such authority code", "ESRI", code);
163
        }
164
    
165
    @Override
166
    public Set<String> getAuthorityCodes(Class<? extends IdentifiedObject> type) throws FactoryException {
167
            LinkedHashSet<String> codes = new LinkedHashSet<String>(); 
168
                ResultSet result = null;
169
                try {
170
                 
171
            String sentence = "SELECT ESRI_CODE"
172
            + " FROM ESRI ORDER BY ESRI_CODE ASC";
173
            
174
                Statement st = null;
175
                st = getConnection().createStatement();
176
                result = st.executeQuery(sentence);
177
                        st.close();
178
                while (result.next()) {
179
                int code = result.getInt(1);
180
                codes.add(Integer.toString(code));
181
            }
182
        } catch (SQLException exception) {
183
                throw new FactoryException(exception);
184
        } finally {
185
                if (result!=null) {
186
                        try {
187
                                        result.close();
188
                                } catch (SQLException e) {}
189
                }
190
        };
191
            return codes;
192
    }
193
}