svn-gvsig-desktop / trunk / extensions / extGPE-gvSIG / src / org / gvsig / fmap / drivers / gpe / writer / ExportTask.java @ 37960
History | View | Annotate | Download (14.5 KB)
1 |
package org.gvsig.fmap.drivers.gpe.writer; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Hashtable; |
7 |
|
8 |
import javax.swing.JComponent; |
9 |
import javax.swing.JOptionPane; |
10 |
|
11 |
import org.cresques.cts.IProjection; |
12 |
import org.gvsig.fmap.drivers.gpe.reader.GPEDriverFactory; |
13 |
import org.gvsig.fmap.drivers.gpe.reader.GPEVectorialDriver; |
14 |
import org.gvsig.fmap.drivers.gpe.utils.GPETypesConversion; |
15 |
import org.gvsig.gpe.GPEDefaults; |
16 |
import org.gvsig.gpe.GPERegister; |
17 |
import org.gvsig.gpe.exceptions.ParserCreationException; |
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; |
26 |
import org.gvsig.gpe.parser.GPEParser; |
27 |
import org.gvsig.gpe.utils.StringUtils; |
28 |
import org.gvsig.gpe.writer.GPEWriterHandler; |
29 |
import org.gvsig.symbology.fmap.symbols.PictureMarkerSymbol; |
30 |
|
31 |
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException; |
32 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
33 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
34 |
import com.hardcode.gdbms.engine.values.Value; |
35 |
import com.iver.andami.PluginServices; |
36 |
import com.iver.andami.messages.NotificationManager; |
37 |
import com.iver.cit.gvsig.ExportTo; |
38 |
import com.iver.cit.gvsig.fmap.MapContext; |
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; |
44 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
45 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
46 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
47 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
48 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
49 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
50 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
51 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
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; |
60 |
import com.iver.cit.gvsig.project.documents.view.gui.IView; |
61 |
import com.iver.utiles.swing.threads.AbstractMonitorableTask; |
62 |
|
63 |
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
64 |
*
|
65 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
66 |
*
|
67 |
* This program is free software; you can redistribute it and/or
|
68 |
* modify it under the terms of the GNU General Public License
|
69 |
* as published by the Free Software Foundation; either version 2
|
70 |
* of the License, or (at your option) any later version.
|
71 |
*
|
72 |
* This program is distributed in the hope that it will be useful,
|
73 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
74 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
75 |
* GNU General Public License for more details.
|
76 |
*
|
77 |
* You should have received a copy of the GNU General Public License
|
78 |
* along with this program; if not, write to the Free Software
|
79 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
80 |
*
|
81 |
* For more information, contact:
|
82 |
*
|
83 |
* Generalitat Valenciana
|
84 |
* Conselleria d'Infraestructures i Transport
|
85 |
* Av. Blasco Ib��ez, 50
|
86 |
* 46010 VALENCIA
|
87 |
* SPAIN
|
88 |
*
|
89 |
* +34 963862235
|
90 |
* gvsig@gva.es
|
91 |
* www.gvsig.gva.es
|
92 |
*
|
93 |
* or
|
94 |
*
|
95 |
* IVER T.I. S.A
|
96 |
* Salamanca 50
|
97 |
* 46005 Valencia
|
98 |
* Spain
|
99 |
*
|
100 |
* +34 963163400
|
101 |
* dac@iver.es
|
102 |
*/
|
103 |
/* CVS MESSAGES:
|
104 |
*
|
105 |
* $Id$
|
106 |
* $Log$
|
107 |
*
|
108 |
*/
|
109 |
/**
|
110 |
* This class writes a gvSIG layer and its children
|
111 |
* (if the driver supports a layer with children)
|
112 |
* @author Jorge Piera LLodr� (jorge.piera@iver.es)
|
113 |
*/
|
114 |
public class ExportTask extends AbstractMonitorableTask{ |
115 |
private FLayer rootLayer = null; |
116 |
private GPEWriterHandler writer = null; |
117 |
private MapContext mapContext = null; |
118 |
private ExportGeometry eGeometry = null; |
119 |
private File file = null; |
120 |
|
121 |
private Hashtable<ISymbol, KmlStyle> symbols = null; |
122 |
private KmlLabelStyle defaultLabelStyle;
|
123 |
|
124 |
public ExportTask(FLayer layer,
|
125 |
GPEWriterHandler writer, |
126 |
MapContext mapContext, |
127 |
File file){
|
128 |
this.rootLayer = layer;
|
129 |
this.writer = writer;
|
130 |
this.mapContext = mapContext;
|
131 |
this.eGeometry = new ExportGeometry(writer); |
132 |
this.file = file;
|
133 |
eGeometry.setProjOrig(layer.getProjection()); |
134 |
if (writer.getFormat().equals("text/xml; subtype=kml/2.1")){ |
135 |
eGeometry.setProjDest(CRSFactory.getCRS("EPSG:4326"));
|
136 |
}else{
|
137 |
eGeometry.setProjDest(layer.getProjection()); |
138 |
} |
139 |
setInitialStep(0);
|
140 |
setDeterminatedProcess(true);
|
141 |
setStatusMessage(PluginServices.getText(this, "gpe_exporting")); |
142 |
} |
143 |
|
144 |
public void run() throws Exception { |
145 |
writer.initialize(); |
146 |
exportLayer(rootLayer); |
147 |
writer.close(); |
148 |
if (isFileLoaded()){
|
149 |
loadExportedFile(); |
150 |
} |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Load the exported file
|
155 |
* @throws GPEParserCreationException
|
156 |
* @throws IOException
|
157 |
*/
|
158 |
private void loadExportedFile() throws ParserCreationException, IOException{ |
159 |
GPEParser parser = GPERegister.createParser(file.toURI()); |
160 |
GPEVectorialDriver driver = GPEDriverFactory.createDriver(parser); |
161 |
driver.open(file); |
162 |
IProjection proj = driver.getProjection(); |
163 |
if(proj == null) |
164 |
proj = eGeometry.getProjDest(); |
165 |
FLayer layer = LayerFactory.createLayer(file.getAbsolutePath(), |
166 |
driver, proj); |
167 |
((IView)PluginServices.getMDIManager().getActiveWindow()). |
168 |
getMapControl().getMapContext().getLayers().addLayer(layer); |
169 |
} |
170 |
|
171 |
/**
|
172 |
* @return true if the exported files have to be loaded
|
173 |
*/
|
174 |
private boolean isFileLoaded(){ |
175 |
int load = JOptionPane.showConfirmDialog( |
176 |
(JComponent) PluginServices.getMDIManager().getActiveWindow()
|
177 |
, PluginServices.getText(this, "insertar_en_la_vista_la_capa_creada"), |
178 |
PluginServices.getText(this,"insertar_capa"), |
179 |
JOptionPane.YES_NO_OPTION);
|
180 |
if (load == JOptionPane.YES_OPTION){ |
181 |
return true; |
182 |
} |
183 |
return false; |
184 |
} |
185 |
|
186 |
/**
|
187 |
* It writes a layer and its children recursively
|
188 |
* @param layer
|
189 |
* The layer to write
|
190 |
*/
|
191 |
private void exportLayer(FLayer layer){ |
192 |
String projection = eGeometry.getProjDest().getAbrev();
|
193 |
|
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 |
|
202 |
//Sets the extent
|
203 |
try {
|
204 |
if (layer.getProjection().getAbrev().compareTo(mapContext.getViewPort().getProjection().getAbrev())==0) |
205 |
writer.startBbox(null,new CoordinatesSequenceBbox(eGeometry.getExtent(layer.getFullExtent())), eGeometry.getProjDest().getAbrev()); |
206 |
else
|
207 |
writer.startBbox(null,new CoordinatesSequenceBbox(layer.getFullExtent()),eGeometry.getProjDest().getAbrev()); |
208 |
writer.endBbox(); |
209 |
} catch (Exception e) { |
210 |
writer.getErrorHandler().addWarning(new ExtentExportWarning(layer,e));
|
211 |
} |
212 |
//Export the layer information
|
213 |
exportLayerInfo(layer); |
214 |
//If the layer has children...
|
215 |
if (layer instanceof LayerCollection){ |
216 |
LayerCollection layers = (LayerCollection)layer; |
217 |
for (int i=0 ; i<layers.getLayersCount() ; i++){ |
218 |
exportLayer(layers.getLayer(i)); |
219 |
} |
220 |
} |
221 |
|
222 |
writer.endLayer(); |
223 |
} |
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 |
|
316 |
/**
|
317 |
* It exports the layer information. Geometries if is
|
318 |
* a vectorial layer, images if is a raster layer...
|
319 |
* @param layer
|
320 |
*/
|
321 |
private void exportLayerInfo(FLayer layer){ |
322 |
try {
|
323 |
if (layer instanceof FLyrVect){ |
324 |
exportVectorialLayer((FLyrVect)layer); |
325 |
} |
326 |
} catch (Exception e) { |
327 |
writer.getErrorHandler().addError(e); |
328 |
} |
329 |
} |
330 |
|
331 |
/**
|
332 |
* Export the geometries of a vectorial layer
|
333 |
* @param layer
|
334 |
* @throws DriverException
|
335 |
* @throws DriverIOException
|
336 |
* @throws ReadDriverException
|
337 |
* @throws InitializeDriverException
|
338 |
* @throws ReadDriverException
|
339 |
*/
|
340 |
private void exportVectorialLayer(FLyrVect layer) throws DriverException, DriverIOException, InitializeDriverException, ReadDriverException { |
341 |
System.out.println(layer.getName());
|
342 |
ReadableVectorial rv = layer.getSource(); |
343 |
SelectableDataSource sds = layer.getRecordset(); |
344 |
rv.start(); |
345 |
//If there is a selection the rows to export have to be
|
346 |
//the selected rows
|
347 |
FBitSet bitSet = sds.getSelection(); |
348 |
int rowCount;
|
349 |
if (bitSet.cardinality() == 0){ |
350 |
rowCount = rv.getShapeCount(); |
351 |
for (int i = 0; i < rowCount; i++) { |
352 |
exportFeature(sds, rv, i, layer); |
353 |
} |
354 |
}else{
|
355 |
rowCount = bitSet.cardinality(); |
356 |
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet |
357 |
.nextSetBit(i + 1)) {
|
358 |
exportFeature(sds, rv, i, layer); |
359 |
} |
360 |
} |
361 |
rv.stop(); |
362 |
} |
363 |
|
364 |
/**
|
365 |
* It writes a feature (geometry + attributes)
|
366 |
* @param sds
|
367 |
* The selectable datasource to get the attributes
|
368 |
* @param rv
|
369 |
* The readable vectorial to get the geoemtries
|
370 |
* @param index
|
371 |
* The feature index
|
372 |
* @param layer
|
373 |
* Only to personalize the exceptions
|
374 |
* @throws ReadDriverException
|
375 |
*/
|
376 |
private void exportFeature(SelectableDataSource sds, ReadableVectorial rv, int index, FLayer layer){ |
377 |
try {
|
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 |
|
417 |
//Add the geoemtry
|
418 |
IGeometry geom = rv.getShape(index); |
419 |
eGeometry.writeGeometry(geom); |
420 |
|
421 |
Value[] values = sds.getRow(index);
|
422 |
for (int i=0 ; i<values.length ; i++){ |
423 |
String fldName = StringUtils.replaceAllString(sds.getFieldName(i), " ", "_"); |
424 |
writer.startElement("",
|
425 |
fldName, |
426 |
values[i].toString()); |
427 |
writer.endElement(); |
428 |
} |
429 |
writer.endFeature(); |
430 |
} catch (Exception e) { |
431 |
writer.getErrorHandler().addError(new FeatureExportException(layer,index,e));
|
432 |
} |
433 |
} |
434 |
|
435 |
/* (non-Javadoc)
|
436 |
* @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
|
437 |
*/
|
438 |
public void finished() { |
439 |
try {
|
440 |
ExportTo.executeCommand((FLyrVect)rootLayer); |
441 |
} catch (Exception e) { |
442 |
NotificationManager.addError(e); |
443 |
} |
444 |
} |
445 |
} |