svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / SRSSolverBase.java @ 46517
History | View | Annotate | Download (6.88 KB)
1 | 45065 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2020 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 3
|
||
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 | 43355 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi; |
25 | |||
26 | 43606 | jjdelcerro | import java.sql.Connection; |
27 | 43355 | jjdelcerro | import java.sql.ResultSet; |
28 | import java.sql.Statement; |
||
29 | import java.util.HashMap; |
||
30 | import java.util.Map; |
||
31 | import org.apache.commons.lang3.StringUtils; |
||
32 | 43606 | jjdelcerro | import org.cresques.cts.IProjection; |
33 | import org.gvsig.fmap.crs.CRSFactory; |
||
34 | 46315 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
35 | 43355 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
36 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
37 | 43687 | jjdelcerro | import org.slf4j.Logger; |
38 | import org.slf4j.LoggerFactory; |
||
39 | 43355 | jjdelcerro | |
40 | 43606 | jjdelcerro | public class SRSSolverBase implements SRSSolver { |
41 | 43738 | jjdelcerro | /*
|
42 | Esta clase asume que los SRS de la BBDD son Integer.
|
||
43 | En caso de que para un gestor de BBDD no lo sea deberia usarse una
|
||
44 | implemetacion alternativa y no esta.
|
||
45 | */
|
||
46 | 43687 | jjdelcerro | protected static final Logger logger = LoggerFactory.getLogger(SRSSolverBase.class); |
47 | 43355 | jjdelcerro | |
48 | 43738 | jjdelcerro | protected Map<Integer,String> database2applicationAbbrev; |
49 | protected Map<String,Integer> applicationAbbrev2database; |
||
50 | 43355 | jjdelcerro | protected JDBCHelper helper;
|
51 | |||
52 | 43606 | jjdelcerro | public SRSSolverBase(JDBCHelper helper) {
|
53 | 43355 | jjdelcerro | this.helper = helper;
|
54 | this.applicationAbbrev2database = new HashMap<>(); |
||
55 | this.database2applicationAbbrev = new HashMap<>(); |
||
56 | } |
||
57 | |||
58 | 43606 | jjdelcerro | @Override
|
59 | 43687 | jjdelcerro | public void add(Object databaseCode, String applicationAbbrev) { |
60 | 43738 | jjdelcerro | this.applicationAbbrev2database.put(applicationAbbrev, (Integer) databaseCode); |
61 | this.database2applicationAbbrev.put((Integer) databaseCode, applicationAbbrev); |
||
62 | 43355 | jjdelcerro | } |
63 | |||
64 | 43606 | jjdelcerro | @Override
|
65 | 46315 | jjdelcerro | public Integer getDatabaseCode(JDBCConnection connection, String applicationAbbrev) { |
66 | 43606 | jjdelcerro | if( this.hasApplicationAbbrev(connection, applicationAbbrev) ) { |
67 | 43355 | jjdelcerro | return this.applicationAbbrev2database.get(applicationAbbrev); |
68 | } |
||
69 | 43738 | jjdelcerro | Integer databaseCode = (Integer) this.searchDatabaseCode(connection, applicationAbbrev); |
70 | // if( databaseCode==null ) {
|
||
71 | // logger.debug("database code srs null.");
|
||
72 | // return null;
|
||
73 | // }
|
||
74 | // logger.debug("database code srs {}, type {}.",
|
||
75 | // new Object[] {
|
||
76 | // databaseCode,
|
||
77 | // databaseCode.getClass().getSimpleName()
|
||
78 | // }
|
||
79 | // );
|
||
80 | 43355 | jjdelcerro | this.add(databaseCode, applicationAbbrev);
|
81 | return databaseCode;
|
||
82 | } |
||
83 | |||
84 | 43606 | jjdelcerro | @Override
|
85 | 46315 | jjdelcerro | public String getApplicationAbbrev(JDBCConnection connection, Object databaseCode) { |
86 | 43606 | jjdelcerro | if( this.hasDatabaseCode(connection, databaseCode) ) { |
87 | 43738 | jjdelcerro | return this.database2applicationAbbrev.get((Integer)databaseCode); |
88 | 43355 | jjdelcerro | } |
89 | 43738 | jjdelcerro | String applicationAbbrev = this.searchApplicationAbbrev(connection, (Integer) databaseCode); |
90 | 43355 | jjdelcerro | if( StringUtils.isEmpty(applicationAbbrev) ) {
|
91 | return null; |
||
92 | } |
||
93 | this.add(databaseCode, applicationAbbrev);
|
||
94 | return applicationAbbrev;
|
||
95 | } |
||
96 | |||
97 | 43606 | jjdelcerro | @Override
|
98 | 46315 | jjdelcerro | public boolean hasDatabaseCode(JDBCConnection connection, Object databaseCode) { |
99 | 43738 | jjdelcerro | return this.database2applicationAbbrev.containsKey((Integer)databaseCode); |
100 | 43355 | jjdelcerro | } |
101 | |||
102 | 43606 | jjdelcerro | @Override
|
103 | 46315 | jjdelcerro | public boolean hasApplicationAbbrev(JDBCConnection connection, String applicationAbbrev) { |
104 | 43355 | jjdelcerro | return this.applicationAbbrev2database.containsKey(applicationAbbrev); |
105 | } |
||
106 | |||
107 | 46315 | jjdelcerro | protected Object searchDatabaseCode(JDBCConnection connection, String applicationAbbrev) { |
108 | 43355 | jjdelcerro | // Initialize sql only for debugging purposes
|
109 | String sql = "select srid, auth_name, auth_srid from spatial_ref_sys where auth_name/auth_srid is '"+applicationAbbrev+"'."; |
||
110 | try {
|
||
111 | 43687 | jjdelcerro | String[] s = applicationAbbrev.split(":"); |
112 | 43734 | jjdelcerro | sql = "select srid, auth_name, auth_srid from spatial_ref_sys where upper(auth_name) = upper('" + s[0] +"') and auth_srid = "+s[1]+" "; |
113 | 43606 | jjdelcerro | Statement st = connection.createStatement();
|
114 | 43355 | jjdelcerro | ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
115 | if ( rs.next() ) {
|
||
116 | 43738 | jjdelcerro | Integer srid = rs.getInt("srid"); |
117 | 43687 | jjdelcerro | return srid;
|
118 | 43355 | jjdelcerro | } |
119 | return null; |
||
120 | } catch (Throwable ex) { |
||
121 | 43687 | jjdelcerro | logger.warn("Can't retrieve SRS code from spatial_ref_sys ("+sql+").", ex); |
122 | throw new RuntimeException("Can't retrieve SRS code from spatial_ref_sys ("+sql+").",ex); |
||
123 | 43355 | jjdelcerro | } |
124 | } |
||
125 | |||
126 | 46315 | jjdelcerro | protected String searchApplicationAbbrev(JDBCConnection connection, Integer databaseCode) { |
127 | 43355 | jjdelcerro | String sql = "select srid, auth_name, auth_srid from spatial_ref_sys where srid = " + databaseCode; |
128 | try {
|
||
129 | 43606 | jjdelcerro | Statement st = connection.createStatement();
|
130 | 43355 | jjdelcerro | ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
131 | if ( rs.next() ) {
|
||
132 | int auth_code = rs.getInt("auth_srid"); |
||
133 | String auth_name = rs.getString("auth_name"); |
||
134 | return auth_name + ":" + auth_code; |
||
135 | } |
||
136 | return null; |
||
137 | } catch (Throwable ex) { |
||
138 | 43687 | jjdelcerro | logger.warn("Can't retrieve SRS from spatial_ref_sys ("+sql+").", ex); |
139 | throw new RuntimeException("Can't retrieve SRS from spatial_ref_sys ("+sql+").",ex); |
||
140 | 43355 | jjdelcerro | } |
141 | } |
||
142 | |||
143 | 43606 | jjdelcerro | @Override
|
144 | 46315 | jjdelcerro | public IProjection getProjection(JDBCConnection connection, Object databaseCode) { |
145 | 43687 | jjdelcerro | if( databaseCode == null ) { |
146 | 43606 | jjdelcerro | return null; |
147 | } |
||
148 | 43687 | jjdelcerro | String s = databaseCode.toString().trim();
|
149 | if( s.equals("0") ) { |
||
150 | return null; |
||
151 | } |
||
152 | 43606 | jjdelcerro | String abbrev = this.getApplicationAbbrev(connection, databaseCode); |
153 | if( StringUtils.isEmpty(abbrev) ) {
|
||
154 | return null; |
||
155 | } |
||
156 | IProjection proj = CRSFactory.getCRS(abbrev); |
||
157 | return proj;
|
||
158 | } |
||
159 | |||
160 | @Override
|
||
161 | 46315 | jjdelcerro | public Integer getDatabaseCode(JDBCConnection connection, IProjection projection) { |
162 | 44606 | jjdelcerro | if( projection==null ) { |
163 | return 0; |
||
164 | } |
||
165 | 43738 | jjdelcerro | Object srscode = this.getDatabaseCode(connection, projection.getAbrev()); |
166 | // logger.debug("database code srs {}, type {}.",
|
||
167 | // new Object[] {
|
||
168 | // srscode,
|
||
169 | // srscode==null? "null":srscode.getClass().getSimpleName(),
|
||
170 | // }
|
||
171 | // );
|
||
172 | return (Integer) srscode; |
||
173 | 43606 | jjdelcerro | } |
174 | |||
175 | 43355 | jjdelcerro | } |