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