Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / repository / EpsgRepository.java @ 229

History | View | Annotate | Download (4.47 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.crs.repository;
42

    
43
import java.sql.ResultSet;
44
import java.sql.SQLException;
45

    
46
import es.idr.teledeteccion.connection.EpsgConnection;
47
import es.idr.teledeteccion.connection.Query;
48
import java.util.logging.Level;
49

    
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

    
53
import org.gvsig.crs.Crs;
54
import org.gvsig.crs.CrsException;
55
import org.gvsig.crs.ICrs;
56
import org.gvsig.crs.ogr.CrsEPSG;
57
import org.gvsig.crs.ogr.Epsg2wkt;
58
import org.gvsig.tools.dispose.Disposable;
59
/**
60
 * Repositorio de CRSs de EPSG
61
 * 
62
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
63
 *
64
 */
65
public class EpsgRepository implements ICrsRepository, Disposable {
66
        private static final Logger logger =
67
        LoggerFactory.getLogger(EpsgRepository.class);
68
        
69
        public EpsgConnection connection = null;
70
        
71
        public EpsgRepository() {
72
                connection = new EpsgConnection();
73
        }
74

    
75
        /**
76
         * Obtiene un CRS a partir de su c?digo EPSG.
77
         * 
78
         * @param code Codigo EPSG del CRS. 
79
         * @return ICrs 
80
         */
81
        public ICrs getCrs(String code) {
82
                int epsg_code  = Integer.parseInt(code);
83
                boolean source_yn = false;
84
                int source_cod = 0;
85
                int datum_code = 0;
86
                int projection_conv_code = 0;
87
                String crs_kind = null;
88
                
89
                Epsg2wkt wkt = null;
90
                
91
                Crs crs = null;
92
                
93
                ResultSet result = null;
94
                String sentence = "SELECT source_geogcrs_code, projection_conv_code, "
95
                        + "coord_ref_sys_kind, datum_code "
96
                        + "FROM epsg_coordinatereferencesystem "
97
                        + "WHERE coord_ref_sys_code = " + code;
98
                                                
99
                connection.setConnectionEPSG();
100
                result = Query.select(sentence,connection.getConnection());        
101
                
102
                /*try {
103
                        connection.shutdown();
104
                } catch (SQLException e) {
105
                        // TODO Auto-generated catch block
106
                        logger....
107
                }*/
108
                
109
                try {
110
                        if (!result.next())
111
                                return null;
112
                        source_cod = result.getInt("source_geogcrs_code");
113
                        projection_conv_code = result.getInt("projection_conv_code");
114
                        crs_kind = result.getString("coord_ref_sys_kind");
115
                        datum_code = result.getInt("datum_code");
116
                } catch (SQLException e1) {
117
                    logger.warn("Error executing the SQL", e1);
118
                    return null;
119
                }
120
                
121
                if (datum_code != 0){
122
                        source_yn = true;
123
                }
124
                else if (source_cod != 0){
125
                        source_yn = false;
126
                }
127
                else source_yn = true;
128
                
129
                CrsEPSG ep = new CrsEPSG(epsg_code, source_yn, source_cod, projection_conv_code, connection);
130
                
131
                if (crs_kind.equals("geographic 2D") || crs_kind.equals("geographic 3D")){
132
                        wkt = new Epsg2wkt(ep , "geog");                        
133
                }
134
                else if (crs_kind.equals("projected")){
135
                        wkt = new Epsg2wkt(ep, "proj");
136
                }
137
                else if (crs_kind.equals("compound")){
138
                        wkt = new Epsg2wkt(ep,"comp");
139
                }
140
                else if (crs_kind.equals("geocentric")){
141
                        wkt = new Epsg2wkt(ep,"geoc");
142
                }
143
                
144
                try {
145
                        crs = new Crs(Integer.parseInt(code),wkt.getWKT());
146
                } catch (CrsException e) {
147
                    logger.warn("Impossible to parse the CRS", e);
148
                    return null;
149
                }
150
                return crs;
151
        }
152

    
153
        public void dispose() {
154
            try {
155
                this.connection.shutdown();
156
                this.connection = null;
157
            } catch (SQLException ex) {
158
                logger.warn("Can't shutdown the connection.",ex);
159
            }
160
        }
161
}