svn-gvsig-desktop / branches / v10 / extensions / extGPE-gvSIG / src / org / gvsig / fmap / drivers / gpe / writer / ExportTask.java @ 18292
History | View | Annotate | Download (6.19 KB)
1 |
package org.gvsig.fmap.drivers.gpe.writer; |
---|---|
2 |
|
3 |
import java.awt.geom.Rectangle2D; |
4 |
|
5 |
import org.gvsig.gpe.writers.GPEWriterHandler; |
6 |
|
7 |
import com.hardcode.gdbms.engine.values.Value; |
8 |
import com.iver.andami.PluginServices; |
9 |
import com.iver.cit.gvsig.fmap.DriverException; |
10 |
import com.iver.cit.gvsig.fmap.MapContext; |
11 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
12 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
13 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
14 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
15 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
16 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
17 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
18 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection; |
19 |
import com.iver.utiles.swing.threads.AbstractMonitorableTask; |
20 |
|
21 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
22 |
*
|
23 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
24 |
*
|
25 |
* This program is free software; you can redistribute it and/or
|
26 |
* modify it under the terms of the GNU General Public License
|
27 |
* as published by the Free Software Foundation; either version 2
|
28 |
* of the License, or (at your option) any later version.
|
29 |
*
|
30 |
* This program is distributed in the hope that it will be useful,
|
31 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
32 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
33 |
* GNU General Public License for more details.
|
34 |
*
|
35 |
* You should have received a copy of the GNU General Public License
|
36 |
* along with this program; if not, write to the Free Software
|
37 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
38 |
*
|
39 |
* For more information, contact:
|
40 |
*
|
41 |
* Generalitat Valenciana
|
42 |
* Conselleria d'Infraestructures i Transport
|
43 |
* Av. Blasco Ib??ez, 50
|
44 |
* 46010 VALENCIA
|
45 |
* SPAIN
|
46 |
*
|
47 |
* +34 963862235
|
48 |
* gvsig@gva.es
|
49 |
* www.gvsig.gva.es
|
50 |
*
|
51 |
* or
|
52 |
*
|
53 |
* IVER T.I. S.A
|
54 |
* Salamanca 50
|
55 |
* 46005 Valencia
|
56 |
* Spain
|
57 |
*
|
58 |
* +34 963163400
|
59 |
* dac@iver.es
|
60 |
*/
|
61 |
/* CVS MESSAGES:
|
62 |
*
|
63 |
* $Id$
|
64 |
* $Log$
|
65 |
*
|
66 |
*/
|
67 |
/**
|
68 |
* This class writes a gvSIG layer and its children
|
69 |
* (if the driver supports a layer with children)
|
70 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
71 |
*/
|
72 |
public class ExportTask extends AbstractMonitorableTask{ |
73 |
private FLayer rootLayer = null; |
74 |
private GPEWriterHandler writer = null; |
75 |
private MapContext mapContext = null; |
76 |
private ExportGeometry eGeometry = null; |
77 |
|
78 |
public ExportTask(FLayer layer, GPEWriterHandler writer, MapContext mapContext){
|
79 |
this.rootLayer = layer;
|
80 |
this.writer = writer;
|
81 |
this.mapContext = mapContext;
|
82 |
this.eGeometry = new ExportGeometry(writer); |
83 |
|
84 |
setInitialStep(0);
|
85 |
setDeterminatedProcess(true);
|
86 |
setStatusMessage(PluginServices.getText(this, "gpe_exporting")); |
87 |
} |
88 |
|
89 |
public void run() throws Exception { |
90 |
writer.initialize(); |
91 |
exportLayer(rootLayer); |
92 |
writer.close(); |
93 |
} |
94 |
|
95 |
/**
|
96 |
* It writes a layer and its children recursively
|
97 |
* @param layer
|
98 |
* The layer to write
|
99 |
*/
|
100 |
private void exportLayer(FLayer layer){ |
101 |
writer.startLayer(null, layer.getName(), null, |
102 |
layer.getProjection().getAbrev(), null);
|
103 |
//Sets the extent
|
104 |
Rectangle2D r2d;
|
105 |
try {
|
106 |
r2d = layer.getFullExtent(); |
107 |
double[] x = new double[2]; |
108 |
x[0] = r2d.getMinX();
|
109 |
x[1] = r2d.getMaxX();
|
110 |
double[] y = new double[2]; |
111 |
y[0] = r2d.getMinY();
|
112 |
y[1] = r2d.getMaxY();
|
113 |
double[] z = new double[2]; |
114 |
z[0] = r2d.getMinY();
|
115 |
z[1] = r2d.getMaxY();
|
116 |
writer.startBbox(null, x, y, z, mapContext.getProjection().getAbrev());
|
117 |
writer.endBbox(); |
118 |
} catch (Exception e) { |
119 |
writer.getErrorHandler().addWarning(new ExtentExportWarning(layer,e));
|
120 |
} |
121 |
//Export the layer information
|
122 |
exportLayerInfo(layer); |
123 |
//If the layer has children...
|
124 |
if (layer instanceof LayerCollection){ |
125 |
LayerCollection layers = (LayerCollection)layer; |
126 |
for (int i=0 ; i<layers.getLayersCount() ; i++){ |
127 |
exportLayer(layers.getLayer(i)); |
128 |
} |
129 |
} |
130 |
writer.endLayer(); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* It exports the layer information. Geometries if is
|
135 |
* a vectorial layer, images if is a raster layer...
|
136 |
* @param layer
|
137 |
*/
|
138 |
private void exportLayerInfo(FLayer layer){ |
139 |
try {
|
140 |
if (layer instanceof FLyrVect){ |
141 |
exportVectorialLayer((FLyrVect)layer); |
142 |
} |
143 |
} catch (Exception e) { |
144 |
writer.getErrorHandler().addError(e); |
145 |
} |
146 |
} |
147 |
|
148 |
/**
|
149 |
* Export the geometries of a vectorial layer
|
150 |
* @param layer
|
151 |
* @throws DriverException
|
152 |
* @throws DriverIOException
|
153 |
* @throws ReadDriverException
|
154 |
*/
|
155 |
private void exportVectorialLayer(FLyrVect layer) throws DriverException, DriverIOException { |
156 |
System.out.println(layer.getName());
|
157 |
ReadableVectorial rv = layer.getSource(); |
158 |
SelectableDataSource sds = layer.getRecordset(); |
159 |
rv.start(); |
160 |
//If there is a selection the rows to export have to be
|
161 |
//the selected rows
|
162 |
FBitSet bitSet = sds.getSelection(); |
163 |
int rowCount;
|
164 |
if (bitSet.cardinality() == 0){ |
165 |
rowCount = rv.getShapeCount(); |
166 |
for (int i = 0; i < rowCount; i++) { |
167 |
exportFeature(sds, rv, i, layer); |
168 |
} |
169 |
}else{
|
170 |
rowCount = bitSet.cardinality(); |
171 |
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet |
172 |
.nextSetBit(i + 1)) {
|
173 |
exportFeature(sds, rv, i, layer); |
174 |
} |
175 |
} |
176 |
rv.stop(); |
177 |
} |
178 |
|
179 |
/**
|
180 |
* It writes a feature (geometry + attributes)
|
181 |
* @param sds
|
182 |
* The selectable datasource to get the attributes
|
183 |
* @param rv
|
184 |
* The readable vectorial to get the geoemtries
|
185 |
* @param index
|
186 |
* The feature index
|
187 |
* @param layer
|
188 |
* Only to personalize the exceptions
|
189 |
* @throws ReadDriverException
|
190 |
*/
|
191 |
private void exportFeature(SelectableDataSource sds, ReadableVectorial rv, int index, FLayer layer){ |
192 |
try {
|
193 |
writer.startFeature(String.valueOf(index), "FEATURE", null); |
194 |
//Add the geoemtry
|
195 |
IGeometry geom = rv.getShape(index); |
196 |
eGeometry.writeGeometry(geom, layer.getProjection().getAbrev()); |
197 |
//Add the attributes
|
198 |
Value[] values = sds.getRow(index);
|
199 |
for (int i=0 ; i<values.length ; i++){ |
200 |
writer.startElement(sds.getFieldName(i), |
201 |
values[i].toString(), |
202 |
null);
|
203 |
writer.endElement(); |
204 |
} |
205 |
writer.endFeature(); |
206 |
} catch (Exception e) { |
207 |
writer.getErrorHandler().addError(new FeatureExportException(layer,index,e));
|
208 |
} |
209 |
} |
210 |
|
211 |
/* (non-Javadoc)
|
212 |
* @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
|
213 |
*/
|
214 |
public void finished() { // TODO Auto-generated method stub |
215 |
|
216 |
} |
217 |
} |