Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_895 / libraries / libCq CMS for java.old / src / org / cresques / geo / cover / Coverage.java @ 10328

History | View | Annotate | Download (18.9 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.geo.cover;
25

    
26
import org.cresques.cts.ICoordTrans;
27
import org.cresques.cts.IProjection;
28
import org.cresques.cts.ProjectionPool;
29
import org.cresques.cts.gt2.CoordSys;
30
//import org.cresques.cts.gt2.CoordTrans;
31

    
32
import org.cresques.geo.Projected;
33
import org.cresques.geo.Projection;
34
import org.cresques.geo.ReProjection;
35

    
36
import org.cresques.io.GeoRasterFile;
37

    
38
import org.cresques.px.*;
39

    
40
import org.cresques.ui.CQApp;
41

    
42
import java.awt.Color;
43

    
44
import java.io.File;
45
import java.io.FilenameFilter;
46

    
47
import java.util.ArrayList;
48
import java.util.Enumeration;
49
import java.util.Hashtable;
50

    
51

    
52
/**
53
 * Cobertura espacial
54
 *
55
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
56
 */
57
public class Coverage implements Projected {
58
    boolean debug = true;
59

    
60
    /*        protected FileSupport [] fSupport = {
61
                    new FileSupport("ecw", EcwFile.class)
62
            };*/
63

    
64
    // Nombre de la cobertura
65
    protected String name = "Orto ICV 1:5.000";
66
    protected String fName = "cover";
67

    
68
    // Direcci?n raiz de los datos
69
    protected String dName = null; //"//sercart/cartografia/ortofotos/Orto_ICV/00009";
70
    protected int nLevels = 1;
71
    protected String hojaPrefix = "";
72
    protected Hashtable hojas = null;
73
    private Hashtable files = null;
74
    protected PxObjList marcos;
75
    public Color color = null;
76
    public Color fillColor = null;
77
    protected boolean loadable = true;
78
    private String fType = "";
79
    protected IProjection proj = null;
80
    private boolean available = true;
81

    
82
    public Coverage() {
83
        hojas = new Hashtable();
84
        files = new Hashtable();
85
    }
86

    
87
    public String getName() {
88
        return name;
89
    }
90

    
91
    public Hashtable getHojas() {
92
        return hojas;
93
    }
94

    
95
    public IProjection getProjection() {
96
        return proj;
97
    }
98

    
99
    public void setProjection(IProjection p) {
100
        proj = p;
101
    }
102

    
103
    public void reProject(ICoordTrans rp) {
104
        // TODO metodo reProject pendiente de implementar
105
    }
106

    
107
    /**
108
     * Asigna el estado de disponibilidad.
109
     * Cuando una cobertura no esta disponible (la unidad est?
110
     * inactiva, no tiene ficheros, etc...) deber? ponerse a false.
111
     * Es true por defecto
112
     * @param av Disponibilidad.
113
     */
114
    public void setAvailable(boolean av) {
115
        available = av;
116
    }
117

    
118
    /**
119
     * Devuelve el estado de disponibilidad.
120
     * @return Disponibilidad.
121
     */
122
    public boolean getAvailable() {
123
        return available;
124
    }
125

    
126
    /**
127
     * Especifica el tipo de los ficheros que forman la cobertura.
128
     * Es un string que ser? procesado por la aplicaci?n. La clase
129
     * Coverage no hace ning?n tratamiento de este valor.
130
     * @param fType Tipo de ficheros.
131
     */
132
    public void setFileType(String fType) {
133
        this.fType = fType;
134
    }
135

    
136
    /**
137
     * Devuelve el tipo de fichero.
138
     * @return Tipo de fichero.
139
     */
140
    public String getFileType() {
141
        return fType;
142
    }
143

    
144
    public void catalogue(String[] files) {
145
        String hName = null;
146

    
147
        for (int i = 0; i < files.length; i++) {
148
            hName = hojaPrefix + files[i];
149
            hojas.put(hName, files[i]);
150

    
151
            if (debug) {
152
                System.out.println("Coverage: hojas.put('" + hName + "'");
153
            }
154
        }
155
    }
156

    
157
    public void catalogue() {
158
        catalogue(dName);
159
    }
160

    
161
    /**
162
     * Hace el cat?logo a partir de la lista de ficheros.
163
     * @param files
164
     */
165
    public void catalogueList(String[][] files) {
166
        IProjection prj = proj;
167
        String hName = null;
168

    
169
        for (int i = 0; i < files.length; i++) {
170
            if (files[i][2] != null) {
171
                if (proj instanceof Projection) {
172
                    prj = (Projection) Projection.getProjectionByName(proj.getDatum(),
173
                                                                      files[i][2]);
174
                } else if (proj instanceof CoordSys) {
175
                    prj = new ProjectionPool().get(files[i][2]);
176
                }
177
            }
178

    
179
            hName = files[i][0];
180
            hojas.put(hName, new Hoja(prj, files[i][1], files[i][0]));
181

    
182
            if (debug) {
183
                System.out.println("Coverage: hojas.put('" + hName + "'");
184
            }
185
        }
186
    }
187

    
188
    /**
189
     * Hace el cat?logo recorriendo un directorio.
190
     * @param dName
191
     */
192
    public void catalogue(String dName) {
193
        System.out.println("Catalogando '" + dName + "' ...");
194

    
195
        File dir = new File(dName);
196

    
197
        if (!dir.exists()) {
198
            System.err.println("Que dice que '" + dName + "'no existe");
199

    
200
            return;
201
        }
202

    
203
        String[] files = dir.list();
204

    
205
        if (files != null) {
206
            catalogue(files);
207
        }
208
    }
209

    
210
    public IObjList[] loadMinuteo() {
211
        IObjList[] minuteo = null;
212

    
213
        return minuteo;
214
    }
215

    
216
    public void saveMinuteo(IObjList[] minuteo) {
217
        for (int i = 0; i < minuteo.length; i++) {
218
        }
219
    }
220

    
221
    /**
222
     * Genera el minuteo, reproyectado.
223
     * @param targetProj proyeccion de destino
224
     * @return array de minuteos
225
     */
226
    public IObjList[] generateMinuteo(IProjection targetProj) {
227
        IObjList[] minuteo = loadMinuteo();
228

    
229
        if (minuteo != null) {
230
            return minuteo;
231
        }
232

    
233
        minuteo = new IObjList[nLevels];
234

    
235
        for (int i = 0; i < minuteo.length; i++)
236
            minuteo[i] = new PxObjList();
237

    
238
        if (hojas.size() == 0) {
239
            catalogue(this.dName);
240
        }
241

    
242
        marcos = new PxObjList();
243

    
244
        IObjList layer = minuteo[0];
245
        IObjList layer1 = null;
246

    
247
        if (nLevels > 1) {
248
            layer1 = minuteo[1];
249
        }
250

    
251
        Enumeration enumeration = hojas.keys();
252
        String hName;
253
        String hCode;
254
        Hoja h;
255
        Object obj = null;
256

    
257
        while (enumeration.hasMoreElements()) {
258
            hName = (String) enumeration.nextElement();
259

    
260
            try {
261
                obj = hojas.get(hName);
262

    
263
                if (obj instanceof String) {
264
                    hCode = (String) obj;
265
                    generateMinuteoHoja(dName + "/" + hCode, hName, layer,
266
                                        layer1);
267
                } else if (obj instanceof Hoja) {
268
                    h = (Hoja) obj;
269
                    generateMinuteoHoja(dName + "/" + h.getCode(), h, layer,
270
                                        layer1);
271
                }
272
            } catch (Exception e) {
273
                // TODO Tengo que tratar los errores de una forma mejor.
274
                System.err.println("Error al cargar la hoja " + hName);
275
                e.printStackTrace();
276
            }
277
        }
278

    
279
        saveMinuteo(minuteo);
280

    
281
        //app.setCurrentMinuteo(layer);
282
        if (proj != targetProj) {
283
            ICoordTrans rp = null;
284

    
285
            if (proj instanceof Projection) {
286
                rp = new ReProjection(((Projection) proj),
287
                                      ((Projection) targetProj));
288
            } else if (proj instanceof CoordSys) {
289
                //rp = new CoordTrans((CoordSys) proj, (CoordSys) targetProj);
290
            }
291

    
292
            layer.reProject(rp);
293

    
294
            if (layer1 != null) {
295
                layer1.reProject(rp);
296
            }
297
        }
298

    
299
        return minuteo;
300
    }
301

    
302
    public void generateMinuteo(CQApp app, String lName, String lName1) {
303
        PxLayer layer1 = null;
304

    
305
        if (color == null) {
306
            color = new Color(64, 220, 64, 255);
307
        }
308

    
309
        if (fillColor == null) {
310
            fillColor = new Color(128, 220, 128, 32);
311
        }
312

    
313
        if (hojas.size() == 0) {
314
            catalogue(this.dName);
315
        }
316

    
317
        marcos = new PxObjList();
318

    
319
        if (nLevels > 0) {
320
            layer1 = app.createLayer(name + lName1, proj);
321
            layer1.setColor(color);
322
            layer1.setFillColor(fillColor);
323
        }
324

    
325
        PxLayer layer = app.createLayer(name + lName, proj);
326
        layer.setColor(color);
327
        layer.setFillColor(fillColor);
328

    
329
        Enumeration enumeration = hojas.keys();
330
        String hName;
331
        String hCode;
332
        Hoja h;
333
        Object obj = null;
334

    
335
        while (enumeration.hasMoreElements()) {
336
            hName = (String) enumeration.nextElement();
337
            obj = hojas.get(hName);
338

    
339
            if (obj.getClass() == String.class) {
340
                hCode = (String) obj;
341
                generateMinuteoHoja(dName + "/" + hCode, hName, layer, layer1);
342
            } else if (obj.getClass() == Hoja.class) {
343
                h = (Hoja) obj;
344
                generateMinuteoHoja(dName + "/" + h.getCode(), h, layer, layer1);
345
            }
346
        }
347

    
348
        app.setCurrentMinuteo(layer);
349

    
350
        if (proj != app.getCurrentProjection()) {
351
            ICoordTrans rp = null;
352

    
353
            if (proj instanceof Projection) {
354
                rp = new ReProjection(((Projection) proj),
355
                                      ((Projection) app.getCurrentProjection()));
356
            }
357

    
358
            layer.reProject(rp);
359

    
360
            if (layer1 != null) {
361
                layer1.reProject(rp);
362
            }
363
        }
364
    }
365

    
366
    public void generateMinuteoHoja(String hFName, Hoja h, IObjList layer,
367
                                    IObjList layer1) {
368
        GeoRasterFile eFile = null;
369
        PxContour contour = null;
370
        String fName = null;
371
        File dir = new File(hFName);
372

    
373
        if (!hFName.startsWith("ecwp:") && !dir.exists()) {
374
            System.err.println("Hoja '" + hFName + "' Inexistente");
375

    
376
            return;
377
        }
378

    
379
        if (dir.isDirectory()) {
380
        } else {
381
            if (!GeoRasterFile.fileIsSupported(hFName)) {
382
                return;
383
            }
384

    
385
            fName = hFName;
386
            System.out.println("Analizo '" + h.getName() + "' ...");
387
            eFile = GeoRasterFile.openFile(null, fName);
388
            contour = new PxContour(eFile.getExtent(), fName, h.getName(),
389
                                    h.getProjection());
390

    
391
            if (proj != h.getProjection()) {
392
                ICoordTrans rp = null;
393

    
394
                if (proj instanceof Projection) {
395
                    rp = new ReProjection(((Projection) h.getProjection()),
396
                                          ((Projection) proj));
397
                } else if (proj instanceof CoordSys) {
398
                   /* rp = new CoordTrans((CoordSys) h.getProjection(),
399
                                        (CoordSys) proj);*/
400
                }
401

    
402
                contour.reProject(rp);
403
            }
404

    
405
            eFile.close();
406
            contour.c(color);
407
            contour.fillColor(fillColor);
408
            layer.add(contour);
409
        }
410
    }
411

    
412
    public void generateMinuteoHoja(String hName, String name, IObjList layer,
413
                                    IObjList layer1) {
414
        GeoRasterFile eFile = null;
415
        PxContour contour = null;
416
        String fName = null;
417
        File dir = new File(hName);
418

    
419
        if (!hName.startsWith("ecwp:") && !dir.exists()) {
420
            System.err.println("Hoja '" + hName + "' Inexistente");
421

    
422
            return;
423
        }
424

    
425
        if (dir.isDirectory()) {
426
            FilenameFilter filter = new FilenameFilter() {
427
                public boolean accept(File dir, String fName) {
428
                    return GeoRasterFile.fileIsSupported(fName);
429
                }
430
            };
431

    
432
            String[] files = dir.list(filter);
433
            System.out.print(files.length + " ficheros.");
434

    
435
            if (files.length > 0) {
436
                Extent extent = new Extent();
437

    
438
                for (int i = 0; i < files.length; i++) {
439
                    fName = hName + "/" + files[i];
440
                    System.out.println("Analizo '" + fName + "' ...");
441
                    eFile = GeoRasterFile.openFile(null, fName);
442
                    contour = new PxContour(eFile.getExtent(), fName, files[i],
443
                                            proj);
444
                    eFile.close();
445

    
446
                    //contour = EcwFile.getContour(fName, files[i], proj);
447
                    layer1.add(contour);
448

    
449
                    //                                        mapview.loadECW(hName+"/"+fName);
450
                    extent.add(contour.getExtent());
451
                }
452

    
453
                if (contour != null) {
454
                    contour = new PxContour(extent, hName, name, proj);
455
                    contour.c(color);
456
                    contour.fillColor(fillColor);
457
                    layer.add(contour);
458
                }
459
            }
460
        } else {
461
            if (!GeoRasterFile.fileIsSupported(hName)) {
462
                return;
463
            }
464

    
465
            fName = hName;
466
            System.out.println("Analizo '" + name + "' ...");
467

    
468
            //contour = EcwFile.getContour(fName, name, proj);
469
            eFile = GeoRasterFile.openFile(null, fName);
470
            contour = new PxContour(eFile.getExtent(), fName, name, proj);
471
            eFile.close();
472
            contour.c(color);
473
            contour.fillColor(fillColor);
474
            layer.add(contour);
475

    
476
            //                        mapview.loadECW(hName+"/"+fName);
477
        }
478
    }
479

    
480
    /**
481
     * Devuelve los ficheros que incluye la hoja.
482
     * @param hoja hoja que se busca.
483
     * @return nombre(s) de fichero(s).
484
     */
485
    public ArrayList getFileNames(String hoja) {
486
        ArrayList fNames = new ArrayList();
487

    
488
        if (!loadable) {
489
            return null;
490
        }
491

    
492
        String fName = null;
493
        Object obj = hojas.get(hoja);
494
        String hCode = null;
495
        String hDName = null;
496
        File dir = null;
497

    
498
        if (obj != null) {
499
            if (obj instanceof String) {
500
                hCode = (String) obj;
501
            } else if (obj instanceof Hoja) {
502
                hCode = ((Hoja) obj).getCode();
503
            }
504

    
505
            hDName = dName + "/" + hCode;
506
            System.out.print("Explorando '" + hDName + "' (" + hoja + ") ... ");
507
            dir = new File(hDName);
508
        }
509

    
510
        if ((obj == null) || (!hDName.startsWith("ecwp:") && !dir.exists())) {
511
            System.err.println("Que dice que no existe");
512

    
513
            return null;
514
        }
515

    
516
        String[] files; /* = dir.list();
517
        /*                if (children == null) {
518
        // Either dir does not exist or is not a directory
519
        } else {
520
        for (int i=0; i<children.length; i++) {
521
         // Get filename of file or directory
522
         String filename = children[i];
523
        }
524
        } */
525

    
526
        // It is also possible to filter the list of returned files.
527
        // This example does not return any files that start with `.'.
528
        if (dir.isDirectory()) {
529
            FilenameFilter filter = new FilenameFilter() {
530
                public boolean accept(File dir, String fName) {
531
                    return GeoRasterFile.fileIsSupported(fName);
532
                }
533
            };
534

    
535
            files = dir.list(filter);
536
            System.out.print(files.length + " ficheros.");
537

    
538
            if (files.length > 0) {
539
                //PxLayer layer = app.createLayer(name+hoja, proj);
540
                for (int i = 0; i < files.length; i++) {
541
                    fName = files[i];
542
                    System.out.println("Cargo ...'" + fName + "'");
543
                    fNames.add(hDName + "/" + fName);
544

    
545
                    //mapview.canvas.zoomExtents();
546
                    //app.getCanvas().repaint();
547
                }
548
            }
549
        } else {
550
            //PxLayer layer = app.getLayerByName(name);
551
            //if (layer == null) {
552
            //        layer = app.createLayer(name, proj);
553
            //}
554
            //app.setCurrentLayer(layer);
555
            fName = hDName;
556

    
557
            if (!GeoRasterFile.fileIsSupported(fName)) {
558
                return null;
559
            }
560

    
561
            System.out.println("Cargo ...'" + fName + "'");
562
            fNames.add(fName);
563

    
564
            //mapview.canvas.zoomExtents();
565
            //app.getCanvas().repaint();
566
        }
567

    
568
        return fNames;
569
    }
570

    
571
    public void loadHoja(CQApp app, String hoja) {
572
        if (!loadable) {
573
            return;
574
        }
575

    
576
        String fName = null;
577
        Object obj = hojas.get(hoja);
578
        String hCode = null;
579

    
580
        if (obj.getClass() == String.class) {
581
            hCode = (String) obj;
582
        } else if (obj.getClass() == Hoja.class) {
583
            hCode = ((Hoja) obj).getCode();
584
        }
585

    
586
        String hDName = dName + "/" + hCode;
587
        System.out.print("Explorando '" + hDName + "' (" + hoja + ") ... ");
588

    
589
        File dir = new File(hDName);
590

    
591
        if ((!hDName.startsWith("ecwp:") && !dir.exists()) ||
592
                (hojas.get(hoja) == null)) {
593
            System.err.println("Que dice que no existe");
594

    
595
            return;
596
        }
597

    
598
        String[] files; /* = dir.list();
599
        /*                if (children == null) {
600
        // Either dir does not exist or is not a directory
601
        } else {
602
        for (int i=0; i<children.length; i++) {
603
         // Get filename of file or directory
604
         String filename = children[i];
605
        }
606
        } */
607

    
608
        // It is also possible to filter the list of returned files.
609
        // This example does not return any files that start with `.'.
610
        if (dir.isDirectory()) {
611
            FilenameFilter filter = new FilenameFilter() {
612
                public boolean accept(File dir, String fName) {
613
                    return GeoRasterFile.fileIsSupported(fName);
614
                }
615
            };
616

    
617
            files = dir.list(filter);
618
            System.out.print(files.length + " ficheros.");
619

    
620
            if (files.length > 0) {
621
                PxLayer layer = app.createLayer(name + hoja, proj);
622

    
623
                for (int i = 0; i < files.length; i++) {
624
                    fName = files[i];
625
                    System.out.println("Cargo ...'" + fName + "'");
626
                    app.loadFile(hDName + "/" + fName, proj);
627

    
628
                    //mapview.canvas.zoomExtents();
629
                    app.getCanvas().repaint();
630
                }
631
            }
632
        } else {
633
            PxLayer layer = app.getLayerByName(name);
634

    
635
            if (layer == null) {
636
                layer = app.createLayer(name, proj);
637
            }
638

    
639
            app.setCurrentLayer(layer);
640
            fName = hDName;
641

    
642
            if (!GeoRasterFile.fileIsSupported(fName)) {
643
                return;
644
            }
645

    
646
            System.out.println("Cargo ...'" + fName + "'");
647
            app.loadFile(fName, proj);
648

    
649
            //mapview.canvas.zoomExtents();
650
            app.getCanvas().repaint();
651
        }
652
    }
653
}