Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / spatialReferenceIdentifyStrategies / AbstractIdentifyStrategy.java @ 846

History | View | Annotate | Download (5.46 KB)

1
package org.gvsig.crs.spatialReferenceIdentifyStrategies;
2

    
3
import org.apache.commons.lang3.StringUtils;
4
import org.gdal.osr.SpatialReference;
5
import org.gvsig.crs.SpatialReferenceIdentifyStrategy;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8

    
9
public abstract class AbstractIdentifyStrategy implements SpatialReferenceIdentifyStrategy {
10

    
11
    protected static Logger logger = LoggerFactory.getLogger(AbstractIdentifyStrategy.class);
12

    
13
    protected String normalizeEsriSrName(String sr_name) {
14
        String name;
15
        name = StringUtils.replace(sr_name, "GCS_", "");
16
        name = StringUtils.replaceChars(name, "()", "");
17
        name = StringUtils.replace(name, "WGS_19", "WGS "); // e.g. WGS_1984 --> WGS 84
18
        name = StringUtils.replace(name, "_19", ""); // e.g. NAD_1927 --> NAD27
19
        name = StringUtils.replace(name, "_20", "20"); // e.g. ITRF_2008 --> ITRF2008
20
        name = StringUtils.replace(name, "_Adj_1958", "(58)");
21
        name = StringUtils.replace(name, "_Bern", " (Bern)");
22
        name = StringUtils.replace(name, "_CGQ77", "(CGQ77)");
23
        name = StringUtils.replace(name, "_CORS96", "(CORS96)");
24
        name = StringUtils.replace(name, "_CSRS", "(CSRS)");
25
        name = StringUtils.replace(name, "_DEF_1976", "(76)");
26
        name = StringUtils.replace(name, "_ED77", "(ED77)");
27
        name = StringUtils.replace(name, "_HARN", "(HARN)");
28
        name = StringUtils.replace(name, "_MA11", "(MA11)");
29
        name = StringUtils.replace(name, "_NSRS2007", "(NSRS2007)");
30
        name = StringUtils.replace(name, "_PA11", "(PA11)");
31
        name = StringUtils.replace(name, "_StatePlane", "");
32
        name = StringUtils.replaceAll(name, "(_FIPS_)\\d{4}", "");
33
        name = StringUtils.replaceAll(name, "_m$", "(m)");
34

    
35
        name = StringUtils.replace(name, "1870_Madrid", "1870 (Madrid)");
36
        name = StringUtils.replace(name, "83_2011", "83(2011)"); // e.g. NAD_1983_2011 --> NAD83(2011)
37
        name = StringUtils.replaceAll(name, "Feet_Intl$", "(ft)");
38
        name = StringUtils.replaceAll(name, "Feet$", "(ftUS)");
39
        name = StringUtils.replace(name, "Ferro", "(Ferro)");
40
        name = StringUtils.replaceAll(name, "ftUS$", "(ftUS)");
41
        name = StringUtils.replaceAll(name, "ft$", "(ft)");
42
        name = StringUtils.replace(name, "Hungarian_1972", "HD72");
43
        name = StringUtils.replace(name, "Indonesian_1974", "ID74");
44
        name = StringUtils.replace(name, "Lisboa", "Lisbon");
45
        name = StringUtils.replace(name, "Lisbon_Lisbon", "Lisbon (Lisbon)");
46
        name = StringUtils.replace(name, "MGI_M", "MGI Austria M"); //"MGI_M" --> "MGI / Austria M"
47
        name = StringUtils.replace(name, "New_Brunswick_Stereographic", "New Brunswick Stereographic (ATS77)");
48
        name = StringUtils.replace(name, "Oslo_Norway", "(Oslo) NGO");
49
        name = StringUtils.replace(name, "Paris", "(Paris)");
50
        name = StringUtils.replace(name, "St_", "St. ");
51
        name = StringUtils.replace(name, "TC_1948", "TC(1948)");
52
        name = StringUtils.replace(name, "ETRS_1989","ETRS89");
53
        name = StringUtils.replace(name, "ETRS_89","ETRS89");
54

    
55
        name = StringUtils.replace(name, "_", " ");
56
        name = StringUtils.replace(name, "-", " ");
57
        name = StringUtils.lowerCase(name);
58
        name = StringUtils.replace(name, "'", "''");
59
        return name;
60
    }
61

    
62
    protected String normalizeEPSGSrName(String sr_name) {
63
        String name;
64
        name = StringUtils.replace(sr_name, " / ", " ");
65
        name = StringUtils.replace(name, "-", " ");
66
        name = StringUtils.lowerCase(name);
67
        name = StringUtils.replace(name, "'", "''");
68
        return name;
69
    }
70

    
71
    protected SpatialReference createSpatialReferenceFromEPSGCode(int code) {
72
        if( code <1 ) {
73
            return null;
74
        }
75
        SpatialReference sr2 = new SpatialReference();
76
        try {
77
            sr2.ImportFromEPSG(code);
78
            return sr2;
79
        } catch (Exception e4) {
80
            logger.warn("Can't cant create from EPSG code " + code + ".", e4);
81
            return null;
82
        }
83
    }
84

    
85
    protected SpatialReference createSpatialReferenceFromESRICode(int code) {
86
        if( code <1 ) {
87
            return null;
88
        }
89
        SpatialReference sr2 = new SpatialReference();
90
        try {
91
            sr2.ImportFromEPSG(code);
92
            if( code > 33000 ) {
93
                if (sr2.IsGeographic() == 1) {
94
                    sr2.SetAuthority("GEOGCS","ESRI",code);
95
                } else {
96
                    sr2.SetAuthority("PROJCS","ESRI",code);
97
                }
98
            }
99
            return sr2;
100
        } catch (Exception e4) {
101
            logger.warn("Can't cant create from ESRI code " + code + ".", e4);
102
            return null;
103
        }
104
    }
105

    
106
    protected String getProj4String(SpatialReference sr) {
107
        String proj4str = null;
108
        try {
109
            proj4str = sr.ExportToProj4();
110
            logger.debug("Source proj4: {}", proj4str);
111
        } catch (Exception e2) {
112
            logger.debug("Can't ExportToProj4.", e2);
113
        }        
114
        return proj4str;
115
    }
116
    
117
    protected boolean autoIdentifyEPSG(SpatialReference sr) {
118
        try {
119
            sr.AutoIdentifyEPSG();
120
            String authorityCode = sr.GetAuthorityCode(null);
121
            if (!StringUtils.isEmpty(authorityCode)) {
122
                logger.debug("AuthorityCode: " + authorityCode);
123
                return true;
124
            }
125
        } catch (Exception e) {
126
            logger.debug("Can't AutoIdentifyEPSG.", e);
127
        }
128
        return false;
129
    }
130
}