Statistics
| Revision:

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 @ 47787

History | View | Annotate | Download (6.89 KB)

1
/**
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
package org.gvsig.fmap.dal.store.jdbc2.spi;
25

    
26
import java.sql.Connection;
27
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
import org.cresques.cts.IProjection;
33
import org.gvsig.fmap.crs.CRSFactory;
34
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
35
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
36
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
public class SRSSolverBase implements SRSSolver {
41
    /*
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
    protected static final Logger logger = LoggerFactory.getLogger(SRSSolverBase.class);
47
    
48
    protected Map<Integer,String> database2applicationAbbrev;
49
    protected Map<String,Integer> applicationAbbrev2database;
50
    protected JDBCHelper helper;
51
    
52
    public SRSSolverBase(JDBCHelper helper) {
53
        this.helper = helper;
54
        this.applicationAbbrev2database = new HashMap<>();
55
        this.database2applicationAbbrev = new HashMap<>();
56
    }
57
    
58
    @Override
59
    public void add(Object databaseCode, String applicationAbbrev) {
60
        this.applicationAbbrev2database.put(applicationAbbrev, (Integer) databaseCode);
61
        this.database2applicationAbbrev.put((Integer) databaseCode, applicationAbbrev);
62
    }
63
    
64
    @Override
65
    public Integer getDatabaseCode(JDBCConnection connection, String applicationAbbrev) {
66
        if( this.hasApplicationAbbrev(connection, applicationAbbrev) ) {
67
            return this.applicationAbbrev2database.get(applicationAbbrev);
68
        }
69
        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
        this.add(databaseCode, applicationAbbrev);
81
        return databaseCode;
82
    }
83
    
84
    @Override
85
    public String getApplicationAbbrev(JDBCConnection connection, Object databaseCode) {
86
        if( this.hasDatabaseCode(connection, databaseCode) ) {
87
            return this.database2applicationAbbrev.get((Integer)databaseCode);
88
        }
89
        String applicationAbbrev = this.searchApplicationAbbrev(connection, (Integer) databaseCode);
90
        if( StringUtils.isEmpty(applicationAbbrev) ) {
91
            return null;
92
        }
93
        this.add(databaseCode, applicationAbbrev);
94
        return applicationAbbrev;
95
    }
96
    
97
    @Override
98
    public boolean hasDatabaseCode(JDBCConnection connection, Object databaseCode) {
99
        return this.database2applicationAbbrev.containsKey((Integer)databaseCode);
100
    }
101
    
102
    @Override
103
    public boolean hasApplicationAbbrev(JDBCConnection connection, String applicationAbbrev) {
104
        return this.applicationAbbrev2database.containsKey(applicationAbbrev);
105
    }
106

    
107
    protected Object searchDatabaseCode(JDBCConnection connection, String applicationAbbrev) {
108
        // 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
            String[] s = applicationAbbrev.split(":");
112
            sql = "select srid, auth_name, auth_srid from spatial_ref_sys where upper(auth_name) = upper('" + s[0] +"') and auth_srid = "+s[1]+" ";
113
            Statement st = connection.createStatement(sql);
114
            ResultSet rs = JDBCUtils.executeQuery(st, sql);
115
            if ( rs.next() ) {
116
                Integer srid = rs.getInt("srid");
117
                return srid;
118
            }            
119
            return null;
120
        } catch (Throwable ex) {
121
            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
        }
124
    }
125

    
126
    protected String searchApplicationAbbrev(JDBCConnection connection, Integer databaseCode) {
127
        String sql = "select srid, auth_name, auth_srid from spatial_ref_sys where srid = " + databaseCode;
128
        try {
129
            Statement st = connection.createStatement(sql);
130
            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
            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
        }
141
    }
142

    
143
    @Override
144
    public IProjection getProjection(JDBCConnection connection, Object databaseCode) {
145
        if( databaseCode == null ) {
146
            return null;
147
        }
148
        String s = databaseCode.toString().trim();
149
        if( s.equals("0") ) {
150
            return null;
151
        }
152
        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
    public Integer getDatabaseCode(JDBCConnection connection, IProjection projection) {
162
        if( projection==null ) {
163
            return 0;
164
        }
165
        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
    }
174

    
175
}