Statistics
| Revision:

svn-gvsig-desktop / import / ext3D / branches / ext3D_v1.1 / lib3DMap / src / com / iver / ai2 / gvsig3d / map3d / layers / Layer3DProps.java @ 15477

History | View | Annotate | Download (11.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
import es.upv.ai2.libjosg.planets.Planet;
37
import es.upv.ai2.libjosg.planets.Planet.PlanetType;
38

    
39
public class Layer3DProps implements IPersistence {
40

    
41
        // types of 3D layers
42
        public static final int layer3DImage = 0;
43
        public static final int layer3DElevation = 1;
44
        public static final int layer3DVector = 2;
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 layers
54

    
55
        protected float m_opacity = 1.0f; // Opacity of layer default 1.0
56
        protected float m_verticalEx = 10.0f; // Opacity of layer default 1.0
57
        protected float m_heigth = 100; // Opacity of layer default 1.0
58
        protected int m_type; // see type enumeration above
59
        protected String m_cacheName = "default";
60
        protected FLayer m_layer;
61
        protected CacheService m_cacheService;
62
        private boolean m_bChooseType = true;
63
        
64
        private boolean m_Zenable = false;
65
        
66
    // used by FLayers3D (3D group layer) to flag when they are actually hooked to TOC or not
67
        private boolean m_hooked = false; 
68

    
69
        private int option;
70

    
71
        private VectorLayerMenu vectorLayerMenu;
72

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

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

    
92
        public void setChooseType(boolean bChoose) {
93
                m_bChooseType = bChoose;
94
        }
95
        
96
        public boolean getHooked() {
97
                return m_hooked;
98
        }
99
        
100
        public void setHooked(boolean hooked) {
101
                m_hooked = hooked;
102
        }
103

    
104
        public void setLayer(FLayer layer) {
105

    
106
                if (m_layer == layer)
107
                        return;
108

    
109
                m_layer = layer;
110

    
111
                // find out data type
112
                if (m_bChooseType) {
113
                        m_bChooseType = false;
114
                        m_type = layer3DImage;
115
                        boolean bCanBeElev = false;
116
                        
117
                        // TODO Un toggle this comment to use WMS extensions
118

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

    
137
                                vectorLayerMenu = new VectorLayerMenu(this,layer.getName());
138
                                vectorLayerMenu.setModal(true);
139
                                vectorLayerMenu.pack();
140
                                vectorLayerMenu.setVisible(true);
141
                                
142
                                
143
//                                PluginServices.getMDIManager().addWindow(vectorLayerMenu);
144
                                
145

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

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

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

    
186
                                if (option == JOptionPane.YES_OPTION)
187
                                        m_type = layer3DElevation;
188
                        }
189

    
190
                }
191
        }
192

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

    
196
                String typeStr;
197
                if (planetType == PlanetType.SPHERICAL_MODE)
198
                        typeStr = "Sph";
199
                else
200
                        typeStr = "Pla" + viewProj.getAbrev();
201

    
202
                if (m_type == layer3DElevation)
203
                        typeStr += "Elev";
204
                else if (m_type == layer3DVector)
205
                        typeStr += "Vect";
206

    
207
                String layerInfo = m_layer.getName();
208
                // TODO Un toggle this comment to use WMS extension
209
                
210
                if (m_layer instanceof FLyrWMS) {
211
                        FLyrWMS wmsLayer = (FLyrWMS) m_layer;
212

    
213
                        // Getting wms layer properties
214
                        HashMap props = wmsLayer.getProperties();
215
                        Vector styles;
216
                        // Getting styles
217
                        styles = (Vector) (props.get("styles"));
218

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

    
229
                        layerInfo = wmsLayer.getHost().toString()
230
                                        + wmsLayer.getLayerQuery() + "_" + layerStyle;
231

    
232
                } else {
233
                        layerInfo = m_layer.getName();
234
                }
235
                /**/
236
                m_cacheName = typeStr + "_" + layerInfo;
237

    
238
                m_cacheName = m_cacheName.replace('/', '_');
239
                m_cacheName = m_cacheName.replace(':', '_');
240
                m_cacheName = m_cacheName.replace('\\', '_');
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

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

    
264
        public CacheService getCacheService() {
265
                return m_cacheService;
266
        }
267

    
268
        public void setCacheService(CacheService srv) {
269
                m_cacheService = srv;
270
        }
271

    
272
        public void setType(int type) {
273
                m_type = type;
274
        }
275

    
276
        public int getType() {
277
                return m_type;
278
        }
279

    
280
        public void setTocOrder(int order) {
281
                m_tocOrder = order;
282
        }
283

    
284
        public int getTocOrder() {
285
                return m_tocOrder;
286
        }
287
        
288
        public void setPlanetOrder(int order) {
289
                m_planetOrder = order;
290
        }
291

    
292
        public int getPlanetOrder() {
293
                return m_planetOrder;
294
        }
295

    
296
        public String getCacheName() {
297
                return m_cacheName;
298
        }
299

    
300
        /**
301
         * Verifies that the vector layer's legend matches the one in the cache
302
         * folder
303
         */
304
        public void VerifyLegend(Planet planet) {
305

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

    
316
                        String legendFileName = cacheFolder + "/" + "legend.xml";
317
                        File legendFile = new File(legendFileName);
318

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

    
336
                                } catch (Exception e) {
337
                                        e.printStackTrace();
338
                                }
339

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

    
346
                                XMLEntity xmlLegend = legend.getXMLEntity();
347

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

    
354
                                } catch (Exception e) {
355
                                        e.printStackTrace();
356
                                }
357
                        }
358
                }
359
        }
360

    
361
        // IPersistance
362

    
363
        public String getClassName() {
364
                return this.getClass().getName();
365
        }
366

    
367
        public XMLEntity getXMLEntity() {
368
                XMLEntity xml = new XMLEntity();
369

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

    
376
                return xml;
377
        }
378

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

    
394
        public float getOpacity() {
395
                return m_opacity;
396
        }
397

    
398
        public void setOpacity(float m_opacity) {
399
                this.m_opacity = m_opacity;
400
        }
401

    
402
        public float getVerticalEx() {
403
                return m_verticalEx;
404
        }
405

    
406
        public void setVerticalEx(float ex) {
407
                m_verticalEx = ex;
408
        }
409

    
410
        public float getHeigth() {
411
                return m_heigth;
412
        }
413

    
414
        public void setHeigth(float m_heigth) {
415
                this.m_heigth = m_heigth;
416
        }
417

    
418
        public boolean isZEnable() {
419
                return m_Zenable;
420
        }
421

    
422
        public void setZEnable(boolean zenable) {
423
                m_Zenable = zenable;
424
        }
425
}