Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGPE-gvSIG / src / org / gvsig / fmap / drivers / gpe / reader / AddFeatureToDriver.java @ 37960

History | View | Annotate | Download (10.6 KB)

1
package org.gvsig.fmap.drivers.gpe.reader;
2

    
3
import java.awt.Color;
4
import java.io.IOException;
5
import java.net.MalformedURLException;
6
import java.net.URI;
7
import java.net.URISyntaxException;
8
import java.util.ArrayList;
9

    
10
import org.gvsig.fmap.drivers.gpe.model.GPEFeature;
11
import org.gvsig.fmap.drivers.gpe.model.GPEMetadata;
12
import org.gvsig.fmap.drivers.gpe.utils.GPETypesConversion;
13
import org.gvsig.symbology.fmap.symbols.PictureMarkerSymbol;
14
import org.gvsig.xmlschema.som.IXSSchemaDocument;
15
import org.gvsig.xmlschema.som.IXSTypeDefinition;
16

    
17
import com.hardcode.gdbms.engine.values.StringValue;
18
import com.hardcode.gdbms.engine.values.Value;
19
import com.hardcode.gdbms.engine.values.ValueFactory;
20
import com.iver.cit.gvsig.fmap.core.FShape;
21
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
22
import com.iver.cit.gvsig.fmap.core.symbols.AbstractMarkerSymbol;
23
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
24
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
25
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
26
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
27
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
28
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
29
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
30
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
31
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
32
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
33
import com.iver.cit.gvsig.project.documents.view.legend.gui.AttrInTableLabeling;
34

    
35
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
36
 *
37
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
38
 *
39
 * This program is free software; you can redistribute it and/or
40
 * modify it under the terms of the GNU General Public License
41
 * as published by the Free Software Foundation; either version 2
42
 * of the License, or (at your option) any later version.
43
 *
44
 * This program is distributed in the hope that it will be useful,
45
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47
 * GNU General Public License for more details.
48
 *
49
 * You should have received a copy of the GNU General Public License
50
 * along with this program; if not, write to the Free Software
51
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
52
 *
53
 * For more information, contact:
54
 *
55
 *  Generalitat Valenciana
56
 *   Conselleria d'Infraestructures i Transport
57
 *   Av. Blasco Ib??ez, 50
58
 *   46010 VALENCIA
59
 *   SPAIN
60
 *
61
 *      +34 963862235
62
 *   gvsig@gva.es
63
 *      www.gvsig.gva.es
64
 *
65
 *    or
66
 *
67
 *   IVER T.I. S.A
68
 *   Salamanca 50
69
 *   46005 Valencia
70
 *   Spain
71
 *
72
 *   +34 963163400
73
 *   dac@iver.es
74
 */
75
/* CVS MESSAGES:
76
 *
77
 * $Id$
78
 * $Log$
79
 *
80
 */
81
/**
82
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
83
 */
84
public class AddFeatureToDriver {
85
        
86
        private class Pair {
87
                Value searchFor = null;
88
                Value newValue = null;                
89
        }
90
        private IXSSchemaDocument schema = null;
91
        
92
        protected VectorialUniqueValueLegend legend = null;
93
        protected AttrInTableLabeling labeling = new AttrInTableLabeling();
94
        protected ArrayList<Pair> valuesToSubstitute = new ArrayList<Pair>();
95
                
96
        public AddFeatureToDriver(){
97
                legend = LegendFactory.createVectorialUniqueValueLegend(FShape.MULTI);
98
                String[] fNames = new String[1];
99
                fNames[0] = "styleUrl";
100
                legend.setClassifyingFieldNames(fNames);
101
                legend.useDefaultSymbol(true);
102
        }
103

    
104
        /**
105
         * Add a feature to a layer
106
         * @param driver
107
         * The driver
108
         * @param feature
109
         * The feature to add
110
         */
111
        public void addFeatureToLayer(GPEVectorialDriver driver, GPEFeature feature){
112
                addFeatureToLayer_(driver, feature);                
113
        }
114
        
115
        public void addMetadataToLayer(GPEVectorialDriver driver, GPEMetadata metadata) {
116
                // Exploramos los metadatos y recuperamos lo necesario para crear la leyenda.
117
//                System.out.println("A crear una leyenda usando metadatos!! " + metadata.getTagType() 
118
//                                + "-" + metadata.getTagData());
119
                String id = metadata.getTagData();
120
                if (metadata.getTagType().equalsIgnoreCase("STYLEMAP")) {
121
                        // Recuperamos el primer styleId y ese es el s?mbolo que sustituiremos cuando nos
122
                        // encontremos el styleUrl de este styleMap
123
                        GPEMetadata firstPair = metadata.getElementAt(0);
124
                        for (int i=0; i < firstPair.getDataList().size(); i++) {
125
                                GPEMetadata aux = firstPair.getElementAt(i);
126
                                String type = aux.getTagType();
127
//                                System.out.println("STYLEMAP: " + type);
128
                                if (type.equalsIgnoreCase("STYLEURL")) {
129
                                        String styleId = aux.getTagData(); 
130
//                                        System.out.println("STYLEMAP: Sustituir " + styleId + " por " + id);
131
                                        Value valStyleId = ValueFactory.createValue(styleId);
132
                                        Pair pair = new Pair();
133
                                        pair.searchFor = valStyleId;
134
                                        pair.newValue = ValueFactory.createValue("#" + id);;
135
                                        valuesToSubstitute.add(pair);                                        
136
                                }                                
137
                        }
138
                        return;
139
                }
140
                
141
                MultiShapeSymbol multiSymbol = new MultiShapeSymbol();
142
                boolean bError = false;
143
                SimpleLineSymbol lineSym = null;
144
                for (int i=0; i < metadata.getDataList().size(); i++) {
145
                        GPEMetadata style = metadata.getElementAt(i);
146
                        String type = style.getTagType();                
147
                        if (type.equalsIgnoreCase("IconStyle")) {
148
                                IMarkerSymbol pointSym = new SimpleMarkerSymbol(); 
149
                                pointSym.setSize(10);
150
                                pointSym.setUnit(-1); // pixels
151
                                
152
                                for (int j=0; j < style.getDataList().size(); j++) {
153
                                        GPEMetadata att = style.getElementAt(j);
154
                                        if (att.getTagType().equalsIgnoreCase("COLOR")) {
155
                                                // format: OPACITY-GG-BB-RR
156
                                                Color color = GPETypesConversion.fromABGRtoColor(att.getTagData());
157
                                                pointSym.setColor(color);
158
                                        }
159
                                        if (att.getTagType().equalsIgnoreCase("ICON")) {
160
                                                if (att.getDataList().size() == 0) {
161
                                                        // TODO: Se adjunta el icono, no es una referencia. Creamos un pointSym por defecto y a por otro.
162
                                                        continue;
163
                                                }
164
                                                GPEMetadata iconData = att.getElementAt(0);
165
                                                try {
166
                                                        URI uri = new URI(iconData.getTagData());
167
                                                        pointSym = new PictureMarkerSymbol(uri.toURL(), uri.toURL());
168
                                                        pointSym.setSize(12);
169
                                                        pointSym.setUnit(-1);
170
                                                } catch (URISyntaxException e) {
171
                                                        e.printStackTrace();
172
                                                        bError = true;
173
                                                } catch (MalformedURLException e) {
174
                                                        e.printStackTrace(); 
175
                                                        bError = true;
176
                                                } catch (IOException e) {
177
                                                        e.printStackTrace();
178
                                                        bError = true;
179
                                                }                                                
180

    
181
                                        }
182

    
183
                                        if (att.getTagType().equalsIgnoreCase("HEADING")) {
184
                                                // Ojo: 0? => N, 90=>E, 180=>S, 270=W
185
                                                // TODO: Convertir anguloKmlToRad
186
                                                double angleDegree = Double.parseDouble(att.getTagData());
187
                                                double rad = GPETypesConversion.kmlDegToRad(angleDegree);
188
                                                ((AbstractMarkerSymbol)pointSym).setRotation(rad);
189
                                        }
190

    
191
                                } // for
192
                                multiSymbol.setMarkerSymbol(pointSym);
193
                        } // IconStyle
194
                        
195
                        else if (type.equalsIgnoreCase("LineStyle")) {
196
                                lineSym = new SimpleLineSymbol(); 
197
                                for (int j=0; j < style.getDataList().size(); j++) {
198
                                        GPEMetadata att = style.getElementAt(j);
199
                                        if (att.getTagType().equalsIgnoreCase("COLOR")) {
200
                                                // format: OPACITY-GG-BB-RR
201
                                                Color color = GPETypesConversion.fromABGRtoColor(att.getTagData());
202
                                                lineSym.setLineColor(color);
203
                                        }
204
                                        if (att.getTagType().equalsIgnoreCase("WIDTH")) {
205
                                                float width = Float.parseFloat(att.getTagData());
206
                                                lineSym.setLineWidth(width);
207
                                        }
208
                                } // for
209
                                multiSymbol.setLineSymbol(lineSym);
210
                        } // LineStyle
211
                        else if (type.equalsIgnoreCase("PolyStyle")) {
212
                                SimpleFillSymbol polySym = new SimpleFillSymbol(); 
213
                                for (int j=0; j < style.getDataList().size(); j++) {
214
                                        GPEMetadata att = style.getElementAt(j);
215
                                        if (att.getTagType().equalsIgnoreCase("COLOR")) {
216
                                                // format: OPACITY-GG-BB-RR
217
                                                Color color = GPETypesConversion.fromABGRtoColor(att.getTagData());
218
                                                polySym.setFillColor(color);
219
                                        }
220
                                        if (att.getTagType().equalsIgnoreCase("FILL")) {
221
                                                int fill = Integer.parseInt(att.getTagData());
222
                                                if (fill == 1)
223
                                                        polySym.setHasFill(true);
224
                                                else
225
                                                        polySym.setHasFill(false);
226

    
227
                                        }
228
                                        if (att.getTagType().equalsIgnoreCase("OUTLINE")) {
229
                                                int outline = Integer.parseInt(att.getTagData());
230
                                                if (outline == 1) {
231
                                                        polySym.setHasOutline(true);
232
                                                        if (polySym.getOutline() == null) {
233
                                                                if (lineSym != null) {
234
                                                                        polySym.setOutline(lineSym);
235
                                                                }
236
                                                                else
237
                                                                {
238
                                                                        polySym.setOutline(new SimpleLineSymbol());
239
                                                                }
240
                                                        }
241
                                                }
242
                                                else
243
                                                        polySym.setHasOutline(false);
244
                                        }
245

    
246
                                        
247
                                } // for
248
                                multiSymbol.setFillSymbol(polySym);
249
                        } // PolyStyle
250
                } // for (no podemos fijar el simbolo dentro del for porque no se pueden a?adir claves repetidas)
251
                StringValue key = ValueFactory.createValue("#" + id);
252
                legend.addSymbol(key, multiSymbol);
253
                
254
                if (driver instanceof KMLVectorialDriver) {
255
                        // Tenemos que hacer un chequeo completo cada vez porque puede llegar un styleMap
256
                        // que hace referencia a un s?mbolo que todav?a no se ha creado.
257
                        Object[] values = legend.getValues();
258
                        ISymbol[] auxSymbols = legend.getSymbols();
259
                        for (int iVal = 0; iVal < valuesToSubstitute.size(); iVal++) {
260
                                Pair pair = valuesToSubstitute.get(iVal);
261
                                Value valStyleId = pair.searchFor;
262
//                                System.out.println("Busco " + valStyleId);
263
                                for (int iSym = 0; iSym < values.length; iSym++) {
264
                                        Value idSym = (Value) values[iSym];
265
                                        if (idSym.doEquals(valStyleId)) {                                                
266
                                                legend.delSymbol(idSym);
267
                                                legend.addSymbol(pair.newValue, auxSymbols[iSym]);
268
                                        }
269
                                }
270
                        }
271

    
272
                        KMLVectorialDriver kmlDriver = (KMLVectorialDriver) driver;
273
                        kmlDriver.setDefaultLegend(legend);
274
                        if (bError) {
275
                                SingleSymbolLegend auxLegend = new SingleSymbolLegend(); 
276
                                kmlDriver.setDefaultLegend(auxLegend);
277
                        }
278
                        if (legend.getSymbols().length ==1) {
279
                                ((IVectorLegend) kmlDriver.getDefaultLegend()).setDefaultSymbol(multiSymbol);
280
                        }
281
                        else {
282
                                MultiShapeSymbol multiSymbolAux = new MultiShapeSymbol();
283
                                ((IVectorLegend) kmlDriver.getDefaultLegend()).setDefaultSymbol(multiSymbolAux);
284
                        }
285

    
286
                }
287
                
288
        }
289

    
290
        /**
291
         * Add a feature to a layer
292
         * @param layer
293
         * The layer
294
         * @param feature
295
         * The feature to add
296
         */
297
        private void addFeatureToLayer_(GPEVectorialDriver driver, GPEFeature feature){
298
                IXSTypeDefinition elementType = null;
299
                //If the feature has a type it will try to retrieve it
300
//                if (feature.getTypeName() != null){
301
//                        elementType = schema.getTypeByName(feature.getTypeName());
302
//                }
303
                //If the type exists in the schema
304
                if (elementType != null){
305
                        //layer.addFeature(feature, elementType);
306
                        driver.addFeature(feature);
307
                        //If the type doesn't exist in the XML schema
308
                }else{
309
                        driver.addFeature(feature);
310
                }                
311
        }
312

    
313
        /**
314
         * @return the schema
315
         */
316
        public IXSSchemaDocument getSchema() {
317
                return schema;
318
        }
319

    
320
        /**
321
         * @param schema the schema to set
322
         */
323
        public void setSchema(IXSSchemaDocument schema) {
324
                this.schema = schema;
325
        }
326

    
327

    
328

    
329
}