Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / spatialReferenceIdentifyStrategies / IdentifyEsri.java @ 841

History | View | Annotate | Download (2.87 KB)

1
package org.gvsig.crs.spatialReferenceIdentifyStrategies;
2

    
3
import es.idr.teledeteccion.connection.EpsgConnection;
4
import es.idr.teledeteccion.connection.Query;
5
import java.sql.ResultSet;
6
import org.apache.commons.lang3.StringUtils;
7
import org.gdal.osr.SpatialReference;
8

    
9
public class IdentifyEsri extends AbstractIdentifyStrategy {
10

    
11
    private int best_code;
12

    
13
    public IdentifyEsri() {
14
    }
15

    
16
    public int getBestAccuracy() {
17
        if( best_code == 0 ) {
18
            return 1000;
19
        }
20
        return 0;
21
    }
22
    
23
    public SpatialReference getBest() {
24
        SpatialReference sr = createSpatialReferenceFromEPSGCode(best_code);
25
        return sr;
26
    }
27

    
28
    public SpatialReference identify(SpatialReference sr) {
29
        if (autoIdentifyEPSG(sr)) {
30
            return sr;
31
        }
32
        
33
        String source_proj4 = getProj4String(sr);
34
        String sql = "";
35
        if (sr.IsGeographic() == 1) {
36
            sql = "SELECT ESRI_CODE, ESRI_WKT FROM ESRI WHERE ESRI_WKT LIKE 'GEOGCS%'"
37
                    + " AND ESRI_GEOG LIKE '" + StringUtils.replace(sr.GetAttrValue("GEOGCS"), "'", "''") + "'";
38
        } else {
39
            if (sr.IsProjected() == 1) {
40
                sql = "SELECT ESRI_CODE, ESRI_WKT FROM ESRI WHERE ESRI_WKT LIKE 'PROJCS%'"
41
                        + " AND ESRI_PROJ LIKE '" + StringUtils.replace(sr.GetAttrValue("PROJCS"), "'", "''") + "'";
42
            }
43
        }
44
        logger.debug(sql);
45
        int code = 0;
46
        best_code = 0;
47
        
48
        EpsgConnection conn = null;
49
        try {
50
            conn = new EpsgConnection();
51
            conn.setConnectionEsri();
52
            ResultSet result = Query.select(sql, conn.getConnection());
53
            while (result.next()) {
54
                code = result.getInt("ESRI_CODE");
55
                String wkt = result.getString("ESRI_WKT");
56
                logger.debug("Trying to match ESRI:{}...", code);
57
                try {
58
                    SpatialReference target_sr = new SpatialReference();
59
                    target_sr.ImportFromWkt(wkt);
60
                    target_sr.MorphFromESRI();
61
                    String target_proj4 = target_sr.ExportToProj4();
62
                    if (StringUtils.equals(source_proj4, target_proj4)) {
63
                        logger.debug("ESRI:{} matches!", code);
64
                        best_code = code;
65
                        return createSpatialReferenceFromESRICode(code);
66
                    }
67
                } catch (Exception e) {
68
                    //logger.debug("Can't identify the ESRI code...");
69
                }
70
            }
71
        } catch(Exception ex) {
72
            
73
        } finally {
74
            if( conn != null ) {
75
                try {
76
                    conn.close();            
77
                } catch(Exception ex1) {
78
                }
79
            }
80
        }
81
        logger.debug("Can't identify the ESRI code...");
82
        best_code = 0;
83
        return null;
84
    }
85

    
86
}