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 @ 360

History | View | Annotate | Download (8.06 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
        // FIXME: borralo
68
        showInfo();
69

    
70
    }
71

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

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

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

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

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

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

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

    
124
        _strCrs=strCrs;
125
    }
126

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
217

    
218
}