Revision 359 org.gvsig.projection.jcrs/trunk/org.gvsig.projection.jcrs/org.gvsig.projection.jcrs.lib/src/main/java/org/gvsig/crs/proj/CrsProj.java
CrsProj.java | ||
---|---|---|
40 | 40 |
|
41 | 41 |
package org.gvsig.crs.proj; |
42 | 42 |
|
43 |
import org.gvsig.crs.CrsException; |
|
43 |
import org.gdal.osr.CoordinateTransformation; |
|
44 |
import org.gdal.osr.SpatialReference; |
|
44 | 45 |
import org.slf4j.Logger; |
45 | 46 |
import org.slf4j.LoggerFactory; |
46 | 47 |
|
47 |
/** |
|
48 |
* Clase que representa un CRS desde el punto de vista de proj4. |
|
49 |
* Ser? utilizada para realizar operaciones con proj4. |
|
50 |
* |
|
51 |
* @author Miguel Garc?a Jim?nez (garciajimenez.miguel@gmail.com) |
|
52 |
* |
|
53 |
*/ |
|
48 |
import org.gvsig.crs.CrsException; |
|
54 | 49 |
|
55 |
public class CrsProj extends JNIBaseCrs {
|
|
50 |
public class CrsProj { |
|
56 | 51 |
|
57 |
private static Logger logger = LoggerFactory.getLogger(CrsProj.class); |
|
58 |
/** |
|
59 |
* Constructor |
|
60 |
* |
|
61 |
* @param strCrs Cadena proj4 que representa el CRS(y los par?metros |
|
62 |
* de transformaci?n). |
|
63 |
*/ |
|
64 |
public CrsProj(String strCrs) { |
|
65 |
try { |
|
66 |
if( strCrs == null ) { |
|
67 |
logger.error("Can't create CRS from 'null' value."); |
|
68 |
return; |
|
69 |
} |
|
70 |
createCrs(strCrs); |
|
71 |
} catch(CrsProjException e) { |
|
72 |
logger.error("Can't create CRS from '"+strCrs+"'.",e); |
|
73 |
} |
|
52 |
private static Logger logger = LoggerFactory.getLogger(CrsProj.class); |
|
74 | 53 |
|
75 |
} |
|
54 |
private SpatialReference spatialReference; |
|
55 |
private String _strCrs; |
|
56 |
// private String abrev = null; |
|
57 |
// private Bounds bounds = null; |
|
76 | 58 |
|
77 |
protected void finalize() { |
|
78 |
deleteCrs(); |
|
79 |
} |
|
80 | 59 |
|
81 |
public void reloadCrs() throws CrsException { |
|
82 |
deleteCrs(); |
|
83 |
try { |
|
84 |
this.createCrs(_strCrs); |
|
85 |
} catch (CrsProjException e) { |
|
86 |
throw new CrsException(e); |
|
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 |
// } |
|
87 | 93 |
|
88 |
} |
|
89 |
} |
|
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 |
|
|
90 | 300 |
} |
Also available in: Unified diff