Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / DielmoOpenLidar / src / com / dielmo / gvsig / lidar / extensions / LidarExtension.java @ 25419

History | View | Annotate | Download (28.7 KB)

1
/* DielmoOpenLiDAR
2
 *
3
 * Copyright (C) 2008 DIELMO 3D S.L. (DIELMO) and Infrastructures  
4
 * and Transports Department of the Valencian Government (CIT)
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., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 *
21
 * For more information, contact:
22
 *
23
 * DIELMO 3D S.L.
24
 * Plaza Vicente Andr?s Estell?s 1 Bajo E
25
 * 46950 Xirivella, Valencia
26
 * SPAIN
27
 *   
28
 * +34 963137212
29
 * dielmo@dielmo.com
30
 * www.dielmo.com
31
 * 
32
 * or
33
 * 
34
 * Generalitat Valenciana
35
 * Conselleria d'Infraestructures i Transport
36
 * Av. Blasco Ib??ez, 50
37
 * 46010 VALENCIA
38
 * SPAIN
39
 *
40
 * +34 963862235
41
 * gvsig@gva.es
42
 * www.gvsig.gva.es
43
 */
44

    
45
/*
46
 * AUTHORS (In addition to DIELMO and CIT):
47
 *  
48
 */
49

    
50
package com.dielmo.gvsig.lidar.extensions;
51

    
52
import java.awt.Color;
53
import java.awt.Component;
54
import java.awt.geom.Rectangle2D;
55
import java.io.File;
56
import java.io.FileInputStream;
57
import java.io.FileNotFoundException;
58
import java.io.InputStreamReader;
59
import java.io.Reader;
60
import java.io.UnsupportedEncodingException;
61
import java.net.URL;
62
import java.text.NumberFormat;
63
import java.util.ArrayList;
64

    
65
import javax.swing.JOptionPane;
66

    
67
import org.exolab.castor.xml.ValidationException;
68

    
69
import com.dielmo.gvsig.lidar.Strategies.LiDARStrategy;
70
import com.dielmo.gvsig.lidar.Strategies.LidarWatcher;
71
import com.dielmo.gvsig.lidar.drivers.LiDARDriver;
72
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
73
import com.hardcode.gdbms.engine.values.ValueFactory;
74
import com.hardcode.gdbms.engine.values.ValueWriter;
75
import com.iver.andami.PluginServices;
76
import com.iver.andami.plugins.Extension;
77
import com.iver.andami.preferences.IPreference;
78
import com.iver.andami.preferences.IPreferenceExtension;
79
import com.iver.cit.gvsig.About;
80
import com.iver.cit.gvsig.fmap.DriverException;
81
import com.iver.cit.gvsig.fmap.MapContext;
82
import com.iver.cit.gvsig.fmap.core.FShape;
83
import com.iver.cit.gvsig.fmap.core.IGeometry;
84
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
85
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
86
import com.iver.cit.gvsig.fmap.layers.FLayer;
87
import com.iver.cit.gvsig.fmap.layers.FLayers;
88
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
89
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
90
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
91
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
92
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
93
import com.iver.cit.gvsig.fmap.layers.XMLException;
94
import com.iver.cit.gvsig.fmap.rendering.FInterval;
95
import com.iver.cit.gvsig.fmap.rendering.Legend;
96
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
97
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
98
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
99
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
100
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
101
import com.iver.cit.gvsig.gui.panels.FPanelAbout;
102
import com.iver.cit.gvsig.project.documents.view.IProjectView;
103
import com.iver.cit.gvsig.project.documents.view.gui.View;
104
import com.iver.utiles.XMLEntity;
105
import com.iver.utiles.xml.XMLEncodingUtils;
106
import com.iver.utiles.xmlEntity.generate.XmlTag;
107

    
108
public class LidarExtension extends Extension implements IPreferenceExtension{                
109
        
110
        private static final IPreference thePreferencePage = new Lidar_Preferences();
111
        
112
        public void initialize() {
113
                
114
                LayerFactory.getDM().addDriver(
115
                                PluginServices.getPluginServices(this).getPluginDirectory()
116
                                , LiDARDriver.driverName, LiDARDriver.class);
117
        
118
        About about=(About)PluginServices.getExtension(About.class);
119
                FPanelAbout panelAbout=about.getAboutPanel();
120
                java.net.URL aboutURL = getClass().getClassLoader().getResource("/about.html");
121
            panelAbout.addAboutUrl("DielmoOpenLidar",aboutURL);
122
        }
123

    
124
        public void execute(String actionCommand) {
125
        
126
                if(actionCommand.equalsIgnoreCase("Z_LEGEND")) {
127
                        zLegend();
128
                }
129
                else if (actionCommand.equalsIgnoreCase("CLASIFICACION_LEGEND")) {
130
                        clasificationLegend();
131
                }
132
                else if (actionCommand.equalsIgnoreCase("INTENSITY_LEGEND")) {
133
                        intensityLegend();                        
134
                }
135
                else if (actionCommand.equalsIgnoreCase("PREFER_LEGEND")) {
136
                        PreferenceLegend();
137
                }
138
                else if(actionCommand.equalsIgnoreCase("DEFAULT_LEGEND")) {
139
                        defaultLegend();
140
                }
141
                return;
142
        }
143
        
144
        /**
145
         * Aplica la leyenda por intervalos para el valor de la tabla intensidad
146
         */
147
        private void intensityLegend() {
148
                FilterByName("Intensity");
149
        }
150
        
151
        /**
152
         * Aplica la leyenda por intervalos para el valor de la tabla z
153
         */
154
        private void zLegend() {
155
                FilterByName("Z");
156
        }
157
        
158
        /**
159
         * Aplica la leyenda por defecto que aplicaria gvSIG, es decir, aplica
160
         * la leyenda para simbolos con colores aleatorios para cada capa.
161
         */
162
        private void defaultLegend() {        
163
                // Cogemos todas las capas de la vista
164
        View vista = (View)PluginServices.getMDIManager().getActiveWindow();
165
                MapContext mapContext = vista.getModel().getMapContext();
166
        FLayers layers = mapContext.getLayers();
167
    
168
        mapContext.beginAtomicEvent();
169
        AplyDefaultLeyend(layers);
170
        mapContext.endAtomicEvent();
171
        }
172
        
173
        /**
174
         * Recorre todas las capas que contiene el FLayers de entrada
175
         * y le aplica la leyenda por defecto (colores aleatorios para cada capa)
176
         * 
177
         * @param layers, capas en las que aplicaremos la leyenda
178
         */
179
        private void AplyDefaultLeyend(FLayers layers) {
180
                SingleSymbolLegend legend;
181
                FSymbol s;
182
                FLayer lyr;
183
                
184
                SingleLayerIterator it = new SingleLayerIterator(layers);
185
        
186
        while (it.hasNext()) {
187
                lyr = it.next();
188
                        if (LiDARDriver.isLidarLayer(lyr)) {
189
                                
190
                                try {
191
                                        FLyrVect lyrVect = (FLyrVect) lyr;
192
                                        s = new FSymbol(FShape.POINT);
193
                                        s.setSize(5);
194
                                        legend = new SingleSymbolLegend(s);
195
                                        lyrVect.setLegend((VectorialLegend) legend);
196
                                        
197
                                } catch (FieldNotFoundException e) {
198
                                        // TODO Auto-generated catch block
199
                                        e.printStackTrace();
200
                                } catch (DriverException e) {
201
                                        // TODO Auto-generated catch block
202
                                        e.printStackTrace();
203
                                }
204
                        }
205
                } // while
206
        }
207

    
208
        /**
209
         * Aplica la leyenda predefinida en Preferencias->Preferencias Lidar
210
         */
211
        private void PreferenceLegend() {
212
                Legend legend = null;
213
                
214
                LiDAR_Mapping lm = new LiDAR_Mapping();
215
                
216
                String preferLegend = lm.getPathLegend();
217
                
218
                File file=new File(preferLegend);
219
                
220
                if(!file.isFile())
221
                        return;
222
                        
223
                // Cogemos todas las capas de la vista
224
        View vista = (View)PluginServices.getMDIManager().getActiveWindow();
225
                MapContext mapContext = vista.getModel().getMapContext();
226
        FLayers layers = mapContext.getLayers();
227
        FLayer lyr;
228
       
229
                if ((file.getPath().endsWith(".gvl") || file.getPath().endsWith(".GVL"))) {        
230
                        legend = readLegend(file);
231
                        
232
                        mapContext.beginAtomicEvent();
233
                        SingleLayerIterator it = new SingleLayerIterator(layers);
234
                
235
                        // le asignamos la leyenda a todas las capas lidar
236
                while (it.hasNext()) {
237
                        
238
                        lyr = it.next();
239
                                if(LiDARDriver.isLidarLayer(lyr)){
240
                                        
241
                                        // clonar leyenda si falla
242
                                        aplyLegend(lyr,legend);
243
                                }
244
                }
245
                mapContext.endAtomicEvent();
246
                }
247
        }
248
        
249
        /**
250
         * Aplica la leyenda para las clasificaciones.
251
         * 
252
         * La clasificacion se corresponde con la definicion que la especificacion
253
         * del LAS 1.1 se da sobre el tema. Por esta causa no se usa actualmente, ya
254
         * que cada lidar puede tener un tipo de clasificacion.
255
         */
256
        private void clasificationLegend() {
257
                
258
                String filtername = "Classification";
259
                VectorialUniqueValueLegend legend = null;
260
                FSymbol mySymbol;
261
     // Cogemos todas las capas de la vista
262
        View vista = (View)PluginServices.getMDIManager().getActiveWindow();
263
                MapContext mapContext = vista.getModel().getMapContext();
264
        FLayers layers = mapContext.getLayers();
265
        FLyrVect lyr = null;
266
        int i;        
267
        
268
                legend = LegendFactory.createVectorialUniqueValueLegend(FShape.POINT);
269
                legend.clear();
270
                legend.setFieldName(filtername);
271
            
272
                mySymbol = setSymbol(PluginServices.getText(null, "Class0"), new Color(0,0,0));
273
                legend.addSymbol(ValueFactory.createValue(0), mySymbol);
274
                
275
                mySymbol = setSymbol(PluginServices.getText(null, "Class1"), new Color(0,0,0));
276
                legend.addSymbol(ValueFactory.createValue(1), mySymbol);
277
                
278
                mySymbol = setSymbol(PluginServices.getText(null, "Class2"), new Color(153,0,0));
279
                legend.addSymbol(ValueFactory.createValue(2), mySymbol);
280
                
281
                mySymbol = setSymbol(PluginServices.getText(null, "Class3"), new Color(153,255,153));
282
                legend.addSymbol(ValueFactory.createValue(3), mySymbol);
283
                
284
                mySymbol = setSymbol(PluginServices.getText(null, "Class4"), new Color(0,204,0));
285
                legend.addSymbol(ValueFactory.createValue(4), mySymbol);
286
                
287
                mySymbol = setSymbol(PluginServices.getText(null, "Class5"), new Color(0,153,0));
288
                legend.addSymbol(ValueFactory.createValue(5), mySymbol);
289
                
290
                mySymbol = setSymbol(PluginServices.getText(null, "Class6"), new Color(255,0,0));
291
                legend.addSymbol(ValueFactory.createValue(6), mySymbol);
292
                
293
                mySymbol = setSymbol(PluginServices.getText(null, "Class7"), new Color(102,0,102));
294
                legend.addSymbol(ValueFactory.createValue(7), mySymbol);
295
                
296
                mySymbol = setSymbol(PluginServices.getText(null, "Class8"), new Color(255,0,204));
297
                legend.addSymbol(ValueFactory.createValue(8), mySymbol);
298
                
299
                mySymbol = setSymbol(PluginServices.getText(null, "Class9"), new Color(51,102,255));
300
                legend.addSymbol(ValueFactory.createValue(9), mySymbol);
301
                
302
                mySymbol = setSymbol(PluginServices.getText(null, "Class12"), new Color(153,255,204));
303
                legend.addSymbol(ValueFactory.createValue(12), mySymbol);
304
                   
305
                // le asignamos la leyenda a todas las capas lidar
306
                for (i = 0; i < layers.getLayersCount(); i++) {
307
                        
308
                        if (LiDARDriver.isLidarLayer(layers.getLayer(i))) {
309
                                
310
                                try {
311
                                        lyr = ((FLyrVect) layers.getLayer(i));
312
                                        VectorialUniqueValueLegend cloneLegend = (VectorialUniqueValueLegend) legend.cloneLegend();
313
                                        lyr.setLegend(cloneLegend);
314
                                } catch (FieldNotFoundException e) {
315
                                        // TODO Auto-generated catch block
316
                                        e.printStackTrace();
317
                                } catch (DriverException e) {
318
                                        // TODO Auto-generated catch block
319
                                        e.printStackTrace();
320
                                } catch (XMLException e) {
321
                                        // TODO Auto-generated catch block
322
                                        e.printStackTrace();
323
                                }
324
                        }                            
325
                }
326
        }
327
        
328
        /** 
329
         * Aplica las leyendas a todas las capas del FLayers, incluida
330
         * las agrupaciones.
331
         * 
332
         * @param layers capas a las que aplicarle la leyenda.
333
         * @param leyenda, leyenda a aplicar
334
         */  
335
        private void aplyLegend(FLayer layer, Legend leyenda) {
336
                
337
                if (layer instanceof FLyrVect) {
338
                                
339
                        try {
340
                                
341
                                FLyrVect lyrVect = (FLyrVect) layer;
342
                                lyrVect.setLegend((VectorialLegend) leyenda.cloneLegend());
343
                                
344
                        } catch (FieldNotFoundException e) {
345
                                // TODO Auto-generated catch block
346
                                e.printStackTrace();
347
                        } catch (DriverException e) {
348
                                // TODO Auto-generated catch block
349
                                e.printStackTrace();
350
                        } catch (XMLException e) {
351
                                // TODO Auto-generated catch block
352
                                e.printStackTrace();
353
                        }
354
                }
355
        }
356
        
357
        /**
358
         * Aplica la leyenda por intervalos del campo filtername pasado por entrada.
359
         * 
360
         * @param filtername, campo por el que filtrar
361
         */
362
        private void FilterByName(String filtername) {
363
                
364
                int k;
365
                VectorialIntervalLegend legend = null;
366
                FInterval[] arrayIntervalos = null;
367
                FSymbol myDefaultSymbol;
368
                ReadableVectorial rvLidar;
369
                SelectableDataSource ds;
370
                int campo,LidarLayerCount=0; 
371
                long NumPointsByLayer, inc=1, j; 
372
    //        double MaxValue=Double.NEGATIVE_INFINITY, MinValue=Double.MAX_VALUE;
373
            double mean = 0, desviacion = 0, value;
374
            ArrayList muestras = new ArrayList();
375
                
376
                // Cogemos todas las capas de la vista
377
        View vista = (View)PluginServices.getMDIManager().getActiveWindow();
378
                MapContext mapContext = vista.getModel().getMapContext();
379
        FLayers layers = mapContext.getLayers();
380
        IGeometry geom;
381
        
382
        Rectangle2D extentView = mapContext.getViewPort().getAdjustedExtent();
383
        Rectangle2D extentLayer;
384
        
385
        FLayer lyr = null;
386
        
387
        SingleLayerIterator it = new SingleLayerIterator(layers);
388
        
389
        // Calculamos el n?mero de capas que intersectan con la vista
390
        while (it.hasNext()) {
391
                
392
                lyr = it.next();
393
                        if(LiDARDriver.isLidarLayer(lyr)){
394
                                
395
                                try {
396
                                        
397
                                extentLayer = lyr.getFullExtent();
398
                        
399
                                if(extentLayer.intersects(extentView)){
400
                                        LidarLayerCount++;
401
                                        }
402
                        
403
                                }catch (DriverException e) {
404
                                        // TODO Auto-generated catch block
405
                                        e.printStackTrace();
406
                                }
407
                        }
408
        }
409
        
410
                // si ninguna capa intersecta con el fullextent dejamos de operar
411
                if(LidarLayerCount == 0)
412
                        return;
413
                
414
                NumPointsByLayer = 5000 / LidarLayerCount;
415
                
416
                SingleLayerIterator it2 = new SingleLayerIterator(layers);
417
        
418
                // Cogemos los valores max y min del campo por el que se filtra de todas las capas. 
419
        while (it2.hasNext()) {
420
                
421
                lyr = it2.next();
422
                        if(LiDARDriver.isLidarLayer(lyr)){
423
                                
424
                                try {
425
                                
426
                                        FLyrVect lyrVect;
427
                                        lyrVect = ((FLyrVect) lyr);
428
                                        rvLidar = lyrVect.getSource();
429
                                        rvLidar.start();
430
                                ds = rvLidar.getRecordset();
431
                                extentLayer = lyrVect.getFullExtent();
432
                                
433
                                if( extentLayer.intersects(extentView)) { 
434
                                        
435
                                        // si el campo por el que se filtra es Z y ademas el fullextent de la capa
436
                                        // esta completamente denro de la vista, cogemos los valores Z de la cabecera
437
                                        // proporcionada por el driver que maneja la capa
438
                                        LiDARDriver ld = (LiDARDriver) rvLidar.getDriver();
439
                                        if(filtername.equals("Z") && ld != null && extentView.contains(extentLayer)) {
440
                                        
441
                                                                value = ld.getLidarHeader().getMinZ();
442
                                                                muestras.add(new Double(value));
443
                                                                value = ld.getLidarHeader().getMaxZ();
444
                                                                muestras.add(new Double(value));
445
                                        } else {
446
                                                
447
                                                        campo = ds.getFieldIndexByName(filtername);
448
                                                
449
                                                        inc = ds.getRowCount() / NumPointsByLayer;
450
                                                        if (inc < 1)
451
                                                                inc = 1;
452
                                                        
453
                                                        for (j=0 ; j<ds.getRowCount(); j+=inc) {
454
                                                                
455
                                                                geom=rvLidar.getShape((int)j);
456
                                                                
457
                                                                // if (geom.intersects(extentView)) {
458
                                                                if (geom.fastIntersects(extentView.getMinX(), extentView.getMinY(), extentView.getWidth(), extentView.getHeight())) {
459
                                                                        
460
                                                                        value=Double.parseDouble(ds.getFieldValue(j, campo).getStringValue(ValueWriter.internalValueWriter));
461
                                                                        muestras.add(new Double(value));
462
                                                                }
463
                                                        }
464
                                        }
465
                                }
466
                                
467
                                rvLidar.stop();
468
                                
469
                                } catch (DriverIOException e) {
470
                                        // TODO Auto-generated catch block
471
                                        e.printStackTrace();
472
                                } catch (DriverException e) {
473
                                        // TODO Auto-generated catch block
474
                                        e.printStackTrace();
475
                                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
476
                                        // TODO Auto-generated catch block
477
                                        e.printStackTrace();
478
                                } catch (RuntimeException e) {
479
                                        // TODO Auto-generated catch block
480
                                        e.printStackTrace();
481
                                }
482
                        }
483
        }
484

    
485
        // creamos la leyenda si tenemos valores por los que filtrar
486
        if(muestras.size()>0){                
487
        
488
                // Creamos el primer y ?ltimo color.
489
                Color startColor = new Color(20,20,20);  //negro
490
                Color endColor = new Color(230,230,230); // gris claro
491
                
492
                //calculamos la media y la desviacion estandar de las muestras para calcular su leyenda
493
                //entre la media +/- 2 veces la desviacion estandar.
494
                for(k=0;k<muestras.size();k++){
495
                        
496
                        value=((Double)(muestras.get(k))).doubleValue();
497
                        mean += value;
498
                }
499
                mean=mean/muestras.size();
500
                for(k=0;k<muestras.size();k++){
501
                        
502
                        value=((Double)(muestras.get(k))).doubleValue();
503
                        desviacion += Math.abs(value-mean);
504
                }
505
                desviacion=desviacion/muestras.size();
506
        
507
                if(filtername.equals("Intensity")){
508
                        double min = 0;
509
                        if(mean-3*desviacion>0)
510
                                min = mean-3*desviacion;
511
                        
512
                        arrayIntervalos = calculateEqualIntervals(100, min, mean+3*desviacion, filtername);
513
                } else{
514
                        arrayIntervalos = calculateEqualIntervals(100, mean-3*desviacion, mean+3*desviacion, filtername);
515
                }
516
                
517
                NumberFormat.getInstance().setMaximumFractionDigits(2);
518
                
519
                int r;
520
                int g;
521
                int b;
522
                int stepR;
523
                int stepG;
524
                int stepB;
525
                r = startColor.getRed();
526
                g = startColor.getGreen();
527
                b = startColor.getBlue();
528
                stepR = (endColor.getRed() - r) / arrayIntervalos.length;
529
                stepG = (endColor.getGreen() - g) / arrayIntervalos.length;
530
                stepB = (endColor.getBlue() - b) / arrayIntervalos.length;
531
                
532
                legend = LegendFactory.createVectorialIntervalLegend(FShape.POINT);
533
                legend.clear();
534
                legend.useDefaultSymbol(true);
535
                legend.setFieldName(filtername);
536
                for (k = 0; k < arrayIntervalos.length; k++) {
537
                        FInterval elIntervalo = arrayIntervalos[k];
538
                    
539
                    Color c = new Color(r, g, b);
540
                    //si no esta creado el simbolo se crea
541
                    myDefaultSymbol = new FSymbol(FShape.POINT, c);
542
                    myDefaultSymbol.setDescription(NumberFormat.getInstance().format(elIntervalo.getMin()) +
543
                                " - " +
544
                                NumberFormat.getInstance().format(elIntervalo.getMax()));
545
                    myDefaultSymbol.setSize(3);
546
                    myDefaultSymbol.setSizeInPixels(true);
547
                    
548
                    //////////////////////////////////////
549
                    // CALCULAMOS UN COLOR APROPIADO
550
                    r = r + stepR;
551
                    g = g + stepG;
552
                    b = b + stepB;
553
                    
554
                    /////////////////////////////////
555
                    legend.addSymbol(elIntervalo, myDefaultSymbol);
556
                }
557
                
558
                // le asignamos la leyenda a todas las capas lidar
559
                SingleLayerIterator it3 = new SingleLayerIterator(layers);
560
                
561
                mapContext.beginAtomicEvent();
562
                        // le asignamos la leyenda a todas las capas lidar
563
                while (it3.hasNext()) {
564
                        
565
                        lyr = it3.next();
566
                                if(LiDARDriver.isLidarLayer(lyr)){
567
                                        aplyLegend(lyr,legend);
568
                                }
569
                }
570
                mapContext.endAtomicEvent();
571
        
572
        } else {
573
                // si el zoom es demasiado grande como para calcular una leyenda en condiciones informamos de ello
574
                JOptionPane.showMessageDialog(null,PluginServices.getText(this, "zoomInadecuado"));
575
        }
576
        }
577

    
578
        /**
579
     * NATURAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
580
     * quieren crear. Los intervalos se distribuyen de forma natural.
581
     *
582
     * @param numIntervals n?mero de intervalos
583
     * @param minValue Valor m?nimo.
584
     * @param maxValue Valor m?ximo.
585
     * @param fieldName Nombre del campo
586
     *
587
     * @return Array con los intervalos.
588
     */
589
    FInterval[] calculateNaturalIntervals(int numIntervals, double minValue,
590
        double maxValue, String fieldName) {
591

    
592
            int i;
593
        int step = (int)((maxValue-minValue)/numIntervals);
594
        while(step<1){
595
                maxValue++;
596
                minValue--;
597
                step = (int)((maxValue-minValue)/numIntervals);
598
        }
599

    
600
        FInterval[] theIntervalArray;
601

    
602
        if (numIntervals > 1) {
603
                theIntervalArray = new FInterval[numIntervals];
604
            theIntervalArray[0] = new FInterval((int)minValue,(int)(minValue+step));
605

    
606
            for ( i = 1; i < (numIntervals - 1); i++) {
607
                theIntervalArray[i] = new FInterval((int)(minValue+i*step),(int)(minValue+(i+1)*step));
608
            }
609
            theIntervalArray[numIntervals - 1] = new FInterval((int)(minValue+i*step), (int)maxValue);
610
        } else {
611
                theIntervalArray = new FInterval[1];
612
            theIntervalArray[0] = new FInterval((int)minValue,(int)maxValue);
613
        }
614

    
615
        return theIntervalArray;
616
    }
617
    
618
         /**
619
     * EQUAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
620
     * quieren crear. Los intervalos se crean con un tama?o igual entre ellos.
621
     *
622
     * @param numIntervals n?mero de intervalos
623
     * @param minValue Valor m?nimo.
624
     * @param maxValue Valor m?ximo.
625
     * @param fieldName Nombre del campo
626
     *
627
     * @return Array con los intervalos.
628
     * @see com.iver.cit.gvsig.project.documents.view.legend.gui;
629
     */
630
         public static FInterval[] calculateEqualIntervals(int numIntervals, double minValue,
631
        double maxValue, String fieldName) {
632

    
633
        FInterval[] theIntervalArray = new FInterval[numIntervals];
634
        double step = (maxValue - minValue) / numIntervals;
635

    
636
        if (numIntervals > 1) {
637
            theIntervalArray[0] = new FInterval(Integer.MIN_VALUE+3, minValue + step);
638

    
639
            for (int i = 1; i < (numIntervals - 1); i++) {
640
                theIntervalArray[i] = new FInterval(minValue + (i * step), minValue + ((i + 1) * step));
641
            }
642

    
643
            theIntervalArray[numIntervals - 1] = new FInterval(minValue + 
644
                            ((numIntervals - 1) * step), Integer.MAX_VALUE);
645
        } else {
646
            theIntervalArray[0] = new FInterval(minValue, maxValue);
647
        }
648

    
649
        return theIntervalArray;
650
    }
651

    
652
    /**
653
     * Set a new Symbol with a name and color.
654
     * 
655
     * @param nameSymbol name of symbol
656
     * @param c color of symbol
657
     * @return the symbol with name and color predefined
658
     */
659
         private FSymbol setSymbol(String nameSymbol, Color c) {
660
            
661
            FSymbol mySymbol;
662
            mySymbol = new FSymbol(FShape.POINT, c);
663
    
664
            mySymbol.setDescription(nameSymbol);            
665
            mySymbol.setSize(3);
666
            mySymbol.setSizeInPixels(true);
667
            
668
            return mySymbol;
669
    }
670
    
671
        public boolean isEnabled() {
672
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
673
                .getActiveWindow();
674
                if (f == null) {
675
                        return false;
676
                }
677
                if (f instanceof View) {
678
                        View vista = (View) f;
679
                        IProjectView model = vista.getModel();
680
                        FLayers layers =  model.getMapContext().getLayers();
681
                        
682
                        FLayer lyr;
683
                        SingleLayerIterator it = new SingleLayerIterator(layers);
684
                        // comprobamos si hay capas lidar
685
                while (it.hasNext()) {
686
                        
687
                        lyr = it.next();
688
                                if(LiDARDriver.isLidarLayer(lyr) &&  lyr.isAvailable()){
689
                                        return true;
690
                                }
691
                }
692
                
693
                }
694
                return false;
695
        }
696

    
697
        public boolean isVisible() {
698
                
699
                
700
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
701
                 .getActiveWindow();
702
                if (f == null) {
703
                    return false;
704
                }
705
                
706
                if (f instanceof View) {
707
                        
708
                    View vista = (View) f;
709
                    IProjectView model = vista.getModel();
710
                    
711
                    FLayers layers =  model.getMapContext().getLayers();
712
                    
713
                 // asignar la estrategia Lidar a las nuevas capas
714
                    setLidarListener(model.getMapContext());
715
                    setLidarListener(model.getMapOverViewContext());
716
                    
717
                    
718
                          FLayer lyr;
719
                        SingleLayerIterator it = new SingleLayerIterator(layers);
720
                
721
                        // comprobamos si hay capas lidar
722
                while (it.hasNext()) {
723
                        
724
                        lyr = it.next();
725
                                if(LiDARDriver.isLidarLayer(lyr) &&  lyr.isAvailable()){
726
                                        return true;
727
                                }
728
                }
729
                }
730
                
731
                return false;
732
        }
733
        
734
        /**
735
         * Nos registramos como escucha de los mapcontext del MapOverview
736
         * y del MapControl para que nos avisen cuando se a?ade una capa.
737
         * Si es de tipo Lidar, le cambiamos la estregia.
738
         * @param vista
739
         */
740
        private void setLidarListener(MapContext mapContext) {
741
                
742
                FLayers layers = mapContext.getLayers();
743
                  FLayer lyr;
744
                SingleLayerIterator it = new SingleLayerIterator(layers);
745
                LiDAR_Mapping map = new LiDAR_Mapping();
746
                int grosor = map.getSizePixel();
747
                
748
                // comprobamos si hay capas lidar
749
        while (it.hasNext()) {
750
                
751
                lyr = it.next();
752
                        if(LiDARDriver.isLidarLayer(lyr) &&  lyr.isAvailable()){
753
                                FLyrVect lyrVect = (FLyrVect) lyr;
754
                                
755
                                
756
                                if (lyrVect.getStrategy() != null) {
757
                                        
758
                                        // si hay una esrategia que no es Lidar la cambiamos
759
                                        if (!(lyrVect.getStrategy() instanceof LiDARStrategy)) {
760
                                                LiDARStrategy lidarStrategy = new LiDARStrategy(lyrVect,grosor);
761
                                                lyrVect.setStrategy(lidarStrategy);                                                                                
762
                                        }
763
                                        
764
                                } else {
765
                                        // si no hay ninguna estrategia definida la asignamos la estrategia Lidar
766
                                        LiDARStrategy lidarStrategy = new LiDARStrategy(lyrVect,grosor);
767
                                        lyrVect.setStrategy(lidarStrategy);
768
                                }
769
                        }
770
        }
771

    
772
                layers.addLayerCollectionListener(LidarWatcher.getInstance());
773
        }
774

    
775
        /**
776
         * Lee la leyenda del fichero file y devuelve la leyenda cargada.
777
         * 
778
         * @param file, fichero donde buscar la leyenda
779
         * @return la leyenda leida.
780
         */
781
        public Legend readLegend(File file) {
782
                
783
                Legend renderer = null;
784
                File xmlFile = new File(file.getAbsolutePath());
785
                try {
786
                        String encoding = XMLEncodingUtils.getEncoding(new FileInputStream(xmlFile));
787
                        InputStreamReader reader=null;
788
                        if (encoding!=null) {
789
                                try {
790
                                        reader = new InputStreamReader(new FileInputStream(xmlFile), encoding);
791
                                        renderer= readLegend(reader, true);
792
                                } catch (UnsupportedEncodingException e) {
793
                                        reader = new InputStreamReader(new FileInputStream(xmlFile),"ISO-8859-1");
794
                                        try {
795
                                                renderer= readLegend(reader, false);
796
                                        } catch (UnsupportedEncodingException e1) {
797
                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage()));
798
                                                return null;
799
                                        }
800
                                }
801
                        }
802
                        else {
803
                                reader = new InputStreamReader(new FileInputStream(xmlFile),"UTF-8");
804
                                try {
805
                                        renderer= readLegend(reader, false);
806
                                } catch (UnsupportedEncodingException e1) {
807
                                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage()));
808
                                        return null;
809
                                }
810
                        }
811
                } catch (FileNotFoundException e) {
812
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto"));
813
                        return null;
814
                } catch (UnsupportedEncodingException e) {
815
                        e.printStackTrace();
816
                }
817
                
818
                return renderer;
819
        }
820
        public Legend readLegend(Reader reader, boolean encodingFollowed) throws UnsupportedEncodingException {
821
                Legend legend = null;
822

    
823
                try {
824
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
825
                        XMLEntity xml=new XMLEntity(tag);
826
                        if (encodingFollowed) {
827
                                if (xml.contains("followHeaderEncoding")) {
828
                                        boolean useEncoding = xml.getBooleanProperty("followHeaderEncoding");
829
                                        if (!useEncoding) {
830
                                                throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe");
831
                                        }
832
                                }
833
                                else {
834
                                        // Old projects didn't contain followHeaderEncoding and they were
835
                                        // not correctly encoded. We throw an exception now, and we'll try
836
                                        // to reopen the project
837
                                        // using the default system encoding.
838
                                        throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe");
839
                                }
840
                        }
841

    
842
                        legend = LegendFactory.createFromXML(xml);
843
                        return legend;
844
                }  catch (ValidationException e) {
845
                        PluginServices.getLogger().error(PluginServices.getText(this, "formato_incorrecto"),e);
846
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
847
                        //NotificationManager.addError("Al leer el proyecto", e);
848
                } catch (XMLException e) {
849
                        PluginServices.getLogger().error(PluginServices.getText(this, "formato_incorrecto"),e);
850
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
851
                        //NotificationManager.addError("Al leer el proyecto", e);
852
                } catch (org.exolab.castor.xml.MarshalException e) {
853
                        // TODO Auto-generated catch block
854
                        e.printStackTrace();
855
                }
856
                return null;
857
        }
858

    
859
        /**
860
         * Usada para recuperar las preferencias Lidar
861
         */
862
        public IPreference getPreferencesPage() {
863
                return thePreferencePage;
864
        }
865
}