Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / lib3DMap-share / src / main / java / com / iver / ai2 / gvsig3d / map3d / layers / Layer3DProps.java @ 25862

History | View | Annotate | Download (14.8 KB)

1
package com.iver.ai2.gvsig3d.map3d.layers;
2

    
3
import java.awt.Component;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.FileReader;
7
import java.io.FileWriter;
8
import java.util.HashMap;
9
import java.util.Hashtable;
10
import java.util.Vector;
11

    
12
import javax.swing.JOptionPane;
13

    
14
import org.cresques.cts.IProjection;
15
import org.exolab.castor.xml.Marshaller;
16
import org.gvsig.cacheservice.CacheService;
17
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
18

    
19
import com.iver.ai2.gvsig3d.gui.VectorLayerMenu;
20
import com.iver.andami.PluginServices;
21
import com.iver.andami.messages.NotificationManager;
22
import com.iver.cit.gvsig.fmap.layers.FLayer;
23
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
24
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
25
import com.iver.cit.gvsig.fmap.layers.FLyrWCS;
26
import com.iver.cit.gvsig.fmap.layers.FLyrWMS;
27
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable;
28
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
29
import com.iver.cit.gvsig.fmap.rendering.ILegend;
30
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
31
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
32
import com.iver.utiles.IPersistence;
33
import com.iver.utiles.XMLEntity;
34
import com.iver.utiles.xmlEntity.generate.XmlTag;
35

    
36
/**
37
 * This class is used to manage all the 3d properties that the layer has.
38
 * 
39
 * @author Salvador Bayarri
40
 * @author Julio Campos 
41
 *
42
 */
43
public class Layer3DProps implements IPersistence {
44

    
45
        // types of 3D layers
46
        public static final int layer3DImage = 0;
47
        public static final int layer3DElevation = 1;
48
        public static final int layer3DVector = 2;
49
        public static final int layer3DOSG = 3;
50
        public static long drawVersion = 0;
51

    
52
        private boolean newLayerOSG = false;
53
        
54
        // public static String m_cacheDir = "c:/data/cache"; // TEST
55
        // Create a .data directorio in user home for caching elements
56
        public static String m_cacheDir = System.getProperty("user.home")
57
                        + "/gvSIG/.data/cache";
58

    
59
        protected int m_tocOrder; // index of layer in TOC (drawing priority)
60
        protected int m_planetOrder; // current stage of layer in planet.
61
        // Can be temporarily different from the TOC order while moving groups of
62
        // layers
63

    
64
        protected float m_opacity = 1.0f; // Opacity of layer default 1.0
65
        protected float m_verticalEx = 10.0f; // Opacity of layer default 1.0
66
        protected float m_heigth = 100; // Opacity of layer default 1.0
67
        protected int m_type; // see type enumeration above
68
        protected String m_cacheName = "default";
69
        protected FLayer m_layer;
70
        protected CacheService m_cacheService;
71
        private boolean m_bChooseType = true;
72

    
73
        private boolean m_Zenable = false;
74

    
75
        // used by FLayers3D (3D group layer) to flag when they are actually hooked
76
        // to TOC or not
77
        private boolean m_hooked = false;
78

    
79
        private int option;
80

    
81
        private VectorLayerMenu vectorLayerMenu;
82
        
83
        private boolean isEditing = false;
84

    
85

    
86
        /**
87
         * the constructor 
88
         */
89
        public Layer3DProps() {
90
                m_tocOrder = -1; // not set
91
                m_planetOrder = -1;
92
        }
93

    
94
        /**
95
         * This method make an instance of the 3D props of the layer that get from parameters
96
         * 
97
         * @param layer 
98
         * @return the properties
99
         */
100
        public static Layer3DProps getLayer3DProps(FLayer layer) {
101
                FLyrDefault baseLayer = (FLyrDefault) layer;
102
                Object propsObj = baseLayer.getProperty("3DLayerExtension");
103
                Layer3DProps props3D = null;
104
                if (propsObj != null) {
105
                        try {
106
                                props3D = (Layer3DProps) propsObj;
107
                        } catch (Exception e) {
108
                                props3D = null;
109
                        }
110
                }
111
                if (drawVersion == 0)
112
                        drawVersion = layer.getDrawVersion();
113
                return props3D;
114
        }
115

    
116
        /**
117
         * Return The editing state
118
         * 
119
         * @return true->editing. false->not editing 
120
         */
121
        public boolean isEditing() {
122
                return isEditing;
123
        }
124
        
125
        /**
126
         * Set the editing state
127
         * 
128
         * @param true->editing. false->not editing
129
         */
130
        public void setEditing(boolean isEditing) {
131
                this.isEditing = isEditing;
132
        }
133
        
134
        public void setChooseType(boolean bChoose) {
135
                m_bChooseType = bChoose;
136
        }
137

    
138
        public boolean getHooked() {
139
                return m_hooked;
140
        }
141

    
142
        public void setHooked(boolean hooked) {
143
                m_hooked = hooked;
144
        }
145

    
146
        /**
147
         * Setting the layer
148
         * 
149
         * @param layer
150
         */
151
        public void setLayer(FLayer layer) {
152

    
153
                if (m_layer == layer)
154
                        return;
155

    
156
                m_layer = layer;
157

    
158
                // find out data type
159
                if (m_bChooseType) {
160
                        m_bChooseType = false;
161
                        m_type = layer3DImage;
162
                        boolean bCanBeElev = false;
163

    
164
                        // TODO Un toggle this comment to use WMS extensions
165

    
166
                        if (layer instanceof FLyrWMS) {
167
                                FLyrWMS wmsLayer = (FLyrWMS) layer;
168
                                String format = wmsLayer.getFormat();
169
                                if (format.regionMatches(true, 0, "image/geotiff", 0, 13)
170
                                                || format.regionMatches(true, 0, "image/tiff", 0, 10))
171
                                        bCanBeElev = true;
172
                        } else if (layer instanceof FLyrWCS) {
173
                                FLyrWCS wcsLayer = (FLyrWCS) layer;
174
                                String format = wcsLayer.getFileFormat();
175
                                Hashtable props = wcsLayer.getProperties();
176
                                String params = (String) props.get("parameter");
177
                                if (format.compareToIgnoreCase("GEOTIFF_INT16") == 0
178
                                                && params.length() == 0)
179
                                        bCanBeElev = true;
180
                        }
181
                        // FEATURES
182
                        else /**/if (layer instanceof ClassifiableVectorial) {
183

    
184
                                if (this.getType() != layer3DOSG) {
185
                                        vectorLayerMenu = new VectorLayerMenu(this, layer.getName());
186
                                        vectorLayerMenu.setModal(true);
187
                                        vectorLayerMenu.pack();
188
                                        vectorLayerMenu.setVisible(true);
189
                                }
190

    
191
                                // PluginServices.getMDIManager().addWindow(vectorLayerMenu);
192

    
193
                                // // choose rasterization of 3D
194
                                // option = JOptionPane.showConfirmDialog(
195
                                // (Component) PluginServices.getMainFrame(),
196
                                // PluginServices
197
                                // .getText(this, "Rasterize_layer_question"),
198
                                // PluginServices.getText(this, "Layer_options"),
199
                                // JOptionPane.YES_NO_OPTION);
200
                                //
201
                                // if (option == JOptionPane.YES_OPTION)
202
                                // m_type = layer3DImage;
203
                                // else
204
                                // m_type = layer3DVector;
205
                                //
206
                                // if (m_type == layer3DVector) {
207
                                // String Altura = JOptionPane.showInputDialog(PluginServices
208
                                // .getText(this, PluginServices.getText(this,
209
                                // "Heigth_layer_question")), "1000");
210
                                //
211
                                // if (Altura != null) {
212
                                // int h = Integer.parseInt(Altura);
213
                                // if (h >= 0)
214
                                // m_heigth = h;
215
                                // }
216
                                // }
217

    
218
                        } else if (layer instanceof FLyrRasterSE) {
219
                                FLyrRasterSE rasterLayer = (FLyrRasterSE) layer;
220
                                if (rasterLayer.getBandCount() == 1)
221
                                        bCanBeElev = true;
222

    
223
                        }
224

    
225
                        if (m_type == layer3DImage && bCanBeElev) {
226
                                option = JOptionPane.showConfirmDialog(
227
                                                (Component) PluginServices.getMainFrame(),
228
                                                PluginServices
229
                                                                .getText(this, "Elevation_layer_question"),
230
                                                PluginServices.getText(this, "Layer_options"),
231
                                                JOptionPane.YES_NO_OPTION);
232

    
233
                                if (option == JOptionPane.YES_OPTION)
234
                                        m_type = layer3DElevation;
235
                        }
236

    
237
                }
238
        }
239

    
240
        /**
241
         * Initialize the cache name for the layer. It uses the planet type the view projection and the type o coordinates 
242
         * 
243
         * @param planetType
244
         * @param viewProj
245
         * @param geocentricCoordinates
246
         */
247
        public void initCacheName(int planetType, IProjection viewProj,
248
                        int geocentricCoordinates) {
249
                // TODO: use full path of source or service, not just layer name
250

    
251
                String typeStr;
252
                if (planetType == geocentricCoordinates)
253
                        typeStr = "Sph";
254
                else
255
                        typeStr = "Pla" + viewProj.getAbrev();
256

    
257
                if (m_type == layer3DElevation)
258
                        typeStr += "Elev";
259
                else if (m_type == layer3DVector)
260
                        typeStr += "Vect";
261

    
262
                String layerInfo = m_layer.getName();
263
                // TODO Un toggle this comment to use WMS extension
264

    
265
                if (m_layer instanceof FLyrWMS) {
266
                        FLyrWMS wmsLayer = (FLyrWMS) m_layer;
267

    
268
                        // Getting wms layer properties
269
                        HashMap props = wmsLayer.getProperties();
270
                        Vector styles;
271
                        // Getting styles
272
                        styles = (Vector) (props.get("styles"));
273

    
274
                        // Adding styles to cache path
275
                        String layerStyle = "";
276
                        if (styles != null) {
277
                                styles.size();
278
                                for (int i = 0; i < styles.size(); i++) {
279
                                        String ele = (String) styles.get(i);
280
                                        layerStyle += ele;
281
                                }
282
                        }
283

    
284
                        layerInfo = wmsLayer.getHost().toString()
285
                                        + wmsLayer.getLayerQuery() + "_" + layerStyle;
286

    
287
                } else {
288
                        layerInfo = m_layer.getName();
289
                }
290
                /**/
291
                m_cacheName = typeStr + "_" + layerInfo;
292

    
293
                m_cacheName = m_cacheName.replace('/', '_');
294
                m_cacheName = m_cacheName.replace(':', '_');
295
                m_cacheName = m_cacheName.replace('\\', '_');
296
                m_cacheName = m_cacheName.replace('*', '_');
297
                m_cacheName = m_cacheName.replace('<', '_');
298
                m_cacheName = m_cacheName.replace('>', '_');
299
                m_cacheName = m_cacheName.replace('?', '_');
300
                m_cacheName = m_cacheName.replace('"', '_');
301
                m_cacheName = m_cacheName.replace('|', '_');
302

    
303
                // filter strange characters out of the cache name
304
                int iChar;
305
                for (iChar = 0; iChar < m_cacheName.length(); iChar++) {
306
                        char c = m_cacheName.charAt(iChar);
307
                        boolean bIsLow = (c >= 'a') && (c <= 'z');
308
                        boolean bIsUp = (c >= 'A') && (c <= 'Z');
309
                        boolean bIsNum = (c >= '0') && (c <= '9');
310
                        if (!bIsLow && !bIsUp && !bIsNum && c != '_' && c != '.') {
311
                                int newCInt = java.lang.Math.abs((int) (c) - (int) 'A');
312
                                newCInt = (newCInt % 26) + (int) 'A';
313
                                char newC = (char) newCInt;
314
                                m_cacheName = m_cacheName.replace(c, newC);
315
                        }
316
                }
317
        }
318

    
319
        /**
320
         * Return the cacheService of this layer. It depends of the type of the layer.
321
         * 
322
         * @return
323
         */
324
        public CacheService getCacheService() {
325
                return m_cacheService;
326
        }
327

    
328
        /**
329
         * Set the cacheService of this layer. It depends of the type of the layer.
330
         * 
331
         * @param srv
332
         */
333
        public void setCacheService(CacheService srv) {
334
                m_cacheService = srv;
335
        }
336

    
337
        /**
338
         * Set the type of this layer
339
         * 
340
         * @param type
341
         */
342
        public void setType(int type) {
343
                m_type = type;
344
        }
345

    
346
        /**
347
         * Get the type of the layer
348
         * 
349
         * @return
350
         */
351
        public int getType() {
352
                return m_type;
353
        }
354

    
355
        /**
356
         * Set the toc order of this layer
357
         * 
358
         * @param order
359
         */
360
        public void setTocOrder(int order) {
361
                m_tocOrder = order;
362
        }
363

    
364
        /**
365
         * Get the toc order of this layer
366
         * 
367
         * @return
368
         */
369
        public int getTocOrder() {
370
                return m_tocOrder;
371
        }
372

    
373
        /**
374
         * Set the planet order of this layer
375
         * 
376
         * @param order
377
         */
378
        public void setPlanetOrder(int order) {
379
                m_planetOrder = order;
380
        }
381

    
382
        /**
383
         * Get the planet order of this layer
384
         * 
385
         * @return
386
         */
387
        public int getPlanetOrder() {
388
                return m_planetOrder;
389
        }
390

    
391
        /**
392
         * Get the cache name for this layer
393
         * 
394
         * @return
395
         */
396
        public String getCacheName() {
397
                return m_cacheName;
398
        }
399

    
400
        /**
401
         * Verifies that the vector layer's legend matches the one in the cache
402
         * folder
403
         */
404
        public void VerifyLegend(String planetName) {
405

    
406
                
407
                //NotificationManager.addInfo(m_cacheDir, null);
408
                if (m_layer instanceof Classifiable) {
409
                        Classifiable legendLyr = (Classifiable) m_layer;
410
                        String cacheFolder = m_cacheDir + "/" + planetName + "/"
411
                                        + m_cacheName;
412
                        File cacheFolderFile = new File(cacheFolder);
413
                        if (!cacheFolderFile.exists())
414
                                cacheFolderFile.mkdir();
415

    
416
                        String legendFileName = cacheFolder + "/" + "legend.xml";
417
                        File legendFile = new File(legendFileName);
418

    
419
                        if (legendFile.exists()) { // read legend
420
                                NotificationManager.addInfo("el fichero existe", null);
421
                                FileReader reader = null;
422
                                try {
423
                                        reader = new FileReader(legendFile);
424
                                } catch (FileNotFoundException e) {
425
                                        return;
426
                                }
427
                                try {
428
                                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
429
                                        XMLEntity legendXml = new XMLEntity(tag);
430
                                        ILegend legend = LegendFactory.createFromXML(legendXml);
431
                                        if (m_layer instanceof FLyrVect) {
432
                                                ((FLyrVect) m_layer).setLegend((IVectorLegend) legend);
433
                                        }
434

    
435
                                } catch (Exception e) {
436
                                        e.printStackTrace();
437
                                }
438

    
439
                        } else { // write legend
440
                                NotificationManager.addInfo("el fichero no existe", null);
441
                                ILegend legend = legendLyr.getLegend();
442
                                if (legend == null)
443
                                        return;
444

    
445
                                XMLEntity xmlLegend = legend.getXMLEntity();
446

    
447
                                try {
448
                                        FileWriter writer = new FileWriter(legendFileName);
449
                                        Marshaller m = new Marshaller(writer);
450
                                        m.setEncoding("ISO-8859-1");
451
                                        m.marshal(xmlLegend.getXmlTag());
452

    
453
                                } catch (Exception e) {
454
                                        e.printStackTrace();
455
                                }
456
                        }
457
                }
458
        }
459

    
460
        // IPersistance
461

    
462
        public String getClassName() {
463
                return this.getClass().getName();
464
        }
465

    
466
        /* (non-Javadoc)
467
         * @see com.iver.utiles.IPersistence#getXMLEntity()
468
         */
469
        public XMLEntity getXMLEntity() {
470
                XMLEntity xml = new XMLEntity();
471

    
472
                xml.putProperty("type", m_type);
473
                xml.putProperty("order", m_tocOrder);
474
                xml.putProperty("opacity", m_opacity);
475
                xml.putProperty("heigth", m_heigth);
476
                xml.putProperty("cacheName", m_cacheName);
477

    
478
                return xml;
479
        }
480

    
481
        /* (non-Javadoc)
482
         * @see com.iver.utiles.IPersistence#setXMLEntity(com.iver.utiles.XMLEntity)
483
         */
484
        public void setXMLEntity(XMLEntity xml) {
485
                if (xml.contains("type")) {
486
                        m_bChooseType = false;
487
                        m_type = xml.getIntProperty("type");
488
                }
489
                if (xml.contains("order"))
490
                        m_tocOrder = xml.getIntProperty("order");
491
                if (xml.contains("opacity"))
492
                        m_opacity = Float.parseFloat(xml.getStringProperty("opacity"));
493
                if (xml.contains("heigth"))
494
                        m_heigth = Float.parseFloat(xml.getStringProperty("heigth"));
495
                if (xml.contains("cacheName"))
496
                        m_cacheName = xml.getStringProperty("cacheName");
497
        }
498

    
499
        /**
500
         * Return the opacity of this layer
501
         * 
502
         * @return
503
         */
504
        public float getOpacity() {
505
                return m_opacity;
506
        }
507

    
508
        /**
509
         * Set the opacity of this layer
510
         * 
511
         * @param A value between 0 and 1 
512
         */
513
        public void setOpacity(float m_opacity) {
514
                this.m_opacity = m_opacity;
515
        }
516

    
517
        /**
518
         * Get the vertical exaggeration for this layer
519
         * 
520
         * @return
521
         */
522
        public float getVerticalEx() {
523
                return m_verticalEx;
524
        }
525

    
526
        /**
527
         * Get the vertical exaggeration from this layer
528
         * 
529
         * @param ex
530
         */
531
        public void setVerticalEx(float ex) {
532
                m_verticalEx = ex;
533
        }
534

    
535
        /**
536
         * Get the height that this layer has when it draws in 3D
537
         * 
538
         * @return
539
         */
540
        public float getHeigth() {
541
                return m_heigth;
542
        }
543

    
544
        /**
545
         * Set the height that this layer has when it draws in 3D
546
         * 
547
         * @param heigth
548
         */
549
        public void setHeigth(float m_heigth) {
550
                this.m_heigth = m_heigth;
551
        }
552

    
553
        /**
554
         * If the features of this layer have z value this method will return true. In the other case it will return false
555
         * 
556
         * @return
557
         */
558
        public boolean isZEnable() {
559
                return m_Zenable;
560
        }
561

    
562
        /**
563
         * Set the z enable
564
         * 
565
         * @param zenable
566
         */
567
        public void setZEnable(boolean zenable) {
568
                m_Zenable = zenable;
569
        }
570

    
571
        /**
572
         * It is to know if this layer is new. It only works with the OSG layers
573
         * 
574
         * @return
575
         */
576
        public boolean isNewLayerOSG() {
577
                return newLayerOSG;
578
        }
579

    
580
        /**
581
         * Set if this layer is new. It only works with OSG layers.
582
         * 
583
         * @param newLayerOSG
584
         */
585
        public void setNewLayerOSG(boolean newLayerOSG) {
586
                this.newLayerOSG = newLayerOSG;
587
        }
588

    
589
}