root / org.gvsig.projection.jcrs / trunk / org.gvsig.projection.jcrs / org.gvsig.projection.jcrs.lib / src / main / java / org / gvsig / crs / CrsWkt.java @ 436
History | View | Annotate | Download (24.5 KB)
1 | 147 | jjdelcerro | /* 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.util.ArrayList; |
||
44 | |||
45 | import javax.units.ConversionException; |
||
46 | import javax.units.Unit; |
||
47 | |||
48 | 436 | fdiaz | import org.gdal.osr.SpatialReference; |
49 | 147 | jjdelcerro | import org.geotools.referencing.CRS; |
50 | import org.geotools.referencing.crs.AbstractSingleCRS; |
||
51 | import org.geotools.referencing.crs.DefaultGeographicCRS; |
||
52 | import org.geotools.referencing.crs.DefaultProjectedCRS; |
||
53 | import org.geotools.referencing.datum.DefaultEllipsoid; |
||
54 | import org.geotools.referencing.datum.DefaultGeodeticDatum; |
||
55 | import org.geotools.referencing.datum.DefaultPrimeMeridian; |
||
56 | import org.opengis.referencing.FactoryException; |
||
57 | import org.opengis.referencing.NoSuchAuthorityCodeException; |
||
58 | import org.opengis.referencing.crs.CoordinateReferenceSystem; |
||
59 | import org.opengis.referencing.datum.PrimeMeridian; |
||
60 | import org.slf4j.Logger; |
||
61 | import org.slf4j.LoggerFactory; |
||
62 | |||
63 | /**
|
||
64 | * Clase que parsea una cadena wkt y consigue todos sus par?metros
|
||
65 | * de manera independiente para poder ser utilizados
|
||
66 | * en distintas funciones
|
||
67 | 436 | fdiaz | *
|
68 | 147 | jjdelcerro | * @author Jos? Luis G?mez Mart?nez (JoseLuis.Gomez@uclm.es)
|
69 | *
|
||
70 | */
|
||
71 | |||
72 | public class CrsWkt { |
||
73 | 436 | fdiaz | private static final Logger logger = LoggerFactory.getLogger(CrsWkt.class); |
74 | 147 | jjdelcerro | |
75 | private String wkt; |
||
76 | private String projcs = ""; |
||
77 | private String geogcs = ""; |
||
78 | private String datum = ""; |
||
79 | private String[] spheroid = {"" ,"",""}; |
||
80 | private String[] primem = {"",""}; |
||
81 | private String[] unit = {"",""}; |
||
82 | private String[] unit_p = {"",""}; //unidades de la projeccion (final de la cadena wkt) |
||
83 | private String projection = ""; |
||
84 | private String[] param_name; |
||
85 | private String[] param_value; |
||
86 | private int contador = 0; |
||
87 | private String[] authority = {"",""}; |
||
88 | 436 | fdiaz | |
89 | 147 | jjdelcerro | int divider=10000; |
90 | 436 | fdiaz | |
91 | 147 | jjdelcerro | /**
|
92 | * Small tolerance factor for rounding errors.
|
||
93 | */
|
||
94 | private static final double EPS = 1E-8; |
||
95 | 436 | fdiaz | |
96 | |||
97 | /**
|
||
98 | *
|
||
99 | */
|
||
100 | public CrsWkt(SpatialReference spatialReference) {
|
||
101 | wkt = spatialReference.ExportToWkt(); |
||
102 | projcs = spatialReference.GetAttrValue("PROJCS",0); |
||
103 | if(projcs==null){ |
||
104 | projcs = "";
|
||
105 | } else {
|
||
106 | unit_p[0] = spatialReference.GetAttrValue("UNIT",0); |
||
107 | unit_p[1] = spatialReference.GetAttrValue("UNIT",1); |
||
108 | projection = spatialReference.GetAttrValue("PROJECTION");
|
||
109 | } |
||
110 | geogcs = spatialReference.GetAttrValue("GEOGCS",0); |
||
111 | datum = spatialReference.GetAttrValue("DATUM",0); |
||
112 | |||
113 | CRSDatum crsDatum = new CRSDatum(spatialReference.GetSemiMajor(), spatialReference.GetInvFlattening());
|
||
114 | |||
115 | spheroid[0] = String.valueOf(spatialReference.GetSemiMinor()); //.GetAttrValue("SPHEROID",0); |
||
116 | spheroid[1] = String.valueOf(spatialReference.GetSemiMajor()); //GetAttrValue("SPHEROID",1); |
||
117 | spheroid[2] = String.valueOf(spatialReference.GetInvFlattening()); //.GetAttrValue("SPHEROID",2); |
||
118 | primem[0] = spatialReference.GetAttrValue("PRIMEM",0); |
||
119 | primem[1] = spatialReference.GetAttrValue("PRIMEM",1); |
||
120 | double toRadians = spatialReference.GetAngularUnits();
|
||
121 | unit[1] = String.valueOf(toRadians); |
||
122 | if(toRadians==1){ |
||
123 | unit[0] = org.gdal.osr.osr.SRS_UA_RADIAN;
|
||
124 | } else {
|
||
125 | unit[0] = org.gdal.osr.osr.SRS_UA_DEGREE;
|
||
126 | } |
||
127 | |||
128 | authority[0] = spatialReference.GetAuthorityName(null); |
||
129 | authority[1] = spatialReference.GetAuthorityCode(null); |
||
130 | |||
131 | } |
||
132 | |||
133 | 147 | jjdelcerro | public CrsWkt(String wkt_spaces) { |
134 | String aux;
|
||
135 | String wkt = ""; |
||
136 | for(int i = 0; i < wkt_spaces.length(); i++) { |
||
137 | aux = ""+wkt_spaces.charAt(i);
|
||
138 | if(!aux.equals(" ")) { |
||
139 | wkt+=aux; |
||
140 | }else {
|
||
141 | wkt += "";
|
||
142 | } |
||
143 | } |
||
144 | if (wkt.length()>15) { |
||
145 | fromWKT(wkt, false);
|
||
146 | } else {
|
||
147 | fromCode(wkt); |
||
148 | } |
||
149 | } |
||
150 | 436 | fdiaz | |
151 | 147 | jjdelcerro | public CrsWkt(CoordinateReferenceSystem crsGT) {
|
152 | AbstractSingleCRS crs = (AbstractSingleCRS)crsGT; |
||
153 | String authority = crs.getName().toString().split(":")[0]; |
||
154 | setAuthority(((AbstractSingleCRS)crsGT).getIdentifiers().iterator().next().toString()); |
||
155 | setWkt(crsGT.toWKT()); |
||
156 | 436 | fdiaz | |
157 | 147 | jjdelcerro | if (crsGT instanceof DefaultProjectedCRS) { |
158 | DefaultProjectedCRS sour = (DefaultProjectedCRS) crsGT; |
||
159 | setProjcs(sour.getName().toString().split(":")[1]); |
||
160 | String[] val = sour.getBaseCRS().getName().toString().split(":"); |
||
161 | if (val.length<2) |
||
162 | setGeogcs(sour.getBaseCRS().getName().toString().split(":")[0]); |
||
163 | else
|
||
164 | setGeogcs(sour.getBaseCRS().getName().toString().split(":")[1]); |
||
165 | DefaultGeodeticDatum d = (DefaultGeodeticDatum) sour.getDatum(); |
||
166 | val = d.getName().toString().split(":");
|
||
167 | if (val.length<2) |
||
168 | setDatumName(d.getName().toString().split(":")[0]); |
||
169 | else
|
||
170 | setDatumName(d.getName().toString().split(":")[1]); |
||
171 | 436 | fdiaz | setSpheroid((DefaultEllipsoid)d.getEllipsoid()); |
172 | 147 | jjdelcerro | setPrimen(d.getPrimeMeridian()); |
173 | val = sour.getConversionFromBase().getMethod().getName().toString().split(":");
|
||
174 | if (val.length<2) |
||
175 | setProjection(sour.getConversionFromBase().getMethod().getName().toString().split(":")[0]); |
||
176 | else
|
||
177 | setProjection(sour.getConversionFromBase().getMethod().getName().toString().split(":")[1]); |
||
178 | String str;
|
||
179 | param_name = new String[sour.getConversionFromBase().getParameterValues().values().size()]; |
||
180 | param_value= new String[sour.getConversionFromBase().getParameterValues().values().size()]; |
||
181 | for (int i=0; i< sour.getConversionFromBase().getParameterValues().values().size();i++) { |
||
182 | str = sour.getConversionFromBase().getParameterValues().values().get(i).toString(); |
||
183 | Unit u = sour.getConversionFromBase().getParameterValues().parameter(str.split("=")[0]).getUnit(); |
||
184 | double value = sour.getConversionFromBase().getParameterValues().parameter(str.split("=")[0]).doubleValue(); |
||
185 | value = convert(value, u.toString()); |
||
186 | param_name[i] = str.split("=")[0]; |
||
187 | param_value [i] = String.valueOf(value);
|
||
188 | } |
||
189 | 436 | fdiaz | |
190 | 147 | jjdelcerro | } |
191 | 436 | fdiaz | |
192 | 147 | jjdelcerro | if (crsGT instanceof DefaultGeographicCRS) { |
193 | DefaultGeographicCRS sour = (DefaultGeographicCRS) crsGT; |
||
194 | String[] val = sour.getName().toString().split(":"); |
||
195 | if (val.length<2) |
||
196 | setGeogcs(sour.getName().toString().split(":")[0]); |
||
197 | else
|
||
198 | setGeogcs(sour.getName().toString().split(":")[1]); |
||
199 | 436 | fdiaz | |
200 | 147 | jjdelcerro | DefaultGeodeticDatum d = (DefaultGeodeticDatum) sour.getDatum(); |
201 | val = d.getName().toString().split(":");
|
||
202 | if (val.length<2) |
||
203 | setDatumName(d.getName().toString().split(":")[0]); |
||
204 | else
|
||
205 | setDatumName(d.getName().toString().split(":")[1]); |
||
206 | 436 | fdiaz | setSpheroid((DefaultEllipsoid)d.getEllipsoid()); |
207 | 147 | jjdelcerro | setPrimen(d.getPrimeMeridian()); |
208 | 436 | fdiaz | |
209 | } |
||
210 | 147 | jjdelcerro | } |
211 | 436 | fdiaz | |
212 | 147 | jjdelcerro | /**
|
213 | * Acepta la cadena wkt y si es un CRS proyectado para conseguir el parser
|
||
214 | * de los dos modelos de cadena wkt que se utilizan, una cuando es base
|
||
215 | * y otra cuando es proyectado
|
||
216 | * @param wkt
|
||
217 | * @param isProj
|
||
218 | */
|
||
219 | private void fromWKT(String wkt, boolean isProj) { |
||
220 | 436 | fdiaz | |
221 | 147 | jjdelcerro | String res = new String(); |
222 | if(!wkt.startsWith("EPSG:")) { |
||
223 | res = "";
|
||
224 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
225 | res += wkt.charAt(contador); |
||
226 | } |
||
227 | if(res.equals("GEOGCS[")) { |
||
228 | 436 | fdiaz | |
229 | 147 | jjdelcerro | contador++; |
230 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
231 | geogcs += wkt.charAt(contador); |
||
232 | } |
||
233 | 436 | fdiaz | |
234 | 147 | jjdelcerro | res = "";
|
235 | contador++; |
||
236 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
237 | res += wkt.charAt(contador); |
||
238 | } |
||
239 | if(res.equals(",DATUM[")) { |
||
240 | contador++; |
||
241 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
242 | datum += wkt.charAt(contador); |
||
243 | } |
||
244 | } |
||
245 | res = "";
|
||
246 | contador++; |
||
247 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
248 | res += wkt.charAt(contador); |
||
249 | } |
||
250 | if(res.equals(",SPHEROID[")) { |
||
251 | contador++; |
||
252 | for(; wkt.charAt(contador) != ']'; contador++) { |
||
253 | while(wkt.charAt(contador) != '"' ) { |
||
254 | spheroid[0] += wkt.charAt(contador);
|
||
255 | contador++; |
||
256 | } |
||
257 | contador +=2;
|
||
258 | while(wkt.charAt(contador) != ',') { |
||
259 | spheroid[1] += wkt.charAt(contador);
|
||
260 | contador++; |
||
261 | } |
||
262 | contador++; |
||
263 | while(wkt.charAt(contador) != ']') { |
||
264 | spheroid[2] += wkt.charAt(contador);
|
||
265 | contador++; |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | contador++; |
||
270 | res = "";
|
||
271 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
272 | res += wkt.charAt(contador); |
||
273 | } |
||
274 | if(res.equals(",PRIMEM[")) { |
||
275 | contador++; |
||
276 | for(; wkt.charAt(contador) != ','; contador++) { |
||
277 | while(wkt.charAt(contador) != '"' ){ |
||
278 | primem[0] += wkt.charAt(contador);
|
||
279 | contador ++; |
||
280 | } |
||
281 | contador +=2;
|
||
282 | while(wkt.charAt(contador) != ']') { |
||
283 | primem[1] += wkt.charAt(contador);
|
||
284 | contador ++; |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | contador++; |
||
289 | res = "";
|
||
290 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
291 | res += wkt.charAt(contador); |
||
292 | } |
||
293 | if(res.equals("UNIT[")) { |
||
294 | contador++; |
||
295 | for(; wkt.charAt(contador) != ','; contador++) { |
||
296 | while(wkt.charAt(contador) != '"' ){ |
||
297 | unit[0] += wkt.charAt(contador);
|
||
298 | contador ++; |
||
299 | } |
||
300 | contador +=2;
|
||
301 | while(wkt.charAt(contador) != ']') { |
||
302 | unit[1] += wkt.charAt(contador);
|
||
303 | contador ++; |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | contador++; |
||
308 | res = "";
|
||
309 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
310 | res += wkt.charAt(contador); |
||
311 | } |
||
312 | if(res.equals("AUTHORITY[")) { |
||
313 | contador++; |
||
314 | for(; wkt.charAt(contador) != ']'; contador++) { |
||
315 | while(wkt.charAt(contador) != '"' ){ |
||
316 | authority[0] += wkt.charAt(contador);
|
||
317 | contador ++; |
||
318 | } |
||
319 | contador +=2;
|
||
320 | while(wkt.charAt(contador) != ']') { |
||
321 | authority[1] += wkt.charAt(contador);
|
||
322 | contador ++; |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | }else if (res.equals("PROJCS[")) { |
||
327 | contador++; |
||
328 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
329 | projcs += wkt.charAt(contador); |
||
330 | } |
||
331 | contador++; |
||
332 | res = "";
|
||
333 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
334 | res += wkt.charAt(contador); |
||
335 | } |
||
336 | if(res.equals(",GEOGCS[")) { |
||
337 | contador++; |
||
338 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
339 | geogcs += wkt.charAt(contador); |
||
340 | } |
||
341 | } |
||
342 | 436 | fdiaz | |
343 | 147 | jjdelcerro | res = "";
|
344 | contador++; |
||
345 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
346 | res += wkt.charAt(contador); |
||
347 | } |
||
348 | if(res.equals(",DATUM[")) { |
||
349 | contador++; |
||
350 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
351 | datum += wkt.charAt(contador); |
||
352 | } |
||
353 | } |
||
354 | res = "";
|
||
355 | contador++; |
||
356 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
357 | res += wkt.charAt(contador); |
||
358 | } |
||
359 | if(res.equals(",SPHEROID[")) { |
||
360 | contador++; |
||
361 | for(; wkt.charAt(contador) != ']'; contador++) { |
||
362 | while(wkt.charAt(contador) != '"' ) { |
||
363 | spheroid[0] += wkt.charAt(contador);
|
||
364 | contador++; |
||
365 | } |
||
366 | contador +=2;
|
||
367 | while(wkt.charAt(contador) != ',') { |
||
368 | spheroid[1] += wkt.charAt(contador);
|
||
369 | contador++; |
||
370 | } |
||
371 | contador++; |
||
372 | while(wkt.charAt(contador) != ']') { |
||
373 | spheroid[2] += wkt.charAt(contador);
|
||
374 | contador++; |
||
375 | } |
||
376 | } |
||
377 | } |
||
378 | contador++; |
||
379 | res = "";
|
||
380 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
381 | res += wkt.charAt(contador); |
||
382 | } |
||
383 | if(res.equals(",PRIMEM[")) { |
||
384 | contador++; |
||
385 | for(; wkt.charAt(contador) != ','; contador++) { |
||
386 | while(wkt.charAt(contador) != '"' ){ |
||
387 | primem[0] += wkt.charAt(contador);
|
||
388 | contador ++; |
||
389 | } |
||
390 | contador +=2;
|
||
391 | while(wkt.charAt(contador) != ']') { |
||
392 | primem[1] += wkt.charAt(contador);
|
||
393 | contador ++; |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | contador++; |
||
398 | res = "";
|
||
399 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
400 | res += wkt.charAt(contador); |
||
401 | } |
||
402 | if(res.equals("UNIT[")) { |
||
403 | contador++; |
||
404 | for(; wkt.charAt(contador) != ']'; contador++) { |
||
405 | while(wkt.charAt(contador) != '"' ){ |
||
406 | unit[0] += wkt.charAt(contador);
|
||
407 | contador ++; |
||
408 | } |
||
409 | contador +=2;
|
||
410 | while(wkt.charAt(contador) != ']') { |
||
411 | unit[1] += wkt.charAt(contador);
|
||
412 | contador ++; |
||
413 | } |
||
414 | } |
||
415 | } |
||
416 | contador++; |
||
417 | res = "";
|
||
418 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
419 | res += wkt.charAt(contador); |
||
420 | } |
||
421 | if(res.equals(",PROJECTION[")) { |
||
422 | contador++; |
||
423 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
424 | projection += wkt.charAt(contador); |
||
425 | } |
||
426 | } |
||
427 | contador = contador+2;
|
||
428 | res = "";
|
||
429 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
430 | res += wkt.charAt(contador); |
||
431 | } |
||
432 | //Hallamos el numero de parametros que tiene la cadena wkt
|
||
433 | int i = 0; |
||
434 | 436 | fdiaz | |
435 | 147 | jjdelcerro | int copiacontador = contador;
|
436 | if(res.equals(",PARAMETER[")) { |
||
437 | do{
|
||
438 | for(; wkt.charAt(copiacontador) != ']'; copiacontador++) { |
||
439 | while(wkt.charAt(copiacontador) != '"' ) |
||
440 | copiacontador++; |
||
441 | copiacontador += 2;
|
||
442 | while(wkt.charAt(copiacontador) != ']' ) |
||
443 | copiacontador++; |
||
444 | copiacontador--; |
||
445 | } |
||
446 | i++; |
||
447 | copiacontador++; |
||
448 | res = "";
|
||
449 | for(; wkt.charAt(copiacontador) != '"'; copiacontador++) { |
||
450 | res += wkt.charAt(copiacontador); |
||
451 | } |
||
452 | } while (res.equals(",PARAMETER[")); |
||
453 | res = ",PARAMETER[";
|
||
454 | } |
||
455 | // Inicializamos los parametros
|
||
456 | param_name = new String[i]; |
||
457 | param_value = new String[i]; |
||
458 | for(int j = 0 ;j < i; j++ ){ |
||
459 | param_name[j] = "";
|
||
460 | param_value[j] = "";
|
||
461 | } |
||
462 | i = 0;
|
||
463 | if(res.equals(",PARAMETER[")) { |
||
464 | do{
|
||
465 | contador++; |
||
466 | for(; wkt.charAt(contador) != ']'; contador++) { |
||
467 | while(wkt.charAt(contador) != '"' ){ |
||
468 | param_name[i] += wkt.charAt(contador); |
||
469 | contador++; |
||
470 | } |
||
471 | contador += 2;
|
||
472 | while(wkt.charAt(contador) != ']' ){ |
||
473 | param_value[i] += wkt.charAt(contador); |
||
474 | contador++; |
||
475 | } |
||
476 | contador--; |
||
477 | } |
||
478 | i++; |
||
479 | contador++; |
||
480 | res = "";
|
||
481 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
482 | res += wkt.charAt(contador); |
||
483 | } |
||
484 | 436 | fdiaz | } while (res.equals(",PARAMETER[")); |
485 | 147 | jjdelcerro | } |
486 | 436 | fdiaz | |
487 | 147 | jjdelcerro | if (res.equals(",UNIT[")){ |
488 | contador++; |
||
489 | for(; wkt.charAt(contador) != ','; contador++) { |
||
490 | while(wkt.charAt(contador) != '"' ){ |
||
491 | unit_p[0] += wkt.charAt(contador);
|
||
492 | contador ++; |
||
493 | } |
||
494 | contador +=2;
|
||
495 | while(wkt.charAt(contador) != ']') { |
||
496 | unit_p[1] += wkt.charAt(contador);
|
||
497 | contador ++; |
||
498 | } |
||
499 | } |
||
500 | } |
||
501 | contador++; |
||
502 | res = "";
|
||
503 | for(; wkt.charAt(contador) != '"'; contador++) { |
||
504 | res += wkt.charAt(contador); |
||
505 | } |
||
506 | if(res.equals("AUTHORITY[")) { |
||
507 | contador++; |
||
508 | for(; wkt.charAt(contador) != ']'; contador++) { |
||
509 | while(wkt.charAt(contador) != '"' ){ |
||
510 | authority[0] += wkt.charAt(contador);
|
||
511 | contador ++; |
||
512 | } |
||
513 | contador +=2;
|
||
514 | while(wkt.charAt(contador) != ']') { |
||
515 | authority[1] += wkt.charAt(contador);
|
||
516 | contador ++; |
||
517 | } |
||
518 | } |
||
519 | } |
||
520 | } |
||
521 | else if (res.equals("GEOCCS[")){ |
||
522 | /*
|
||
523 | * parte necesaria para capturar la cadena geocentrica...
|
||
524 | */
|
||
525 | 436 | fdiaz | |
526 | 147 | jjdelcerro | } |
527 | }else
|
||
528 | geogcs = wkt; |
||
529 | } |
||
530 | 436 | fdiaz | |
531 | 147 | jjdelcerro | public String getProjection() { |
532 | return projection;
|
||
533 | } |
||
534 | 436 | fdiaz | |
535 | 147 | jjdelcerro | public String getProjcs() { |
536 | return projcs;
|
||
537 | } |
||
538 | 436 | fdiaz | |
539 | 147 | jjdelcerro | public String getGeogcs() { |
540 | return geogcs;
|
||
541 | } |
||
542 | 436 | fdiaz | |
543 | 147 | jjdelcerro | public String getDatumName() { |
544 | return datum;
|
||
545 | } |
||
546 | 436 | fdiaz | |
547 | 147 | jjdelcerro | public String[] getSpheroid() { |
548 | return spheroid;
|
||
549 | } |
||
550 | 436 | fdiaz | |
551 | 147 | jjdelcerro | public String[] getPrimen() { |
552 | return primem;
|
||
553 | } |
||
554 | 436 | fdiaz | |
555 | 147 | jjdelcerro | public String getName() { |
556 | 436 | fdiaz | if(projcs == "") |
557 | 147 | jjdelcerro | return geogcs;
|
558 | return projcs;
|
||
559 | } |
||
560 | 436 | fdiaz | |
561 | 147 | jjdelcerro | public String[] getUnit() { |
562 | return unit;
|
||
563 | } |
||
564 | 436 | fdiaz | |
565 | 147 | jjdelcerro | public String[] getUnit_p() { |
566 | return unit_p;
|
||
567 | } |
||
568 | 436 | fdiaz | |
569 | 147 | jjdelcerro | public String[] getParam_name() { |
570 | return param_name;
|
||
571 | } |
||
572 | 436 | fdiaz | |
573 | 147 | jjdelcerro | public String[] getParam_value() { |
574 | return param_value;
|
||
575 | } |
||
576 | 436 | fdiaz | |
577 | 147 | jjdelcerro | public String[] getAuthority(){ |
578 | return authority;
|
||
579 | } |
||
580 | 436 | fdiaz | |
581 | 147 | jjdelcerro | /*
|
582 | * Parser a medio hacer, orientado a objeto y recursivo.
|
||
583 | * by LWS.
|
||
584 | */
|
||
585 | 436 | fdiaz | |
586 | 147 | jjdelcerro | public static class WKT { |
587 | public class Param { |
||
588 | String key;
|
||
589 | ArrayList values = new ArrayList(); |
||
590 | ArrayList params = new ArrayList(); |
||
591 | public int pos = 0; |
||
592 | public Param(String key) { |
||
593 | this.key = key;
|
||
594 | } |
||
595 | public void addValue(String name) { |
||
596 | values.add(name); |
||
597 | } |
||
598 | public void addParam(Param p) { |
||
599 | params.add(p); |
||
600 | } |
||
601 | } |
||
602 | String data;
|
||
603 | public WKT(String data) { |
||
604 | this.data = data;
|
||
605 | WKT.Param param = parseParam(0);
|
||
606 | } |
||
607 | private WKT.Param parseParam(int pos) { |
||
608 | WKT.Param param = null;
|
||
609 | String key, name;
|
||
610 | 436 | fdiaz | |
611 | 147 | jjdelcerro | int l = data.length();
|
612 | for (int i = pos; i<l;) { |
||
613 | int nextParam = data.indexOf(",", i); |
||
614 | if (nextParam == i) {
|
||
615 | nextParam = data.indexOf(",", ++i);
|
||
616 | } |
||
617 | int cierra = data.indexOf("]", i); |
||
618 | int abre = data.indexOf("[", i); |
||
619 | if (cierra < abre) { // Esta mal |
||
620 | pinta(" =>");
|
||
621 | param.pos = cierra; |
||
622 | return param;
|
||
623 | } |
||
624 | if (param == null) { |
||
625 | if (abre > 0) { //hay claves |
||
626 | key = data.substring(i, data.indexOf("[", i));
|
||
627 | pinta(key+ " <= ");
|
||
628 | i = abre+1;
|
||
629 | param = new WKT.Param(key);
|
||
630 | } |
||
631 | } else {
|
||
632 | if (data.substring(i).startsWith("\"")) { |
||
633 | name = data.substring(i+1, data.indexOf("\"", i+1)); |
||
634 | i+=name.length()+2;
|
||
635 | pinta("|"+name+"|,"); |
||
636 | param.addValue(name); |
||
637 | } else if (nextParam<abre) { |
||
638 | name = data.substring(i, data.indexOf(",", i));
|
||
639 | i+=name.length(); |
||
640 | pinta(name+",");
|
||
641 | param.addValue(name); |
||
642 | } else {
|
||
643 | Param p = parseParam(i); |
||
644 | i = p.pos+1;
|
||
645 | } |
||
646 | } |
||
647 | } |
||
648 | return param;
|
||
649 | } |
||
650 | static int cnt=0; |
||
651 | public static void pinta(String str) { |
||
652 | cnt++; |
||
653 | if (cnt>60) |
||
654 | System.exit(1); |
||
655 | System.out.println(str);
|
||
656 | } |
||
657 | } |
||
658 | 436 | fdiaz | |
659 | 147 | jjdelcerro | private void fromCode(String code) { |
660 | CoordinateReferenceSystem source; |
||
661 | setAuthority(code); |
||
662 | try {
|
||
663 | source = CRS.decode(code); |
||
664 | setWkt(source.toWKT()); |
||
665 | 436 | fdiaz | |
666 | 147 | jjdelcerro | if (source instanceof DefaultProjectedCRS) { |
667 | DefaultProjectedCRS sour = (DefaultProjectedCRS) source; |
||
668 | setProjcs(sour.getName().toString().split(":")[1]); |
||
669 | String[] val = sour.getBaseCRS().getName().toString().split(":"); |
||
670 | if (val.length<2) |
||
671 | setGeogcs(sour.getBaseCRS().getName().toString().split(":")[0]); |
||
672 | else
|
||
673 | setGeogcs(sour.getBaseCRS().getName().toString().split(":")[1]); |
||
674 | DefaultGeodeticDatum d = (DefaultGeodeticDatum) sour.getDatum(); |
||
675 | val = d.getName().toString().split(":");
|
||
676 | if (val.length<2) |
||
677 | setDatumName(d.getName().toString().split(":")[0]); |
||
678 | else
|
||
679 | setDatumName(d.getName().toString().split(":")[1]); |
||
680 | 436 | fdiaz | setSpheroid((DefaultEllipsoid)d.getEllipsoid()); |
681 | 147 | jjdelcerro | setPrimen(d.getPrimeMeridian()); |
682 | val = sour.getConversionFromBase().getMethod().getName().toString().split(":");
|
||
683 | if (val.length<2) |
||
684 | setProjection(sour.getConversionFromBase().getMethod().getName().toString().split(":")[0]); |
||
685 | else
|
||
686 | setProjection(sour.getConversionFromBase().getMethod().getName().toString().split(":")[1]); |
||
687 | String str;
|
||
688 | param_name = new String[sour.getConversionFromBase().getParameterValues().values().size()]; |
||
689 | param_value= new String[sour.getConversionFromBase().getParameterValues().values().size()]; |
||
690 | for (int i=0; i< sour.getConversionFromBase().getParameterValues().values().size();i++) { |
||
691 | str = sour.getConversionFromBase().getParameterValues().values().get(i).toString(); |
||
692 | Unit u = sour.getConversionFromBase().getParameterValues().parameter(str.split("=")[0]).getUnit(); |
||
693 | double value = sour.getConversionFromBase().getParameterValues().parameter(str.split("=")[0]).doubleValue(); |
||
694 | value = convert(value, u.toString()); |
||
695 | param_name[i] = str.split("=")[0]; |
||
696 | param_value [i] = String.valueOf(value);
|
||
697 | } |
||
698 | 436 | fdiaz | |
699 | 147 | jjdelcerro | } |
700 | 436 | fdiaz | |
701 | 147 | jjdelcerro | if (source instanceof DefaultGeographicCRS) { |
702 | DefaultGeographicCRS sour = (DefaultGeographicCRS) source; |
||
703 | String[] val = sour.getName().toString().split(":"); |
||
704 | if (val.length<2) |
||
705 | setGeogcs(sour.getName().toString().split(":")[0]); |
||
706 | else
|
||
707 | setGeogcs(sour.getName().toString().split(":")[1]); |
||
708 | 436 | fdiaz | |
709 | 147 | jjdelcerro | DefaultGeodeticDatum d = (DefaultGeodeticDatum) sour.getDatum(); |
710 | val = d.getName().toString().split(":");
|
||
711 | if (val.length<2) |
||
712 | setDatumName(d.getName().toString().split(":")[0]); |
||
713 | else
|
||
714 | setDatumName(d.getName().toString().split(":")[1]); |
||
715 | 436 | fdiaz | setSpheroid((DefaultEllipsoid)d.getEllipsoid()); |
716 | 147 | jjdelcerro | setPrimen(d.getPrimeMeridian()); |
717 | 436 | fdiaz | |
718 | 147 | jjdelcerro | } |
719 | } catch (NoSuchAuthorityCodeException e) { //Pertenece a CRS |
||
720 | logger.error("Can't create CRS from code '"+code+"'.",e); |
||
721 | } catch (FactoryException e) { //Pertenece a CRS |
||
722 | logger.error("Can't create CRS from code '"+code+"'.",e); |
||
723 | 436 | fdiaz | } |
724 | |||
725 | 147 | jjdelcerro | } |
726 | 436 | fdiaz | |
727 | 147 | jjdelcerro | private void parseWKT(String data) { |
728 | WKT wkt = new WKT(data);
|
||
729 | } |
||
730 | 436 | fdiaz | |
731 | 147 | jjdelcerro | public void setProjcs (String proj) { |
732 | this.projcs = proj;
|
||
733 | } |
||
734 | 436 | fdiaz | |
735 | 147 | jjdelcerro | public void setProjection (String projection) { |
736 | this.projection = projection;
|
||
737 | } |
||
738 | public void setGeogcs (String geo) { |
||
739 | this.geogcs = geo;
|
||
740 | } |
||
741 | 436 | fdiaz | |
742 | 147 | jjdelcerro | public void setDatumName (String dat) { |
743 | this.datum = dat;
|
||
744 | } |
||
745 | 436 | fdiaz | |
746 | 147 | jjdelcerro | public void setSpheroid (DefaultEllipsoid ellips) { |
747 | Unit u = ellips.getAxisUnit(); |
||
748 | double semi_major = convert( ellips.getSemiMajorAxis(), u.toString());
|
||
749 | double inv_f = convert( ellips.getInverseFlattening(), u.toString());
|
||
750 | String[] val = ellips.getName().toString().split(":"); |
||
751 | if (val.length<2) |
||
752 | this.spheroid[0] = ellips.getName().toString().split(":")[0]; |
||
753 | else
|
||
754 | this.spheroid[0] = ellips.getName().toString().split(":")[1]; |
||
755 | this.spheroid[1] = String.valueOf(semi_major); |
||
756 | this.spheroid[2] = String.valueOf(inv_f); |
||
757 | } |
||
758 | 436 | fdiaz | |
759 | 147 | jjdelcerro | public void setPrimen (PrimeMeridian prim) { |
760 | DefaultPrimeMeridian pm = (DefaultPrimeMeridian)(prim); |
||
761 | Unit u = pm.getAngularUnit(); |
||
762 | double value = convert( pm.getGreenwichLongitude(), u.toString());
|
||
763 | String[] val = pm.getName().toString().split(":"); |
||
764 | if (val.length<2) |
||
765 | this.primem[0] = pm.getName().toString().split(":")[0]; |
||
766 | else
|
||
767 | this.primem[0] = pm.getName().toString().split(":")[1]; |
||
768 | this.primem[1] = String.valueOf(value); |
||
769 | } |
||
770 | 436 | fdiaz | |
771 | 147 | jjdelcerro | public void setAuthority (String aut) { |
772 | 436 | fdiaz | this.authority = aut.split(":"); |
773 | 147 | jjdelcerro | } |
774 | 436 | fdiaz | |
775 | 147 | jjdelcerro | public void setWkt(String wkt){ |
776 | this.wkt = wkt;
|
||
777 | } |
||
778 | 436 | fdiaz | |
779 | 147 | jjdelcerro | public String getWkt() { |
780 | return wkt;
|
||
781 | } |
||
782 | 436 | fdiaz | |
783 | 147 | jjdelcerro | public double convert(double value, String measure) throws ConversionException { |
784 | 436 | fdiaz | if (measure.equals("D.MS")) { |
785 | 147 | jjdelcerro | value *= this.divider;
|
786 | int deg,min;
|
||
787 | deg = (int) (value/10000); value -= 10000*deg; |
||
788 | min = (int) (value/ 100); value -= 100*min; |
||
789 | if (min<=-60 || min>=60) { // Accepts NaN |
||
790 | if (Math.abs(Math.abs(min) - 100) <= EPS) { |
||
791 | if (min >= 0) deg++; else deg--; |
||
792 | min = 0;
|
||
793 | } else {
|
||
794 | throw new ConversionException("Invalid minutes: "+min); |
||
795 | } |
||
796 | } |
||
797 | if (value<=-60 || value>=60) { // Accepts NaN |
||
798 | if (Math.abs(Math.abs(value) - 100) <= EPS) { |
||
799 | if (value >= 0) min++; else min--; |
||
800 | value = 0;
|
||
801 | } else {
|
||
802 | throw new ConversionException("Invalid secondes: "+value); |
||
803 | } |
||
804 | } |
||
805 | value = ((value/60) + min)/60 + deg; |
||
806 | return value;
|
||
807 | } |
||
808 | 436 | fdiaz | if (measure.equals("grad") || measure.equals("grade")) |
809 | return ((value * 180.0) / 200.0); |
||
810 | if (measure.equals(""+(char)176)) |
||
811 | 147 | jjdelcerro | return value;
|
812 | 436 | fdiaz | if (measure.equals("DMS") ) |
813 | return value;
|
||
814 | if (measure.equals("m")) |
||
815 | return value;
|
||
816 | 147 | jjdelcerro | //TODO revisar este if, y ver si seria correcto...
|
817 | if (measure.startsWith("[m*")) { |
||
818 | return value*Double.parseDouble(measure.substring(3,measure.length()-1)); |
||
819 | } |
||
820 | 436 | fdiaz | if (measure.equals("")) |
821 | 147 | jjdelcerro | return value;
|
822 | 436 | fdiaz | if (measure.equalsIgnoreCase("ft")||measure.equalsIgnoreCase("foot")||measure.equalsIgnoreCase("feet")) |
823 | 147 | jjdelcerro | return (value*0.3048/1.0); |
824 | 436 | fdiaz | |
825 | 147 | jjdelcerro | throw new ConversionException("Conversion no contemplada: "+measure); |
826 | } |
||
827 | } |