gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.app / org.gvsig.geoprocess.app.mainplugin / src / main / java / org / gvsig / geoprocess / core / gvVectorLayer.java @ 175
History | View | Annotate | Download (15.4 KB)
1 |
package org.gvsig.geoprocess.core; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.sql.Types; |
5 |
import java.util.Iterator; |
6 |
|
7 |
import com.vividsolutions.jts.geom.Geometry; |
8 |
|
9 |
import es.unex.sextante.core.Sextante; |
10 |
import es.unex.sextante.dataObjects.AbstractVectorLayer; |
11 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
12 |
import es.unex.sextante.dataObjects.IVectorLayer; |
13 |
import es.unex.sextante.outputs.FileOutputChannel; |
14 |
import es.unex.sextante.outputs.IOutputChannel; |
15 |
|
16 |
import org.cresques.cts.IProjection; |
17 |
|
18 |
import org.gvsig.fmap.dal.DALLocator; |
19 |
import org.gvsig.fmap.dal.DataManager; |
20 |
import org.gvsig.fmap.dal.DataStoreParameters; |
21 |
import org.gvsig.fmap.dal.DataTypes; |
22 |
import org.gvsig.fmap.dal.exception.DataException; |
23 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
24 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
25 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
26 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
27 |
import org.gvsig.fmap.dal.feature.FeatureType; |
28 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
29 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
30 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
31 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
32 |
import org.gvsig.fmap.geom.GeometryLocator; |
33 |
import org.gvsig.fmap.geom.GeometryManager; |
34 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
35 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
36 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
37 |
import org.gvsig.fmap.geom.operation.fromjts.FromJTS; |
38 |
import org.gvsig.fmap.geom.type.GeometryType; |
39 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
40 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
41 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
42 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
43 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
44 |
import org.gvsig.tools.ToolsLocator; |
45 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
46 |
|
47 |
public class gvVectorLayer extends AbstractVectorLayer { |
48 |
|
49 |
private final int PRECISION = 5; |
50 |
private FeatureStore featureStore = null; |
51 |
private IProjection m_Projection = null; |
52 |
private FeatureType featureType = null; |
53 |
private GeometryManager geometryManager = null; |
54 |
private FLyrVect m_Layer;
|
55 |
private String m_sName; |
56 |
private IOutputChannel m_Channel;
|
57 |
|
58 |
public gvVectorLayer() {
|
59 |
geometryManager = GeometryLocator.getGeometryManager(); |
60 |
} |
61 |
|
62 |
public void create(final String sName, final String sFilename, |
63 |
final int iShapeType, final Class<?>[] types, final String[] sFields, |
64 |
final Object crs, final int[] fieldSize) { |
65 |
|
66 |
try {
|
67 |
m_sName = sName; |
68 |
m_Projection = (IProjection) crs; |
69 |
|
70 |
final DataManager datamanager = DALLocator.getDataManager();
|
71 |
final FilesystemServerExplorerParameters explorerParams =
|
72 |
(FilesystemServerExplorerParameters) datamanager |
73 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
74 |
explorerParams.setRoot(new File(sFilename).getParent()); |
75 |
final FilesystemServerExplorer explorer =
|
76 |
(FilesystemServerExplorer) datamanager.openServerExplorer( |
77 |
FilesystemServerExplorer.NAME, explorerParams); |
78 |
final NewFeatureStoreParameters newParams =
|
79 |
(NewFeatureStoreParameters) explorer.getAddParameters(new File( |
80 |
sFilename)); |
81 |
|
82 |
EditableFeatureType editableFeatureType = |
83 |
newParams.getDefaultFeatureType(); |
84 |
featureType = editableFeatureType; |
85 |
loadFeatureType(sFields, types, iShapeType, crs, |
86 |
editableFeatureType, fieldSize); |
87 |
|
88 |
newParams.setDefaultFeatureType(featureType); |
89 |
newParams.setDynValue("crs", m_Projection);
|
90 |
|
91 |
explorer.add(newParams.getDataStoreName(), newParams, true);
|
92 |
final DataManager manager = DALLocator.getDataManager();
|
93 |
featureStore = |
94 |
(FeatureStore) manager.openStore(newParams.getDataStoreName(), |
95 |
newParams); |
96 |
featureStore.edit(FeatureStore.MODE_APPEND); |
97 |
featureType = featureStore.getDefaultFeatureType(); |
98 |
|
99 |
} catch (final Exception e) { |
100 |
Sextante.addErrorToLog(e); |
101 |
} |
102 |
} |
103 |
|
104 |
private void loadFeatureType(final String[] fields, final Class<?>[] types, |
105 |
final int shapeType, final Object crs, |
106 |
final EditableFeatureType featureType, final int[] fieldSize) { |
107 |
final int[] iTypes = getTypes(types); |
108 |
|
109 |
for (int i = 0; i < fields.length; i++) { |
110 |
if (iTypes[i] == org.gvsig.fmap.geom.DataTypes.GEOMETRY) {
|
111 |
addGeometryAttribute(fields[i], shapeType, featureType); |
112 |
} else {
|
113 |
featureType.add(fields[i], iTypes[i]).setSize(fieldSize[i]) |
114 |
.setPrecision(PRECISION); |
115 |
} |
116 |
} |
117 |
|
118 |
FeatureAttributeDescriptor desc = |
119 |
featureType.getAttributeDescriptor("GEOMETRY");
|
120 |
if (desc == null) { |
121 |
addGeometryAttribute("GEOMETRY", shapeType, featureType);
|
122 |
} |
123 |
featureType.setDefaultGeometryAttributeName("GEOMETRY");
|
124 |
} |
125 |
|
126 |
private void addGeometryAttribute(final String fieldName, |
127 |
final int shapeType, final EditableFeatureType featureType) { |
128 |
try {
|
129 |
GeometryType type = |
130 |
geometryManager.getGeometryType(getgvSIGShapeType(shapeType), |
131 |
org.gvsig.fmap.geom.Geometry.SUBTYPES.GEOM2D); |
132 |
featureType.add(fieldName, DataTypes.GEOMETRY) |
133 |
.setGeometryType(type); |
134 |
} catch (GeometryTypeNotSupportedException e) {
|
135 |
throw new RuntimeException( |
136 |
"Error getting geometry type with type = "
|
137 |
+ getgvSIGShapeType(shapeType) + ", subtype = "
|
138 |
+ org.gvsig.fmap.geom.Geometry.SUBTYPES.GEOM2D, e); |
139 |
} catch (GeometryTypeNotValidException e) {
|
140 |
throw new RuntimeException( |
141 |
"Error getting geometry type with type = "
|
142 |
+ getgvSIGShapeType(shapeType) + ", subtype = "
|
143 |
+ org.gvsig.fmap.geom.Geometry.SUBTYPES.GEOM2D, e); |
144 |
} |
145 |
} |
146 |
|
147 |
private int[] getTypes(Class<?>[] classes) { |
148 |
DataTypesManager typesManager = ToolsLocator.getDataTypesManager(); |
149 |
int[] types = new int[classes.length]; |
150 |
for (int i = 0; i < classes.length; i++) { |
151 |
types[i] = typesManager.getDataType(classes[i]).getType(); |
152 |
} |
153 |
return types;
|
154 |
} |
155 |
|
156 |
public void create(final FLyrVect layer) { |
157 |
|
158 |
m_Layer = layer; |
159 |
try {
|
160 |
featureStore = layer.getFeatureStore(); |
161 |
featureType = featureStore.getDefaultFeatureType(); |
162 |
m_Projection = layer.getProjection(); |
163 |
} catch (final Exception e) { |
164 |
Sextante.addErrorToLog(e); |
165 |
} |
166 |
|
167 |
} |
168 |
|
169 |
@SuppressWarnings("deprecation") |
170 |
public void open() { |
171 |
|
172 |
if (m_Layer == null) { |
173 |
try {
|
174 |
m_Layer = |
175 |
(FLyrVect) LayerFactory.getInstance().createLayer( |
176 |
getFilename(), featureStore); |
177 |
} catch (final LoadLayerException e) { |
178 |
Sextante.addErrorToLog(e); |
179 |
throw new RuntimeException(e); |
180 |
} |
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
public void close() { |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Returns the length of field
|
190 |
*
|
191 |
* @param dataType
|
192 |
* @return length of field
|
193 |
*/
|
194 |
public int getDataTypeLength(final int dataType) { |
195 |
|
196 |
switch (dataType) {
|
197 |
case Types.NUMERIC: |
198 |
case Types.DOUBLE: |
199 |
case Types.REAL: |
200 |
case Types.FLOAT: |
201 |
case Types.BIGINT: |
202 |
case Types.INTEGER: |
203 |
case Types.DECIMAL: |
204 |
return 20; |
205 |
case Types.CHAR: |
206 |
case Types.VARCHAR: |
207 |
case Types.LONGVARCHAR: |
208 |
return 254; |
209 |
case Types.DATE: |
210 |
return 8; |
211 |
case Types.BOOLEAN: |
212 |
case Types.BIT: |
213 |
return 1; |
214 |
} |
215 |
return 0; |
216 |
|
217 |
} |
218 |
|
219 |
@SuppressWarnings("unchecked") |
220 |
public void addFeature(final Geometry geom, final Object[] values) { |
221 |
try {
|
222 |
org.gvsig.fmap.geom.Geometry iGeo = null;
|
223 |
|
224 |
final GeometryOperationContext ctx = new GeometryOperationContext(); |
225 |
ctx.setAttribute(FromJTS.PARAM, geom); |
226 |
try {
|
227 |
iGeo = |
228 |
(org.gvsig.fmap.geom.Geometry) geometryManager |
229 |
.invokeOperation(FromJTS.NAME, ctx); |
230 |
} catch (final GeometryOperationNotSupportedException e) { |
231 |
Sextante.addErrorToLog(e); |
232 |
return;
|
233 |
} catch (final GeometryOperationException e) { |
234 |
Sextante.addErrorToLog(e); |
235 |
return;
|
236 |
} |
237 |
|
238 |
final EditableFeature ef = featureStore.createNewFeature();
|
239 |
final Iterator<FeatureAttributeDescriptor> features = |
240 |
featureType.iterator(); |
241 |
|
242 |
int i = 0; |
243 |
while (features.hasNext()) {
|
244 |
final FeatureAttributeDescriptor featureAttributeDescriptor =
|
245 |
features.next(); |
246 |
// if (!featureAttributeDescriptor.getName().equals(
|
247 |
// featureType.getDefaultGeometryAttributeName())) {
|
248 |
// ef.set(
|
249 |
// featureAttributeDescriptor.getName().substring(
|
250 |
// 0,
|
251 |
// Math.min(11, featureAttributeDescriptor.getName()
|
252 |
// .length())), values[i]);
|
253 |
// i++;
|
254 |
// }
|
255 |
if (!featureAttributeDescriptor.getName().equals(
|
256 |
featureType.getDefaultGeometryAttributeName())) { |
257 |
ef.set(featureAttributeDescriptor.getName(), values[i]); |
258 |
} |
259 |
i++; |
260 |
} |
261 |
ef.set(featureType.getDefaultGeometryAttributeIndex(), iGeo); |
262 |
featureStore.insert(ef); |
263 |
} catch (final Exception e) { |
264 |
e.printStackTrace(); |
265 |
} |
266 |
} |
267 |
|
268 |
public IFeatureIterator iterator() {
|
269 |
|
270 |
if (m_Layer != null) { |
271 |
return new gvFeatureIterator(m_Layer, getFilters()); |
272 |
} else {
|
273 |
// cannot iterate layers being edited
|
274 |
return new gvFeatureIterator(); |
275 |
} |
276 |
} |
277 |
|
278 |
public String getFieldName(final int i) { |
279 |
|
280 |
if (featureStore != null) { |
281 |
return ((FeatureAttributeDescriptor) featureType.get(i)).getName();
|
282 |
} |
283 |
return null; |
284 |
|
285 |
} |
286 |
|
287 |
public Class<?> getFieldType(final int i) { |
288 |
return featureStore == null ? null : featureType |
289 |
.getAttributeDescriptor(i).getDataType().getDefaultClass(); |
290 |
} |
291 |
|
292 |
public int getFieldCount() { |
293 |
if (featureStore != null) { |
294 |
return featureType.size();
|
295 |
} |
296 |
return 0; |
297 |
|
298 |
} |
299 |
|
300 |
@Override
|
301 |
public int getShapesCount() { |
302 |
|
303 |
if (featureStore != null) { |
304 |
try {
|
305 |
return (int) featureStore.getFeatureSet().getSize(); |
306 |
} catch (final DataException e) { |
307 |
Sextante.addErrorToLog(e); |
308 |
return 0; |
309 |
} |
310 |
} |
311 |
return 0; |
312 |
|
313 |
} |
314 |
|
315 |
public int getShapeType() { |
316 |
if (featureStore != null) { |
317 |
return getShapeTypeFromGvSIGShapeType(((FeatureAttributeDescriptor) featureType
|
318 |
.get(featureType.getDefaultGeometryAttributeIndex())) |
319 |
.getGeomType().getType()); |
320 |
} |
321 |
return 0; |
322 |
} |
323 |
|
324 |
private int getShapeTypeFromGvSIGShapeType(final int shapeType) { |
325 |
|
326 |
switch (shapeType) {
|
327 |
case org.gvsig.fmap.geom.Geometry.TYPES.SURFACE:
|
328 |
return IVectorLayer.SHAPE_TYPE_POLYGON;
|
329 |
case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
|
330 |
case org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE:
|
331 |
return IVectorLayer.SHAPE_TYPE_LINE;
|
332 |
case org.gvsig.fmap.geom.Geometry.TYPES.POINT:
|
333 |
case org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT:
|
334 |
return IVectorLayer.SHAPE_TYPE_POINT;
|
335 |
default:
|
336 |
return IVectorLayer.SHAPE_TYPE_POLYGON;
|
337 |
} |
338 |
|
339 |
} |
340 |
|
341 |
private int getgvSIGShapeType(final int shapeType) { |
342 |
|
343 |
switch (shapeType) {
|
344 |
case IVectorLayer.SHAPE_TYPE_POLYGON:
|
345 |
return org.gvsig.fmap.geom.Geometry.TYPES.SURFACE;
|
346 |
case IVectorLayer.SHAPE_TYPE_LINE:
|
347 |
return org.gvsig.fmap.geom.Geometry.TYPES.CURVE;
|
348 |
case IVectorLayer.SHAPE_TYPE_POINT:
|
349 |
return org.gvsig.fmap.geom.Geometry.TYPES.POINT;
|
350 |
default:
|
351 |
return org.gvsig.fmap.geom.Geometry.TYPES.SURFACE;
|
352 |
} |
353 |
|
354 |
} |
355 |
|
356 |
public String getName() { |
357 |
|
358 |
if (m_Layer != null) { |
359 |
return m_Layer.getName();
|
360 |
} else {
|
361 |
return m_sName;
|
362 |
} |
363 |
} |
364 |
|
365 |
public void postProcess() { |
366 |
|
367 |
if (featureStore == null) { |
368 |
return;
|
369 |
} |
370 |
try {
|
371 |
featureStore.finishEditing(); |
372 |
} catch (final DataException e) { |
373 |
// No puede finalizar la edici�n. Es posible que se haya cerrado
|
374 |
// previamente.
|
375 |
} |
376 |
|
377 |
/*
|
378 |
* FLyrVect vectorLayer = null;
|
379 |
* if (inputParameters instanceof String) {
|
380 |
* vectorLayer = (FLyrVect) FileTools.openLayer((String)
|
381 |
* inputParameters, s_Name, m_Projection);
|
382 |
* }
|
383 |
* if (inputParameters instanceof OutputParameters) {
|
384 |
* vectorLayer = (FLyrVect) FileTools.openLayer(((OutputParameters)
|
385 |
* inputParameters).getDataParameters(), s_Name,
|
386 |
* m_Projection);
|
387 |
* }
|
388 |
* create(vectorLayer);
|
389 |
*/
|
390 |
|
391 |
} |
392 |
|
393 |
public String getFilename() { |
394 |
|
395 |
if (featureStore != null) { |
396 |
final DataStoreParameters dsp = featureStore.getParameters();
|
397 |
if (dsp instanceof FilesystemStoreParameters) { |
398 |
return ((FilesystemStoreParameters) dsp).getFile()
|
399 |
.getAbsolutePath(); |
400 |
} else {
|
401 |
return null; |
402 |
} |
403 |
} else {
|
404 |
return null; |
405 |
} |
406 |
|
407 |
} |
408 |
|
409 |
public Object getCRS() { |
410 |
|
411 |
return m_Projection;
|
412 |
|
413 |
} |
414 |
|
415 |
public void setName(final String name) { |
416 |
|
417 |
m_sName = name; |
418 |
// inputParameters = name;
|
419 |
|
420 |
} |
421 |
|
422 |
@Override
|
423 |
public Object getBaseDataObject() { |
424 |
|
425 |
return m_Layer;
|
426 |
|
427 |
} |
428 |
|
429 |
public void free() { |
430 |
|
431 |
m_Layer.dispose(); |
432 |
featureStore.dispose(); |
433 |
|
434 |
m_Layer = null;
|
435 |
featureStore = null;
|
436 |
} |
437 |
|
438 |
public IOutputChannel getOutputChannel() {
|
439 |
|
440 |
if (featureStore != null) { |
441 |
final DataStoreParameters dsp = featureStore.getParameters();
|
442 |
if (dsp instanceof FilesystemStoreParameters) { |
443 |
String file =
|
444 |
((FilesystemStoreParameters) dsp).getFile() |
445 |
.getAbsolutePath(); |
446 |
return new FileOutputChannel(file); |
447 |
} else {
|
448 |
return null; |
449 |
} |
450 |
} else {
|
451 |
return m_Channel;
|
452 |
} |
453 |
|
454 |
} |
455 |
|
456 |
public boolean canBeEdited() { |
457 |
|
458 |
// we support only file-based, so we can overwrite
|
459 |
return true; |
460 |
|
461 |
} |
462 |
|
463 |
public FeatureStore getFeatureStore() {
|
464 |
return featureStore;
|
465 |
} |
466 |
|
467 |
} |