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