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

View differences:

ExportTask.java
2 2

  
3 3
import java.io.File;
4 4
import java.io.IOException;
5
import java.util.ArrayList;
6
import java.util.Hashtable;
5 7

  
6 8
import javax.swing.JComponent;
7 9
import javax.swing.JOptionPane;
......
9 11
import org.cresques.cts.IProjection;
10 12
import org.gvsig.fmap.drivers.gpe.reader.GPEDriverFactory;
11 13
import org.gvsig.fmap.drivers.gpe.reader.GPEVectorialDriver;
14
import org.gvsig.fmap.drivers.gpe.utils.GPETypesConversion;
12 15
import org.gvsig.gpe.GPEDefaults;
13 16
import org.gvsig.gpe.GPERegister;
14 17
import org.gvsig.gpe.exceptions.ParserCreationException;
15 18
import org.gvsig.gpe.gml.utils.GMLUtilsParser;
19
import org.gvsig.gpe.kml.utils.KmlCompoundStyle;
20
import org.gvsig.gpe.kml.utils.KmlIconStyle;
21
import org.gvsig.gpe.kml.utils.KmlLabelStyle;
22
import org.gvsig.gpe.kml.utils.KmlLineStyle;
23
import org.gvsig.gpe.kml.utils.KmlPolygonStyle;
24
import org.gvsig.gpe.kml.utils.KmlStyle;
25
import org.gvsig.gpe.kml.writer.GPEKmlWriterHandlerImplementor;
16 26
import org.gvsig.gpe.parser.GPEParser;
17 27
import org.gvsig.gpe.utils.StringUtils;
18 28
import org.gvsig.gpe.writer.GPEWriterHandler;
29
import org.gvsig.symbology.fmap.symbols.PictureMarkerSymbol;
19 30

  
20 31
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
21 32
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
......
26 37
import com.iver.cit.gvsig.ExportTo;
27 38
import com.iver.cit.gvsig.fmap.MapContext;
28 39
import com.iver.cit.gvsig.fmap.core.IGeometry;
40
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
41
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
42
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
43
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
29 44
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
30 45
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
31 46
import com.iver.cit.gvsig.fmap.layers.FBitSet;
......
35 50
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
36 51
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
37 52
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection;
53
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend;
54
import com.iver.cit.gvsig.fmap.rendering.ILegend;
55
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
56
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.AttrInTableLabelingStrategy;
57
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.ILabelingStrategy;
58
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelClass;
59
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.LabelingFactory;
38 60
import com.iver.cit.gvsig.project.documents.view.gui.IView;
39 61
import com.iver.utiles.swing.threads.AbstractMonitorableTask;
40 62

  
......
96 118
	private ExportGeometry eGeometry = null;	
97 119
	private File file = null;
98 120
	
121
	private Hashtable<ISymbol, KmlStyle> symbols = null;
122
	private KmlLabelStyle defaultLabelStyle;
123
	
99 124
	public ExportTask(FLayer layer, 
100 125
			GPEWriterHandler writer,
101 126
			MapContext mapContext,
......
107 132
		this.file = file;
108 133
		eGeometry.setProjOrig(layer.getProjection());
109 134
		if (writer.getFormat().equals("text/xml; subtype=kml/2.1")){
110
			eGeometry.setProjDest(CRSFactory.getCRS("EPSG:4326"));			
135
			eGeometry.setProjDest(CRSFactory.getCRS("EPSG:4326"));	
111 136
		}else{
112 137
			eGeometry.setProjDest(layer.getProjection());
113 138
		}
......
167 192
		String projection = eGeometry.getProjDest().getAbrev();
168 193
		
169 194
		writer.startLayer(null, null, layer.getName(), null, projection);
195
		
196
		if (writer.getWriterHandlerImplementor() instanceof GPEKmlWriterHandlerImplementor) {
197
			GPEKmlWriterHandlerImplementor kmlWriterHandler = (GPEKmlWriterHandlerImplementor) writer.getWriterHandlerImplementor();
198
			KmlStyle[] styles = convertSymbols(layer);
199
			kmlWriterHandler.writeStyles(styles);
200
		}
201
		
170 202
		//Sets the extent
171 203
		try {
172 204
			if (layer.getProjection().getAbrev().compareTo(mapContext.getViewPort().getProjection().getAbrev())==0)
......
186 218
				exportLayer(layers.getLayer(i));
187 219
			}
188 220
		}
221
		
189 222
		writer.endLayer();
190 223
	}
191 224

  
225
	private KmlStyle[] convertSymbols(FLayer layer) {
226
		FLyrVect lyrVect = (FLyrVect) layer;
227
		IVectorLegend legend = (IVectorLegend) lyrVect.getLegend();
228
		ISymbol defaultSym = legend.getDefaultSymbol();
229
		ArrayList<KmlStyle> aux = new ArrayList<KmlStyle>();
230
		KmlStyle defaultStyle = convertSymbol(defaultSym);
231
		defaultStyle.setId("defaultStyle");
232
		aux.add(defaultStyle);
233
		symbols = new Hashtable<ISymbol, KmlStyle>();
234
		symbols.put(defaultSym, defaultStyle);
235
		
236
		// we only support a default labelStyle for all texts
237
		defaultLabelStyle = new KmlLabelStyle();
238
		
239
		if (lyrVect.isLabeled()) {
240
			ILabelingStrategy labelStrategy = lyrVect.getLabelingStrategy();
241
			if (labelStrategy instanceof AttrInTableLabelingStrategy) { 
242
				// we only support simple labels
243
				AttrInTableLabelingStrategy lbs = (AttrInTableLabelingStrategy) labelStrategy;
244
				defaultLabelStyle.setColor(lbs.getFixedColor());
245
				defaultLabelStyle.setId("labelStyle");
246
				aux.add(defaultLabelStyle);
247

  
248
			}
249
		}
250
		if (legend instanceof IClassifiedVectorLegend) {
251
			IClassifiedVectorLegend cLeg = (IClassifiedVectorLegend) legend;
252
			ISymbol[] symbolsAux = cLeg.getSymbols();
253
			for (int i=0; i < symbolsAux.length; i++) {
254
				ISymbol s = symbolsAux[i];
255
				KmlStyle style = convertSymbol(s);
256
				style.setId(i + "");
257
				aux.add(style);
258
				symbols.put(s, style);
259
			}
260
		}
261

  
262
		return aux.toArray(new KmlStyle[0]);
263
	}
264

  
265
	private KmlStyle convertSymbol(ISymbol s) {
266
		KmlStyle style = null;
267
		
268
		if (s instanceof IMarkerSymbol) {
269
			KmlIconStyle icoStyle = new KmlIconStyle();
270
			icoStyle.setColor(((IMarkerSymbol) s).getColor());
271
	
272
			if (s instanceof PictureMarkerSymbol) {
273
				float angKml = (float) GPETypesConversion.RadTokmlDeg(((IMarkerSymbol) s).getRotation());
274
				icoStyle.setHeading(angKml);
275
			
276
				//TODO: CONVERT IMAGES TO HREF... but I don't know how to do it.
277
			}
278
			
279
			style = icoStyle;
280
		}
281
		
282
		if (s instanceof ILineSymbol) {
283
			ILineSymbol lSym = (ILineSymbol) s;
284
			KmlLineStyle lStyle = new KmlLineStyle();
285
			lStyle.setColor(lSym.getColor());
286
			lStyle.setWidth((float) lSym.getLineWidth());
287
			
288
			style = lStyle;
289
	
290
		}
291

  
292
		if (s instanceof IFillSymbol) {
293
			IFillSymbol fSym = (IFillSymbol) s;
294
			KmlCompoundStyle cStyle = new KmlCompoundStyle();
295
			KmlLineStyle lStyle = new KmlLineStyle();
296
			ILineSymbol lSym = fSym.getOutline();
297
			lStyle.setColor(lSym.getColor());
298
			lStyle.setWidth((float) lSym.getLineWidth());
299

  
300
			KmlPolygonStyle fStyle = new KmlPolygonStyle();
301
			fStyle.setColor(fSym.getFillColor());
302
			fStyle.setOutline(fSym.hasOutline());
303
			fStyle.setFill(fSym.hasFill());
304
			
305
			cStyle.setLineStyle(lStyle);
306
			cStyle.setPolygonStyle(fStyle);
307
			
308
			style = cStyle;
309
	
310
		}
311

  
312
		
313
		return style;
314
	}
315

  
192 316
	/**
193 317
	 * It exports the layer information. Geometries if is
194 318
	 * a vectorial layer, images if is a raster layer...
......
251 375
	 */
252 376
	private void exportFeature(SelectableDataSource sds, ReadableVectorial rv, int index, FLayer layer){
253 377
		try {
254
			writer.startFeature(String.valueOf(index), "FEATURE", null);
378

  
379
			int fieldName = sds.getFieldIndexByName("name");
380
			String name = null;
381
			if (fieldName != -1)
382
				name = sds.getFieldValue(index, fieldName).toString();
383
			if (writer.getWriterHandlerImplementor() instanceof GPEKmlWriterHandlerImplementor) {
384
				GPEKmlWriterHandlerImplementor kmlWriterHandler = (GPEKmlWriterHandlerImplementor) writer.getWriterHandlerImplementor();
385
				FLyrVect lyrVect = (FLyrVect) layer;
386
				if (lyrVect.getLabelingStrategy() != null) {
387
					ILabelingStrategy labelStrategy = lyrVect.getLabelingStrategy();
388
					String fieldLabel = labelStrategy.getUsedFields()[0];
389
					int idfieldlabel = sds.getFieldIndexByName(fieldLabel);
390
					name = sds.getFieldValue(index, idfieldlabel).toString();
391
				}
392
			}			
393
			writer.startFeature(String.valueOf(index), "FEATURE", name);
394
			
395
			//Add the attributes
396
			if (writer.getWriterHandlerImplementor() instanceof GPEKmlWriterHandlerImplementor) {
397
				GPEKmlWriterHandlerImplementor kmlWriterHandler = (GPEKmlWriterHandlerImplementor) writer.getWriterHandlerImplementor();
398
				// If KML, we add a fieldName styleUrl in order to export simbology
399
				// If the layer is labelled, we can use the text label as <name> in order to
400
				// allow Google Earth to put labels.
401
				FLyrVect lyrVect = (FLyrVect) layer;
402
				IVectorLegend legend = (IVectorLegend) lyrVect.getLegend();
403
				ISymbol s = legend.getSymbol(index);
404
				KmlStyle style = symbols.get(s);
405
				if (lyrVect.isLabeled()) {
406
					writer.startElement("", "styleUrl",	"#" + defaultLabelStyle.getId());
407
					writer.endElement();					
408
				}
409
				else
410
				{
411
					writer.startElement("", "styleUrl",	"#" + style.getId());
412
					writer.endElement();
413
				}
414
				
415
			}
416

  
255 417
			//Add the geoemtry
256 418
			IGeometry geom = rv.getShape(index);			
257 419
			eGeometry.writeGeometry(geom);
258
			//Add the attributes
420
			
259 421
			Value[] values = sds.getRow(index);
260
			for (int i=0 ; i<values.length ; i++){				
422
			for (int i=0 ; i<values.length ; i++){	
423
				String fldName = StringUtils.replaceAllString(sds.getFieldName(i), " ", "_"); 
261 424
				writer.startElement("", 
262
						StringUtils.replaceAllString(sds.getFieldName(i), " ", "_"),
425
						fldName,
263 426
						values[i].toString());
264 427
				writer.endElement();
265 428
			}

Also available in: Unified diff