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

View differences:

AddFeatureToDriver.java
1 1
package org.gvsig.fmap.drivers.gpe.reader;
2 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

  
3 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;
4 14
import org.gvsig.xmlschema.som.IXSSchemaDocument;
5 15
import org.gvsig.xmlschema.som.IXSTypeDefinition;
6 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

  
7 35
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8 36
 *
9 37
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
......
54 82
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
55 83
 */
56 84
public class AddFeatureToDriver {
85
	
86
	private class Pair {
87
		Value searchFor = null;
88
		Value newValue = null;		
89
	}
57 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>();
58 95
		
59 96
	public AddFeatureToDriver(){
60
		
97
		legend = LegendFactory.createVectorialUniqueValueLegend(FShape.MULTI);
98
		String[] fNames = new String[1];
99
		fNames[0] = "styleUrl";
100
		legend.setClassifyingFieldNames(fNames);
101
		legend.useDefaultSymbol(true);
61 102
	}
62 103

  
63 104
	/**
......
70 111
	public void addFeatureToLayer(GPEVectorialDriver driver, GPEFeature feature){
71 112
		addFeatureToLayer_(driver, feature);		
72 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
						}						
73 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

  
74 290
	/**
75 291
	 * Add a feature to a layer
76 292
	 * @param layer

Also available in: Unified diff