root / org.gvsig.jcrs / libJCRS / src / org / gvsig / crs / CrsGT.java @ 38
History | View | Annotate | Download (10.6 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; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Graphics2D; |
45 |
import java.awt.geom.Point2D; |
46 |
import java.awt.geom.Rectangle2D; |
47 |
|
48 |
import org.cresques.cts.ICoordTrans; |
49 |
import org.cresques.cts.IDatum; |
50 |
import org.cresques.cts.IProjection; |
51 |
import org.cresques.geo.ViewPortData; |
52 |
import org.geotools.referencing.crs.AbstractDerivedCRS; |
53 |
import org.geotools.referencing.crs.AbstractSingleCRS; |
54 |
import org.geotools.referencing.datum.DefaultGeodeticDatum; |
55 |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
56 |
|
57 |
import org.gvsig.crs.proj.CrsProj; |
58 |
import org.gvsig.crs.proj.CrsProjException; |
59 |
import org.gvsig.crs.proj.JNIBaseCrs; |
60 |
import org.gvsig.crs.proj.OperationCrsException; |
61 |
import org.gvsig.fmap.crs.CRSFactory; |
62 |
|
63 |
/**
|
64 |
* Clase que representa un CRS basado en GeoTools/GeoApi.
|
65 |
*
|
66 |
* @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
67 |
*
|
68 |
*/
|
69 |
public class CrsGT implements ICrs { |
70 |
private static final Color basicGridColor = new Color(64, 64, 64, 128); |
71 |
|
72 |
/**
|
73 |
* CRS GeoApi. Nucleo de la clare CrsGT.
|
74 |
*/
|
75 |
private CoordinateReferenceSystem crsGT = null; |
76 |
|
77 |
/**
|
78 |
*Cadena proj4
|
79 |
*/
|
80 |
private String proj4String = null; |
81 |
|
82 |
private Color gridColor = basicGridColor; |
83 |
|
84 |
/**
|
85 |
* Par?metros de transformaci?n para el CRS fuente en formato proj4.
|
86 |
*/
|
87 |
private String sourceTrParams = null; |
88 |
|
89 |
/**
|
90 |
* Par?metros de transformaci?n para el CRS destino en formato proj4.
|
91 |
*/
|
92 |
private String targetTrParams = null; |
93 |
|
94 |
/**
|
95 |
* CRS operable con proj4.
|
96 |
*/
|
97 |
private CrsProj crsProj = null; |
98 |
|
99 |
/**
|
100 |
* CRS Base operable con proj4.
|
101 |
*/
|
102 |
private CrsProj crsProjBase = null; |
103 |
|
104 |
/**
|
105 |
* Provisional, hasta que se elimine getCrsWkt de ICrs.
|
106 |
*/
|
107 |
private CrsWkt crsWkt = null; |
108 |
|
109 |
/**
|
110 |
* Conversor de CRSs a cadenas proj4.
|
111 |
*/
|
112 |
private Proj4 proj4 = null; |
113 |
|
114 |
|
115 |
|
116 |
/**
|
117 |
* Constructor a partir de un CoordinateReferenceSystem
|
118 |
*
|
119 |
* @param crsGT
|
120 |
* @throws CrsProjException
|
121 |
*/
|
122 |
public CrsGT(CoordinateReferenceSystem crsGT){
|
123 |
this.crsGT = crsGT;
|
124 |
} |
125 |
|
126 |
public int getCode() { |
127 |
return Integer.valueOf(getAbrev().split(":")[1]).intValue(); |
128 |
} |
129 |
|
130 |
public CrsWkt getCrsWkt() {
|
131 |
if (crsWkt==null) |
132 |
crsWkt = new CrsWkt(crsGT);
|
133 |
return crsWkt;
|
134 |
} |
135 |
|
136 |
public String getWKT() { |
137 |
return crsGT.toWKT();
|
138 |
} |
139 |
|
140 |
public void setTransformationParams(String SourceParams, String TargetParams) { |
141 |
this.sourceTrParams = SourceParams;
|
142 |
this.targetTrParams = TargetParams;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* Devuelve los parametros de la transformacion del crs fuente
|
147 |
* @return
|
148 |
*/
|
149 |
public String getSourceTransformationParams() { |
150 |
return this.sourceTrParams; |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Devuelve los parametros de la transformacion del crs destino
|
155 |
* @return
|
156 |
*/
|
157 |
public String getTargetTransformationParams() { |
158 |
return this.targetTrParams; |
159 |
} |
160 |
|
161 |
public Point2D createPoint(double x, double y) { |
162 |
return new Point2D.Double(x,y); |
163 |
} |
164 |
|
165 |
public void drawGrid(Graphics2D g, ViewPortData vp) { |
166 |
// TODO Auto-generated method stub
|
167 |
|
168 |
} |
169 |
|
170 |
public Point2D fromGeo(Point2D gPt, Point2D mPt) { |
171 |
// TODO Auto-generated method stub
|
172 |
return null; |
173 |
} |
174 |
|
175 |
public String getAbrev() { |
176 |
return ((AbstractSingleCRS)crsGT).getIdentifiers().iterator().next().toString();
|
177 |
} |
178 |
|
179 |
public ICoordTrans getCT(IProjection dest) {
|
180 |
|
181 |
try {
|
182 |
if (dest == this) |
183 |
return null; |
184 |
COperation operation = null;
|
185 |
if(((ICrs)dest).getSourceTransformationParams() != null || ((ICrs)dest).getTargetTransformationParams() != null) |
186 |
operation = new COperation(this, (ICrs)dest,((ICrs)dest).getTargetTransformationParams(),((ICrs)dest).getSourceTransformationParams()); |
187 |
else
|
188 |
operation = new COperation(this, (ICrs)dest,sourceTrParams,targetTrParams); |
189 |
return operation;
|
190 |
}catch (CrsException e) {
|
191 |
throw new RuntimeException(e); |
192 |
} |
193 |
|
194 |
/*
|
195 |
try {
|
196 |
operation = new COperation(this, (ICrs)dest);
|
197 |
} catch (CrsException e) {
|
198 |
// TODO Auto-generated catch block
|
199 |
e.printStackTrace();
|
200 |
}
|
201 |
|
202 |
if (!getTransformationParams().equals("")){
|
203 |
if (isParamsInTarget())
|
204 |
operation.setParamsCrsProj(new CrsProj(crsDest.getProj4String()+getTransformationParams()), true);
|
205 |
else
|
206 |
operation.setParamsCrsProj(new CrsProj(getProj4String()+getTransformationParams()), false);
|
207 |
return operation;
|
208 |
}
|
209 |
|
210 |
return operation; */
|
211 |
} |
212 |
|
213 |
public IDatum getDatum() {
|
214 |
DefaultGeodeticDatum datumGT = (DefaultGeodeticDatum)((AbstractSingleCRS)crsGT).getDatum(); |
215 |
CRSDatum datum = new CRSDatum(datumGT.getEllipsoid().getSemiMajorAxis(),datumGT.getEllipsoid().getInverseFlattening());
|
216 |
return datum;
|
217 |
} |
218 |
|
219 |
public Color getGridColor() { |
220 |
return gridColor;
|
221 |
} |
222 |
|
223 |
public double getScale(double minX, double maxX, double width, double dpi) { |
224 |
double scale = 0D; |
225 |
//Esto ahora se hace fuera
|
226 |
// if (!isProjected()) { // Es geogr?fico; calcula la escala.
|
227 |
// scale = ((maxX - minX) * // grados
|
228 |
//
|
229 |
// // 1852.0 metros x minuto de meridiano
|
230 |
// (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
|
231 |
// width; // pixels
|
232 |
// }
|
233 |
// else{
|
234 |
scale = ((maxX - minX) * // metros
|
235 |
(dpi / 2.54 * 100.0)) / // px / metro |
236 |
width; // pixels
|
237 |
// }
|
238 |
return scale;
|
239 |
} |
240 |
|
241 |
public double getScale(double minX, double maxX, double minY, double maxY, double width, double dpi) { |
242 |
|
243 |
double scale = 0D; |
244 |
double incX = (maxX-minX);
|
245 |
|
246 |
if (!isProjected()) {
|
247 |
double a = getDatum().getESemiMajorAxis();
|
248 |
double invF = getDatum().getEIFlattening();
|
249 |
double meanY = (minY+maxY)/2.0; |
250 |
double radius = 0.0; |
251 |
|
252 |
|
253 |
if (invF == Double.POSITIVE_INFINITY){ |
254 |
radius = a; |
255 |
} |
256 |
else{
|
257 |
double e2 = 2.0/invF-Math.pow(1.0/invF,2.0); |
258 |
radius = a/Math.sqrt(1.0-e2*Math.pow(Math.sin(meanY*Math.PI/180.0),2.0))*Math.cos(meanY*Math.PI/180.0); |
259 |
} |
260 |
incX *= Math.PI/180.0*radius; |
261 |
} |
262 |
|
263 |
scale = (incX * // metros
|
264 |
(dpi / 2.54 * 100.0)) / // px / metro |
265 |
width; // pixels
|
266 |
|
267 |
return scale;
|
268 |
} |
269 |
|
270 |
public boolean isProjected() { |
271 |
if (crsGT instanceof AbstractDerivedCRS){ |
272 |
return true; |
273 |
}else
|
274 |
return false; |
275 |
} |
276 |
|
277 |
public void setGridColor(Color c) { |
278 |
gridColor = c; |
279 |
} |
280 |
|
281 |
public Point2D toGeo(Point2D pt) { |
282 |
if (isProjected()){
|
283 |
double x[] = {pt.getX()}; |
284 |
double y[] = {pt.getY()}; |
285 |
double z[] = {0D}; |
286 |
try {
|
287 |
JNIBaseCrs.operate( x , y, z, |
288 |
getCrsProj(),getCrsProjBase()); |
289 |
} catch (OperationCrsException e) {
|
290 |
throw new RuntimeException(e); |
291 |
} |
292 |
return new Point2D.Double(x[0],y[0]); |
293 |
} |
294 |
else
|
295 |
return pt;
|
296 |
} |
297 |
|
298 |
/**
|
299 |
* @param targetParam
|
300 |
*/
|
301 |
/*public void setParamsInTarget(boolean targetParam) {
|
302 |
this.paramsInTarget = targetParam;
|
303 |
}*/
|
304 |
|
305 |
/**
|
306 |
*
|
307 |
* @return Cadena proj4 Correspondiente al CRS.
|
308 |
* @throws CrsException
|
309 |
*/
|
310 |
public String getProj4String() throws CrsException { |
311 |
if (proj4String == null) |
312 |
proj4String = getProj4().exportToProj4(crsGT); |
313 |
|
314 |
return proj4String;
|
315 |
} |
316 |
|
317 |
public CoordinateReferenceSystem getCrsGT() {
|
318 |
return crsGT;
|
319 |
} |
320 |
|
321 |
public CrsProj getCrsProj() {
|
322 |
if (crsProj == null) |
323 |
try {
|
324 |
crsProj = new CrsProj(getProj4String());
|
325 |
} catch (CrsException e) {
|
326 |
throw new RuntimeException(e); |
327 |
} |
328 |
return crsProj;
|
329 |
} |
330 |
|
331 |
private CrsProj getCrsProjBase() {
|
332 |
if (crsProjBase == null){ |
333 |
AbstractDerivedCRS derivedCRS = (AbstractDerivedCRS)crsGT; |
334 |
try {
|
335 |
crsProjBase = new CrsProj(getProj4().exportToProj4(derivedCRS.getBaseCRS()));
|
336 |
} catch (CrsException e) {
|
337 |
throw new RuntimeException(e); |
338 |
} |
339 |
} |
340 |
return crsProjBase;
|
341 |
} |
342 |
|
343 |
private Proj4 getProj4() {
|
344 |
if (proj4 == null) |
345 |
try {
|
346 |
proj4 = new Proj4();
|
347 |
} catch (CrsException e) {
|
348 |
throw new RuntimeException(e); |
349 |
} |
350 |
return proj4;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* @return Authority:code:proj@Sourceparam@TargerParam@
|
355 |
*/
|
356 |
public String getFullCode() { |
357 |
if (this.sourceTrParams == null && this.targetTrParams == null) |
358 |
return getAbrev();
|
359 |
String sourceParams = ""; |
360 |
String targetParams = ""; |
361 |
if (sourceTrParams != null) |
362 |
sourceParams = sourceTrParams; |
363 |
if (targetTrParams != null) |
364 |
targetParams = targetTrParams; |
365 |
|
366 |
return getAbrev()+":proj@"+sourceParams+"@"+targetParams; |
367 |
} |
368 |
|
369 |
public Rectangle2D getExtent(Rectangle2D extent,double scale,double wImage,double hImage,double mapUnits,double distanceUnits,double dpi) { |
370 |
double w =0; |
371 |
double h =0; |
372 |
double wExtent =0; |
373 |
double hExtent =0; |
374 |
// if (isProjected()) {
|
375 |
w = ((wImage / dpi) * 2.54);
|
376 |
h = ((hImage / dpi) * 2.54);
|
377 |
wExtent =w * scale / mapUnits; |
378 |
hExtent =h * scale / mapUnits; |
379 |
|
380 |
//En las no proyectadas, ahora viene el calculo hecho
|
381 |
//al haber incluido los grados en el array DISTANCETRANS2METER del MapContext
|
382 |
// }else {
|
383 |
// w = ((wImage / dpi) * 2.54);
|
384 |
// h = ((hImage / dpi) * 2.54);
|
385 |
// wExtent =(w*scale*distanceUnits)/ (mapUnits*1852.0*60.0);
|
386 |
// hExtent =(h*scale*distanceUnits)/ (mapUnits*1852.0*60.0);
|
387 |
// }
|
388 |
double xExtent = extent.getCenterX() - wExtent/2; |
389 |
double yExtent = extent.getCenterY() - hExtent/2; |
390 |
Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent); |
391 |
return rec;
|
392 |
} |
393 |
|
394 |
public Object clone() throws CloneNotSupportedException { |
395 |
return CRSFactory.getCRS( this.getFullCode() ); |
396 |
} |
397 |
|
398 |
} |