svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / ExportTo.java @ 23647
History | View | Annotate | Download (24.4 KB)
1 | 5267 | fjp | package com.iver.cit.gvsig; |
---|---|---|---|
2 | |||
3 | import java.awt.Component; |
||
4 | 6371 | caballero | import java.awt.geom.Point2D; |
5 | 5267 | fjp | import java.io.File; |
6 | 6313 | fjp | import java.io.IOException; |
7 | 21666 | vcaballero | import java.sql.Driver; |
8 | 6798 | fjp | import java.sql.Types; |
9 | 6371 | caballero | import java.util.Vector; |
10 | 5267 | fjp | |
11 | 5284 | fjp | import javax.swing.JComponent; |
12 | 5267 | fjp | import javax.swing.JFileChooser; |
13 | import javax.swing.JOptionPane; |
||
14 | import javax.swing.ProgressMonitor; |
||
15 | |||
16 | 7767 | caballero | import org.cresques.cts.ICoordTrans; |
17 | 22373 | jmvivo | import org.gvsig.fmap.data.feature.InitializeWriterException; |
18 | 21666 | vcaballero | import org.gvsig.fmap.geom.primitive.FShape; |
19 | 22025 | jmvivo | import org.gvsig.fmap.geom.utils.FLabel; |
20 | 21666 | vcaballero | import org.gvsig.fmap.mapcontext.MapContext; |
21 | import org.gvsig.fmap.mapcontext.layers.FBitSet; |
||
22 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
23 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
24 | import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
||
25 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
26 | 7767 | caballero | |
27 | 21666 | vcaballero | import sun.awt.SunHints.Value; |
28 | |||
29 | 5267 | fjp | import com.iver.andami.PluginServices; |
30 | import com.iver.andami.messages.NotificationManager; |
||
31 | import com.iver.andami.plugins.Extension; |
||
32 | 7410 | caballero | import com.iver.cit.gvsig.project.documents.view.IProjectView; |
33 | 7304 | caballero | import com.iver.cit.gvsig.project.documents.view.gui.View; |
34 | 11971 | caballero | import com.iver.cit.gvsig.vectorialdb.ConnectionSettings; |
35 | import com.iver.cit.gvsig.vectorialdb.DlgConnection; |
||
36 | 6621 | fjp | import com.iver.utiles.PostProcessSupport; |
37 | 5267 | fjp | import com.iver.utiles.SimpleFileFilter; |
38 | 5317 | fjp | import com.iver.utiles.swing.threads.AbstractMonitorableTask; |
39 | 5267 | fjp | |
40 | public class ExportTo extends Extension { |
||
41 | 7391 | jaume | private String lastPath = null; |
42 | 5284 | fjp | private class WriterTask extends AbstractMonitorableTask |
43 | { |
||
44 | FLyrVect lyrVect; |
||
45 | IWriter writer; |
||
46 | int rowCount;
|
||
47 | ReadableVectorial va; |
||
48 | SelectableDataSource sds; |
||
49 | FBitSet bitSet; |
||
50 | 6878 | cesar | MapContext mapContext; |
51 | 6313 | fjp | VectorialDriver reader; |
52 | 6371 | caballero | |
53 | 10626 | caballero | public WriterTask(MapContext mapContext, FLyrVect lyr, IWriter writer, Driver reader) throws ReadDriverException |
54 | 5284 | fjp | { |
55 | 6313 | fjp | this.mapContext = mapContext;
|
56 | 5284 | fjp | this.lyrVect = lyr;
|
57 | this.writer = writer;
|
||
58 | 6313 | fjp | this.reader = (VectorialDriver) reader;
|
59 | 5675 | caballero | |
60 | 5284 | fjp | setInitialStep(0);
|
61 | setDeterminatedProcess(true);
|
||
62 | setStatusMessage(PluginServices.getText(this, "exportando_features")); |
||
63 | 5675 | caballero | |
64 | 5284 | fjp | va = lyrVect.getSource(); |
65 | sds = lyrVect.getRecordset(); |
||
66 | |||
67 | bitSet = sds.getSelection(); |
||
68 | |||
69 | if (bitSet.cardinality() == 0) |
||
70 | rowCount = va.getShapeCount(); |
||
71 | else
|
||
72 | rowCount = bitSet.cardinality(); |
||
73 | |||
74 | setFinalStep(rowCount); |
||
75 | 5675 | caballero | |
76 | 5284 | fjp | } |
77 | public void run() throws Exception { |
||
78 | 11354 | caballero | va.start(); |
79 | 7767 | caballero | ICoordTrans ct = lyrVect.getCoordTrans(); |
80 | DriverAttributes attr = va.getDriverAttributes(); |
||
81 | boolean bMustClone = false; |
||
82 | if (attr != null) { |
||
83 | if (attr.isLoadedInMemory()) {
|
||
84 | bMustClone = attr.isLoadedInMemory(); |
||
85 | } |
||
86 | } |
||
87 | 6371 | caballero | if (lyrVect instanceof FLyrAnnotation && lyrVect.getShapeType()!=FShape.POINT) { |
88 | SHPLayerDefinition lyrDef=(SHPLayerDefinition)writer.getTableDefinition(); |
||
89 | lyrDef.setShapeType(FShape.POINT); |
||
90 | writer.initialize(lyrDef); |
||
91 | } |
||
92 | |||
93 | 5284 | fjp | // Creamos la tabla.
|
94 | writer.preProcess(); |
||
95 | |||
96 | if (bitSet.cardinality() == 0) { |
||
97 | rowCount = va.getShapeCount(); |
||
98 | for (int i = 0; i < rowCount; i++) { |
||
99 | IGeometry geom = va.getShape(i); |
||
100 | 14586 | vcaballero | if (geom == null) { |
101 | continue;
|
||
102 | } |
||
103 | 6371 | caballero | if (lyrVect instanceof FLyrAnnotation && geom.getGeometryType()!=FShape.POINT) { |
104 | Point2D p=FLabel.createLabelPoint((FShape)geom.getInternalShape());
|
||
105 | geom=ShapeFactory.createPoint2D(p.getX(),p.getY()); |
||
106 | } |
||
107 | 7767 | caballero | if (ct != null) { |
108 | if (bMustClone)
|
||
109 | geom = geom.cloneGeometry(); |
||
110 | geom.reProject(ct); |
||
111 | } |
||
112 | 5284 | fjp | reportStep(); |
113 | setNote(PluginServices.getText(this, "exporting_") + i); |
||
114 | if (isCanceled())
|
||
115 | break;
|
||
116 | |||
117 | if (geom != null) { |
||
118 | Value[] values = sds.getRow(i);
|
||
119 | IFeature feat = new DefaultFeature(geom, values, "" + i); |
||
120 | DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
||
121 | DefaultRowEdited.STATUS_ADDED, i); |
||
122 | writer.process(edRow); |
||
123 | } |
||
124 | } |
||
125 | } else {
|
||
126 | int counter = 0; |
||
127 | for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet |
||
128 | .nextSetBit(i + 1)) {
|
||
129 | IGeometry geom = va.getShape(i); |
||
130 | 14586 | vcaballero | if (geom == null) { |
131 | continue;
|
||
132 | } |
||
133 | 6371 | caballero | if (lyrVect instanceof FLyrAnnotation && geom.getGeometryType()!=FShape.POINT) { |
134 | Point2D p=FLabel.createLabelPoint((FShape)geom.getInternalShape());
|
||
135 | geom=ShapeFactory.createPoint2D(p.getX(),p.getY()); |
||
136 | } |
||
137 | 7767 | caballero | if (ct != null) { |
138 | if (bMustClone)
|
||
139 | geom = geom.cloneGeometry(); |
||
140 | geom.reProject(ct); |
||
141 | } |
||
142 | 5284 | fjp | reportStep(); |
143 | setNote(PluginServices.getText(this, "exporting_") + counter); |
||
144 | if (isCanceled())
|
||
145 | break;
|
||
146 | |||
147 | if (geom != null) { |
||
148 | Value[] values = sds.getRow(i);
|
||
149 | IFeature feat = new DefaultFeature(geom, values, "" + i); |
||
150 | DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
||
151 | DefaultRowEdited.STATUS_ADDED, i); |
||
152 | |||
153 | writer.process(edRow); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | } |
||
158 | |||
159 | writer.postProcess(); |
||
160 | 11354 | caballero | va.stop(); |
161 | 6471 | caballero | if (reader != null){ |
162 | int res = JOptionPane.showConfirmDialog( |
||
163 | 6880 | cesar | (JComponent) PluginServices.getMDIManager().getActiveWindow()
|
164 | 6471 | caballero | , PluginServices.getText(this, "insertar_en_la_vista_la_capa_creada"), |
165 | PluginServices.getText(this,"insertar_capa"), |
||
166 | 6313 | fjp | JOptionPane.YES_NO_OPTION);
|
167 | 6471 | caballero | |
168 | 6313 | fjp | if (res == JOptionPane.YES_OPTION) |
169 | { |
||
170 | 6621 | fjp | PostProcessSupport.executeCalls(); |
171 | ILayerDefinition lyrDef = (ILayerDefinition) writer.getTableDefinition(); |
||
172 | 6313 | fjp | FLayer newLayer = LayerFactory.createLayer( |
173 | 6621 | fjp | lyrDef.getName(), reader, mapContext.getProjection()); |
174 | 6313 | fjp | mapContext.getLayers().addLayer(newLayer); |
175 | } |
||
176 | } |
||
177 | 5284 | fjp | |
178 | } |
||
179 | 11634 | cesar | /* (non-Javadoc)
|
180 | * @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
|
||
181 | */
|
||
182 | public void finished() { |
||
183 | // TODO Auto-generated method stub
|
||
184 | 11971 | caballero | |
185 | 11634 | cesar | } |
186 | 5675 | caballero | |
187 | 5284 | fjp | } |
188 | 6371 | caballero | private class MultiWriterTask extends AbstractMonitorableTask{ |
189 | Vector tasks=new Vector(); |
||
190 | public void addTask(WriterTask wt) { |
||
191 | tasks.add(wt); |
||
192 | } |
||
193 | public void run() throws Exception { |
||
194 | for (int i = 0; i < tasks.size(); i++) { |
||
195 | ((WriterTask)tasks.get(i)).run(); |
||
196 | } |
||
197 | tasks.clear(); |
||
198 | } |
||
199 | 11634 | cesar | /* (non-Javadoc)
|
200 | * @see com.iver.utiles.swing.threads.IMonitorableTask#finished()
|
||
201 | */
|
||
202 | public void finished() { |
||
203 | // TODO Auto-generated method stub
|
||
204 | 11971 | caballero | |
205 | 11634 | cesar | } |
206 | 5284 | fjp | |
207 | 6371 | caballero | |
208 | } |
||
209 | 5267 | fjp | /**
|
210 | * @see com.iver.andami.plugins.IExtension#initialize()
|
||
211 | */
|
||
212 | public void initialize() { |
||
213 | } |
||
214 | |||
215 | /**
|
||
216 | * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
||
217 | */
|
||
218 | public void execute(String actionCommand) { |
||
219 | 6877 | cesar | com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
220 | 6880 | cesar | .getActiveWindow(); |
221 | 5267 | fjp | |
222 | if (f instanceof View) { |
||
223 | View vista = (View) f; |
||
224 | 7410 | caballero | IProjectView model = vista.getModel(); |
225 | 6878 | cesar | MapContext mapa = model.getMapContext(); |
226 | 5267 | fjp | FLayers layers = mapa.getLayers(); |
227 | FLayer[] actives = layers.getActives();
|
||
228 | try {
|
||
229 | // NOTA: SI HAY UNA SELECCI?N, SOLO SE SALVAN LOS SELECCIONADOS
|
||
230 | for (int i = 0; i < actives.length; i++) { |
||
231 | if (actives[i] instanceof FLyrVect) { |
||
232 | FLyrVect lv = (FLyrVect) actives[i]; |
||
233 | int numSelec = lv.getRecordset().getSelection()
|
||
234 | .cardinality(); |
||
235 | if (numSelec > 0) { |
||
236 | 5284 | fjp | int resp = JOptionPane.showConfirmDialog( |
237 | 5806 | caballero | (Component) PluginServices.getMainFrame(),
|
238 | PluginServices.getText(this,"se_van_a_guardar_") + numSelec |
||
239 | + PluginServices.getText(this,"features_desea_continuar"), |
||
240 | PluginServices.getText(this,"export_to"), JOptionPane.YES_NO_OPTION); |
||
241 | if (resp != JOptionPane.YES_OPTION) { |
||
242 | 5267 | fjp | continue;
|
243 | } |
||
244 | } // if numSelec > 0
|
||
245 | if (actionCommand.equals("SHP")) { |
||
246 | 6313 | fjp | saveToShp(mapa, lv); |
247 | 5267 | fjp | } |
248 | if (actionCommand.equals("DXF")) { |
||
249 | 6313 | fjp | saveToDxf(mapa, lv); |
250 | 5267 | fjp | } |
251 | if (actionCommand.equals("POSTGIS")) { |
||
252 | 6313 | fjp | saveToPostGIS(mapa, lv); |
253 | 5267 | fjp | } |
254 | 6433 | jorpiell | if (actionCommand.equals("GML")) { |
255 | saveToGml(mapa, lv); |
||
256 | } |
||
257 | 5267 | fjp | } // actives[i]
|
258 | } // for
|
||
259 | 10626 | caballero | } catch (ReadDriverException e) {
|
260 | 19100 | vcaballero | NotificationManager.showMessageError(e.getMessage(),e); |
261 | 5267 | fjp | } |
262 | |||
263 | } |
||
264 | } |
||
265 | |||
266 | 10626 | caballero | public void saveToPostGIS(MapContext mapContext, FLyrVect layer){ |
267 | 5267 | fjp | try {
|
268 | String tableName = JOptionPane.showInputDialog(PluginServices |
||
269 | .getText(this, "intro_tablename")); |
||
270 | if (tableName == null) |
||
271 | return;
|
||
272 | 6621 | fjp | tableName = tableName.toLowerCase(); |
273 | 5267 | fjp | DlgConnection dlg = new DlgConnection();
|
274 | dlg.setModal(true);
|
||
275 | dlg.setVisible(true);
|
||
276 | ConnectionSettings cs = dlg.getConnSettings(); |
||
277 | if (cs == null) |
||
278 | return;
|
||
279 | 11971 | caballero | IConnection conex = ConnectionFactory.createConnection(cs |
280 | 5267 | fjp | .getConnectionString(), cs.getUser(), cs.getPassw()); |
281 | |||
282 | 15813 | jmvivo | |
283 | DBLayerDefinition originalDef = null;
|
||
284 | if (layer.getSource().getDriver() instanceof IVectorialDatabaseDriver) { |
||
285 | originalDef=((IVectorialDatabaseDriver) layer.getSource().getDriver()).getLyrDef(); |
||
286 | } |
||
287 | |||
288 | |||
289 | 5267 | fjp | DBLayerDefinition dbLayerDef = new DBLayerDefinition();
|
290 | 11971 | caballero | // Fjp:
|
291 | // Cambio: En Postgis, el nombre de cat?logo est? siempre vac?o. Es algo heredado de Oracle, que no se usa.
|
||
292 | // dbLayerDef.setCatalogName(cs.getDb());
|
||
293 | dbLayerDef.setCatalogName("");
|
||
294 | |||
295 | // A?adimos el schema dentro del layer definition para poder tenerlo en cuenta.
|
||
296 | dbLayerDef.setSchema(cs.getSchema()); |
||
297 | |||
298 | 5267 | fjp | dbLayerDef.setTableName(tableName); |
299 | 6621 | fjp | dbLayerDef.setName(tableName); |
300 | 5267 | fjp | dbLayerDef.setShapeType(layer.getShapeType()); |
301 | SelectableDataSource sds = layer.getRecordset(); |
||
302 | 15813 | jmvivo | |
303 | 5267 | fjp | FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
304 | dbLayerDef.setFieldsDesc(fieldsDescrip); |
||
305 | 6798 | fjp | // Creamos el driver. OJO: Hay que a?adir el campo ID a la
|
306 | // definici?n de campos.
|
||
307 | |||
308 | 15813 | jmvivo | if (originalDef != null){ |
309 | dbLayerDef.setFieldID(originalDef.getFieldID()); |
||
310 | dbLayerDef.setFieldGeometry(originalDef.getFieldGeometry()); |
||
311 | |||
312 | }else{
|
||
313 | // Search for id field name
|
||
314 | int index=0; |
||
315 | String fieldName="gid"; |
||
316 | while (findFileByName(fieldsDescrip,fieldName) != -1){ |
||
317 | index++; |
||
318 | fieldName="gid"+index;
|
||
319 | } |
||
320 | dbLayerDef.setFieldID(fieldName); |
||
321 | |||
322 | // search for geom field name
|
||
323 | index=0;
|
||
324 | fieldName="the_geom";
|
||
325 | while (findFileByName(fieldsDescrip,fieldName) != -1){ |
||
326 | index++; |
||
327 | fieldName="the_geom"+index;
|
||
328 | } |
||
329 | dbLayerDef.setFieldGeometry(fieldName); |
||
330 | |||
331 | } |
||
332 | |||
333 | // if id field dosen't exist we add it
|
||
334 | if (findFileByName(fieldsDescrip,dbLayerDef.getFieldID()) == -1) |
||
335 | { |
||
336 | 6798 | fjp | int numFieldsAnt = fieldsDescrip.length;
|
337 | FieldDescription[] newFields = new FieldDescription[dbLayerDef.getFieldsDesc().length + 1]; |
||
338 | for (int i=0; i < numFieldsAnt; i++) |
||
339 | { |
||
340 | newFields[i] = fieldsDescrip[i]; |
||
341 | } |
||
342 | newFields[numFieldsAnt] = new FieldDescription();
|
||
343 | newFields[numFieldsAnt].setFieldDecimalCount(0);
|
||
344 | newFields[numFieldsAnt].setFieldType(Types.INTEGER);
|
||
345 | newFields[numFieldsAnt].setFieldLength(7);
|
||
346 | newFields[numFieldsAnt].setFieldName("gid");
|
||
347 | dbLayerDef.setFieldsDesc(newFields); |
||
348 | |||
349 | } |
||
350 | |||
351 | |||
352 | 5267 | fjp | dbLayerDef.setWhereClause("");
|
353 | 16735 | vcaballero | String strSRID = layer.getProjection().getAbrev();
|
354 | 5267 | fjp | dbLayerDef.setSRID_EPSG(strSRID); |
355 | dbLayerDef.setConnection(conex); |
||
356 | |||
357 | 8662 | caballero | PostGISWriter writer=(PostGISWriter)LayerFactory.getWM().getWriter("PostGIS Writer");
|
358 | 5267 | fjp | writer.setWriteAll(true);
|
359 | writer.setCreateTable(true);
|
||
360 | writer.initialize(dbLayerDef); |
||
361 | 6471 | caballero | PostGisDriver postGISDriver=new PostGisDriver();
|
362 | postGISDriver.setLyrDef(dbLayerDef); |
||
363 | postGISDriver.open(); |
||
364 | 6621 | fjp | PostProcessSupport.clearList(); |
365 | Object[] params = new Object[2]; |
||
366 | params[0] = conex;
|
||
367 | params[1] = dbLayerDef;
|
||
368 | 6712 | caballero | PostProcessSupport.addToPostProcess(postGISDriver, "setData",
|
369 | 6621 | fjp | params, 1);
|
370 | 5267 | fjp | |
371 | 6471 | caballero | writeFeatures(mapContext, layer, writer, postGISDriver); |
372 | |||
373 | 5267 | fjp | } catch (DriverLoadException e) {
|
374 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
375 | 11971 | caballero | } catch (DBException e) {
|
376 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
377 | 10626 | caballero | } catch (InitializeWriterException e) {
|
378 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
379 | 10626 | caballero | } catch (ReadDriverException e) {
|
380 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
381 | 5267 | fjp | } |
382 | |||
383 | } |
||
384 | 5675 | caballero | |
385 | 6621 | fjp | /**
|
386 | * Lanza un thread en background que escribe las features. Cuando termina, pregunta al usuario si quiere
|
||
387 | * a?adir la nueva capa a la vista. Para eso necesita un driver de lectura ya configurado.
|
||
388 | * @param mapContext
|
||
389 | * @param layer
|
||
390 | * @param writer
|
||
391 | * @param reader
|
||
392 | 10626 | caballero | * @throws ReadDriverException
|
393 | 6621 | fjp | * @throws DriverException
|
394 | * @throws DriverIOException
|
||
395 | */
|
||
396 | 10626 | caballero | private void writeFeatures(MapContext mapContext, FLyrVect layer, IWriter writer, Driver reader) throws ReadDriverException |
397 | 5284 | fjp | { |
398 | 6313 | fjp | PluginServices.cancelableBackgroundExecution(new WriterTask(mapContext, layer, writer, reader));
|
399 | 5284 | fjp | } |
400 | 10626 | caballero | private void writeMultiFeatures(MapContext mapContext, FLyrVect layers, IWriter[] writers, Driver[] readers) throws ReadDriverException{ |
401 | 6371 | caballero | MultiWriterTask mwt=new MultiWriterTask();
|
402 | for (int i=0;i<writers.length;i++) { |
||
403 | 6471 | caballero | mwt.addTask(new WriterTask(mapContext, layers, writers[i], readers[i]));
|
404 | 6371 | caballero | } |
405 | PluginServices.cancelableBackgroundExecution(mwt); |
||
406 | } |
||
407 | 5267 | fjp | /**
|
408 | * @param layer
|
||
409 | * FLyrVect to obtain features. If selection, only selected
|
||
410 | * features will be precessed.
|
||
411 | * @param writer
|
||
412 | * (Must be already initialized)
|
||
413 | 10626 | caballero | * @throws ReadDriverException
|
414 | * @throws ProcessWriterException
|
||
415 | * @throws ExpansionFileReadException
|
||
416 | 5267 | fjp | * @throws EditionException
|
417 | * @throws DriverException
|
||
418 | * @throws DriverIOException
|
||
419 | * @throws com.hardcode.gdbms.engine.data.driver.DriverException
|
||
420 | */
|
||
421 | 10626 | caballero | public void writeFeaturesNoThread(FLyrVect layer, IWriter writer) throws ReadDriverException, VisitorException, ExpansionFileReadException{ |
422 | 5267 | fjp | ReadableVectorial va = layer.getSource(); |
423 | SelectableDataSource sds = layer.getRecordset(); |
||
424 | |||
425 | // Creamos la tabla.
|
||
426 | writer.preProcess(); |
||
427 | |||
428 | int rowCount;
|
||
429 | FBitSet bitSet = layer.getRecordset().getSelection(); |
||
430 | 5284 | fjp | |
431 | if (bitSet.cardinality() == 0) |
||
432 | rowCount = va.getShapeCount(); |
||
433 | else
|
||
434 | rowCount = bitSet.cardinality(); |
||
435 | |||
436 | ProgressMonitor progress = new ProgressMonitor( |
||
437 | 6880 | cesar | (JComponent) PluginServices.getMDIManager().getActiveWindow(),
|
438 | 5284 | fjp | PluginServices.getText(this, "exportando_features"), |
439 | PluginServices.getText(this, "exportando_features"), 0, |
||
440 | rowCount); |
||
441 | |||
442 | progress.setMillisToDecideToPopup(200);
|
||
443 | progress.setMillisToPopup(500);
|
||
444 | |||
445 | 5267 | fjp | if (bitSet.cardinality() == 0) { |
446 | rowCount = va.getShapeCount(); |
||
447 | for (int i = 0; i < rowCount; i++) { |
||
448 | IGeometry geom = va.getShape(i); |
||
449 | |||
450 | 5284 | fjp | progress.setProgress(i); |
451 | if (progress.isCanceled())
|
||
452 | break;
|
||
453 | |||
454 | 5267 | fjp | if (geom != null) { |
455 | Value[] values = sds.getRow(i);
|
||
456 | IFeature feat = new DefaultFeature(geom, values, "" + i); |
||
457 | DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
||
458 | 10626 | caballero | IRowEdited.STATUS_ADDED, i); |
459 | 5267 | fjp | writer.process(edRow); |
460 | } |
||
461 | } |
||
462 | } else {
|
||
463 | 5284 | fjp | int counter = 0; |
464 | 5267 | fjp | for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet |
465 | .nextSetBit(i + 1)) {
|
||
466 | IGeometry geom = va.getShape(i); |
||
467 | |||
468 | 5284 | fjp | progress.setProgress(counter++); |
469 | if (progress.isCanceled())
|
||
470 | break;
|
||
471 | |||
472 | 5267 | fjp | if (geom != null) { |
473 | Value[] values = sds.getRow(i);
|
||
474 | IFeature feat = new DefaultFeature(geom, values, "" + i); |
||
475 | DefaultRowEdited edRow = new DefaultRowEdited(feat,
|
||
476 | 10626 | caballero | IRowEdited.STATUS_ADDED, i); |
477 | 5267 | fjp | |
478 | writer.process(edRow); |
||
479 | } |
||
480 | } |
||
481 | |||
482 | } |
||
483 | |||
484 | writer.postProcess(); |
||
485 | 5284 | fjp | progress.close(); |
486 | 5267 | fjp | } |
487 | |||
488 | 10626 | caballero | public void saveToDxf(MapContext mapContext, FLyrVect layer) { |
489 | 5267 | fjp | try {
|
490 | 7391 | jaume | JFileChooser jfc = new JFileChooser(lastPath); |
491 | 5267 | fjp | SimpleFileFilter filterShp = new SimpleFileFilter("dxf", |
492 | PluginServices.getText(this, "dxf_files")); |
||
493 | jfc.setFileFilter(filterShp); |
||
494 | if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
||
495 | File newFile = jfc.getSelectedFile();
|
||
496 | String path = newFile.getAbsolutePath();
|
||
497 | if (!(path.toLowerCase().endsWith(".dxf"))) { |
||
498 | path = path + ".dxf";
|
||
499 | } |
||
500 | newFile = new File(path); |
||
501 | |||
502 | DxfWriter writer = (DxfWriter) LayerFactory.getWM().getWriter( |
||
503 | "DXF Writer");
|
||
504 | SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
||
505 | SelectableDataSource sds = layer.getRecordset(); |
||
506 | FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
||
507 | lyrDef.setFieldsDesc(fieldsDescrip); |
||
508 | lyrDef.setFile(newFile); |
||
509 | lyrDef.setName(newFile.getName()); |
||
510 | lyrDef.setShapeType(layer.getShapeType()); |
||
511 | writer.setFile(newFile); |
||
512 | writer.initialize(lyrDef); |
||
513 | writer.setProjection(layer.getProjection()); |
||
514 | DxfFieldsMapping fieldsMapping = new DxfFieldsMapping();
|
||
515 | // TODO: Recuperar aqu? los campos del cuadro de di?logo.
|
||
516 | writer.setFieldMapping(fieldsMapping); |
||
517 | 6471 | caballero | DXFMemoryDriver dxfDriver=new DXFMemoryDriver();
|
518 | dxfDriver.open(newFile); |
||
519 | writeFeatures(mapContext, layer, writer, dxfDriver); |
||
520 | 7391 | jaume | String fileName = newFile.getAbsolutePath();
|
521 | lastPath = fileName.substring(0, fileName.lastIndexOf(File.separatorChar)); |
||
522 | 5267 | fjp | } |
523 | |||
524 | 10626 | caballero | } catch (ReadDriverException e) {
|
525 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
526 | 10626 | caballero | } catch (InitializeWriterException e) {
|
527 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
528 | 10661 | caballero | } catch (DriverLoadException e) {
|
529 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
530 | 5267 | fjp | } |
531 | |||
532 | } |
||
533 | |||
534 | 10626 | caballero | public void saveToShp(MapContext mapContext, FLyrVect layer) { |
535 | 5267 | fjp | try {
|
536 | JFileChooser jfc = new JFileChooser(); |
||
537 | SimpleFileFilter filterShp = new SimpleFileFilter("shp", |
||
538 | PluginServices.getText(this, "shp_files")); |
||
539 | jfc.setFileFilter(filterShp); |
||
540 | if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
||
541 | File newFile = jfc.getSelectedFile();
|
||
542 | String path = newFile.getAbsolutePath();
|
||
543 | 5806 | caballero | if( newFile.exists()){
|
544 | int resp = JOptionPane.showConfirmDialog( |
||
545 | (Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"), |
||
546 | PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
||
547 | if (resp != JOptionPane.YES_OPTION) { |
||
548 | return;
|
||
549 | } |
||
550 | } |
||
551 | 5267 | fjp | if (!(path.toLowerCase().endsWith(".shp"))) { |
552 | path = path + ".shp";
|
||
553 | } |
||
554 | newFile = new File(path); |
||
555 | |||
556 | 6471 | caballero | |
557 | 6712 | caballero | |
558 | 5267 | fjp | SelectableDataSource sds = layer.getRecordset(); |
559 | FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
||
560 | 6712 | caballero | |
561 | 5267 | fjp | if (layer.getShapeType() == FShape.MULTI) // Exportamos a 3 |
562 | 5284 | fjp | // ficheros
|
563 | 5267 | fjp | { |
564 | 6471 | caballero | ShpWriter writer1 = (ShpWriter) LayerFactory.getWM().getWriter( |
565 | "Shape Writer");
|
||
566 | Driver[] drivers=new Driver[3]; |
||
567 | 6371 | caballero | ShpWriter[] writers=new ShpWriter[3]; |
568 | 6471 | caballero | |
569 | 5267 | fjp | // puntos
|
570 | 6712 | caballero | String auxPoint = path.replaceFirst(".shp", "_points.shp"); |
571 | 6471 | caballero | |
572 | 6712 | caballero | SHPLayerDefinition lyrDefPoint = new SHPLayerDefinition();
|
573 | lyrDefPoint.setFieldsDesc(fieldsDescrip); |
||
574 | File filePoints = new File(auxPoint); |
||
575 | lyrDefPoint.setFile(filePoints); |
||
576 | lyrDefPoint.setName(filePoints.getName()); |
||
577 | lyrDefPoint.setShapeType(FShape.POINT); |
||
578 | 6471 | caballero | writer1.setFile(filePoints); |
579 | 6712 | caballero | writer1.initialize(lyrDefPoint); |
580 | 6471 | caballero | writers[0]=writer1;
|
581 | drivers[0]=getOpenShpDriver(filePoints);
|
||
582 | //drivers[0]=null;
|
||
583 | 5267 | fjp | |
584 | 6712 | caballero | |
585 | |||
586 | 6471 | caballero | ShpWriter writer2 = (ShpWriter) LayerFactory.getWM().getWriter( |
587 | "Shape Writer");
|
||
588 | 5267 | fjp | // Lineas
|
589 | 6712 | caballero | String auxLine = path.replaceFirst(".shp", "_line.shp"); |
590 | SHPLayerDefinition lyrDefLine = new SHPLayerDefinition();
|
||
591 | lyrDefLine.setFieldsDesc(fieldsDescrip); |
||
592 | |||
593 | File fileLines = new File(auxLine); |
||
594 | lyrDefLine.setFile(fileLines); |
||
595 | lyrDefLine.setName(fileLines.getName()); |
||
596 | lyrDefLine.setShapeType(FShape.LINE); |
||
597 | 6471 | caballero | writer2.setFile(fileLines); |
598 | 6712 | caballero | writer2.initialize(lyrDefLine); |
599 | 6471 | caballero | writers[1]=writer2;
|
600 | drivers[1]=getOpenShpDriver(fileLines);
|
||
601 | //drivers[1]=null;
|
||
602 | 5267 | fjp | |
603 | 6471 | caballero | ShpWriter writer3 = (ShpWriter) LayerFactory.getWM().getWriter( |
604 | "Shape Writer");
|
||
605 | 5267 | fjp | // Pol?gonos
|
606 | 6712 | caballero | String auxPolygon = path.replaceFirst(".shp", "_polygons.shp"); |
607 | SHPLayerDefinition lyrDefPolygon = new SHPLayerDefinition();
|
||
608 | lyrDefPolygon.setFieldsDesc(fieldsDescrip); |
||
609 | File filePolygons = new File(auxPolygon); |
||
610 | lyrDefPolygon.setFile(filePolygons); |
||
611 | lyrDefPolygon.setName(filePolygons.getName()); |
||
612 | lyrDefPolygon.setShapeType(FShape.POLYGON); |
||
613 | 6471 | caballero | writer3.setFile(filePolygons); |
614 | 6712 | caballero | writer3.initialize(lyrDefPolygon); |
615 | 6471 | caballero | writers[2]=writer3;
|
616 | drivers[2]=getOpenShpDriver(filePolygons);
|
||
617 | //drivers[2]=null;
|
||
618 | 6371 | caballero | |
619 | 6471 | caballero | writeMultiFeatures(mapContext,layer, writers, drivers); |
620 | 5267 | fjp | } else {
|
621 | 6471 | caballero | ShpWriter writer = (ShpWriter) LayerFactory.getWM().getWriter( |
622 | "Shape Writer");
|
||
623 | IndexedShpDriver drv = getOpenShpDriver(newFile); |
||
624 | 6712 | caballero | SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
625 | lyrDef.setFieldsDesc(fieldsDescrip); |
||
626 | 5267 | fjp | lyrDef.setFile(newFile); |
627 | lyrDef.setName(newFile.getName()); |
||
628 | lyrDef.setShapeType(layer.getShapeType()); |
||
629 | writer.setFile(newFile); |
||
630 | writer.initialize(lyrDef); |
||
631 | 8181 | fjp | // CODIGO PARA EXPORTAR UN SHP A UN CHARSET DETERMINADO
|
632 | // ES UTIL PARA QUE UN DBF SE VEA CORRECTAMENTE EN EXCEL, POR EJEMPLO
|
||
633 | 8591 | caballero | // Charset resul = (Charset) JOptionPane.showInputDialog((Component)PluginServices.getMDIManager().getActiveWindow(),
|
634 | 8181 | fjp | // PluginServices.getText(ExportTo.class, "select_charset_for_writing"),
|
635 | 8591 | caballero | // "Charset", JOptionPane.QUESTION_MESSAGE, null,
|
636 | 8181 | fjp | // Charset.availableCharsets().values().toArray(),
|
637 | // writer.getCharsetForWriting().displayName());
|
||
638 | // if (resul == null)
|
||
639 | // return;
|
||
640 | // Charset charset = resul;
|
||
641 | // writer.setCharsetForWriting(charset);
|
||
642 | 6471 | caballero | writeFeatures(mapContext, layer, writer, drv); |
643 | 5267 | fjp | |
644 | } |
||
645 | } |
||
646 | 10626 | caballero | } catch (InitializeWriterException e) {
|
647 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
648 | 10626 | caballero | } catch (OpenDriverException e) {
|
649 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
650 | 10626 | caballero | } catch (ReadDriverException e) {
|
651 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
652 | 10661 | caballero | } catch (DriverLoadException e) {
|
653 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
654 | 6471 | caballero | } |
655 | 5267 | fjp | |
656 | } |
||
657 | 6433 | jorpiell | /**
|
658 | * This method saves a layer to GML
|
||
659 | * @param mapContext
|
||
660 | * @param layer
|
||
661 | * @throws EditionException
|
||
662 | * @throws DriverIOException
|
||
663 | */
|
||
664 | 10626 | caballero | public void saveToGml(MapContext mapContext, FLyrVect layer) { |
665 | 6433 | jorpiell | try {
|
666 | JFileChooser jfc = new JFileChooser(); |
||
667 | SimpleFileFilter filterShp = new SimpleFileFilter("gml", |
||
668 | PluginServices.getText(this, "gml_files")); |
||
669 | jfc.setFileFilter(filterShp); |
||
670 | if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
||
671 | File newFile = jfc.getSelectedFile();
|
||
672 | String path = newFile.getAbsolutePath();
|
||
673 | if (!(path.toLowerCase().endsWith(".gml"))) { |
||
674 | path = path + ".gml";
|
||
675 | } |
||
676 | newFile = new File(path); |
||
677 | 6479 | caballero | |
678 | 6433 | jorpiell | GMLWriter writer = (GMLWriter)LayerFactory.getWM().getWriter("GML Writer");
|
679 | 6479 | caballero | |
680 | 6433 | jorpiell | SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
681 | SelectableDataSource sds = layer.getRecordset(); |
||
682 | FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
||
683 | lyrDef.setFieldsDesc(fieldsDescrip); |
||
684 | 6486 | jorpiell | lyrDef.setName(newFile.getName()); |
685 | 6433 | jorpiell | lyrDef.setShapeType(layer.getShapeType()); |
686 | 6479 | caballero | |
687 | 6433 | jorpiell | writer.setFile(newFile); |
688 | 6479 | caballero | writer.setSchema(lyrDef); |
689 | 6433 | jorpiell | writer.setBoundedBy(layer.getFullExtent(),layer.getProjection()); |
690 | 6486 | jorpiell | writer.initialize(lyrDef); |
691 | 6479 | caballero | GMLDriver gmlDriver=new GMLDriver();
|
692 | gmlDriver.open(newFile); |
||
693 | writeFeatures(mapContext, layer, writer, gmlDriver); |
||
694 | 6433 | jorpiell | } |
695 | 5267 | fjp | |
696 | 10626 | caballero | } catch (SchemaEditionException e) {
|
697 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
698 | 10626 | caballero | } catch (InitializeWriterException e) {
|
699 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
700 | 10626 | caballero | } catch (ReadDriverException e) {
|
701 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
702 | 10661 | caballero | } catch (DriverLoadException e) {
|
703 | 12739 | caballero | NotificationManager.addError(e.getMessage(),e); |
704 | 6479 | caballero | } |
705 | 6433 | jorpiell | |
706 | } |
||
707 | 10626 | caballero | private IndexedShpDriver getOpenShpDriver(File fileShp) throws OpenDriverException { |
708 | 6471 | caballero | IndexedShpDriver drv = new IndexedShpDriver();
|
709 | if (!fileShp.exists()) {
|
||
710 | 10626 | caballero | try {
|
711 | fileShp.createNewFile(); |
||
712 | File newFileSHX=new File(fileShp.getAbsolutePath().replaceAll("[.]shp",".shx")); |
||
713 | newFileSHX.createNewFile(); |
||
714 | File newFileDBF=new File(fileShp.getAbsolutePath().replaceAll("[.]shp",".dbf")); |
||
715 | newFileDBF.createNewFile(); |
||
716 | } catch (IOException e) { |
||
717 | throw new FileNotFoundDriverException("SHP",e,fileShp.getAbsolutePath()); |
||
718 | } |
||
719 | 6471 | caballero | } |
720 | drv.open(fileShp); |
||
721 | return drv;
|
||
722 | } |
||
723 | 5267 | fjp | /**
|
724 | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
||
725 | */
|
||
726 | public boolean isEnabled() { |
||
727 | 5675 | caballero | int status = EditionUtilities.getEditionStatus();
|
728 | if (( status == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE || status == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
|
||
729 | || (status == EditionUtilities.EDITION_STATUS_MULTIPLE_VECTORIAL_LAYER_ACTIVE)|| (status == EditionUtilities.EDITION_STATUS_MULTIPLE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)) |
||
730 | 5562 | fjp | { |
731 | return true; |
||
732 | } |
||
733 | 6604 | caballero | return false; |
734 | 5267 | fjp | } |
735 | |||
736 | /**
|
||
737 | * @see com.iver.andami.plugins.IExtension#isVisible()
|
||
738 | */
|
||
739 | public boolean isVisible() { |
||
740 | 6877 | cesar | com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
741 | 6880 | cesar | .getActiveWindow(); |
742 | 5267 | fjp | |
743 | if (f == null) { |
||
744 | return false; |
||
745 | } |
||
746 | |||
747 | 6604 | caballero | if (f instanceof View) |
748 | 5267 | fjp | return true; |
749 | 6604 | caballero | return false; |
750 | 5267 | fjp | } |
751 | |||
752 | 15813 | jmvivo | private int findFileByName(FieldDescription[] fields, String fieldName){ |
753 | for (int i=0; i < fields.length; i++) |
||
754 | { |
||
755 | FieldDescription f = fields[i]; |
||
756 | if (f.getFieldName().equalsIgnoreCase(fieldName))
|
||
757 | { |
||
758 | return i;
|
||
759 | } |
||
760 | } |
||
761 | |||
762 | return -1; |
||
763 | |||
764 | } |
||
765 | |||
766 | 5267 | fjp | } |