Statistics
| Revision:

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

History | View | Annotate | Download (11.7 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
import org.gvsig.osgvp.planets.Planet;
19

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

    
37
public class Layer3DProps implements IPersistence {
38

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

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

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

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

    
67
        private int option;
68

    
69
        private VectorLayerMenu vectorLayerMenu;
70

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

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

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

    
102
        public void setLayer(FLayer layer) {
103

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

    
107
                m_layer = layer;
108

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

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

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

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

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

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

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

    
188
                }
189
        }
190

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

    
194
                String typeStr;
195
                if (planetType == Planet.CoordinateSystemType.GEOCENTRIC)
196
                        typeStr = "Sph";
197
                else
198
                        typeStr = "Pla" + viewProj.getAbrev();
199

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

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

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

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

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

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

    
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
                m_cacheName = m_cacheName.replace('|', '_');
245

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
344
                                XMLEntity xmlLegend = legend.getXMLEntity();
345

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

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

    
359
        // IPersistance
360

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

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

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

    
374
                return xml;
375
        }
376

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

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

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

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

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

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

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

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

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