svn-gvsig-desktop / tags / Root_CqCMSGisPlanet / libraries / libCq CMS for java.old / src / org / cresques / geo / cover / Coverage.java @ 1933
History | View | Annotate | Download (13.7 KB)
1 |
/*
|
---|---|
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 |
import java.util.ArrayList; |
9 |
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 |
import org.cresques.cts.ICoordTrans; |
20 |
import org.cresques.cts.IProjection; |
21 |
import org.cresques.cts.ProjectionPool; |
22 |
import org.cresques.cts.gt2.CoordSys; |
23 |
import org.cresques.cts.gt2.CoordTrans; |
24 |
import org.cresques.geo.Projected; |
25 |
import org.cresques.geo.Projection; |
26 |
import org.cresques.geo.ReProjection; |
27 |
import org.cresques.io.GeoRasterFile; |
28 |
|
29 |
/**
|
30 |
* Cobertura espacial
|
31 |
*
|
32 |
* @author "Luis W. Sevilla" <sevilla_lui@gva.es>
|
33 |
*/
|
34 |
|
35 |
public class Coverage implements Projected { |
36 |
boolean debug = true; |
37 |
/* protected FileSupport [] fSupport = {
|
38 |
new FileSupport("ecw", EcwFile.class)
|
39 |
};*/
|
40 |
// Nombre de la cobertura
|
41 |
protected String name = "Orto ICV 1:5.000"; |
42 |
protected String fName = "cover"; |
43 |
// 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 |
private Hashtable files = null; |
49 |
|
50 |
protected PxObjList marcos;
|
51 |
|
52 |
public Color color = null; |
53 |
public Color fillColor = null; |
54 |
protected boolean loadable = true; |
55 |
|
56 |
private String fType = ""; |
57 |
|
58 |
protected IProjection proj = null; |
59 |
|
60 |
private boolean available = true; |
61 |
|
62 |
public Coverage() {
|
63 |
hojas = new Hashtable(); |
64 |
files = new Hashtable(); |
65 |
} |
66 |
|
67 |
public String getName() { return name; } |
68 |
public Hashtable getHojas() { return hojas; } |
69 |
|
70 |
public IProjection getProjection() { return proj; } |
71 |
public void setProjection(IProjection p) { proj = p; } |
72 |
public void reProject(ICoordTrans rp) { |
73 |
// TODO metodo reProject pendiente de implementar
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* 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 |
*/
|
83 |
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 |
|
90 |
/**
|
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 |
public void catalogue(String [] files) { |
105 |
String hName = null; |
106 |
for (int i=0; i<files.length; i++) { |
107 |
hName = hojaPrefix+files[i]; |
108 |
hojas.put(hName, files[i]); |
109 |
if (debug) System.out.println("Coverage: hojas.put('"+hName+"'"); |
110 |
} |
111 |
} |
112 |
|
113 |
public void catalogue() { |
114 |
catalogue(dName); |
115 |
} |
116 |
/**
|
117 |
* Hace el cat?logo a partir de la lista de ficheros.
|
118 |
* @param files
|
119 |
*/
|
120 |
public void catalogueList(String [][] files) { |
121 |
IProjection prj = proj; |
122 |
String hName = null; |
123 |
for (int i=0; i<files.length; i++) { |
124 |
if (files[i][2] != null) |
125 |
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 |
} |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Hace el cat?logo recorriendo un directorio.
|
137 |
* @param dName
|
138 |
*/
|
139 |
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 |
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 |
/**
|
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 |
IObjList [] minuteo = loadMinuteo();
|
170 |
if (minuteo != null) return minuteo; |
171 |
minuteo = new IObjList[nLevels];
|
172 |
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 |
try {
|
187 |
obj = hojas.get(hName); |
188 |
if (obj instanceof String) { |
189 |
hCode = (String) obj;
|
190 |
generateMinuteoHoja(dName+"/"+hCode, hName, layer, layer1);
|
191 |
} else if (obj instanceof Hoja) { |
192 |
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 |
e.printStackTrace(); |
199 |
} |
200 |
} |
201 |
saveMinuteo(minuteo); |
202 |
//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 |
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 |
ICoordTrans rp = null;
|
248 |
if (proj instanceof Projection) |
249 |
rp = new ReProjection(((Projection) proj), ((Projection)app.getCurrentProjection()));
|
250 |
layer.reProject(rp); |
251 |
if (layer1 != null) layer1.reProject(rp); |
252 |
} |
253 |
} |
254 |
|
255 |
public void generateMinuteoHoja(String hFName, Hoja h, IObjList layer, IObjList layer1) { |
256 |
GeoRasterFile eFile = null;
|
257 |
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 |
if (!GeoRasterFile.fileIsSupported(hFName) ) return; |
267 |
fName = hFName; |
268 |
System.out.println("Analizo '"+h.getName()+"' ..."); |
269 |
eFile = GeoRasterFile.openFile(null, fName);
|
270 |
contour = new PxContour(eFile.getExtent(), fName, h.getName(), h.getProjection());
|
271 |
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 |
eFile.close(); |
280 |
contour.c(color); |
281 |
contour.fillColor(fillColor); |
282 |
layer.add(contour); |
283 |
} |
284 |
} |
285 |
|
286 |
public void generateMinuteoHoja(String hName, String name, IObjList layer, IObjList layer1) { |
287 |
GeoRasterFile eFile = null;
|
288 |
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 |
return GeoRasterFile.fileIsSupported(fName);
|
299 |
} |
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 |
eFile = GeoRasterFile.openFile(null, fName);
|
310 |
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 |
if (!GeoRasterFile.fileIsSupported(hName) ) return; |
327 |
fName = hName; |
328 |
System.out.println("Analizo '"+name+"' ..."); |
329 |
//contour = EcwFile.getContour(fName, name, proj);
|
330 |
eFile = GeoRasterFile.openFile(null, fName);
|
331 |
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 |
/**
|
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 |
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 |
return GeoRasterFile.fileIsSupported(fName);
|
453 |
} |
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 |
app.loadFile(hDName+"/"+fName, proj);
|
464 |
//mapview.canvas.zoomExtents();
|
465 |
app.getCanvas().repaint(); |
466 |
} |
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 |
if (!GeoRasterFile.fileIsSupported(fName)) return; |
476 |
System.out.println("Cargo ...'"+fName+"'"); |
477 |
app.loadFile(fName, proj); |
478 |
//mapview.canvas.zoomExtents();
|
479 |
app.getCanvas().repaint(); |
480 |
} |
481 |
} |
482 |
} |