svn-gvsig-desktop / tags / Root_CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / geo / cover / Coverage.java @ 1732
History | View | Annotate | Download (13.7 KB)
1 | 2 | luisw | /*
|
---|---|---|---|
2 | * Created on 25-abr-2004
|
||
3 | *
|
||
4 | * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
||
5 | */
|
||
6 | package org.cresques.geo.cover; |
||
7 | |||
8 | 210 | luisw | import java.util.ArrayList; |
9 | 2 | luisw | import java.util.Hashtable; |
10 | import java.util.Enumeration; |
||
11 | |||
12 | import java.io.File; |
||
13 | import java.io.FilenameFilter; |
||
14 | |||
15 | import java.awt.Color; |
||
16 | |||
17 | import org.cresques.px.*; |
||
18 | import org.cresques.ui.CQApp; |
||
19 | 94 | luisw | import org.cresques.cts.ICoordTrans; |
20 | import org.cresques.cts.IProjection; |
||
21 | 210 | luisw | import org.cresques.cts.ProjectionPool; |
22 | import org.cresques.cts.gt2.CoordSys; |
||
23 | import org.cresques.cts.gt2.CoordTrans; |
||
24 | 2 | luisw | import org.cresques.geo.Projected; |
25 | import org.cresques.geo.Projection; |
||
26 | import org.cresques.geo.ReProjection; |
||
27 | 210 | luisw | import org.cresques.io.GeoRasterFile; |
28 | 2 | luisw | |
29 | /**
|
||
30 | * Cobertura espacial
|
||
31 | *
|
||
32 | * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
||
33 | */
|
||
34 | |||
35 | public class Coverage implements Projected { |
||
36 | 210 | luisw | boolean debug = true; |
37 | /* protected FileSupport [] fSupport = {
|
||
38 | 2 | luisw | new FileSupport("ecw", EcwFile.class)
|
39 | 210 | luisw | };*/
|
40 | 2 | luisw | // Nombre de la cobertura
|
41 | protected String name = "Orto ICV 1:5.000"; |
||
42 | 1731 | luisw | protected String fName = "cover"; |
43 | 2 | luisw | // Direcci?n raiz de los datos
|
44 | protected String dName = null; //"//sercart/cartografia/ortofotos/Orto_ICV/00009"; |
||
45 | protected int nLevels = 1; |
||
46 | protected String hojaPrefix = ""; |
||
47 | protected Hashtable hojas = null; |
||
48 | 210 | luisw | private Hashtable files = null; |
49 | |||
50 | 2 | luisw | protected PxObjList marcos;
|
51 | |||
52 | 210 | luisw | public Color color = null; |
53 | public Color fillColor = null; |
||
54 | 2 | luisw | protected boolean loadable = true; |
55 | |||
56 | 210 | luisw | private String fType = ""; |
57 | |||
58 | 94 | luisw | protected IProjection proj = null; |
59 | 2 | luisw | |
60 | 210 | luisw | private boolean available = true; |
61 | |||
62 | 2 | luisw | public Coverage() {
|
63 | hojas = new Hashtable(); |
||
64 | 210 | luisw | files = new Hashtable(); |
65 | 2 | luisw | } |
66 | |||
67 | public String getName() { return name; } |
||
68 | public Hashtable getHojas() { return hojas; } |
||
69 | |||
70 | 94 | luisw | public IProjection getProjection() { return proj; } |
71 | public void setProjection(IProjection p) { proj = p; } |
||
72 | public void reProject(ICoordTrans rp) { |
||
73 | 2 | luisw | // TODO metodo reProject pendiente de implementar
|
74 | } |
||
75 | |||
76 | /**
|
||
77 | 210 | luisw | * Asigna el estado de disponibilidad.
|
78 | * Cuando una cobertura no esta disponible (la unidad est?
|
||
79 | * inactiva, no tiene ficheros, etc...) deber? ponerse a false.
|
||
80 | * Es true por defecto
|
||
81 | * @param av Disponibilidad.
|
||
82 | 203 | jmorell | */
|
83 | 210 | luisw | public void setAvailable(boolean av) { available = av; } |
84 | /**
|
||
85 | * Devuelve el estado de disponibilidad.
|
||
86 | * @return Disponibilidad.
|
||
87 | */
|
||
88 | public boolean getAvailable() { return available; } |
||
89 | 2 | luisw | |
90 | 210 | luisw | /**
|
91 | * Especifica el tipo de los ficheros que forman la cobertura.
|
||
92 | * Es un string que ser? procesado por la aplicaci?n. La clase
|
||
93 | * Coverage no hace ning?n tratamiento de este valor.
|
||
94 | * @param fType Tipo de ficheros.
|
||
95 | */
|
||
96 | public void setFileType(String fType) { this.fType = fType; } |
||
97 | |||
98 | /**
|
||
99 | * Devuelve el tipo de fichero.
|
||
100 | * @return Tipo de fichero.
|
||
101 | */
|
||
102 | public String getFileType() { return fType; } |
||
103 | |||
104 | 2 | luisw | public void catalogue(String [] files) { |
105 | 210 | luisw | String hName = null; |
106 | 2 | luisw | for (int i=0; i<files.length; i++) { |
107 | 210 | luisw | hName = hojaPrefix+files[i]; |
108 | hojas.put(hName, files[i]); |
||
109 | if (debug) System.out.println("Coverage: hojas.put('"+hName+"'"); |
||
110 | 2 | luisw | } |
111 | } |
||
112 | |||
113 | public void catalogue() { |
||
114 | 13 | luisw | catalogue(dName); |
115 | 2 | luisw | } |
116 | 210 | luisw | /**
|
117 | * Hace el cat?logo a partir de la lista de ficheros.
|
||
118 | * @param files
|
||
119 | */
|
||
120 | 2 | luisw | public void catalogueList(String [][] files) { |
121 | 210 | luisw | IProjection prj = proj; |
122 | String hName = null; |
||
123 | 2 | luisw | for (int i=0; i<files.length; i++) { |
124 | if (files[i][2] != null) |
||
125 | 210 | luisw | if (proj instanceof Projection) |
126 | prj = (Projection) Projection.getProjectionByName(proj.getDatum(), files[i][2]);
|
||
127 | else if (proj instanceof CoordSys) |
||
128 | prj = ProjectionPool.get(files[i][2]);
|
||
129 | hName = files[i][0];
|
||
130 | hojas.put(hName, new Hoja(prj, files[i][1], files[i][0])); |
||
131 | if (debug) System.out.println("Coverage: hojas.put('"+hName+"'"); |
||
132 | 2 | luisw | } |
133 | } |
||
134 | 210 | luisw | |
135 | /**
|
||
136 | * Hace el cat?logo recorriendo un directorio.
|
||
137 | * @param dName
|
||
138 | */
|
||
139 | 2 | luisw | public void catalogue(String dName) { |
140 | System.out.println("Catalogando '"+dName+"' ..."); |
||
141 | File dir = new File(dName); |
||
142 | if (!dir.exists()) {
|
||
143 | System.err.println("Que dice que '"+dName+"'no existe"); |
||
144 | return;
|
||
145 | } |
||
146 | String[] files = dir.list(); |
||
147 | if (files != null) { |
||
148 | catalogue(files); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | 1731 | luisw | public IObjList [] loadMinuteo() { |
153 | IObjList [] minuteo = null; |
||
154 | return minuteo;
|
||
155 | } |
||
156 | |||
157 | public void saveMinuteo(IObjList [] minuteo) { |
||
158 | for (int i=0; i<minuteo.length; i++) { |
||
159 | |||
160 | } |
||
161 | } |
||
162 | 210 | luisw | /**
|
163 | * Genera el minuteo, reproyectado.
|
||
164 | * @param targetProj proyeccion de destino
|
||
165 | * @return array de minuteos
|
||
166 | */
|
||
167 | |||
168 | public IObjList[] generateMinuteo(IProjection targetProj) { |
||
169 | 1731 | luisw | IObjList [] minuteo = loadMinuteo();
|
170 | if (minuteo != null) return minuteo; |
||
171 | minuteo = new IObjList[nLevels];
|
||
172 | 210 | luisw | for (int i=0; i<minuteo.length; i++) |
173 | minuteo[i] = new PxObjList();
|
||
174 | if (hojas.size()== 0) |
||
175 | catalogue(this.dName);
|
||
176 | marcos = new PxObjList();
|
||
177 | IObjList layer = minuteo[0], layer1 = null; |
||
178 | if (nLevels > 1) layer1 = minuteo[1]; |
||
179 | |||
180 | Enumeration enum = hojas.keys(); |
||
181 | String hName, hCode;
|
||
182 | Hoja h; |
||
183 | Object obj = null; |
||
184 | while (enum.hasMoreElements()) { |
||
185 | hName = (String) enum.nextElement(); |
||
186 | 868 | luisw | try {
|
187 | obj = hojas.get(hName); |
||
188 | 1731 | luisw | if (obj instanceof String) { |
189 | 868 | luisw | hCode = (String) obj;
|
190 | generateMinuteoHoja(dName+"/"+hCode, hName, layer, layer1);
|
||
191 | 1731 | luisw | } else if (obj instanceof Hoja) { |
192 | 868 | luisw | h = (Hoja) obj; |
193 | generateMinuteoHoja(dName+"/"+h.getCode(), h, layer, layer1);
|
||
194 | } |
||
195 | } catch (Exception e) { |
||
196 | // TODO Tengo que tratar los errores de una forma mejor.
|
||
197 | System.err.println("Error al cargar la hoja "+hName); |
||
198 | 1731 | luisw | e.printStackTrace(); |
199 | 210 | luisw | } |
200 | } |
||
201 | 1731 | luisw | saveMinuteo(minuteo); |
202 | 210 | luisw | //app.setCurrentMinuteo(layer);
|
203 | if (proj != targetProj) {
|
||
204 | ICoordTrans rp = null;
|
||
205 | if (proj instanceof Projection) |
||
206 | rp = new ReProjection(((Projection) proj), ((Projection) targetProj));
|
||
207 | else if (proj instanceof CoordSys) |
||
208 | rp = new CoordTrans((CoordSys) proj, (CoordSys) targetProj);
|
||
209 | layer.reProject(rp); |
||
210 | if (layer1 != null) layer1.reProject(rp); |
||
211 | } |
||
212 | return minuteo;
|
||
213 | } |
||
214 | |||
215 | 2 | luisw | public void generateMinuteo(CQApp app, String lName, String lName1) { |
216 | PxLayer layer1 = null;
|
||
217 | if (color == null) color = new Color(64,220,64,255); |
||
218 | if (fillColor == null) fillColor = new Color(128,220,128,32); |
||
219 | if (hojas.size()== 0) |
||
220 | catalogue(this.dName);
|
||
221 | marcos = new PxObjList();
|
||
222 | if (nLevels>0) { |
||
223 | layer1 = app.createLayer(name+lName1, proj); |
||
224 | layer1.setColor(color); |
||
225 | layer1.setFillColor(fillColor); |
||
226 | } |
||
227 | PxLayer layer = app.createLayer(name+lName, proj); |
||
228 | layer.setColor(color); |
||
229 | layer.setFillColor(fillColor); |
||
230 | Enumeration enum = hojas.keys(); |
||
231 | String hName, hCode;
|
||
232 | Hoja h; |
||
233 | Object obj = null; |
||
234 | while (enum.hasMoreElements()) { |
||
235 | hName = (String) enum.nextElement(); |
||
236 | obj = hojas.get(hName); |
||
237 | if (obj.getClass() == String.class) { |
||
238 | hCode = (String) obj;
|
||
239 | generateMinuteoHoja(dName+"/"+hCode, hName, layer, layer1);
|
||
240 | } else if (obj.getClass() == Hoja.class) { |
||
241 | h = (Hoja) obj; |
||
242 | generateMinuteoHoja(dName+"/"+h.getCode(), h, layer, layer1);
|
||
243 | } |
||
244 | } |
||
245 | app.setCurrentMinuteo(layer); |
||
246 | if (proj != app.getCurrentProjection()) {
|
||
247 | 94 | luisw | ICoordTrans rp = null;
|
248 | if (proj instanceof Projection) |
||
249 | rp = new ReProjection(((Projection) proj), ((Projection)app.getCurrentProjection()));
|
||
250 | 2 | luisw | layer.reProject(rp); |
251 | if (layer1 != null) layer1.reProject(rp); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | 210 | luisw | public void generateMinuteoHoja(String hFName, Hoja h, IObjList layer, IObjList layer1) { |
256 | GeoRasterFile eFile = null;
|
||
257 | 2 | luisw | PxContour contour = null;
|
258 | String fName = null; |
||
259 | File dir = new File(hFName); |
||
260 | if (!hFName.startsWith("ecwp:") && !dir.exists()) { |
||
261 | System.err.println("Hoja '"+hFName+"' Inexistente"); |
||
262 | return;
|
||
263 | } |
||
264 | if (dir.isDirectory()) {
|
||
265 | } else {
|
||
266 | 210 | luisw | if (!GeoRasterFile.fileIsSupported(hFName) ) return; |
267 | 2 | luisw | fName = hFName; |
268 | System.out.println("Analizo '"+h.getName()+"' ..."); |
||
269 | 210 | luisw | eFile = GeoRasterFile.openFile(null, fName);
|
270 | 2 | luisw | contour = new PxContour(eFile.getExtent(), fName, h.getName(), h.getProjection());
|
271 | 210 | luisw | if (proj != h.getProjection()) {
|
272 | ICoordTrans rp = null;
|
||
273 | if (proj instanceof Projection) |
||
274 | rp = new ReProjection(((Projection) h.getProjection()), ((Projection) proj));
|
||
275 | else if (proj instanceof CoordSys) |
||
276 | rp = new CoordTrans((CoordSys) h.getProjection(), (CoordSys) proj);
|
||
277 | contour.reProject(rp); |
||
278 | } |
||
279 | 2 | luisw | eFile.close(); |
280 | contour.c(color); |
||
281 | contour.fillColor(fillColor); |
||
282 | layer.add(contour); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | 210 | luisw | public void generateMinuteoHoja(String hName, String name, IObjList layer, IObjList layer1) { |
287 | GeoRasterFile eFile = null;
|
||
288 | 2 | luisw | PxContour contour = null;
|
289 | String fName = null; |
||
290 | File dir = new File(hName); |
||
291 | if (!hName.startsWith("ecwp:") && !dir.exists()) { |
||
292 | System.err.println("Hoja '"+hName+"' Inexistente"); |
||
293 | return;
|
||
294 | } |
||
295 | if (dir.isDirectory()) {
|
||
296 | FilenameFilter filter = new FilenameFilter() { |
||
297 | public boolean accept(File dir, String fName) { |
||
298 | 210 | luisw | return GeoRasterFile.fileIsSupported(fName);
|
299 | 2 | luisw | } |
300 | }; |
||
301 | String [] files = dir.list(filter); |
||
302 | System.out.print(files.length+" ficheros."); |
||
303 | |||
304 | if (files.length > 0) { |
||
305 | Extent extent = new Extent();
|
||
306 | for (int i=0; i<files.length; i++) { |
||
307 | fName = hName+"/"+files[i];
|
||
308 | System.out.println("Analizo '"+fName+"' ..."); |
||
309 | 210 | luisw | eFile = GeoRasterFile.openFile(null, fName);
|
310 | 2 | luisw | contour = new PxContour(eFile.getExtent(), fName, files[i], proj);
|
311 | eFile.close(); |
||
312 | |||
313 | //contour = EcwFile.getContour(fName, files[i], proj);
|
||
314 | layer1.add(contour); |
||
315 | // mapview.loadECW(hName+"/"+fName);
|
||
316 | extent.add(contour.getExtent()); |
||
317 | } |
||
318 | if (contour != null) { |
||
319 | contour = new PxContour(extent, hName, name, proj);
|
||
320 | contour.c(color); |
||
321 | contour.fillColor(fillColor); |
||
322 | layer.add(contour); |
||
323 | } |
||
324 | } |
||
325 | } else {
|
||
326 | 210 | luisw | if (!GeoRasterFile.fileIsSupported(hName) ) return; |
327 | 2 | luisw | fName = hName; |
328 | System.out.println("Analizo '"+name+"' ..."); |
||
329 | //contour = EcwFile.getContour(fName, name, proj);
|
||
330 | 210 | luisw | eFile = GeoRasterFile.openFile(null, fName);
|
331 | 2 | luisw | contour = new PxContour(eFile.getExtent(), fName, name, proj);
|
332 | eFile.close(); |
||
333 | contour.c(color); |
||
334 | contour.fillColor(fillColor); |
||
335 | layer.add(contour); |
||
336 | // mapview.loadECW(hName+"/"+fName);
|
||
337 | } |
||
338 | } |
||
339 | |||
340 | 210 | luisw | /**
|
341 | * Devuelve los ficheros que incluye la hoja.
|
||
342 | * @param hoja hoja que se busca.
|
||
343 | * @return nombre(s) de fichero(s).
|
||
344 | */
|
||
345 | |||
346 | public ArrayList getFileNames(String hoja) { |
||
347 | ArrayList fNames = new ArrayList(); |
||
348 | |||
349 | if (!loadable) return null; |
||
350 | String fName = null; |
||
351 | Object obj = hojas.get(hoja);
|
||
352 | String hCode = null; |
||
353 | String hDName = null; |
||
354 | File dir = null; |
||
355 | if (obj != null) { |
||
356 | if (obj instanceof String) { |
||
357 | hCode = (String) obj;
|
||
358 | } else if (obj instanceof Hoja) { |
||
359 | hCode = ((Hoja) obj).getCode(); |
||
360 | } |
||
361 | hDName = dName+"/"+hCode;
|
||
362 | System.out.print("Explorando '"+hDName+"' ("+hoja+") ... " ); |
||
363 | dir = new File(hDName); |
||
364 | } |
||
365 | if (obj == null || |
||
366 | (!hDName.startsWith("ecwp:") && !dir.exists()) ) {
|
||
367 | System.err.println("Que dice que no existe"); |
||
368 | return null; |
||
369 | } |
||
370 | |||
371 | String[] files;/* = dir.list(); |
||
372 | /* if (children == null) {
|
||
373 | // Either dir does not exist or is not a directory
|
||
374 | } else {
|
||
375 | for (int i=0; i<children.length; i++) {
|
||
376 | // Get filename of file or directory
|
||
377 | String filename = children[i];
|
||
378 | }
|
||
379 | } */
|
||
380 | |||
381 | // It is also possible to filter the list of returned files.
|
||
382 | // This example does not return any files that start with `.'.
|
||
383 | if (dir.isDirectory()) {
|
||
384 | FilenameFilter filter = new FilenameFilter() { |
||
385 | public boolean accept(File dir, String fName) { |
||
386 | return GeoRasterFile.fileIsSupported(fName);
|
||
387 | } |
||
388 | }; |
||
389 | files = dir.list(filter); |
||
390 | System.out.print(files.length+" ficheros."); |
||
391 | |||
392 | if (files.length > 0) { |
||
393 | //PxLayer layer = app.createLayer(name+hoja, proj);
|
||
394 | for (int i=0; i<files.length; i++) { |
||
395 | fName = files[i]; |
||
396 | System.out.println("Cargo ...'"+fName+"'"); |
||
397 | fNames.add(hDName+"/"+fName);
|
||
398 | //mapview.canvas.zoomExtents();
|
||
399 | //app.getCanvas().repaint();
|
||
400 | } |
||
401 | } |
||
402 | } else {
|
||
403 | //PxLayer layer = app.getLayerByName(name);
|
||
404 | //if (layer == null) {
|
||
405 | // layer = app.createLayer(name, proj);
|
||
406 | //}
|
||
407 | //app.setCurrentLayer(layer);
|
||
408 | fName = hDName; |
||
409 | if (!GeoRasterFile.fileIsSupported(fName)) return null; |
||
410 | System.out.println("Cargo ...'"+fName+"'"); |
||
411 | fNames.add(fName); |
||
412 | //mapview.canvas.zoomExtents();
|
||
413 | //app.getCanvas().repaint();
|
||
414 | } |
||
415 | return fNames;
|
||
416 | } |
||
417 | |||
418 | 2 | luisw | public void loadHoja(CQApp app, String hoja) { |
419 | if (!loadable) return; |
||
420 | String fName = null; |
||
421 | Object obj = hojas.get(hoja);
|
||
422 | String hCode=null; |
||
423 | if (obj.getClass() == String.class) { |
||
424 | hCode = (String) obj;
|
||
425 | } else if (obj.getClass() == Hoja.class) { |
||
426 | hCode = ((Hoja) obj).getCode(); |
||
427 | } |
||
428 | String hDName = dName+"/"+hCode; |
||
429 | System.out.print("Explorando '"+hDName+"' ("+hoja+") ... " ); |
||
430 | File dir = new File(hDName); |
||
431 | if ((!hDName.startsWith("ecwp:") && !dir.exists()) || |
||
432 | hojas.get(hoja) == null) {
|
||
433 | System.err.println("Que dice que no existe"); |
||
434 | return;
|
||
435 | } |
||
436 | |||
437 | String[] files;/* = dir.list(); |
||
438 | /* if (children == null) {
|
||
439 | // Either dir does not exist or is not a directory
|
||
440 | } else {
|
||
441 | for (int i=0; i<children.length; i++) {
|
||
442 | // Get filename of file or directory
|
||
443 | String filename = children[i];
|
||
444 | }
|
||
445 | } */
|
||
446 | |||
447 | // It is also possible to filter the list of returned files.
|
||
448 | // This example does not return any files that start with `.'.
|
||
449 | if (dir.isDirectory()) {
|
||
450 | FilenameFilter filter = new FilenameFilter() { |
||
451 | public boolean accept(File dir, String fName) { |
||
452 | 210 | luisw | return GeoRasterFile.fileIsSupported(fName);
|
453 | 2 | luisw | } |
454 | }; |
||
455 | files = dir.list(filter); |
||
456 | System.out.print(files.length+" ficheros."); |
||
457 | |||
458 | if (files.length > 0) { |
||
459 | PxLayer layer = app.createLayer(name+hoja, proj); |
||
460 | for (int i=0; i<files.length; i++) { |
||
461 | fName = files[i]; |
||
462 | System.out.println("Cargo ...'"+fName+"'"); |
||
463 | 93 | luisw | app.loadFile(hDName+"/"+fName, proj);
|
464 | 2 | luisw | //mapview.canvas.zoomExtents();
|
465 | 92 | luisw | app.getCanvas().repaint(); |
466 | 2 | luisw | } |
467 | } |
||
468 | } else {
|
||
469 | PxLayer layer = app.getLayerByName(name); |
||
470 | if (layer == null) { |
||
471 | layer = app.createLayer(name, proj); |
||
472 | } |
||
473 | app.setCurrentLayer(layer); |
||
474 | fName = hDName; |
||
475 | 210 | luisw | if (!GeoRasterFile.fileIsSupported(fName)) return; |
476 | 2 | luisw | System.out.println("Cargo ...'"+fName+"'"); |
477 | 93 | luisw | app.loadFile(fName, proj); |
478 | 2 | luisw | //mapview.canvas.zoomExtents();
|
479 | 92 | luisw | app.getCanvas().repaint(); |
480 | 2 | luisw | } |
481 | } |
||
482 | } |