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 @ 41655
History | View | Annotate | Download (23.2 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 = 11; |
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 |
String pref = name.substring(0, 6) + "_" + name.substring(len - 2) + "_"; |
149 |
String resp = null; |
150 |
for (int i=0; i<10; i++) { |
151 |
resp = pref + i; |
152 |
if (!values.contains(resp)) {
|
153 |
return resp;
|
154 |
} |
155 |
} |
156 |
/*
|
157 |
* Very strange to get here...
|
158 |
* THISISAVERYLONGNAME => THISISA_1, THISISA_2 ... THISISA_999
|
159 |
*/
|
160 |
pref = name.substring(0, 7) + "_"; |
161 |
for (int i=0; i<1000; i++) { |
162 |
resp = pref + i; |
163 |
if (!values.contains(resp)) {
|
164 |
return resp;
|
165 |
} |
166 |
} |
167 |
/*
|
168 |
* Should not get here
|
169 |
*/
|
170 |
return name.substring(0, 4) + "_" + (System.currentTimeMillis() % 1000000); |
171 |
} |
172 |
|
173 |
public void export(FeatureSet featureSet) throws ExporttoServiceException { |
174 |
|
175 |
ExporttoServiceException throw_exp = null;
|
176 |
File item_shp = null; |
177 |
String pathFile = theShapeFile.getAbsolutePath();
|
178 |
String withoutShp = pathFile.replaceAll("\\.shp", ""); |
179 |
|
180 |
File single_file = new File(withoutShp + ".shp"); |
181 |
|
182 |
String layer_name = single_file.getName();
|
183 |
int lastp = layer_name.lastIndexOf(".shp"); |
184 |
if (lastp > -1) { |
185 |
layer_name = layer_name.substring(0, lastp);
|
186 |
} |
187 |
|
188 |
initializeParams(featureSet, single_file); |
189 |
|
190 |
if (geometryType == Geometry.TYPES.GEOMETRY) {
|
191 |
// POINT
|
192 |
String fileName = withoutShp + "_point" + ".shp"; |
193 |
item_shp = new File(fileName); |
194 |
initializeParams(featureSet, item_shp); |
195 |
|
196 |
newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
197 |
taskStatus.setTittle("Exporting points");
|
198 |
|
199 |
try {
|
200 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
201 |
featureSet, Geometry.TYPES.POINT, true);
|
202 |
} catch (ExporttoServiceException ee) {
|
203 |
throw_exp = ee; |
204 |
} |
205 |
|
206 |
finishAction(layer_name + " (point)", item_shp, projection);
|
207 |
|
208 |
// CURVE
|
209 |
fileName = withoutShp + "_curve" + ".shp"; |
210 |
item_shp = new File(fileName); |
211 |
initializeParams(featureSet, item_shp); |
212 |
newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
213 |
taskStatus.setTittle("Exporting curves");
|
214 |
|
215 |
try {
|
216 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
217 |
featureSet, Geometry.TYPES.CURVE, true);
|
218 |
} catch (ExporttoServiceException ee) {
|
219 |
throw_exp = ee; |
220 |
} |
221 |
|
222 |
finishAction(layer_name + " (curve)", item_shp, projection);
|
223 |
|
224 |
// SURFACE
|
225 |
fileName = withoutShp + "_surface" + ".shp"; |
226 |
item_shp = new File(fileName); |
227 |
initializeParams(featureSet, item_shp); |
228 |
newFeatureStoreParameters.setDynValue("shpfile", item_shp);
|
229 |
taskStatus.setTittle("Exporting surfaces");
|
230 |
|
231 |
try {
|
232 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
233 |
featureSet, Geometry.TYPES.SURFACE, true);
|
234 |
} catch (ExporttoServiceException ee) {
|
235 |
throw_exp = ee; |
236 |
} |
237 |
finishAction(layer_name + " (surface)", item_shp, projection);
|
238 |
|
239 |
} else {
|
240 |
|
241 |
// params already initialized
|
242 |
newFeatureStoreParameters.setDynValue("shpfile", single_file);
|
243 |
try {
|
244 |
export(filesystemServerExplorer, newFeatureStoreParameters,
|
245 |
featureSet, geometryType, false);
|
246 |
} catch (ExporttoServiceException ee) {
|
247 |
throw_exp = ee; |
248 |
} |
249 |
finishAction(layer_name, single_file, projection); |
250 |
} |
251 |
this.taskStatus.terminate();
|
252 |
this.taskStatus.remove();
|
253 |
|
254 |
if (throw_exp != null) { |
255 |
throw throw_exp;
|
256 |
} else {
|
257 |
if (origNameToDbfName.size() > 0) { |
258 |
showNewNamesDialog(origNameToDbfName); |
259 |
} |
260 |
} |
261 |
} |
262 |
|
263 |
private void showNewNamesDialog(Map<String, String> map) { |
264 |
|
265 |
Iterator<String> iter = map.keySet().iterator(); |
266 |
|
267 |
String msg = Messages.getText("_Some_field_names_too_long_automatically_changed"); |
268 |
msg = msg + ":\n";
|
269 |
String k, v;
|
270 |
while (iter.hasNext()) {
|
271 |
k = iter.next(); |
272 |
v = map.get(k); |
273 |
msg = msg + "\n" + k + " > " + v; |
274 |
} |
275 |
|
276 |
JOptionPane.showMessageDialog(
|
277 |
this.filePanel,
|
278 |
msg, |
279 |
Messages.getText("export_progress"),
|
280 |
JOptionPane.WARNING_MESSAGE);
|
281 |
} |
282 |
|
283 |
private void initializeParams( |
284 |
FeatureSet featureSet, File out_shp_file)
|
285 |
throws ExporttoServiceException {
|
286 |
|
287 |
DataManager dataManager = DALLocator.getDataManager(); |
288 |
|
289 |
FilesystemServerExplorerParameters explorerParams; |
290 |
try {
|
291 |
explorerParams = |
292 |
(FilesystemServerExplorerParameters) dataManager |
293 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
294 |
} catch (InitializeException e) {
|
295 |
throw new ExporttoServiceException(e); |
296 |
} catch (ProviderNotRegisteredException e) {
|
297 |
throw new ExporttoServiceException(e); |
298 |
} |
299 |
explorerParams.setRoot(out_shp_file.getParent()); |
300 |
|
301 |
try {
|
302 |
filesystemServerExplorer = |
303 |
(FilesystemServerExplorer) dataManager.openServerExplorer( |
304 |
"FilesystemExplorer", explorerParams);
|
305 |
} catch (ValidateDataParametersException e) {
|
306 |
throw new ExporttoServiceException(e); |
307 |
} catch (InitializeException e) {
|
308 |
throw new ExporttoServiceException(e); |
309 |
} catch (ProviderNotRegisteredException e) {
|
310 |
throw new ExporttoServiceException(e); |
311 |
} |
312 |
|
313 |
try {
|
314 |
newFeatureStoreParameters = |
315 |
(NewFeatureStoreParameters) filesystemServerExplorer |
316 |
.getAddParameters(out_shp_file); |
317 |
} catch (DataException e) {
|
318 |
throw new ExporttoServiceException(e); |
319 |
} |
320 |
|
321 |
newFeatureStoreParameters.setDynValue("CRS", projection);
|
322 |
|
323 |
geometryType = |
324 |
featureSet.getDefaultFeatureType().getDefaultGeometryAttribute() |
325 |
.getGeomType().getType(); |
326 |
|
327 |
} |
328 |
|
329 |
private void export(FilesystemServerExplorer explorer, |
330 |
NewFeatureStoreParameters params, FeatureSet featureSet, |
331 |
int geometryType, boolean checkType) throws ExporttoServiceException { |
332 |
|
333 |
String providerName = params.getDataStoreName();
|
334 |
String explorerName = explorer.getProviderName();
|
335 |
boolean there_was_error = false; |
336 |
|
337 |
DisposableIterator it = null;
|
338 |
try {
|
339 |
EditableFeatureType type = |
340 |
featureStore.getDefaultFeatureType().getCopy().getEditable(); |
341 |
FeatureAttributeDescriptor fad = |
342 |
(FeatureAttributeDescriptor) type.get(type |
343 |
.getDefaultGeometryAttributeName()); |
344 |
type.remove(fad.getName()); |
345 |
EditableFeatureAttributeDescriptor efad = |
346 |
type.add(fad.getName(), fad.getType(), fad.getSize()); |
347 |
efad.setDefaultValue(fad.getDefaultValue()); |
348 |
|
349 |
GeometryType gty = null;
|
350 |
try {
|
351 |
gty = geoManager.getGeometryType( |
352 |
geometryType, Geometry.SUBTYPES.GEOM2D); |
353 |
} catch (Exception e) { |
354 |
throw new ExporttoServiceException(e); |
355 |
} |
356 |
|
357 |
efad.setGeometryType(gty); |
358 |
|
359 |
efad.setPrecision(fad.getPrecision()); |
360 |
type.setDefaultGeometryAttributeName(fad.getName()); |
361 |
|
362 |
// ==========================
|
363 |
fixNames(type); |
364 |
// ==========================
|
365 |
|
366 |
params.setDefaultFeatureType(type); |
367 |
|
368 |
params.setDynValue("geometryType", null); |
369 |
|
370 |
DataManager manager = DALLocator.getDataManager(); |
371 |
|
372 |
manager.newStore(explorerName, providerName, params, true);
|
373 |
FeatureStore target = |
374 |
(FeatureStore) manager.openStore(providerName, params); |
375 |
|
376 |
FeatureType targetType = target.getDefaultFeatureType(); |
377 |
|
378 |
taskStatus.setRangeOfValues(0, featureSet.getSize());
|
379 |
|
380 |
target.edit(FeatureStore.MODE_APPEND); |
381 |
it = featureSet.fastIterator(); |
382 |
int featureCount = 0; |
383 |
|
384 |
// ================================================
|
385 |
// Reprojection stuff
|
386 |
Geometry reproj_geom = null;
|
387 |
EditableFeature edit_feat = null;
|
388 |
IProjection sourceProjection = |
389 |
featureStore.getDefaultFeatureType().getDefaultGeometryAttribute().getSRS(); |
390 |
|
391 |
ICoordTrans coord_trans = null;
|
392 |
// this comparison is perhaps too preventive
|
393 |
// we could have two instances of same projection
|
394 |
// so we would do more computations than needed
|
395 |
if (sourceProjection != this.projection) { |
396 |
coord_trans = sourceProjection.getCT(this.projection);
|
397 |
} |
398 |
// ================================================
|
399 |
|
400 |
List<Geometry> extracted = null; |
401 |
Geometry gitem = null;
|
402 |
|
403 |
while (it.hasNext()) {
|
404 |
|
405 |
Feature feature = (Feature) it.next(); |
406 |
gitem = feature.getDefaultGeometry(); |
407 |
|
408 |
if (checkType) {
|
409 |
extracted = getGeometriesFrom(gitem, geometryType); |
410 |
if (extracted.size() == 0) { |
411 |
// found no geometries of correct type
|
412 |
continue;
|
413 |
} else {
|
414 |
if (geometryType != Geometry.TYPES.POINT) {
|
415 |
// If not points, merge geometries
|
416 |
// (curves or surfaces)
|
417 |
try {
|
418 |
gitem = union(extracted); |
419 |
extracted = new ArrayList<Geometry>(); |
420 |
extracted.add(gitem); |
421 |
} catch (Exception ex) { |
422 |
there_was_error = true;
|
423 |
logger.info("Error in union.", ex);
|
424 |
} |
425 |
} else {
|
426 |
// only in the case of points, we can have several
|
427 |
// geometries if source is multipoint
|
428 |
} |
429 |
} |
430 |
} else {
|
431 |
extracted = new ArrayList<Geometry>(); |
432 |
extracted.add(gitem); |
433 |
} |
434 |
|
435 |
for (int i=0; i<extracted.size(); i++) { |
436 |
gitem = extracted.get(i); |
437 |
//gitem = force2D(gitem, geometryType);
|
438 |
edit_feat = target.createNewFeature(true);
|
439 |
there_was_error = there_was_error | |
440 |
/*
|
441 |
* Accumulate error in boolean.
|
442 |
* This also fixes field names (using origNameToDbfName)
|
443 |
*/
|
444 |
setNonNulls(featureStore.getDefaultFeatureType(), |
445 |
targetType, feature, edit_feat); |
446 |
edit_feat.setDefaultGeometry(gitem); |
447 |
// ================================================
|
448 |
// Reprojection stuff
|
449 |
if (coord_trans != null) { |
450 |
reproj_geom = edit_feat.getDefaultGeometry(); |
451 |
reproj_geom = reproj_geom.cloneGeometry(); |
452 |
reproj_geom.reProject(coord_trans); |
453 |
edit_feat.setDefaultGeometry(reproj_geom); |
454 |
} |
455 |
// ================================================
|
456 |
target.insert(edit_feat); |
457 |
} |
458 |
|
459 |
featureCount++; |
460 |
this.taskStatus.setCurValue(featureCount);
|
461 |
|
462 |
if (this.taskStatus.isCancellationRequested()) { |
463 |
return;
|
464 |
} |
465 |
} |
466 |
target.finishEditing(); |
467 |
target.dispose(); |
468 |
} catch (Exception e) { |
469 |
throw new ExporttoServiceException(e); |
470 |
} finally {
|
471 |
DisposeUtils.dispose(it); |
472 |
} |
473 |
|
474 |
if (there_was_error) {
|
475 |
Exception cause = new Exception( |
476 |
"_Issues_with_attributes_or_geometries");
|
477 |
throw new ExporttoServiceException(cause); |
478 |
} |
479 |
} |
480 |
|
481 |
|
482 |
private void fixNames(EditableFeatureType eft) { |
483 |
|
484 |
FeatureAttributeDescriptor[] atts = eft.getAttributeDescriptors();
|
485 |
EditableFeatureAttributeDescriptor efad = null;
|
486 |
for (int i=0; i<atts.length; i++) { |
487 |
String new_name = origNameToDbfName.get(atts[i].getName());
|
488 |
if (new_name != null) { |
489 |
eft.remove(atts[i].getName()); |
490 |
efad = eft.add(new_name, atts[i].getType(), atts[i].getSize()); |
491 |
efad.setPrecision(atts[i].getPrecision()); |
492 |
} |
493 |
} |
494 |
} |
495 |
|
496 |
/**
|
497 |
* @param gitem
|
498 |
* @param geometryType2
|
499 |
* @return
|
500 |
*/
|
501 |
private Geometry force2D(Geometry ge, int gt) throws CreateGeometryException { |
502 |
|
503 |
if (ge.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM2D) {
|
504 |
return ge;
|
505 |
} else {
|
506 |
switch (gt) {
|
507 |
case Geometry.TYPES.POINT:
|
508 |
Point p = (Point) ge; |
509 |
return geoManager.createPoint(
|
510 |
p.getX(), p.getY(), |
511 |
Geometry.SUBTYPES.GEOM2D); |
512 |
case Geometry.TYPES.CURVE:
|
513 |
return geoManager.createCurve(ge.getGeneralPath(), Geometry.SUBTYPES.GEOM2D);
|
514 |
case Geometry.TYPES.SURFACE:
|
515 |
return geoManager.createSurface(ge.getGeneralPath(), Geometry.SUBTYPES.GEOM2D);
|
516 |
default:
|
517 |
return ge;
|
518 |
} |
519 |
} |
520 |
} |
521 |
|
522 |
/**
|
523 |
* @param feature
|
524 |
* @param edit_feat
|
525 |
*/
|
526 |
private boolean setNonNulls( |
527 |
FeatureType src_ft, FeatureType target_ft, |
528 |
Feature feat, EditableFeature edit_f) { |
529 |
|
530 |
boolean error = false; |
531 |
FeatureAttributeDescriptor[] atts = src_ft.getAttributeDescriptors();
|
532 |
String orig_name = null; |
533 |
String dbf_name = null; |
534 |
for (int i=0; i<atts.length; i++) { |
535 |
if (atts[i].getType() != org.gvsig.fmap.geom.DataTypes.GEOMETRY) {
|
536 |
|
537 |
Object val = null; |
538 |
|
539 |
try {
|
540 |
orig_name = atts[i].getName(); |
541 |
dbf_name = origNameToDbfName.get(orig_name); |
542 |
if (dbf_name == null) { |
543 |
dbf_name = orig_name; |
544 |
} |
545 |
val = feat.get(orig_name); |
546 |
if (val != null) { |
547 |
edit_f.set(dbf_name, val); |
548 |
} |
549 |
} catch (Exception ex) { |
550 |
logger.info("Error while getting/setting value", ex);
|
551 |
error = true;
|
552 |
} |
553 |
} |
554 |
} |
555 |
return error;
|
556 |
} |
557 |
|
558 |
private Geometry union(List<Geometry> geoms) |
559 |
throws GeometryOperationNotSupportedException, GeometryOperationException {
|
560 |
|
561 |
if (geoms == null || geoms.size() == 0) { |
562 |
return null; |
563 |
} |
564 |
|
565 |
if (geoms.size() == 1) { |
566 |
return geoms.get(0); |
567 |
} |
568 |
|
569 |
Geometry resp = geoms.get(0);
|
570 |
for (int i=1; i<geoms.size(); i++) { |
571 |
resp = resp.union(geoms.get(i)); |
572 |
} |
573 |
return resp;
|
574 |
} |
575 |
/**
|
576 |
* @param feat_geom_type
|
577 |
* @param type must be POINT, LINE or POLYGON (Geometry.TYPES)
|
578 |
* @return
|
579 |
*/
|
580 |
private List<Geometry> getGeometriesFrom(Geometry in_geom, int type) { |
581 |
|
582 |
List<Geometry> resp = new ArrayList<Geometry>(); |
583 |
Aggregate agg = null;
|
584 |
|
585 |
/*
|
586 |
* If input geometry is aggregate, search its
|
587 |
* primitives
|
588 |
*/
|
589 |
if (in_geom instanceof Aggregate) { |
590 |
agg = (Aggregate) in_geom; |
591 |
Geometry item = null;
|
592 |
List<Geometry> add_parts = new ArrayList<Geometry>(); |
593 |
for (int i=0; i<agg.getPrimitivesNumber(); i++) { |
594 |
item = agg.getPrimitiveAt(i); |
595 |
add_parts = getGeometriesFrom(item, type); |
596 |
resp.addAll(add_parts); |
597 |
} |
598 |
return resp;
|
599 |
} |
600 |
|
601 |
// ============================================
|
602 |
|
603 |
switch (type) {
|
604 |
case Geometry.TYPES.POINT:
|
605 |
if (in_geom instanceof Point) { |
606 |
resp.add(in_geom); |
607 |
} |
608 |
// =======================================================
|
609 |
break;
|
610 |
case Geometry.TYPES.CURVE:
|
611 |
if (in_geom instanceof Curve) { |
612 |
resp.add(in_geom); |
613 |
} |
614 |
// =======================================================
|
615 |
break;
|
616 |
case Geometry.TYPES.SURFACE:
|
617 |
if (in_geom instanceof Surface) { |
618 |
resp.add(in_geom); |
619 |
} |
620 |
// =======================================================
|
621 |
break;
|
622 |
} |
623 |
return resp;
|
624 |
} |
625 |
|
626 |
private void finishAction(String layerName, File shp_file, IProjection proj) |
627 |
throws ExporttoServiceException {
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
if (exporttoServiceFinishAction != null) { |
634 |
|
635 |
/*
|
636 |
* Export is done. We notify with a SHPStoreParameters,
|
637 |
* not with the NewSHPParameters we have used:
|
638 |
*/
|
639 |
DataStoreParameters shp_params = null;
|
640 |
try {
|
641 |
shp_params = DALLocator.getDataManager().createStoreParameters("Shape");
|
642 |
shp_params.setDynValue("shpfile", shp_file);
|
643 |
shp_params.setDynValue("CRS", proj);
|
644 |
|
645 |
shp_params.validate(); |
646 |
} catch (Exception e) { |
647 |
throw new ExporttoServiceException(e); |
648 |
} |
649 |
exporttoServiceFinishAction.finished(layerName, shp_params); |
650 |
} |
651 |
} |
652 |
|
653 |
public void setFinishAction( |
654 |
ExporttoServiceFinishAction exporttoServiceFinishAction) { |
655 |
this.exporttoServiceFinishAction = exporttoServiceFinishAction;
|
656 |
} |
657 |
} |