svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.sqlite / org.gvsig.sqlite.provider / src / main / java / org / gvsig / sqlite / dal / geopackage / GeopackageSRSSolver.java @ 47456
History | View | Annotate | Download (4.45 KB)
1 |
package org.gvsig.sqlite.dal.geopackage; |
---|---|
2 |
|
3 |
import java.sql.ResultSet; |
4 |
import java.sql.Statement; |
5 |
import org.apache.commons.lang3.StringUtils; |
6 |
import org.cresques.cts.IProjection; |
7 |
import org.gvsig.fmap.crs.CRSFactory; |
8 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
9 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
10 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
11 |
import org.gvsig.fmap.dal.store.jdbc2.spi.SRSSolverBase; |
12 |
import org.sqlite.SQLiteConnection; |
13 |
|
14 |
/**
|
15 |
*
|
16 |
* @author jjdelcerro
|
17 |
*/
|
18 |
@SuppressWarnings("UseSpecificCatch") |
19 |
public class GeopackageSRSSolver extends SRSSolverBase { |
20 |
|
21 |
/*friend*/ GeopackageSRSSolver() {
|
22 |
super(null); |
23 |
} |
24 |
|
25 |
@Override
|
26 |
protected Object searchDatabaseCode(JDBCConnection connection, String applicationAbbrev) { |
27 |
return this.searchDatabaseCode((SQLiteConnection)connection.get(), applicationAbbrev); |
28 |
} |
29 |
|
30 |
@Override
|
31 |
protected String searchApplicationAbbrev(JDBCConnection connection, Integer databaseCode) { |
32 |
return this.searchApplicationAbbrev((SQLiteConnection)connection.get(), databaseCode); |
33 |
} |
34 |
|
35 |
protected Object searchDatabaseCode(SQLiteConnection connection, String applicationAbbrev) { |
36 |
// Initialize sql only for debugging purposes
|
37 |
String sql = "select srs_id, organization, organization_coordsys_id from gpkg_spatial_ref_sys where organization/organization_coordsys_id is '"+applicationAbbrev+"'."; |
38 |
try {
|
39 |
String[] s = applicationAbbrev.split(":"); |
40 |
sql = "select srs_id, organization, organization_coordsys_id from gpkg_spatial_ref_sys where upper(organization) = upper('" + s[0] +"') and organization_coordsys_id = "+s[1]+" "; |
41 |
Statement st = connection.createStatement();
|
42 |
ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
43 |
if ( rs.next() ) {
|
44 |
Integer srid = rs.getInt("srs_id"); |
45 |
return srid;
|
46 |
} |
47 |
return null; |
48 |
} catch (Throwable ex) { |
49 |
logger.warn("Can't retrieve SRS code from gpkg_spatial_ref_sys ("+sql+").", ex); |
50 |
throw new RuntimeException("Can't retrieve SRS code from gpkg_spatial_ref_sys ("+sql+").",ex); |
51 |
} |
52 |
} |
53 |
|
54 |
protected String searchApplicationAbbrev(SQLiteConnection connection, Integer databaseCode) { |
55 |
String sql = "select srs_id, organization, organization_coordsys_id from gpkg_spatial_ref_sys where srs_id = " + databaseCode; |
56 |
try {
|
57 |
Statement st = connection.createStatement();
|
58 |
ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
59 |
if ( rs.next() ) {
|
60 |
int auth_code = rs.getInt("organization_coordsys_id"); |
61 |
String auth_name = rs.getString("organization"); |
62 |
return auth_name + ":" + auth_code; |
63 |
} |
64 |
return null; |
65 |
} catch (Throwable ex) { |
66 |
logger.warn("Can't retrieve SRS from gpkg_spatial_ref_sys ("+sql+").", ex); |
67 |
throw new RuntimeException("Can't retrieve SRS from gpkg_spatial_ref_sys ("+sql+").",ex); |
68 |
} |
69 |
} |
70 |
|
71 |
public Integer getDatabaseCode(SQLiteConnection connection, String applicationAbbrev) { |
72 |
if( this.hasApplicationAbbrev(null, applicationAbbrev) ) { |
73 |
return this.applicationAbbrev2database.get(applicationAbbrev); |
74 |
} |
75 |
Integer databaseCode = (Integer) this.searchDatabaseCode(connection, applicationAbbrev); |
76 |
this.add(databaseCode, applicationAbbrev);
|
77 |
return databaseCode;
|
78 |
} |
79 |
|
80 |
public IProjection getProjection(SQLiteConnection connection, Object databaseCode) { |
81 |
if( databaseCode == null ) { |
82 |
return null; |
83 |
} |
84 |
String s = databaseCode.toString().trim();
|
85 |
if( s.equals("0") ) { |
86 |
return null; |
87 |
} |
88 |
String abbrev = this.getApplicationAbbrev(connection, databaseCode); |
89 |
if( StringUtils.isEmpty(abbrev) ) {
|
90 |
return null; |
91 |
} |
92 |
IProjection proj = CRSFactory.getCRS(abbrev); |
93 |
return proj;
|
94 |
} |
95 |
|
96 |
public String getApplicationAbbrev(SQLiteConnection connection, Object databaseCode) { |
97 |
if( this.hasDatabaseCode(null, databaseCode) ) { |
98 |
return this.database2applicationAbbrev.get((Integer)databaseCode); |
99 |
} |
100 |
String applicationAbbrev = this.searchApplicationAbbrev(connection, (Integer) databaseCode); |
101 |
if( StringUtils.isEmpty(applicationAbbrev) ) {
|
102 |
return null; |
103 |
} |
104 |
this.add(databaseCode, applicationAbbrev);
|
105 |
return applicationAbbrev;
|
106 |
} |
107 |
|
108 |
} |