svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / util / WktUtils.java @ 12522
History | View | Annotate | Download (8.29 KB)
1 | 12494 | nacho | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2006 IVER T.I. 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 | package org.gvsig.raster.util; |
||
20 | |||
21 | /**
|
||
22 | * Clase que genera c?digos EPSG a partir de cadenas Wkt.
|
||
23 | * @author Miguel ?ngel Querol Carratal? <miguelangel.querol@iver.es>
|
||
24 | *
|
||
25 | */
|
||
26 | public class WktUtils { |
||
27 | |||
28 | private String wkt = null; |
||
29 | private String datum = null; |
||
30 | private String proj = null; |
||
31 | private String zone = null; |
||
32 | private String hemi = null; |
||
33 | |||
34 | |||
35 | public WktUtils(String wkt){ |
||
36 | super();
|
||
37 | this.wkt = wkt;
|
||
38 | parseWkt(); |
||
39 | normalize(); |
||
40 | } |
||
41 | |||
42 | |||
43 | /**
|
||
44 | * Devuerlve un string con c?digo EPSG correspondiente a la proyecci?n indicada por
|
||
45 | * la cadena Wkt pasada como par?metro.
|
||
46 | * @param wkt
|
||
47 | * @return
|
||
48 | */
|
||
49 | public String getEPSG(){ |
||
50 | String epsg = null; |
||
51 | |||
52 | if((datum.toLowerCase().equals("unknown")) || (proj.toLowerCase().equals("unknown"))) |
||
53 | if((zone.equals("unknown")) || (hemi.equals("unknown"))){ |
||
54 | epsg = "unknown";
|
||
55 | return epsg;
|
||
56 | } |
||
57 | |||
58 | if ((datum.equals("WGS 84")) || (datum.equals("WGS_1948")) || (datum.equals("WGS84"))){ |
||
59 | if (proj.equals("geo")){ |
||
60 | epsg = "4326";
|
||
61 | }else if (proj.equals("utm")){ |
||
62 | if ((hemi.equals("N"))) epsg = "326"; |
||
63 | if (hemi.equals("S")) epsg = "327"; |
||
64 | if ((Integer.valueOf(zone)).intValue() <= 9) |
||
65 | epsg = epsg + "0" + zone;
|
||
66 | else
|
||
67 | epsg = epsg + zone; |
||
68 | }else if (proj.equals("LCC Canada")){ |
||
69 | epsg = "42101";
|
||
70 | } |
||
71 | }else if(datum.equals("ED50")){ |
||
72 | if (proj.equals("geo")){ |
||
73 | epsg = "4230";
|
||
74 | }else if(proj.equals("utm")){ |
||
75 | epsg = "230";
|
||
76 | if ((Integer.valueOf(zone)).intValue() <= 9) |
||
77 | epsg = epsg + "0" + zone;
|
||
78 | else
|
||
79 | epsg = epsg + zone; |
||
80 | } |
||
81 | }else if((datum.equals("Datum 73")) || (datum.equals("datum 73")) || (datum.equals("datum_73"))){ |
||
82 | if (proj.equals("geo")){ |
||
83 | epsg = "4274";
|
||
84 | }else if (proj.equals("Modified Portuguese Grid")){ |
||
85 | epsg = "27492";
|
||
86 | } |
||
87 | }else if(datum.equals("NAD27")){ |
||
88 | if(proj.equals("geo")){ |
||
89 | epsg = "4267";
|
||
90 | }else if(proj.equals("utm")){ |
||
91 | epsg = "267";
|
||
92 | if ((Integer.valueOf(zone)).intValue() <= 9) |
||
93 | epsg = epsg + "0" + zone;
|
||
94 | else
|
||
95 | epsg = epsg + zone; |
||
96 | } |
||
97 | }else if (datum.equals("NAD83")){ |
||
98 | if(proj.equals("geo")){ |
||
99 | epsg = "4269";
|
||
100 | }else if(proj.equals("utm")){ |
||
101 | epsg = "269";
|
||
102 | if ((Integer.valueOf(zone)).intValue() <= 9) |
||
103 | epsg = epsg + "0" + zone;
|
||
104 | else
|
||
105 | epsg = epsg + zone; |
||
106 | }else if(proj.equals("LCC Canada")){ |
||
107 | epsg = "42304";
|
||
108 | } |
||
109 | }else if ((datum.equals("La canoa")) || datum.equals("la canoa") || datum.equals("La Canoa") || datum.equals("La_canoa")){ |
||
110 | if(proj.equals("geo")){ |
||
111 | epsg = "4247";
|
||
112 | }else if (proj.equals("utm")){ |
||
113 | epsg = "247";
|
||
114 | if (((Integer.valueOf(zone)).intValue() < 18) || ((Integer.valueOf(zone)).intValue() > 20)) |
||
115 | epsg = "unknown";
|
||
116 | else
|
||
117 | epsg = epsg + zone; |
||
118 | } |
||
119 | }else if ((datum.equals("NTF (Paris)")) || (datum.equals("NTF")) || (datum.equals("NTF(Paris)"))){ |
||
120 | if(proj.equals("geo")){ |
||
121 | epsg = "4807";
|
||
122 | }else if(proj.equals("France II")){ |
||
123 | epsg = "27582";
|
||
124 | } |
||
125 | }else if ((datum.equals("ETRS89")) || (datum.equals("ETRS 89"))){ |
||
126 | if(proj.equals("geo")){ |
||
127 | epsg = "4258";
|
||
128 | }else if(proj.equals("utm")){ |
||
129 | epsg = "258";
|
||
130 | if ((Integer.valueOf(zone)).intValue() <= 9) |
||
131 | epsg = epsg + "0" + zone;
|
||
132 | else
|
||
133 | epsg = epsg + zone; |
||
134 | } |
||
135 | }else if ((datum.equals("Campo Inchauspe")) || datum.equals("Campo_Inchauspe")){ |
||
136 | if(proj.equals("geo")){ |
||
137 | epsg = "4221";
|
||
138 | }else if(proj.equals("Argentina")){ |
||
139 | epsg = "2219" + zone;
|
||
140 | } |
||
141 | }else if ((datum.equals("RGF93")) || (datum.equals("RGF 93")) || datum.equals("RGF_93")){ |
||
142 | if(proj.equals("geo")){ |
||
143 | epsg = "4171";
|
||
144 | }else if(proj.equals("Lambert_93")){ |
||
145 | epsg = "2154";
|
||
146 | } |
||
147 | } |
||
148 | |||
149 | if (epsg == null) epsg = "unknown"; |
||
150 | |||
151 | return epsg;
|
||
152 | } |
||
153 | |||
154 | /**
|
||
155 | * Parsea una cadena WKT obteniendo de ella el datum, la proyecci?n y la zona de la imagen.
|
||
156 | * Est? por probar con diferentes cadenas WKT.
|
||
157 | */
|
||
158 | private void parseWkt(){ |
||
159 | String head = null; |
||
160 | String auxProj = null; |
||
161 | String auxDatum = null; |
||
162 | String auxZone = null; |
||
163 | |||
164 | int index = 0; |
||
165 | int lastIndex = 0; |
||
166 | |||
167 | index = wkt.indexOf("[");
|
||
168 | if (index != -1) |
||
169 | head = wkt.substring(0, index);
|
||
170 | |||
171 | |||
172 | if (head.equals("GEOGCS")){ |
||
173 | // En este caso sabemos que la proyeccion esta en geodesicas
|
||
174 | index = wkt.indexOf("\"");
|
||
175 | auxDatum = wkt.substring(index + 1, wkt.indexOf("\"", index + 1)); |
||
176 | datum = auxDatum; |
||
177 | proj = "geo";
|
||
178 | |||
179 | System.out.println("Parseado ----->>> " + datum +" " + proj); |
||
180 | }else if (head.equals("PROJCS")){ |
||
181 | // Obtenemos el datum de la proyecci?n.
|
||
182 | int geogcs = wkt.indexOf("GEOGCS[\""); |
||
183 | if (geogcs != -1) |
||
184 | geogcs = geogcs + 8;
|
||
185 | int geogcsEnd = wkt.indexOf("\"", geogcs); |
||
186 | if (geogcs != -1 && geogcsEnd != -1) |
||
187 | auxDatum = wkt.substring(geogcs, geogcsEnd); |
||
188 | |||
189 | index = wkt.indexOf("PROJCS[\"");
|
||
190 | if (index != -1) |
||
191 | index = index + 8;
|
||
192 | lastIndex = wkt.indexOf("\"", index);
|
||
193 | if (index != -1 && lastIndex != -1){ |
||
194 | auxZone = wkt.substring(index, lastIndex); |
||
195 | if ((index = auxZone.indexOf("UTM")) != -1){ |
||
196 | this.proj = "utm"; |
||
197 | if (((index = auxZone.indexOf("zone")) != -1) || ((index = auxZone.indexOf("Zone")) != -1 )){ |
||
198 | String aux = null; |
||
199 | index = index + 5;
|
||
200 | aux = auxZone.substring(index); |
||
201 | if ((aux.length() <= 4)){ |
||
202 | if ((index = aux.indexOf("N")) != -1){ |
||
203 | hemi = "N";
|
||
204 | zone = aux.substring(0, index);
|
||
205 | }else if ((index = aux.indexOf("S")) != -1){ |
||
206 | hemi = "S";
|
||
207 | zone = aux.substring(0, index);
|
||
208 | }else{
|
||
209 | zone = "unknown";
|
||
210 | hemi = "N";
|
||
211 | } |
||
212 | }else{
|
||
213 | if (((index = aux.indexOf("Northern")) != -1) || ((index = aux.indexOf("northern")) != -1)){ |
||
214 | hemi = "N";
|
||
215 | zone = aux.substring(0, index);
|
||
216 | }else if (((index = aux.indexOf("Southern")) != -1) || ((index = aux.indexOf("southern")) != -1)){ |
||
217 | hemi = "N";
|
||
218 | zone = aux.substring(0, index);
|
||
219 | }else{
|
||
220 | zone = "unknown";
|
||
221 | hemi = "N";
|
||
222 | } |
||
223 | } |
||
224 | }else{
|
||
225 | zone = "unknown";
|
||
226 | hemi = "unknown";
|
||
227 | } |
||
228 | }else if((index = auxZone.indexOf("LCC Canada")) != -1 ){ |
||
229 | this.proj = "LCC Canada"; |
||
230 | }else if((index = auxZone.indexOf("Lambert-93")) != -1 ){ |
||
231 | this.proj = "Lambert_93"; |
||
232 | }else if ((index = auxZone.indexOf("Argentina")) != -1){ |
||
233 | this.proj = "Argentina"; |
||
234 | index = index +10;
|
||
235 | zone = auxZone.substring(index); |
||
236 | }else if ((index = auxZone.indexOf("Modified Portuguese Grid")) != -1 ){ |
||
237 | this.proj = "Modified Portuguese Grid"; |
||
238 | }else if ((index = auxZone.indexOf("NRCan LCC Canada")) != -1 ){ |
||
239 | this.proj = "NRCan LCC Canada"; |
||
240 | }else if ((index = auxZone.indexOf("France II")) != -1 ){ |
||
241 | this.proj = "France II"; |
||
242 | }else {
|
||
243 | this.proj = "Unknown"; |
||
244 | } |
||
245 | |||
246 | datum = auxDatum; |
||
247 | |||
248 | //Normalizamos la zona
|
||
249 | if ((zone != null) && (!zone.equals("unknown"))){ |
||
250 | if(zone.indexOf(" ") != -1) |
||
251 | zone = zone.replaceAll( " ", ""); |
||
252 | if(zone.indexOf(",") != -1) |
||
253 | zone = zone.replaceAll(",", ""); |
||
254 | //Comprobamos que la zona sea un valor numerico, si no lo es, lo dejamos en unknown
|
||
255 | try{
|
||
256 | Integer.valueOf(zone);
|
||
257 | }catch (NumberFormatException e){ |
||
258 | zone = "unknown";
|
||
259 | } |
||
260 | } |
||
261 | |||
262 | |||
263 | |||
264 | }else{
|
||
265 | datum = "Unknown";
|
||
266 | } |
||
267 | |||
268 | } |
||
269 | |||
270 | //TODO: Comprobar todas las salidas que puede dar este m?todo (nulls, unknowns...)
|
||
271 | } |
||
272 | |||
273 | private void normalize(){ |
||
274 | if (zone == null) zone = "unknown"; |
||
275 | if (hemi == null) hemi = "unknown"; |
||
276 | if (proj == null) proj = "unknown"; |
||
277 | if (datum == null) datum = "unknown"; |
||
278 | } |
||
279 | |||
280 | public String getDatum(){ |
||
281 | return this.datum; |
||
282 | } |
||
283 | |||
284 | public String getZone(){ |
||
285 | return this.zone; |
||
286 | } |
||
287 | |||
288 | public String getProj(){ |
||
289 | return this.proj; |
||
290 | } |
||
291 | } |