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

History | View | Annotate | Download (6.8 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.JDBCHelper;
35
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
36
import org.slf4j.Logger;
37
import org.slf4j.LoggerFactory;
38

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

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

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

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

    
174
}