root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / proj / CrsProj.java @ 359
History | View | Annotate | Download (10.9 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 |
// private String abrev = null;
|
57 |
// private Bounds bounds = null;
|
58 |
|
59 |
|
60 |
// public class Bounds {
|
61 |
// double minX;
|
62 |
// double maxX;
|
63 |
// double minY;
|
64 |
// double maxY;
|
65 |
// public Bounds(double minX, double maxX, double minY, double maxY){
|
66 |
// this.minX = minX;
|
67 |
// this.maxX = maxX;
|
68 |
// this.minY = minY;
|
69 |
// this.maxY = maxY;
|
70 |
// }
|
71 |
//
|
72 |
//
|
73 |
// public double getMinX() {
|
74 |
// return minX;
|
75 |
// }
|
76 |
//
|
77 |
//
|
78 |
// public double getMaxX() {
|
79 |
// return maxX;
|
80 |
// }
|
81 |
//
|
82 |
//
|
83 |
// public double getMinY() {
|
84 |
// return minY;
|
85 |
// }
|
86 |
//
|
87 |
//
|
88 |
// public double getMaxY() {
|
89 |
// return maxY;
|
90 |
// }
|
91 |
//
|
92 |
// }
|
93 |
|
94 |
public CrsProj(String proj4String) { |
95 |
try {
|
96 |
if( proj4String == null ) { |
97 |
logger.error("Can't create CRS from 'null' value.");
|
98 |
return;
|
99 |
} |
100 |
createCrs(proj4String); |
101 |
} catch(CrsProjException e) {
|
102 |
logger.error("Can't create CRS from '"+proj4String+"'.",e); |
103 |
} |
104 |
// FIXME: borralo
|
105 |
showInfo(); |
106 |
|
107 |
} |
108 |
|
109 |
//FIXME: Esto era un parche para intentar generar el envelope de un sistema de referencia.
|
110 |
//TODO: A eliminar.
|
111 |
// public CrsProj(String proj4String, String abrev) {
|
112 |
// //FIXME: Esto es un parche para ver la diferencia entre las cadenas proj4
|
113 |
// // que nos da gvSIG y las que nos da gdal. A Eliminar
|
114 |
//// if(StringUtils.startsWith(abrev, "EPSG:")){
|
115 |
//// SpatialReference sr = new SpatialReference();
|
116 |
//// sr.ImportFromEPSG(Integer.parseInt(StringUtils.removeStart(abrev, "EPSG:")));
|
117 |
//// proj4String = sr.ExportToProj4();
|
118 |
//// }
|
119 |
//
|
120 |
// this(proj4String);
|
121 |
//// try {
|
122 |
//// if (proj4String == null) {
|
123 |
//// logger.error("Can't create CRS from 'null' value.");
|
124 |
//// return;
|
125 |
//// }
|
126 |
//// createCrs(proj4String);
|
127 |
//// } catch (CrsProjException e) {
|
128 |
//// logger.error("Can't create CRS from '" + proj4String + "'.", e);
|
129 |
//// }
|
130 |
// this.abrev = abrev;
|
131 |
// showInfo();
|
132 |
// }
|
133 |
//
|
134 |
// public Bounds getBounds() {
|
135 |
// if (bounds == null) {
|
136 |
// String unit = spatialReference.GetAttrValue("UNIT");
|
137 |
// String unitConv = spatialReference.GetAttrValue("UNIT", 1);
|
138 |
// if (StringUtils.equalsIgnoreCase(unit, osr.SRS_UA_DEGREE)) {
|
139 |
// bounds = new Bounds(-180, 180, -90, 90);
|
140 |
// } else if (StringUtils.equalsIgnoreCase(unit, osr.SRS_UA_RADIAN)) {
|
141 |
// bounds = new Bounds(-Math.PI, Math.PI, -Math.PI / 2, Math.PI / 2);
|
142 |
// } else {
|
143 |
// bounds =
|
144 |
// new Bounds(-spatialReference.GetSemiMajor() * 2* Math.PI / Double.parseDouble(unitConv),
|
145 |
// +spatialReference.GetSemiMajor() * 2 * Math.PI / Double.parseDouble(unitConv),
|
146 |
// -spatialReference.GetSemiMinor() * 2 * Math.PI / Double.parseDouble(unitConv),
|
147 |
// +spatialReference.GetSemiMinor() * 2 * Math.PI / Double.parseDouble(unitConv));
|
148 |
// }
|
149 |
// }
|
150 |
//
|
151 |
// return bounds;
|
152 |
// }
|
153 |
|
154 |
private void showInfo() { |
155 |
logger.info("=========================================================================");
|
156 |
logger.info("_strCrs" + " = " + _strCrs); |
157 |
logger.info("IsProjected" + " = " + spatialReference.IsProjected()); |
158 |
logger.info("IsGeographic" + " = " + spatialReference.IsGeographic()); |
159 |
logger.info("IsGeocentric" + " = " + spatialReference.IsGeocentric()); |
160 |
logger.info("IsVertical" + " = " + spatialReference.IsVertical()); |
161 |
|
162 |
logger.info("SemiMajor" + " = " + spatialReference.GetSemiMajor()); |
163 |
logger.info("SemiMinor" + " = " + spatialReference.GetSemiMinor()); |
164 |
logger.info("InvFlattening" + " = " + spatialReference.GetInvFlattening()); |
165 |
|
166 |
logger.info("PROJCS" + " = " + spatialReference.GetAttrValue("PROJCS")); |
167 |
logger.info("GEOGCS" + " = " + spatialReference.GetAttrValue("GEOGCS")); |
168 |
logger.info("DATUM" + " = " + spatialReference.GetAttrValue("DATUM")); |
169 |
logger.info("SPHEROID" + " = " + spatialReference.GetAttrValue("SPHEROID")); |
170 |
logger.info("PROJECTION" + " = " + spatialReference.GetAttrValue("PROJECTION")); |
171 |
logger.info("UNIT" + " = " + spatialReference.GetAttrValue("UNIT")); |
172 |
logger.info("conversion a metros" + " = " + spatialReference.GetAttrValue("UNIT",1)); |
173 |
logger.info("Linear unit name" + " = " + spatialReference.GetLinearUnitsName()); |
174 |
logger.info("linear units" + " = " + spatialReference.GetLinearUnits()); |
175 |
logger.info("Angular units" + " = " + spatialReference.GetAngularUnits()); |
176 |
logger.info("TOWGS84" + " = " + spatialReference.GetAttrValue("TOWGS84")); |
177 |
logger.info("TOWGS84" + " = " + spatialReference.GetAttrValue("TOWGS84",1)); |
178 |
logger.info("TOWGS84" + " = " + spatialReference.GetAttrValue("TOWGS84",2)); |
179 |
logger.info("SRS_PP_LATITUDE_OF_ORIGIN" + " = " + spatialReference.GetAttrValue("SRS_PP_LATITUDE_OF_ORIGIN")); |
180 |
logger.info("=========================================================================");
|
181 |
} |
182 |
|
183 |
protected void createCrs(String strCrs) throws CrsProjException { |
184 |
spatialReference = new SpatialReference();
|
185 |
|
186 |
int errNo = 0; |
187 |
try {
|
188 |
errNo = spatialReference.ImportFromProj4(strCrs); |
189 |
} catch (Exception e) { |
190 |
throw new CrsProjException("Error creating CRS", e); |
191 |
} |
192 |
if (errNo!=0) { |
193 |
throw new CrsProjException("Error creating CRS. Erro code = "+errNo); |
194 |
} |
195 |
|
196 |
try {
|
197 |
errNo = spatialReference.Fixup(); |
198 |
} catch (Exception e) { |
199 |
throw new CrsProjException("Error fixing up CRS", e); |
200 |
} |
201 |
|
202 |
if (errNo!=0) { |
203 |
throw new CrsProjException("Error creating CRS. Erro code = "+errNo); |
204 |
} |
205 |
|
206 |
_strCrs=strCrs; |
207 |
} |
208 |
|
209 |
public void reloadCrs() throws CrsException { |
210 |
// do nothing
|
211 |
} |
212 |
|
213 |
public void changeStrCrs(String code) { |
214 |
_strCrs += code; |
215 |
} |
216 |
|
217 |
public boolean isLatlong() { |
218 |
return spatialReference.IsGeographic()!=0; |
219 |
} |
220 |
|
221 |
public boolean isGeocent() { |
222 |
return spatialReference.IsGeocentric()!=0; |
223 |
} |
224 |
|
225 |
public String getUnits() { |
226 |
return spatialReference.GetAttrValue("UNIT"); |
227 |
} |
228 |
|
229 |
public double getConversionFactorToMeters() { |
230 |
return Double.parseDouble(spatialReference.GetAttrValue("UNIT",1)); |
231 |
} |
232 |
|
233 |
public String getStr() { |
234 |
return spatialReference.ExportToProj4();
|
235 |
} |
236 |
|
237 |
public long getPtr() { |
238 |
return SpatialReference.getCPtr(spatialReference);
|
239 |
} |
240 |
|
241 |
public SpatialReference getSpatialReference(){
|
242 |
return this.spatialReference; |
243 |
} |
244 |
|
245 |
public static int operate(double[] firstCoord, double[] secondCoord, double[] thirdCoord, CrsProj srcCrs, |
246 |
CrsProj destCrs) throws OperationCrsException {
|
247 |
|
248 |
try {
|
249 |
logger.warn("CrsProj.operate: No deber?a pasar por aqu?. (Excepcion provocada para ver de d?nde viene).");
|
250 |
throw new Exception(); |
251 |
} catch (Exception e) { |
252 |
e.printStackTrace(); |
253 |
} |
254 |
|
255 |
long srcCrsPtr = srcCrs.getPtr();
|
256 |
long destCrsPtr = destCrs.getPtr();
|
257 |
if (srcCrsPtr == 0 || destCrsPtr == 0) { |
258 |
throw new OperationCrsException(srcCrs, destCrs, ""); |
259 |
} |
260 |
|
261 |
int points = firstCoord.length;
|
262 |
if (secondCoord.length > points){
|
263 |
points = secondCoord.length; |
264 |
} |
265 |
if (thirdCoord.length > points){
|
266 |
points = thirdCoord.length; |
267 |
} |
268 |
|
269 |
CoordinateTransformation ct = null;
|
270 |
try {
|
271 |
ct = CoordinateTransformation.CreateCoordinateTransformation(srcCrs.spatialReference, destCrs.spatialReference); |
272 |
} catch (Exception e) { |
273 |
throw new OperationCrsException(srcCrs, destCrs, "Can't create transformation"); |
274 |
} |
275 |
try {
|
276 |
if (ct!=null) { |
277 |
for (int i = 0; i < points; i++) { |
278 |
double[] resultCoords = ct.TransformPoint(firstCoord[i], secondCoord[i], thirdCoord[i]); |
279 |
firstCoord[i] = resultCoords[0];
|
280 |
secondCoord[i] = resultCoords[1];
|
281 |
thirdCoord[i] = resultCoords[2];
|
282 |
} |
283 |
} |
284 |
} catch (Exception e) { |
285 |
throw new OperationCrsException(srcCrs, destCrs, "Can't transform points"); |
286 |
} |
287 |
return 0; |
288 |
} |
289 |
|
290 |
public static int compareDatums(CrsProj crs1, CrsProj crs2) { |
291 |
long crs1Ptr = crs1.getPtr();
|
292 |
long crs2Ptr = crs2.getPtr();
|
293 |
if (crs1Ptr == 0 || crs2Ptr == 0) { |
294 |
throw new IllegalArgumentException("Coordinate compare error: " + crs1.getStr() + " to " + crs2.getStr()); |
295 |
} |
296 |
return Long.valueOf(crs1Ptr).compareTo(Long.valueOf(crs2Ptr)); |
297 |
} |
298 |
|
299 |
|
300 |
} |