Statistics
| Revision:

root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / proj / CrsProj.java @ 436

History | View | Annotate | Download (8.04 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.proj;
42

    
43
import org.gdal.osr.CoordinateTransformation;
44
import org.gdal.osr.SpatialReference;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
import org.gvsig.crs.CrsException;
49

    
50
public class CrsProj {
51

    
52
    private static Logger logger = LoggerFactory.getLogger(CrsProj.class);
53

    
54
    private SpatialReference spatialReference;
55
    private String _strCrs;
56

    
57
        public CrsProj(String proj4String) {
58
        try {
59
            if( proj4String == null ) {
60
                logger.error("Can't create CRS from 'null' value.");
61
                return;
62
            }
63
            createCrs(proj4String);
64
        } catch(CrsProjException e) {
65
            logger.error("Can't create CRS from '"+proj4String+"'.",e);
66
        }
67
        // showInfo();
68

    
69
    }
70

    
71
    private void showInfo() {
72
                logger.info("=========================================================================");
73
                logger.info("_strCrs" + " = " + _strCrs);
74
                logger.info("IsProjected" + " = " + spatialReference.IsProjected());
75
                logger.info("IsGeographic" + " = " + spatialReference.IsGeographic());
76
                logger.info("IsGeocentric" + " = " + spatialReference.IsGeocentric());
77
                logger.info("IsVertical" + " = " + spatialReference.IsVertical());
78

    
79
                logger.info("SemiMajor" + " = " + spatialReference.GetSemiMajor());
80
                logger.info("SemiMinor" + " = " + spatialReference.GetSemiMinor());
81
                logger.info("InvFlattening" + " = " + spatialReference.GetInvFlattening());
82

    
83
                logger.info("PROJCS" + " = " + spatialReference.GetAttrValue("PROJCS"));
84
                logger.info("GEOGCS" + " = " + spatialReference.GetAttrValue("GEOGCS"));
85
                logger.info("DATUM" + " = " + spatialReference.GetAttrValue("DATUM"));
86
                logger.info("SPHEROID" + " = " + spatialReference.GetAttrValue("SPHEROID"));
87
                logger.info("PROJECTION" + " = " + spatialReference.GetAttrValue("PROJECTION"));
88
                logger.info("UNIT" + " = " + spatialReference.GetAttrValue("UNIT"));
89
                logger.info("conversion a metros" + " = " + spatialReference.GetAttrValue("UNIT",1));
90
                logger.info("Linear unit name" + " = " + spatialReference.GetLinearUnitsName());
91
                logger.info("linear units" + " = " + spatialReference.GetLinearUnits());
92
                logger.info("Angular units" + " = " + spatialReference.GetAngularUnits());
93
                logger.info("TOWGS84" + " = " + spatialReference.GetAttrValue("TOWGS84"));
94
                logger.info("TOWGS84" + " = " + spatialReference.GetAttrValue("TOWGS84",1));
95
                logger.info("TOWGS84" + " = " + spatialReference.GetAttrValue("TOWGS84",2));
96
                logger.info("SRS_PP_LATITUDE_OF_ORIGIN" + " = " + spatialReference.GetAttrValue("SRS_PP_LATITUDE_OF_ORIGIN"));
97
                logger.info("=========================================================================");
98
    }
99

    
100
    protected void createCrs(String strCrs) throws CrsProjException {
101
        spatialReference = new SpatialReference();
102

    
103
        int errNo = 0;
104
        try {
105
            errNo = spatialReference.ImportFromProj4(strCrs);
106
        } catch (Exception e) {
107
            throw new CrsProjException("Error creating CRS", e);
108
        }
109
        if (errNo!=0) {
110
            throw new CrsProjException("Error creating CRS. Erro code = "+errNo);
111
        }
112

    
113
        try {
114
            errNo = spatialReference.Fixup();
115
        } catch (Exception e) {
116
            throw new CrsProjException("Error fixing up CRS", e);
117
        }
118

    
119
        if (errNo!=0) {
120
            throw new CrsProjException("Error creating CRS. Erro code = "+errNo);
121
        }
122

    
123
        _strCrs=strCrs;
124
    }
125

    
126
    public void reloadCrs() throws CrsException {
127
        // do nothing
128
    }
129

    
130
    public void changeStrCrs(String code) {
131
        _strCrs += code;
132
    }
133

    
134
    public boolean isLatlong() {
135
        return spatialReference.IsGeographic()!=0;
136
    }
137

    
138
    public boolean isGeocent() {
139
        return spatialReference.IsGeocentric()!=0;
140
    }
141

    
142
    public String getUnits() {
143
        return spatialReference.GetAttrValue("UNIT",0);
144
    }
145

    
146
    public double getConversionFactorToMeters() {
147
        return Double.parseDouble(spatialReference.GetAttrValue("UNIT",1));
148
    }
149

    
150
    public String getStr() {
151
        return spatialReference.ExportToProj4();
152
    }
153

    
154
    public long getPtr() {
155
        return SpatialReference.getCPtr(spatialReference);
156
    }
157

    
158
    public SpatialReference getSpatialReference(){
159
        return this.spatialReference;
160
    }
161

    
162
    public static int operate(double[] firstCoord, double[] secondCoord, double[] thirdCoord, CrsProj srcCrs,
163
        CrsProj destCrs) throws OperationCrsException {
164

    
165
        try {
166
            logger.warn("CrsProj.operate: No deber?a pasar por aqu?. (Excepcion provocada para ver de d?nde viene).");
167
            throw new Exception();
168
        } catch (Exception e) {
169
            e.printStackTrace();
170
        }
171

    
172
        long srcCrsPtr = srcCrs.getPtr();
173
        long destCrsPtr = destCrs.getPtr();
174
        if (srcCrsPtr == 0 || destCrsPtr == 0) {
175
            throw new OperationCrsException(srcCrs, destCrs, "");
176
        }
177

    
178
        int points =  firstCoord.length;
179
        if (secondCoord.length > points){
180
            points = secondCoord.length;
181
        }
182
        if (thirdCoord.length > points){
183
            points = thirdCoord.length;
184
        }
185

    
186
        CoordinateTransformation ct = null;
187
        try {
188
            ct = CoordinateTransformation.CreateCoordinateTransformation(srcCrs.spatialReference, destCrs.spatialReference);
189
        } catch (Exception e) {
190
            throw new OperationCrsException(srcCrs, destCrs, "Can't create transformation");
191
        }
192
        try {
193
            if (ct!=null) {
194
                for (int i = 0; i < points; i++) {
195
                    double[] resultCoords = ct.TransformPoint(firstCoord[i], secondCoord[i], thirdCoord[i]);
196
                    firstCoord[i] = resultCoords[0];
197
                    secondCoord[i] = resultCoords[1];
198
                    thirdCoord[i] = resultCoords[2];
199
                }
200
            }
201
        } catch (Exception e) {
202
            throw new OperationCrsException(srcCrs, destCrs, "Can't transform points");
203
        }
204
        return 0;
205
    }
206

    
207
    public static int compareDatums(CrsProj crs1, CrsProj crs2) {
208
        long crs1Ptr = crs1.getPtr();
209
        long crs2Ptr = crs2.getPtr();
210
        if (crs1Ptr == 0 || crs2Ptr == 0) {
211
            throw new IllegalArgumentException("Coordinate compare error: " + crs1.getStr() + " to " + crs2.getStr());
212
        }
213
        return Long.valueOf(crs1Ptr).compareTo(Long.valueOf(crs2Ptr));
214
    }
215

    
216

    
217
}