svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.swing / org.gvsig.exportto.swing.prov / org.gvsig.exportto.swing.prov.shape / src / main / java / org / gvsig / exportto / swing / prov / shape / ExporttoShapeService.java @ 42191
History | View | Annotate | Download (27 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.exportto.swing.prov.shape; |
||
25 | |||
26 | import java.io.File; |
||
27 | import java.util.ArrayList; |
||
28 | 40602 | jldominguez | import java.util.Collection; |
29 | import java.util.HashMap; |
||
30 | import java.util.Iterator; |
||
31 | 40435 | jjdelcerro | import java.util.List; |
32 | 40602 | jldominguez | import java.util.Map; |
33 | 40435 | jjdelcerro | |
34 | 40602 | jldominguez | import javax.swing.JOptionPane; |
35 | |||
36 | 40435 | jjdelcerro | import org.cresques.cts.ICoordTrans; |
37 | import org.cresques.cts.IProjection; |
||
38 | 42191 | fdiaz | import org.slf4j.Logger; |
39 | import org.slf4j.LoggerFactory; |
||
40 | |||
41 | 40435 | jjdelcerro | import org.gvsig.exportto.ExporttoService; |
42 | import org.gvsig.exportto.ExporttoServiceException; |
||
43 | import org.gvsig.exportto.ExporttoServiceFinishAction; |
||
44 | 40602 | jldominguez | import org.gvsig.exportto.swing.prov.file.panel.SelectFileOptionPanel; |
45 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
46 | import org.gvsig.fmap.dal.DataManager; |
||
47 | 41023 | jjdelcerro | import org.gvsig.fmap.dal.DataStoreParameters; |
48 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
49 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
50 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
51 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
52 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
53 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
54 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
55 | import org.gvsig.fmap.dal.feature.Feature; |
||
56 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
57 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
58 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
59 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
60 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
61 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
62 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
63 | 42166 | jbadia | import org.gvsig.fmap.geom.DataTypes; |
64 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.Geometry; |
65 | import org.gvsig.fmap.geom.GeometryLocator; |
||
66 | import org.gvsig.fmap.geom.GeometryManager; |
||
67 | import org.gvsig.fmap.geom.aggregate.Aggregate; |
||
68 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
69 | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
||
70 | import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
||
71 | import org.gvsig.fmap.geom.primitive.Curve; |
||
72 | 42191 | fdiaz | import org.gvsig.fmap.geom.primitive.Line; |
73 | import org.gvsig.fmap.geom.primitive.OrientablePrimitive; |
||
74 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Point; |
75 | 42191 | fdiaz | import org.gvsig.fmap.geom.primitive.Polygon; |
76 | 40435 | jjdelcerro | import org.gvsig.fmap.geom.primitive.Surface; |
77 | import org.gvsig.fmap.geom.type.GeometryType; |
||
78 | 40602 | jldominguez | import org.gvsig.i18n.Messages; |
79 | 40435 | jjdelcerro | import org.gvsig.tools.dispose.DisposableIterator; |
80 | import org.gvsig.tools.dispose.DisposeUtils; |
||
81 | import org.gvsig.tools.task.AbstractMonitorableTask; |
||
82 | |||
83 | 42191 | fdiaz | |
84 | 40435 | jjdelcerro | /**
|
85 | * @author gvSIG Team
|
||
86 | * @version $Id$
|
||
87 | 42191 | fdiaz | *
|
88 | 40435 | jjdelcerro | */
|
89 | public class ExporttoShapeService extends AbstractMonitorableTask implements |
||
90 | ExporttoService { |
||
91 | |||
92 | private static Logger logger = LoggerFactory.getLogger(ExporttoShapeService.class); |
||
93 | 42191 | fdiaz | |
94 | 41745 | jjdelcerro | public static final int MAX_FIELD_NAME_LENGTH = 10; |
95 | 42191 | fdiaz | |
96 | 40435 | jjdelcerro | private File theShapeFile; |
97 | private IProjection projection;
|
||
98 | private FeatureStore featureStore;
|
||
99 | 40602 | jldominguez | private Map<String, String> origNameToDbfName; |
100 | 40435 | jjdelcerro | |
101 | private int geometryType = -1; |
||
102 | private NewFeatureStoreParameters newFeatureStoreParameters;
|
||
103 | private FilesystemServerExplorer filesystemServerExplorer;
|
||
104 | 40602 | jldominguez | private SelectFileOptionPanel filePanel = null; |
105 | 42191 | fdiaz | |
106 | 40435 | jjdelcerro | private static GeometryManager geoManager = GeometryLocator.getGeometryManager(); |
107 | |||
108 | private ExporttoServiceFinishAction exporttoServiceFinishAction;
|
||
109 | |||
110 | 40602 | jldominguez | public ExporttoShapeService(
|
111 | SelectFileOptionPanel fPanel, |
||
112 | FeatureStore featureStore, |
||
113 | IProjection projection) { |
||
114 | 42191 | fdiaz | |
115 | 40435 | jjdelcerro | super("Export to shape"); |
116 | this.featureStore = featureStore;
|
||
117 | 40602 | jldominguez | this.filePanel = fPanel;
|
118 | this.theShapeFile = fPanel.getSelectedFile();
|
||
119 | 40435 | jjdelcerro | this.projection = projection;
|
120 | 42191 | fdiaz | |
121 | 40602 | jldominguez | try {
|
122 | origNameToDbfName = getNames(featureStore.getDefaultFeatureType()); |
||
123 | } catch (DataException e) {
|
||
124 | logger.error("While getting def feat type.", e);
|
||
125 | } |
||
126 | 40435 | jjdelcerro | } |
127 | |||
128 | 40602 | jldominguez | private Map<String, String> getNames(FeatureType ft) { |
129 | 42191 | fdiaz | |
130 | 40602 | jldominguez | Map<String, String> resp = new HashMap<String, String>(); |
131 | FeatureAttributeDescriptor[] atts = ft.getAttributeDescriptors();
|
||
132 | for (int i=0; i<atts.length; i++) { |
||
133 | 42166 | jbadia | //if (atts[i].getName().length() > MAX_FIELD_NAME_LENGTH) {
|
134 | 40602 | jldominguez | String new_name = getNewName(atts[i].getName(), resp.values());
|
135 | resp.put(atts[i].getName(), new_name); |
||
136 | 42191 | fdiaz | //}
|
137 | 40602 | jldominguez | } |
138 | return resp;
|
||
139 | } |
||
140 | 40435 | jjdelcerro | |
141 | 40602 | jldominguez | private String getNewName(String name, Collection<String> values) { |
142 | 42191 | fdiaz | |
143 | int len = name.length();
|
||
144 | 40602 | jldominguez | if (len <= MAX_FIELD_NAME_LENGTH) {
|
145 | /*
|
||
146 | * Should not happen
|
||
147 | */
|
||
148 | return name;
|
||
149 | } |
||
150 | 42191 | fdiaz | |
151 | 42166 | jbadia | String resp = null; |
152 | 40602 | jldominguez | /*
|
153 | * Composes a new name with start and end of old name and an index
|
||
154 | * THISISAVERYLONGNAME => THISI_AME_1, THISI_AME_2 ... THISI_AME_9
|
||
155 | */
|
||
156 | 41745 | jjdelcerro | /*
|
157 | 40602 | jldominguez | String pref = name.substring(0, 6) + "_" + name.substring(len - 2) + "_";
|
158 | for (int i=0; i<10; i++) {
|
||
159 | resp = pref + i;
|
||
160 | if (!values.contains(resp)) {
|
||
161 | return resp;
|
||
162 | }
|
||
163 | }
|
||
164 | 41745 | jjdelcerro | */
|
165 | 40602 | jldominguez | /*
|
166 | * Very strange to get here...
|
||
167 | * THISISAVERYLONGNAME => THISISA_1, THISISA_2 ... THISISA_999
|
||
168 | */
|
||
169 | 41745 | jjdelcerro | /*
|
170 | 42166 | jbadia | String pref = name.substring(0, 7) + "_";
|
171 | 40602 | jldominguez | for (int i=0; i<1000; i++) {
|
172 | resp = pref + i;
|
||
173 | if (!values.contains(resp)) {
|
||
174 | return resp;
|
||
175 | }
|
||
176 | }
|
||
177 | 41745 | jjdelcerro | */
|
178 | 40602 | jldominguez | /*
|
179 | 41745 | jjdelcerro | * GDAL field name truncation method (extended from 100 to 255)
|
180 | * THISISAVERYLONGNAME => THISISAVER, THISISAV_1 ... THISISAV_9,
|
||
181 | * THISISAV10 ... THISISAV99, THISISA100 ... THISISA255
|
||
182 | * (255 = max number of fields in a SHP)
|
||
183 | */
|
||
184 | 42166 | jbadia | //String resp = null;
|
185 | 41745 | jjdelcerro | for (int i=0; i<255; i++) { |
186 | if (i==0) |
||
187 | resp = name.substring(0, 10); |
||
188 | else if (i<=9) |
||
189 | resp = name.substring(0, 8) + "_" + i; |
||
190 | else if (i<=99) |
||
191 | resp = name.substring(0, 8) + i; |
||
192 | else
|
||
193 | resp = name.substring(0, 7) + i; |
||
194 | if (!values.contains(resp))
|
||
195 | return resp;
|
||
196 | } |
||
197 | /*
|
||
198 | 40602 | jldominguez | * Should not get here
|
199 | */
|
||
200 | return name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000); |
||
201 | } |
||
202 | |||
203 | public void export(FeatureSet featureSet) throws ExporttoServiceException { |
||
204 | |||
205 | 40435 | jjdelcerro | ExporttoServiceException throw_exp = null;
|
206 | File item_shp = null; |
||
207 | String pathFile = theShapeFile.getAbsolutePath();
|
||
208 | String withoutShp = pathFile.replaceAll("\\.shp", ""); |
||
209 | 42191 | fdiaz | |
210 | 40435 | jjdelcerro | File single_file = new File(withoutShp + ".shp"); |
211 | 42191 | fdiaz | |
212 | 40435 | jjdelcerro | String layer_name = single_file.getName();
|
213 | int lastp = layer_name.lastIndexOf(".shp"); |
||
214 | if (lastp > -1) { |
||
215 | layer_name = layer_name.substring(0, lastp);
|
||
216 | } |
||
217 | 42191 | fdiaz | |
218 | 40435 | jjdelcerro | initializeParams(featureSet, single_file); |
219 | |||
220 | if (geometryType == Geometry.TYPES.GEOMETRY) {
|
||
221 | 42191 | fdiaz | // SURFACE
|
222 | String fileName = withoutShp + "_surface" + ".shp"; |
||
223 | 40435 | jjdelcerro | item_shp = new File(fileName); |
224 | initializeParams(featureSet, item_shp); |
||
225 | newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
||
226 | 42191 | fdiaz | taskStatus.setTitle("Exporting surfaces");
|
227 | |||
228 | 40435 | jjdelcerro | try {
|
229 | export(filesystemServerExplorer, newFeatureStoreParameters,
|
||
230 | 42191 | fdiaz | featureSet, Geometry.TYPES.SURFACE, true);
|
231 | 40435 | jjdelcerro | } catch (ExporttoServiceException ee) {
|
232 | throw_exp = ee; |
||
233 | } |
||
234 | 42191 | fdiaz | finishAction(layer_name + " (surface)", item_shp, projection);
|
235 | 40435 | jjdelcerro | |
236 | // CURVE
|
||
237 | fileName = withoutShp + "_curve" + ".shp"; |
||
238 | item_shp = new File(fileName); |
||
239 | initializeParams(featureSet, item_shp); |
||
240 | newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
||
241 | 42191 | fdiaz | taskStatus.setTitle("Exporting curves");
|
242 | |||
243 | 40435 | jjdelcerro | try {
|
244 | export(filesystemServerExplorer, newFeatureStoreParameters,
|
||
245 | featureSet, Geometry.TYPES.CURVE, true);
|
||
246 | } catch (ExporttoServiceException ee) {
|
||
247 | throw_exp = ee; |
||
248 | } |
||
249 | 42191 | fdiaz | |
250 | 40490 | jldominguez | finishAction(layer_name + " (curve)", item_shp, projection);
|
251 | 40435 | jjdelcerro | |
252 | 42191 | fdiaz | // POINT
|
253 | fileName = withoutShp + "_point" + ".shp"; |
||
254 | 40435 | jjdelcerro | item_shp = new File(fileName); |
255 | initializeParams(featureSet, item_shp); |
||
256 | 42191 | fdiaz | |
257 | 40435 | jjdelcerro | newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
258 | 42191 | fdiaz | taskStatus.setTitle("Exporting points");
|
259 | |||
260 | 40435 | jjdelcerro | try {
|
261 | export(filesystemServerExplorer, newFeatureStoreParameters,
|
||
262 | 42191 | fdiaz | featureSet, Geometry.TYPES.POINT, true);
|
263 | 40435 | jjdelcerro | } catch (ExporttoServiceException ee) {
|
264 | throw_exp = ee; |
||
265 | } |
||
266 | |||
267 | 42191 | fdiaz | finishAction(layer_name + " (point)", item_shp, projection);
|
268 | |||
269 | |||
270 | 40435 | jjdelcerro | } else {
|
271 | 42191 | fdiaz | |
272 | 40435 | jjdelcerro | // params already initialized
|
273 | newFeatureStoreParameters.setDynValue("shpfile", single_file);
|
||
274 | try {
|
||
275 | export(filesystemServerExplorer, newFeatureStoreParameters,
|
||
276 | featureSet, geometryType, false);
|
||
277 | } catch (ExporttoServiceException ee) {
|
||
278 | throw_exp = ee; |
||
279 | } |
||
280 | 40490 | jldominguez | finishAction(layer_name, single_file, projection); |
281 | 40435 | jjdelcerro | } |
282 | this.taskStatus.terminate();
|
||
283 | this.taskStatus.remove();
|
||
284 | 42191 | fdiaz | |
285 | 40435 | jjdelcerro | if (throw_exp != null) { |
286 | throw throw_exp;
|
||
287 | 40602 | jldominguez | } else {
|
288 | if (origNameToDbfName.size() > 0) { |
||
289 | showNewNamesDialog(origNameToDbfName); |
||
290 | } |
||
291 | 40435 | jjdelcerro | } |
292 | } |
||
293 | |||
294 | 40602 | jldominguez | private void showNewNamesDialog(Map<String, String> map) { |
295 | 42191 | fdiaz | |
296 | 40602 | jldominguez | Iterator<String> iter = map.keySet().iterator(); |
297 | 42191 | fdiaz | |
298 | 40602 | jldominguez | String msg = Messages.getText("_Some_field_names_too_long_automatically_changed"); |
299 | msg = msg + ":\n";
|
||
300 | String k, v;
|
||
301 | 42166 | jbadia | boolean hasModifiedNames = false; |
302 | 40602 | jldominguez | while (iter.hasNext()) {
|
303 | k = iter.next(); |
||
304 | v = map.get(k); |
||
305 | 42166 | jbadia | if(!k.equalsIgnoreCase(v)){
|
306 | 42191 | fdiaz | msg = msg + "\n" + k + " > " + v; |
307 | 42166 | jbadia | hasModifiedNames = true;
|
308 | } |
||
309 | 40602 | jldominguez | } |
310 | 42191 | fdiaz | |
311 | 42166 | jbadia | if(hasModifiedNames){
|
312 | 42191 | fdiaz | JOptionPane.showMessageDialog(
|
313 | 40602 | jldominguez | this.filePanel,
|
314 | msg, |
||
315 | Messages.getText("export_progress"),
|
||
316 | JOptionPane.WARNING_MESSAGE);
|
||
317 | } |
||
318 | 42191 | fdiaz | } |
319 | 40602 | jldominguez | |
320 | private void initializeParams( |
||
321 | 40435 | jjdelcerro | FeatureSet featureSet, File out_shp_file)
|
322 | throws ExporttoServiceException {
|
||
323 | |||
324 | DataManager dataManager = DALLocator.getDataManager(); |
||
325 | |||
326 | FilesystemServerExplorerParameters explorerParams; |
||
327 | try {
|
||
328 | explorerParams = |
||
329 | (FilesystemServerExplorerParameters) dataManager |
||
330 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
331 | } catch (InitializeException e) {
|
||
332 | throw new ExporttoServiceException(e); |
||
333 | } catch (ProviderNotRegisteredException e) {
|
||
334 | throw new ExporttoServiceException(e); |
||
335 | } |
||
336 | explorerParams.setRoot(out_shp_file.getParent()); |
||
337 | |||
338 | try {
|
||
339 | filesystemServerExplorer = |
||
340 | (FilesystemServerExplorer) dataManager.openServerExplorer( |
||
341 | "FilesystemExplorer", explorerParams);
|
||
342 | } catch (ValidateDataParametersException e) {
|
||
343 | throw new ExporttoServiceException(e); |
||
344 | } catch (InitializeException e) {
|
||
345 | throw new ExporttoServiceException(e); |
||
346 | } catch (ProviderNotRegisteredException e) {
|
||
347 | throw new ExporttoServiceException(e); |
||
348 | } |
||
349 | |||
350 | try {
|
||
351 | newFeatureStoreParameters = |
||
352 | (NewFeatureStoreParameters) filesystemServerExplorer |
||
353 | .getAddParameters(out_shp_file); |
||
354 | } catch (DataException e) {
|
||
355 | throw new ExporttoServiceException(e); |
||
356 | } |
||
357 | |||
358 | newFeatureStoreParameters.setDynValue("CRS", projection);
|
||
359 | |||
360 | geometryType = |
||
361 | featureSet.getDefaultFeatureType().getDefaultGeometryAttribute() |
||
362 | .getGeomType().getType(); |
||
363 | |||
364 | } |
||
365 | |||
366 | private void export(FilesystemServerExplorer explorer, |
||
367 | NewFeatureStoreParameters params, FeatureSet featureSet, |
||
368 | int geometryType, boolean checkType) throws ExporttoServiceException { |
||
369 | 42191 | fdiaz | |
370 | 40435 | jjdelcerro | String providerName = params.getDataStoreName();
|
371 | String explorerName = explorer.getProviderName();
|
||
372 | boolean there_was_error = false; |
||
373 | |||
374 | DisposableIterator it = null;
|
||
375 | try {
|
||
376 | EditableFeatureType type = |
||
377 | 40602 | jldominguez | featureStore.getDefaultFeatureType().getCopy().getEditable(); |
378 | 42191 | fdiaz | |
379 | 40435 | jjdelcerro | FeatureAttributeDescriptor fad = |
380 | (FeatureAttributeDescriptor) type.get(type |
||
381 | .getDefaultGeometryAttributeName()); |
||
382 | 42191 | fdiaz | |
383 | // ==========================
|
||
384 | fixNames(type); |
||
385 | // ==========================
|
||
386 | |||
387 | 40435 | jjdelcerro | type.remove(fad.getName()); |
388 | EditableFeatureAttributeDescriptor efad = |
||
389 | type.add(fad.getName(), fad.getType(), fad.getSize()); |
||
390 | efad.setDefaultValue(fad.getDefaultValue()); |
||
391 | 42191 | fdiaz | |
392 | int gsubtype = fad.getGeomType().getSubType();
|
||
393 | // El shp solo soporta los subtipos 2D y 3D
|
||
394 | switch( gsubtype ) {
|
||
395 | case Geometry.SUBTYPES.GEOM2D:
|
||
396 | break;
|
||
397 | case Geometry.SUBTYPES.GEOM3D:
|
||
398 | break;
|
||
399 | default:
|
||
400 | // Forzaremos las geometrias a 3D
|
||
401 | gsubtype = Geometry.SUBTYPES.GEOM3D; |
||
402 | break;
|
||
403 | } |
||
404 | 40435 | jjdelcerro | GeometryType gty = null;
|
405 | try {
|
||
406 | 42191 | fdiaz | gty = geoManager.getGeometryType(geometryType, gsubtype); |
407 | 40435 | jjdelcerro | } catch (Exception e) { |
408 | throw new ExporttoServiceException(e); |
||
409 | } |
||
410 | 42191 | fdiaz | |
411 | 40435 | jjdelcerro | efad.setGeometryType(gty); |
412 | 42191 | fdiaz | |
413 | 42166 | jbadia | efad.setName(origNameToDbfName.get(fad.getName())); |
414 | 40435 | jjdelcerro | efad.setPrecision(fad.getPrecision()); |
415 | 42191 | fdiaz | |
416 | |||
417 | 42166 | jbadia | type.setDefaultGeometryAttributeName(efad.getName()); |
418 | 42191 | fdiaz | |
419 | params.setDefaultFeatureType(type.getNotEditableCopy()); |
||
420 | |||
421 | 40435 | jjdelcerro | params.setDynValue("geometryType", null); |
422 | |||
423 | DataManager manager = DALLocator.getDataManager(); |
||
424 | |||
425 | manager.newStore(explorerName, providerName, params, true);
|
||
426 | FeatureStore target = |
||
427 | (FeatureStore) manager.openStore(providerName, params); |
||
428 | |||
429 | FeatureType targetType = target.getDefaultFeatureType(); |
||
430 | |||
431 | taskStatus.setRangeOfValues(0, featureSet.getSize());
|
||
432 | |||
433 | target.edit(FeatureStore.MODE_APPEND); |
||
434 | it = featureSet.fastIterator(); |
||
435 | int featureCount = 0; |
||
436 | |||
437 | // ================================================
|
||
438 | // Reprojection stuff
|
||
439 | Geometry reproj_geom = null;
|
||
440 | EditableFeature edit_feat = null;
|
||
441 | IProjection sourceProjection = |
||
442 | featureStore.getDefaultFeatureType().getDefaultGeometryAttribute().getSRS(); |
||
443 | |||
444 | ICoordTrans coord_trans = null;
|
||
445 | // this comparison is perhaps too preventive
|
||
446 | // we could have two instances of same projection
|
||
447 | // so we would do more computations than needed
|
||
448 | 42090 | jbadia | if (sourceProjection != null && sourceProjection != this.projection) { |
449 | 42191 | fdiaz | coord_trans = sourceProjection.getCT(this.projection);
|
450 | 40435 | jjdelcerro | } |
451 | // ================================================
|
||
452 | |||
453 | List<Geometry> extracted = null; |
||
454 | Geometry gitem = null;
|
||
455 | 42191 | fdiaz | |
456 | 40435 | jjdelcerro | while (it.hasNext()) {
|
457 | 42191 | fdiaz | |
458 | 40435 | jjdelcerro | Feature feature = (Feature) it.next(); |
459 | gitem = feature.getDefaultGeometry(); |
||
460 | |||
461 | if (checkType) {
|
||
462 | extracted = getGeometriesFrom(gitem, geometryType); |
||
463 | if (extracted.size() == 0) { |
||
464 | // found no geometries of correct type
|
||
465 | continue;
|
||
466 | } else {
|
||
467 | if (geometryType != Geometry.TYPES.POINT) {
|
||
468 | // If not points, merge geometries
|
||
469 | // (curves or surfaces)
|
||
470 | try {
|
||
471 | gitem = union(extracted); |
||
472 | extracted = new ArrayList<Geometry>(); |
||
473 | extracted.add(gitem); |
||
474 | } catch (Exception ex) { |
||
475 | there_was_error = true;
|
||
476 | logger.info("Error in union.", ex);
|
||
477 | } |
||
478 | } else {
|
||
479 | // only in the case of points, we can have several
|
||
480 | // geometries if source is multipoint
|
||
481 | } |
||
482 | } |
||
483 | } else {
|
||
484 | extracted = new ArrayList<Geometry>(); |
||
485 | extracted.add(gitem); |
||
486 | } |
||
487 | |||
488 | for (int i=0; i<extracted.size(); i++) { |
||
489 | gitem = extracted.get(i); |
||
490 | 42191 | fdiaz | |
491 | if( gsubtype==Geometry.SUBTYPES.GEOM2D ) {
|
||
492 | gitem = force2D(gitem, geometryType); |
||
493 | } else {
|
||
494 | gitem = force3D(gitem, geometryType); |
||
495 | } |
||
496 | 40435 | jjdelcerro | edit_feat = target.createNewFeature(true);
|
497 | there_was_error = there_was_error | |
||
498 | 40602 | jldominguez | /*
|
499 | * Accumulate error in boolean.
|
||
500 | * This also fixes field names (using origNameToDbfName)
|
||
501 | */
|
||
502 | setNonNulls(featureStore.getDefaultFeatureType(), |
||
503 | targetType, feature, edit_feat); |
||
504 | 40435 | jjdelcerro | edit_feat.setDefaultGeometry(gitem); |
505 | // ================================================
|
||
506 | // Reprojection stuff
|
||
507 | if (coord_trans != null) { |
||
508 | reproj_geom = edit_feat.getDefaultGeometry(); |
||
509 | reproj_geom = reproj_geom.cloneGeometry(); |
||
510 | reproj_geom.reProject(coord_trans); |
||
511 | edit_feat.setDefaultGeometry(reproj_geom); |
||
512 | } |
||
513 | // ================================================
|
||
514 | target.insert(edit_feat); |
||
515 | } |
||
516 | |||
517 | featureCount++; |
||
518 | this.taskStatus.setCurValue(featureCount);
|
||
519 | |||
520 | if (this.taskStatus.isCancellationRequested()) { |
||
521 | return;
|
||
522 | } |
||
523 | } |
||
524 | target.finishEditing(); |
||
525 | target.dispose(); |
||
526 | } catch (Exception e) { |
||
527 | throw new ExporttoServiceException(e); |
||
528 | } finally {
|
||
529 | DisposeUtils.dispose(it); |
||
530 | } |
||
531 | 42191 | fdiaz | |
532 | 40435 | jjdelcerro | if (there_was_error) {
|
533 | Exception cause = new Exception( |
||
534 | "_Issues_with_attributes_or_geometries");
|
||
535 | throw new ExporttoServiceException(cause); |
||
536 | } |
||
537 | } |
||
538 | |||
539 | 42191 | fdiaz | |
540 | 40602 | jldominguez | private void fixNames(EditableFeatureType eft) { |
541 | 42191 | fdiaz | |
542 | 40602 | jldominguez | FeatureAttributeDescriptor[] atts = eft.getAttributeDescriptors();
|
543 | EditableFeatureAttributeDescriptor efad = null;
|
||
544 | for (int i=0; i<atts.length; i++) { |
||
545 | 42191 | fdiaz | String new_name = origNameToDbfName.get(atts[i].getName());
|
546 | 40602 | jldominguez | if (new_name != null) { |
547 | eft.remove(atts[i].getName()); |
||
548 | efad = eft.add(new_name, atts[i].getType(), atts[i].getSize()); |
||
549 | efad.setPrecision(atts[i].getPrecision()); |
||
550 | 42166 | jbadia | if(atts[i].getDataType().getType() == DataTypes.GEOMETRY){
|
551 | efad.setGeometryType(atts[i].getGeomType()); |
||
552 | } |
||
553 | 40602 | jldominguez | } |
554 | } |
||
555 | } |
||
556 | |||
557 | /**
|
||
558 | 40435 | jjdelcerro | * @param gitem
|
559 | * @param geometryType2
|
||
560 | * @return
|
||
561 | */
|
||
562 | private Geometry force2D(Geometry ge, int gt) throws CreateGeometryException { |
||
563 | 42191 | fdiaz | |
564 | 40435 | jjdelcerro | if (ge.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM2D) {
|
565 | return ge;
|
||
566 | } else {
|
||
567 | switch (gt) {
|
||
568 | case Geometry.TYPES.POINT:
|
||
569 | Point p = (Point) ge; |
||
570 | 42191 | fdiaz | Point point = geoManager.createPoint(
|
571 | 40435 | jjdelcerro | p.getX(), p.getY(), |
572 | Geometry.SUBTYPES.GEOM2D); |
||
573 | 42191 | fdiaz | return point;
|
574 | 40435 | jjdelcerro | case Geometry.TYPES.CURVE:
|
575 | return geoManager.createCurve(ge.getGeneralPath(), Geometry.SUBTYPES.GEOM2D);
|
||
576 | case Geometry.TYPES.SURFACE:
|
||
577 | return geoManager.createSurface(ge.getGeneralPath(), Geometry.SUBTYPES.GEOM2D);
|
||
578 | default:
|
||
579 | return ge;
|
||
580 | } |
||
581 | } |
||
582 | } |
||
583 | |||
584 | /**
|
||
585 | 42191 | fdiaz | * @param gitem
|
586 | * @param geometryType2
|
||
587 | * @return
|
||
588 | */
|
||
589 | private Geometry force3D(Geometry ge, int gt) throws CreateGeometryException { |
||
590 | // Los try catch que hay abajo, son para asegurarse de que la geometr?a original
|
||
591 | // tiene las 3 dimensiones necesarias. A veces nos llegan geometr?as que aunque son de subtipo GEOM3D
|
||
592 | // los puntos del generalPath que contienen no son 3D y no tienen la coordenada Z
|
||
593 | Point point;
|
||
594 | switch (gt) {
|
||
595 | case Geometry.TYPES.POINT:
|
||
596 | Point p = (Point) ge; |
||
597 | point = geoManager.createPoint( |
||
598 | p.getX(), p.getY(), |
||
599 | Geometry.SUBTYPES.GEOM3D); |
||
600 | try {
|
||
601 | point.setCoordinateAt(2, p.getCoordinateAt(2)); |
||
602 | } catch (Exception e) { |
||
603 | point.setCoordinateAt(2, 0); |
||
604 | } |
||
605 | return point;
|
||
606 | case Geometry.TYPES.CURVE:
|
||
607 | Line line = geoManager.createLine(Geometry.SUBTYPES.GEOM3D);
|
||
608 | for (int i = 0; i < ((OrientablePrimitive)ge).getNumVertices(); i++) { |
||
609 | Point vertex = ((OrientablePrimitive)ge).getVertex(i);
|
||
610 | point = geoManager.createPoint(vertex.getX(), vertex.getY(), Geometry.SUBTYPES.GEOM3D); |
||
611 | try {
|
||
612 | point.setCoordinateAt(2, vertex.getCoordinateAt(2)); |
||
613 | } catch (Exception e) { |
||
614 | point.setCoordinateAt(2, 0); |
||
615 | } |
||
616 | line.addVertex(point); |
||
617 | }; |
||
618 | return line;
|
||
619 | |||
620 | case Geometry.TYPES.SURFACE:
|
||
621 | Polygon polygon = geoManager.createPolygon(Geometry.SUBTYPES.GEOM3D);
|
||
622 | for (int i = 0; i < ((OrientablePrimitive)ge).getNumVertices(); i++) { |
||
623 | Point vertex = ((OrientablePrimitive)ge).getVertex(i);
|
||
624 | point = geoManager.createPoint(vertex.getX(), vertex.getY(), Geometry.SUBTYPES.GEOM3D); |
||
625 | try {
|
||
626 | point.setCoordinateAt(2, vertex.getCoordinateAt(2)); |
||
627 | } catch (Exception e) { |
||
628 | point.setCoordinateAt(2, 0); |
||
629 | } |
||
630 | polygon.addVertex(point); |
||
631 | }; |
||
632 | return polygon;
|
||
633 | default:
|
||
634 | return ge;
|
||
635 | } |
||
636 | } |
||
637 | private void createPoint(int geom3d) { |
||
638 | // TODO Auto-generated method stub
|
||
639 | |||
640 | } |
||
641 | |||
642 | /**
|
||
643 | 40435 | jjdelcerro | * @param feature
|
644 | * @param edit_feat
|
||
645 | */
|
||
646 | private boolean setNonNulls( |
||
647 | 40602 | jldominguez | FeatureType src_ft, FeatureType target_ft, |
648 | 40435 | jjdelcerro | Feature feat, EditableFeature edit_f) { |
649 | 42191 | fdiaz | |
650 | 40435 | jjdelcerro | boolean error = false; |
651 | 40602 | jldominguez | FeatureAttributeDescriptor[] atts = src_ft.getAttributeDescriptors();
|
652 | String orig_name = null; |
||
653 | String dbf_name = null; |
||
654 | 40435 | jjdelcerro | for (int i=0; i<atts.length; i++) { |
655 | if (atts[i].getType() != org.gvsig.fmap.geom.DataTypes.GEOMETRY) {
|
||
656 | 42191 | fdiaz | |
657 | 40435 | jjdelcerro | Object val = null; |
658 | 42191 | fdiaz | |
659 | 40435 | jjdelcerro | try {
|
660 | 40602 | jldominguez | orig_name = atts[i].getName(); |
661 | dbf_name = origNameToDbfName.get(orig_name); |
||
662 | if (dbf_name == null) { |
||
663 | 42191 | fdiaz | dbf_name = orig_name; |
664 | 40602 | jldominguez | } |
665 | val = feat.get(orig_name); |
||
666 | 40435 | jjdelcerro | if (val != null) { |
667 | 40602 | jldominguez | edit_f.set(dbf_name, val); |
668 | 40435 | jjdelcerro | } |
669 | } catch (Exception ex) { |
||
670 | logger.info("Error while getting/setting value", ex);
|
||
671 | error = true;
|
||
672 | } |
||
673 | } |
||
674 | } |
||
675 | return error;
|
||
676 | } |
||
677 | |||
678 | private Geometry union(List<Geometry> geoms) |
||
679 | throws GeometryOperationNotSupportedException, GeometryOperationException {
|
||
680 | 42191 | fdiaz | |
681 | 40435 | jjdelcerro | if (geoms == null || geoms.size() == 0) { |
682 | return null; |
||
683 | } |
||
684 | 42191 | fdiaz | |
685 | 40435 | jjdelcerro | if (geoms.size() == 1) { |
686 | return geoms.get(0); |
||
687 | } |
||
688 | 42191 | fdiaz | |
689 | 40435 | jjdelcerro | Geometry resp = geoms.get(0);
|
690 | for (int i=1; i<geoms.size(); i++) { |
||
691 | resp = resp.union(geoms.get(i)); |
||
692 | } |
||
693 | return resp;
|
||
694 | } |
||
695 | /**
|
||
696 | * @param feat_geom_type
|
||
697 | * @param type must be POINT, LINE or POLYGON (Geometry.TYPES)
|
||
698 | * @return
|
||
699 | */
|
||
700 | private List<Geometry> getGeometriesFrom(Geometry in_geom, int type) { |
||
701 | 42191 | fdiaz | |
702 | 40435 | jjdelcerro | List<Geometry> resp = new ArrayList<Geometry>(); |
703 | Aggregate agg = null;
|
||
704 | 42191 | fdiaz | |
705 | 40435 | jjdelcerro | /*
|
706 | * If input geometry is aggregate, search its
|
||
707 | * primitives
|
||
708 | */
|
||
709 | if (in_geom instanceof Aggregate) { |
||
710 | agg = (Aggregate) in_geom; |
||
711 | Geometry item = null;
|
||
712 | List<Geometry> add_parts = new ArrayList<Geometry>(); |
||
713 | for (int i=0; i<agg.getPrimitivesNumber(); i++) { |
||
714 | item = agg.getPrimitiveAt(i); |
||
715 | add_parts = getGeometriesFrom(item, type); |
||
716 | resp.addAll(add_parts); |
||
717 | } |
||
718 | return resp;
|
||
719 | } |
||
720 | 42191 | fdiaz | |
721 | 40435 | jjdelcerro | // ============================================
|
722 | 42191 | fdiaz | |
723 | 40435 | jjdelcerro | switch (type) {
|
724 | case Geometry.TYPES.POINT:
|
||
725 | if (in_geom instanceof Point) { |
||
726 | resp.add(in_geom); |
||
727 | } |
||
728 | // =======================================================
|
||
729 | break;
|
||
730 | case Geometry.TYPES.CURVE:
|
||
731 | if (in_geom instanceof Curve) { |
||
732 | resp.add(in_geom); |
||
733 | } |
||
734 | // =======================================================
|
||
735 | break;
|
||
736 | case Geometry.TYPES.SURFACE:
|
||
737 | if (in_geom instanceof Surface) { |
||
738 | resp.add(in_geom); |
||
739 | } |
||
740 | // =======================================================
|
||
741 | break;
|
||
742 | } |
||
743 | return resp;
|
||
744 | } |
||
745 | |||
746 | 40490 | jldominguez | private void finishAction(String layerName, File shp_file, IProjection proj) |
747 | throws ExporttoServiceException {
|
||
748 | 42191 | fdiaz | |
749 | 40435 | jjdelcerro | if (exporttoServiceFinishAction != null) { |
750 | 42191 | fdiaz | |
751 | 40490 | jldominguez | /*
|
752 | * Export is done. We notify with a SHPStoreParameters,
|
||
753 | * not with the NewSHPParameters we have used:
|
||
754 | */
|
||
755 | 41023 | jjdelcerro | DataStoreParameters shp_params = null;
|
756 | 40490 | jldominguez | try {
|
757 | 41023 | jjdelcerro | shp_params = DALLocator.getDataManager().createStoreParameters("Shape");
|
758 | shp_params.setDynValue("shpfile", shp_file);
|
||
759 | shp_params.setDynValue("CRS", proj);
|
||
760 | 42191 | fdiaz | |
761 | 40490 | jldominguez | shp_params.validate(); |
762 | 41023 | jjdelcerro | } catch (Exception e) { |
763 | 40490 | jldominguez | throw new ExporttoServiceException(e); |
764 | } |
||
765 | exporttoServiceFinishAction.finished(layerName, shp_params); |
||
766 | 40435 | jjdelcerro | } |
767 | } |
||
768 | |||
769 | public void setFinishAction( |
||
770 | ExporttoServiceFinishAction exporttoServiceFinishAction) { |
||
771 | this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
||
772 | } |
||
773 | } |