Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / lib3DMap-share / src / main / java / org / gvsig / gvsig3d / map3d / layers / Layer3DProps.java @ 21936

History | View | Annotate | Download (11.7 KB)

1
package org.gvsig.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
import org.gvsig.gvsig3d.gui.VectorLayerMenu;
19

    
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

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

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

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

    
66
        private int option;
67

    
68
        private VectorLayerMenu vectorLayerMenu;
69

    
70
        public Layer3DProps() {
71
                m_tocOrder = -1; // not set
72
                m_planetOrder = -1;
73
        }
74

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

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

    
101
        public void setLayer(FLayer layer) {
102

    
103
                if (m_layer == layer)
104
                        return;
105

    
106
                m_layer = layer;
107

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

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

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

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

    
168
                        } else if (layer instanceof FLyrRasterSE) {
169
                                FLyrRasterSE rasterLayer = (FLyrRasterSE) layer;
170
                                if (rasterLayer.getBandCount() == 1)
171
                                        bCanBeElev = true;
172
                                
173
                        }
174

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

    
183
                                if (option == JOptionPane.YES_OPTION)
184
                                        m_type = layer3DElevation;
185
                        }
186

    
187
                }
188
        }
189

    
190
        public void initCacheName(int planetType, IProjection viewProj,int geocentricCoordinates) {
191
                // TODO: use full path of source or service, not just layer name
192

    
193
                String typeStr;
194
                if (planetType == geocentricCoordinates)
195
                        typeStr = "Sph";
196
                else
197
                        typeStr = "Pla" + viewProj.getAbrev();
198

    
199
                if (m_type == layer3DElevation)
200
                        typeStr += "Elev";
201
                else if (m_type == layer3DVector)
202
                        typeStr += "Vect";
203

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

    
210
                        // Getting wms layer properties
211
                        HashMap props = wmsLayer.getProperties();
212
                        Vector styles;
213
                        // Getting styles
214
                        styles = (Vector) (props.get("styles"));
215

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

    
226
                        layerInfo = wmsLayer.getHost().toString()
227
                                        + wmsLayer.getLayerQuery() + "_" + layerStyle;
228

    
229
                } else {
230
                        layerInfo = m_layer.getName();
231
                }
232
                /**/
233
                m_cacheName = typeStr + "_" + layerInfo;
234

    
235
                m_cacheName = m_cacheName.replace('/', '_');
236
                m_cacheName = m_cacheName.replace(':', '_');
237
                m_cacheName = m_cacheName.replace('\\', '_');
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

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

    
261
        public CacheService getCacheService() {
262
                return m_cacheService;
263
        }
264

    
265
        public void setCacheService(CacheService srv) {
266
                m_cacheService = srv;
267
        }
268

    
269
        public void setType(int type) {
270
                m_type = type;
271
        }
272

    
273
        public int getType() {
274
                return m_type;
275
        }
276

    
277
        public void setTocOrder(int order) {
278
                m_tocOrder = order;
279
        }
280

    
281
        public int getTocOrder() {
282
                return m_tocOrder;
283
        }
284
        
285
        public void setPlanetOrder(int order) {
286
                m_planetOrder = order;
287
        }
288

    
289
        public int getPlanetOrder() {
290
                return m_planetOrder;
291
        }
292

    
293
        public String getCacheName() {
294
                return m_cacheName;
295
        }
296

    
297
        /**
298
         * Verifies that the vector layer's legend matches the one in the cache
299
         * folder
300
         */
301
        public void VerifyLegend(String planetName) {
302

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

    
313
                        String legendFileName = cacheFolder + "/" + "legend.xml";
314
                        File legendFile = new File(legendFileName);
315

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

    
333
                                } catch (Exception e) {
334
                                        e.printStackTrace();
335
                                }
336

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

    
343
                                XMLEntity xmlLegend = legend.getXMLEntity();
344

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

    
351
                                } catch (Exception e) {
352
                                        e.printStackTrace();
353
                                }
354
                        }
355
                }
356
        }
357

    
358
        // IPersistance
359

    
360
        public String getClassName() {
361
                return this.getClass().getName();
362
        }
363

    
364
        public XMLEntity getXMLEntity() {
365
                XMLEntity xml = new XMLEntity();
366

    
367
                xml.putProperty("type", m_type);
368
                xml.putProperty("order", m_tocOrder);
369
                xml.putProperty("opacity", m_opacity);
370
                xml.putProperty("heigth", m_heigth);
371
                xml.putProperty("cacheName", m_cacheName);
372

    
373
                return xml;
374
        }
375

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

    
391
        public float getOpacity() {
392
                return m_opacity;
393
        }
394

    
395
        public void setOpacity(float m_opacity) {
396
                this.m_opacity = m_opacity;
397
        }
398

    
399
        public float getVerticalEx() {
400
                return m_verticalEx;
401
        }
402

    
403
        public void setVerticalEx(float ex) {
404
                m_verticalEx = ex;
405
        }
406

    
407
        public float getHeigth() {
408
                return m_heigth;
409
        }
410

    
411
        public void setHeigth(float m_heigth) {
412
                this.m_heigth = m_heigth;
413
        }
414

    
415
        public boolean isZEnable() {
416
                return m_Zenable;
417
        }
418

    
419
        public void setZEnable(boolean zenable) {
420
                m_Zenable = zenable;
421
        }
422

    
423
}