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