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