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