Statistics
| Revision:

root / trunk / libraries / lib3DMap-share / src / main / java / com / iver / ai2 / gvsig3d / map3d / layers / Layer3DProps.java @ 22630

History | View | Annotate | Download (12 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
public class Layer3DProps implements IPersistence {
37

    
38
        // types of 3D layers
39
        public static final int layer3DImage = 0;
40
        public static final int layer3DElevation = 1;
41
        public static final int layer3DVector = 2;
42
        public static final int layer3DOSG = 3;
43

    
44
        private boolean newLayerOSG = false;
45
        
46
        // public static String m_cacheDir = "c:/data/cache"; // TEST
47
        // Create a .data directorio in user home for caching elements
48
        public static String m_cacheDir = System.getProperty("user.home")
49
                        + "/gvSIG/.data/cache";
50

    
51
        protected int m_tocOrder; // index of layer in TOC (drawing priority)
52
        protected int m_planetOrder; // current stage of layer in planet.
53
        // Can be temporarily different from the TOC order while moving groups of
54
        // layers
55

    
56
        protected float m_opacity = 1.0f; // Opacity of layer default 1.0
57
        protected float m_verticalEx = 10.0f; // Opacity of layer default 1.0
58
        protected float m_heigth = 100; // Opacity of layer default 1.0
59
        protected int m_type; // see type enumeration above
60
        protected String m_cacheName = "default";
61
        protected FLayer m_layer;
62
        protected CacheService m_cacheService;
63
        private boolean m_bChooseType = true;
64

    
65
        private boolean m_Zenable = false;
66

    
67
        // used by FLayers3D (3D group layer) to flag when they are actually hooked
68
        // to TOC or not
69
        private boolean m_hooked = false;
70

    
71
        private int option;
72

    
73
        private VectorLayerMenu vectorLayerMenu;
74

    
75
        public Layer3DProps() {
76
                m_tocOrder = -1; // not set
77
                m_planetOrder = -1;
78
        }
79

    
80
        public static Layer3DProps getLayer3DProps(FLayer layer) {
81
                FLyrDefault baseLayer = (FLyrDefault) layer;
82
                Object propsObj = baseLayer.getProperty("3DLayerExtension");
83
                Layer3DProps props3D = null;
84
                if (propsObj != null) {
85
                        try {
86
                                props3D = (Layer3DProps) propsObj;
87
                        } catch (Exception e) {
88
                                props3D = null;
89
                        }
90
                }
91
                return props3D;
92
        }
93

    
94
        public void setChooseType(boolean bChoose) {
95
                m_bChooseType = bChoose;
96
        }
97

    
98
        public boolean getHooked() {
99
                return m_hooked;
100
        }
101

    
102
        public void setHooked(boolean hooked) {
103
                m_hooked = hooked;
104
        }
105

    
106
        public void setLayer(FLayer layer) {
107

    
108
                if (m_layer == layer)
109
                        return;
110

    
111
                m_layer = layer;
112

    
113
                // find out data type
114
                if (m_bChooseType) {
115
                        m_bChooseType = false;
116
                        m_type = layer3DImage;
117
                        boolean bCanBeElev = false;
118

    
119
                        // TODO Un toggle this comment to use WMS extensions
120

    
121
                        if (layer instanceof FLyrWMS) {
122
                                FLyrWMS wmsLayer = (FLyrWMS) layer;
123
                                String format = wmsLayer.getFormat();
124
                                if (format.regionMatches(true, 0, "image/geotiff", 0, 13)
125
                                                || format.regionMatches(true, 0, "image/tiff", 0, 10))
126
                                        bCanBeElev = true;
127
                        } else if (layer instanceof FLyrWCS) {
128
                                FLyrWCS wcsLayer = (FLyrWCS) layer;
129
                                String format = wcsLayer.getFileFormat();
130
                                Hashtable props = wcsLayer.getProperties();
131
                                String params = (String) props.get("parameter");
132
                                if (format.compareToIgnoreCase("GEOTIFF_INT16") == 0
133
                                                && params.length() == 0)
134
                                        bCanBeElev = true;
135
                        }
136
                        // FEATURES
137
                        else /**/if (layer instanceof ClassifiableVectorial) {
138

    
139
                                if (this.getType() != layer3DOSG) {
140
                                        vectorLayerMenu = new VectorLayerMenu(this, layer.getName());
141
                                        vectorLayerMenu.setModal(true);
142
                                        vectorLayerMenu.pack();
143
                                        vectorLayerMenu.setVisible(true);
144
                                }
145

    
146
                                // PluginServices.getMDIManager().addWindow(vectorLayerMenu);
147

    
148
                                // // choose rasterization of 3D
149
                                // option = JOptionPane.showConfirmDialog(
150
                                // (Component) PluginServices.getMainFrame(),
151
                                // PluginServices
152
                                // .getText(this, "Rasterize_layer_question"),
153
                                // PluginServices.getText(this, "Layer_options"),
154
                                // JOptionPane.YES_NO_OPTION);
155
                                //
156
                                // if (option == JOptionPane.YES_OPTION)
157
                                // m_type = layer3DImage;
158
                                // else
159
                                // m_type = layer3DVector;
160
                                //
161
                                // if (m_type == layer3DVector) {
162
                                // String Altura = JOptionPane.showInputDialog(PluginServices
163
                                // .getText(this, PluginServices.getText(this,
164
                                // "Heigth_layer_question")), "1000");
165
                                //
166
                                // if (Altura != null) {
167
                                // int h = Integer.parseInt(Altura);
168
                                // if (h >= 0)
169
                                // m_heigth = h;
170
                                // }
171
                                // }
172

    
173
                        } else if (layer instanceof FLyrRasterSE) {
174
                                FLyrRasterSE rasterLayer = (FLyrRasterSE) layer;
175
                                if (rasterLayer.getBandCount() == 1)
176
                                        bCanBeElev = true;
177

    
178
                        }
179

    
180
                        if (m_type == layer3DImage && bCanBeElev) {
181
                                option = JOptionPane.showConfirmDialog(
182
                                                (Component) PluginServices.getMainFrame(),
183
                                                PluginServices
184
                                                                .getText(this, "Elevation_layer_question"),
185
                                                PluginServices.getText(this, "Layer_options"),
186
                                                JOptionPane.YES_NO_OPTION);
187

    
188
                                if (option == JOptionPane.YES_OPTION)
189
                                        m_type = layer3DElevation;
190
                        }
191

    
192
                }
193
        }
194

    
195
        public void initCacheName(int planetType, IProjection viewProj,
196
                        int geocentricCoordinates) {
197
                // TODO: use full path of source or service, not just layer name
198

    
199
                String typeStr;
200
                if (planetType == geocentricCoordinates)
201
                        typeStr = "Sph";
202
                else
203
                        typeStr = "Pla" + viewProj.getAbrev();
204

    
205
                if (m_type == layer3DElevation)
206
                        typeStr += "Elev";
207
                else if (m_type == layer3DVector)
208
                        typeStr += "Vect";
209

    
210
                String layerInfo = m_layer.getName();
211
                // TODO Un toggle this comment to use WMS extension
212

    
213
                if (m_layer instanceof FLyrWMS) {
214
                        FLyrWMS wmsLayer = (FLyrWMS) m_layer;
215

    
216
                        // Getting wms layer properties
217
                        HashMap props = wmsLayer.getProperties();
218
                        Vector styles;
219
                        // Getting styles
220
                        styles = (Vector) (props.get("styles"));
221

    
222
                        // Adding styles to cache path
223
                        String layerStyle = "";
224
                        if (styles != null) {
225
                                styles.size();
226
                                for (int i = 0; i < styles.size(); i++) {
227
                                        String ele = (String) styles.get(i);
228
                                        layerStyle += ele;
229
                                }
230
                        }
231

    
232
                        layerInfo = wmsLayer.getHost().toString()
233
                                        + wmsLayer.getLayerQuery() + "_" + layerStyle;
234

    
235
                } else {
236
                        layerInfo = m_layer.getName();
237
                }
238
                /**/
239
                m_cacheName = typeStr + "_" + layerInfo;
240

    
241
                m_cacheName = m_cacheName.replace('/', '_');
242
                m_cacheName = m_cacheName.replace(':', '_');
243
                m_cacheName = m_cacheName.replace('\\', '_');
244
                m_cacheName = m_cacheName.replace('*', '_');
245
                m_cacheName = m_cacheName.replace('<', '_');
246
                m_cacheName = m_cacheName.replace('>', '_');
247
                m_cacheName = m_cacheName.replace('?', '_');
248
                m_cacheName = m_cacheName.replace('"', '_');
249
                m_cacheName = m_cacheName.replace('|', '_');
250

    
251
                // filter strange characters out of the cache name
252
                int iChar;
253
                for (iChar = 0; iChar < m_cacheName.length(); iChar++) {
254
                        char c = m_cacheName.charAt(iChar);
255
                        boolean bIsLow = (c >= 'a') && (c <= 'z');
256
                        boolean bIsUp = (c >= 'A') && (c <= 'Z');
257
                        boolean bIsNum = (c >= '0') && (c <= '9');
258
                        if (!bIsLow && !bIsUp && !bIsNum && c != '_' && c != '.') {
259
                                int newCInt = java.lang.Math.abs((int) (c) - (int) 'A');
260
                                newCInt = (newCInt % 26) + (int) 'A';
261
                                char newC = (char) newCInt;
262
                                m_cacheName = m_cacheName.replace(c, newC);
263
                        }
264
                }
265
        }
266

    
267
        public CacheService getCacheService() {
268
                return m_cacheService;
269
        }
270

    
271
        public void setCacheService(CacheService srv) {
272
                m_cacheService = srv;
273
        }
274

    
275
        public void setType(int type) {
276
                m_type = type;
277
        }
278

    
279
        public int getType() {
280
                return m_type;
281
        }
282

    
283
        public void setTocOrder(int order) {
284
                m_tocOrder = order;
285
        }
286

    
287
        public int getTocOrder() {
288
                return m_tocOrder;
289
        }
290

    
291
        public void setPlanetOrder(int order) {
292
                m_planetOrder = order;
293
        }
294

    
295
        public int getPlanetOrder() {
296
                return m_planetOrder;
297
        }
298

    
299
        public String getCacheName() {
300
                return m_cacheName;
301
        }
302

    
303
        /**
304
         * Verifies that the vector layer's legend matches the one in the cache
305
         * folder
306
         */
307
        public void VerifyLegend(String planetName) {
308

    
309
                NotificationManager.addInfo("Estoy aki", null);
310
                NotificationManager.addInfo(m_cacheDir, null);
311
                if (m_layer instanceof Classifiable) {
312
                        Classifiable legendLyr = (Classifiable) m_layer;
313
                        String cacheFolder = m_cacheDir + "/" + planetName + "/"
314
                                        + m_cacheName;
315
                        File cacheFolderFile = new File(cacheFolder);
316
                        if (!cacheFolderFile.exists())
317
                                cacheFolderFile.mkdir();
318

    
319
                        String legendFileName = cacheFolder + "/" + "legend.xml";
320
                        File legendFile = new File(legendFileName);
321

    
322
                        if (legendFile.exists()) { // read legend
323
                                NotificationManager.addInfo("el fichero existe", null);
324
                                FileReader reader = null;
325
                                try {
326
                                        reader = new FileReader(legendFile);
327
                                } catch (FileNotFoundException e) {
328
                                        return;
329
                                }
330
                                try {
331
                                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
332
                                        XMLEntity legendXml = new XMLEntity(tag);
333
                                        ILegend legend = LegendFactory.createFromXML(legendXml);
334
                                        if (m_layer instanceof FLyrVect) {
335
                                                ((FLyrVect) m_layer).setLegend((IVectorLegend) legend);
336
                                        }
337

    
338
                                } catch (Exception e) {
339
                                        e.printStackTrace();
340
                                }
341

    
342
                        } else { // write legend
343
                                NotificationManager.addInfo("el fichero no existe", null);
344
                                ILegend legend = legendLyr.getLegend();
345
                                if (legend == null)
346
                                        return;
347

    
348
                                XMLEntity xmlLegend = legend.getXMLEntity();
349

    
350
                                try {
351
                                        FileWriter writer = new FileWriter(legendFileName);
352
                                        Marshaller m = new Marshaller(writer);
353
                                        m.setEncoding("ISO-8859-1");
354
                                        m.marshal(xmlLegend.getXmlTag());
355

    
356
                                } catch (Exception e) {
357
                                        e.printStackTrace();
358
                                }
359
                        }
360
                }
361
        }
362

    
363
        // IPersistance
364

    
365
        public String getClassName() {
366
                return this.getClass().getName();
367
        }
368

    
369
        public XMLEntity getXMLEntity() {
370
                XMLEntity xml = new XMLEntity();
371

    
372
                xml.putProperty("type", m_type);
373
                xml.putProperty("order", m_tocOrder);
374
                xml.putProperty("opacity", m_opacity);
375
                xml.putProperty("heigth", m_heigth);
376
                xml.putProperty("cacheName", m_cacheName);
377

    
378
                return xml;
379
        }
380

    
381
        public void setXMLEntity(XMLEntity xml) {
382
                if (xml.contains("type")) {
383
                        m_bChooseType = false;
384
                        m_type = xml.getIntProperty("type");
385
                }
386
                if (xml.contains("order"))
387
                        m_tocOrder = xml.getIntProperty("order");
388
                if (xml.contains("opacity"))
389
                        m_opacity = Float.parseFloat(xml.getStringProperty("opacity"));
390
                if (xml.contains("heigth"))
391
                        m_heigth = Float.parseFloat(xml.getStringProperty("heigth"));
392
                if (xml.contains("cacheName"))
393
                        m_cacheName = xml.getStringProperty("cacheName");
394
        }
395

    
396
        public float getOpacity() {
397
                return m_opacity;
398
        }
399

    
400
        public void setOpacity(float m_opacity) {
401
                this.m_opacity = m_opacity;
402
        }
403

    
404
        public float getVerticalEx() {
405
                return m_verticalEx;
406
        }
407

    
408
        public void setVerticalEx(float ex) {
409
                m_verticalEx = ex;
410
        }
411

    
412
        public float getHeigth() {
413
                return m_heigth;
414
        }
415

    
416
        public void setHeigth(float m_heigth) {
417
                this.m_heigth = m_heigth;
418
        }
419

    
420
        public boolean isZEnable() {
421
                return m_Zenable;
422
        }
423

    
424
        public void setZEnable(boolean zenable) {
425
                m_Zenable = zenable;
426
        }
427

    
428
        public boolean isNewLayerOSG() {
429
                return newLayerOSG;
430
        }
431

    
432
        public void setNewLayerOSG(boolean newLayerOSG) {
433
                this.newLayerOSG = newLayerOSG;
434
        }
435

    
436
}