gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / FlyrVectIVectorLayer.java @ 981
History | View | Annotate | Download (22.9 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 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 2
|
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.geoprocess.lib.sextante.dataObjects; |
25 |
|
26 |
import java.awt.geom.Rectangle2D; |
27 |
import java.io.File; |
28 |
import java.sql.Types; |
29 |
import java.util.ArrayList; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
|
33 |
import org.cresques.cts.IProjection; |
34 |
|
35 |
import org.gvsig.fmap.dal.DALLocator; |
36 |
import org.gvsig.fmap.dal.DataManager; |
37 |
import org.gvsig.fmap.dal.DataServerExplorer; |
38 |
import org.gvsig.fmap.dal.DataStoreParameters; |
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.exception.ReadException; |
41 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
42 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
43 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
44 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
45 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
46 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
47 |
import org.gvsig.fmap.dal.feature.FeatureType; |
48 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
49 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
50 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
52 |
import org.gvsig.fmap.geom.DataTypes; |
53 |
import org.gvsig.fmap.geom.GeometryLocator; |
54 |
import org.gvsig.fmap.geom.GeometryManager; |
55 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
56 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
57 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
58 |
import org.gvsig.fmap.geom.operation.fromjts.FromJTS; |
59 |
import org.gvsig.fmap.geom.type.GeometryType; |
60 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
61 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
62 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
63 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
64 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
65 |
import org.gvsig.tools.ToolsLocator; |
66 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
67 |
import org.gvsig.tools.locator.LocatorException; |
68 |
|
69 |
import com.vividsolutions.jts.geom.Geometry; |
70 |
|
71 |
import es.unex.sextante.core.Sextante; |
72 |
import es.unex.sextante.dataObjects.AbstractVectorLayer; |
73 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
74 |
import es.unex.sextante.dataObjects.IVectorLayer; |
75 |
import es.unex.sextante.outputs.FileOutputChannel; |
76 |
import es.unex.sextante.outputs.IOutputChannel; |
77 |
import org.slf4j.Logger; |
78 |
import org.slf4j.LoggerFactory; |
79 |
|
80 |
public class FlyrVectIVectorLayer extends AbstractVectorLayer { |
81 |
|
82 |
private static final Logger logger = LoggerFactory.getLogger(FlyrVectIVectorLayer.class); |
83 |
|
84 |
private static final DataManager DATA_MANAGER = DALLocator.getDataManager(); |
85 |
|
86 |
private final int PRECISION = 5; |
87 |
private FeatureStore featureStore = null; |
88 |
private IProjection m_Projection = null; |
89 |
private FeatureType featureType = null; |
90 |
private GeometryManager geometryManager = null; |
91 |
private FLyrVect m_Layer;
|
92 |
private String m_sName; |
93 |
private IOutputChannel m_Channel;
|
94 |
private String fileName = null; |
95 |
|
96 |
private List<String> names = new ArrayList<String>(); |
97 |
private List<Class<?>> types = new ArrayList<Class<?>>(); |
98 |
|
99 |
|
100 |
|
101 |
// private Object inputParameters = null;
|
102 |
|
103 |
public FlyrVectIVectorLayer() {
|
104 |
geometryManager = GeometryLocator.getGeometryManager(); |
105 |
} |
106 |
|
107 |
public void create(String sName, Object inputParams, int iShapeType, |
108 |
Class[] types, String[] sFields, Object crs, int[] fieldSize) { |
109 |
create(sName, inputParams, iShapeType, |
110 |
types, sFields, crs, fieldSize, org.gvsig.fmap.geom.Geometry.SUBTYPES.UNKNOWN); |
111 |
} |
112 |
public void create(String sName, Object inputParams, int iShapeType, |
113 |
Class[] types, String[] sFields, Object crs, int[] fieldSize, int subtype) { |
114 |
try {
|
115 |
// inputParameters = inputParams;
|
116 |
m_sName = sName; |
117 |
m_Projection = (IProjection) crs; |
118 |
|
119 |
if (inputParams instanceof OutputParameters) { |
120 |
OutputParameters outputParam = (OutputParameters) inputParams; |
121 |
NewFeatureStoreParameters newParams = |
122 |
outputParam.getDataParameters(); |
123 |
DataServerExplorer dataServerExplorer = |
124 |
outputParam.getExplorer(); |
125 |
|
126 |
featureType = newParams.getDefaultFeatureType(); |
127 |
loadFeatureType(sFields, types, iShapeType, crs, |
128 |
(EditableFeatureType) featureType, fieldSize, subtype); |
129 |
newParams.setDynValue("CRS", m_Projection);
|
130 |
|
131 |
DATA_MANAGER.newStore(dataServerExplorer.getProviderName(), |
132 |
newParams.getDataStoreName(), newParams, true);
|
133 |
|
134 |
featureStore = |
135 |
(FeatureStore) DATA_MANAGER.openStore( |
136 |
newParams.getDataStoreName(), newParams); |
137 |
initializeNamesAndTypes(); |
138 |
featureStore.edit(FeatureStore.MODE_APPEND); |
139 |
} |
140 |
|
141 |
if (inputParams instanceof FilesystemStoreParameters) { |
142 |
// TODO: Terminar para todo tipo fuentes de datos. No se puede
|
143 |
// hacer ahora pq falta poder obtener un FeatureType del
|
144 |
// parameter.
|
145 |
// Esto es as? pq el store registra parameters para cargar
|
146 |
// ficheros y no para escribirlos.
|
147 |
String path =
|
148 |
((FilesystemStoreParameters) inputParams).getFile() |
149 |
.getAbsolutePath(); |
150 |
create(sName, path, iShapeType, types, sFields, crs, fieldSize); |
151 |
} |
152 |
|
153 |
if (inputParams instanceof String) { |
154 |
create(sName, (String) inputParams, iShapeType, types, sFields,
|
155 |
crs, fieldSize); |
156 |
} |
157 |
|
158 |
} catch (Exception e) { |
159 |
Sextante.addErrorToLog(e); |
160 |
} |
161 |
} |
162 |
|
163 |
public void create(final String sName, final String sFilename, |
164 |
final int iShapeType, final Class<?>[] types, final String[] sFields, |
165 |
final Object crs, final int[] fieldSize) { |
166 |
create(sName, sFilename, iShapeType, types, sFields, crs, fieldSize, org.gvsig.fmap.geom.Geometry.SUBTYPES.UNKNOWN); |
167 |
} |
168 |
|
169 |
public void create(final String sName, final String sFilename, |
170 |
final int iShapeType, final Class<?>[] types, final String[] sFields, |
171 |
final Object crs, final int[] fieldSize, int subtype) { |
172 |
|
173 |
try {
|
174 |
// inputParameters = sFilename;
|
175 |
m_sName = sName; |
176 |
m_Projection = (IProjection) crs; |
177 |
fileName = sFilename; |
178 |
|
179 |
final FilesystemServerExplorerParameters explorerParams =
|
180 |
(FilesystemServerExplorerParameters) DATA_MANAGER |
181 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
182 |
explorerParams.setRoot(new File(sFilename).getParent()); |
183 |
final FilesystemServerExplorer explorer =
|
184 |
(FilesystemServerExplorer) DATA_MANAGER.openServerExplorer( |
185 |
FilesystemServerExplorer.NAME, explorerParams); |
186 |
File file = new File(sFilename); |
187 |
final NewFeatureStoreParameters newParams =
|
188 |
(NewFeatureStoreParameters) explorer.getAddParameters(file); |
189 |
|
190 |
|
191 |
EditableFeatureType editableFeatureType = |
192 |
newParams.getDefaultFeatureType(); |
193 |
featureType = editableFeatureType; |
194 |
loadFeatureType(sFields, types, iShapeType, crs, |
195 |
editableFeatureType, fieldSize, subtype); |
196 |
|
197 |
newParams.setDefaultFeatureType(featureType); |
198 |
newParams.setDynValue("crs", m_Projection);
|
199 |
|
200 |
explorer.add(newParams.getDataStoreName(), newParams, true);
|
201 |
|
202 |
DataStoreParameters params = DATA_MANAGER.createStoreParameters(newParams.getDataStoreName()); |
203 |
params.setDynValue("shpfile", sFilename);
|
204 |
params.setDynValue("crs", m_Projection);
|
205 |
|
206 |
featureStore = (FeatureStore) DATA_MANAGER.openStore(params.getDataStoreName(), params); |
207 |
featureStore.edit(FeatureStore.MODE_APPEND); |
208 |
featureType = featureStore.getDefaultFeatureType(); |
209 |
initializeNamesAndTypes(); |
210 |
|
211 |
} catch (final Exception e) { |
212 |
Sextante.addErrorToLog(e); |
213 |
} |
214 |
} |
215 |
|
216 |
private void loadFeatureType(final String[] fields, final Class<?>[] types, |
217 |
final int shapeType, final Object crs, |
218 |
final EditableFeatureType featureType, final int[] fieldSize, final int subtype) { |
219 |
final int[] iTypes = getTypes(types); |
220 |
|
221 |
for (int i = 0; i < fields.length; i++) { |
222 |
if (iTypes[i] == DataTypes.GEOMETRY) {
|
223 |
// No se hace nada porque el resultado de los geoprocesos deben dejar una ?nica columna de geometr?a
|
224 |
// que a?adimos al final
|
225 |
} else {
|
226 |
featureType.add(fields[i], iTypes[i]) |
227 |
.setAllowNull(true)
|
228 |
.setSize(fieldSize[i]) |
229 |
.setPrecision(PRECISION); |
230 |
} |
231 |
} |
232 |
|
233 |
FeatureAttributeDescriptor desc = |
234 |
featureType.getDefaultGeometryAttribute(); |
235 |
if (desc == null) { |
236 |
addGeometryAttribute("GEOMETRY", shapeType, subtype, featureType);
|
237 |
featureType.setDefaultGeometryAttributeName("GEOMETRY");
|
238 |
} |
239 |
} |
240 |
|
241 |
private void addGeometryAttribute(final String fieldName, |
242 |
final int shapeType, final int subtype, final EditableFeatureType featureType) { |
243 |
try {
|
244 |
GeometryType type; |
245 |
if (subtype == org.gvsig.fmap.geom.Geometry.SUBTYPES.UNKNOWN) {
|
246 |
type = |
247 |
geometryManager.getGeometryType(getgvSIGShapeType(shapeType), |
248 |
org.gvsig.fmap.geom.Geometry.SUBTYPES.GEOM2D); |
249 |
} else {
|
250 |
type = |
251 |
geometryManager.getGeometryType(getgvSIGShapeType(shapeType),subtype); |
252 |
} |
253 |
featureType.add(fieldName, DataTypes.GEOMETRY) |
254 |
.setGeometryType(type); |
255 |
} catch (GeometryTypeNotSupportedException e) {
|
256 |
throw new RuntimeException( |
257 |
"Error getting geometry type with type = "
|
258 |
+ getgvSIGShapeType(shapeType) + ", subtype = "
|
259 |
+ org.gvsig.fmap.geom.Geometry.SUBTYPES.GEOM2D, e); |
260 |
} catch (GeometryTypeNotValidException e) {
|
261 |
throw new RuntimeException( |
262 |
"Error getting geometry type with type = "
|
263 |
+ getgvSIGShapeType(shapeType) + ", subtype = "
|
264 |
+ org.gvsig.fmap.geom.Geometry.SUBTYPES.GEOM2D, e); |
265 |
} |
266 |
} |
267 |
|
268 |
private int[] getTypes(Class<?>[] classes) { |
269 |
DataTypesManager typesManager = ToolsLocator.getDataTypesManager(); |
270 |
int[] types = new int[classes.length]; |
271 |
for (int i = 0; i < classes.length; i++) { |
272 |
types[i] = typesManager.getDataType(classes[i]).getType(); |
273 |
} |
274 |
return types;
|
275 |
} |
276 |
|
277 |
public void create(final FLyrVect layer) { |
278 |
m_Layer = layer; |
279 |
try {
|
280 |
featureStore = layer.getFeatureStore(); |
281 |
featureType = featureStore.getDefaultFeatureType(); |
282 |
initializeNamesAndTypes(); |
283 |
m_Projection = layer.getProjection(); |
284 |
} catch (final Exception e) { |
285 |
Sextante.addErrorToLog(e); |
286 |
} |
287 |
|
288 |
} |
289 |
|
290 |
public void open() { |
291 |
if (m_Layer == null) { |
292 |
try {
|
293 |
|
294 |
m_Layer = (FLyrVect) MapContextLocator.getMapContextManager().createLayer( |
295 |
getFilename(), featureStore); |
296 |
} catch (final LoadLayerException e) { |
297 |
Sextante.addErrorToLog(e); |
298 |
throw new RuntimeException(e); |
299 |
} |
300 |
} |
301 |
} |
302 |
|
303 |
public void close() { |
304 |
if( this.m_Layer==null ) { |
305 |
this.free();
|
306 |
} |
307 |
} |
308 |
|
309 |
/**
|
310 |
* Returns the length of field
|
311 |
*
|
312 |
* @param dataType
|
313 |
* @return length of field
|
314 |
*/
|
315 |
public int getDataTypeLength(final int dataType) { |
316 |
switch (dataType) {
|
317 |
case Types.NUMERIC: |
318 |
case Types.DOUBLE: |
319 |
case Types.REAL: |
320 |
case Types.FLOAT: |
321 |
case Types.BIGINT: |
322 |
case Types.INTEGER: |
323 |
case Types.DECIMAL: |
324 |
return 20; |
325 |
case Types.CHAR: |
326 |
case Types.VARCHAR: |
327 |
case Types.LONGVARCHAR: |
328 |
return 254; |
329 |
case Types.DATE: |
330 |
return 8; |
331 |
case Types.BOOLEAN: |
332 |
case Types.BIT: |
333 |
return 1; |
334 |
} |
335 |
return 0; |
336 |
} |
337 |
|
338 |
@SuppressWarnings("unchecked") |
339 |
public void addFeature(final Geometry geom, final Object[] values) { |
340 |
try {
|
341 |
org.gvsig.fmap.geom.Geometry iGeo = null;
|
342 |
|
343 |
final GeometryOperationContext ctx = new GeometryOperationContext(); |
344 |
ctx.setAttribute(FromJTS.PARAM, geom); |
345 |
try {
|
346 |
iGeo = |
347 |
(org.gvsig.fmap.geom.Geometry) geometryManager |
348 |
.invokeOperation(FromJTS.NAME, ctx); |
349 |
} catch (final GeometryOperationNotSupportedException e) { |
350 |
Sextante.addErrorToLog(e); |
351 |
return;
|
352 |
} catch (final GeometryOperationException e) { |
353 |
Sextante.addErrorToLog(e); |
354 |
return;
|
355 |
} |
356 |
|
357 |
final EditableFeature ef = featureStore.createNewFeature();
|
358 |
final Iterator<FeatureAttributeDescriptor> features = |
359 |
featureType.iterator(); |
360 |
|
361 |
int i = 0; |
362 |
while (features.hasNext()) {
|
363 |
final FeatureAttributeDescriptor featureAttributeDescriptor =
|
364 |
features.next(); |
365 |
if (!featureAttributeDescriptor.getName().equals(
|
366 |
featureType.getDefaultGeometryAttributeName())) { |
367 |
ef.set(featureAttributeDescriptor.getName(), values[i]); |
368 |
} |
369 |
i++; |
370 |
} |
371 |
ef.set(featureType.getDefaultGeometryAttributeIndex(), iGeo); |
372 |
featureStore.insert(ef); |
373 |
} catch (final Exception e) { |
374 |
e.printStackTrace(); |
375 |
} |
376 |
} |
377 |
|
378 |
public IFeatureIterator iterator() {
|
379 |
if (m_Layer != null) { |
380 |
return new DALIFeatureIterator(m_Layer, getFilters()); |
381 |
} else {
|
382 |
// cannot iterate layers being edited
|
383 |
return new DALIFeatureIterator(); |
384 |
} |
385 |
} |
386 |
|
387 |
public String getFieldName(final int i) { |
388 |
if (featureStore != null) { |
389 |
return ((FeatureAttributeDescriptor) featureType.get(i)).getName();
|
390 |
} |
391 |
return null; |
392 |
} |
393 |
|
394 |
public Class<?> getFieldType(final int i) { |
395 |
return types.get(i);
|
396 |
} |
397 |
|
398 |
public int getFieldCount() { |
399 |
if (featureStore != null) { |
400 |
return names.size();
|
401 |
} |
402 |
return 0; |
403 |
} |
404 |
|
405 |
@Override
|
406 |
public int getShapesCount() { |
407 |
if (featureStore != null) { |
408 |
try {
|
409 |
FeatureSelection featureSelection = featureStore.getFeatureSelection(); |
410 |
if (!featureSelection.isEmpty()) {
|
411 |
return (int)featureSelection.getSize(); |
412 |
} else {
|
413 |
return (int)featureStore.getFeatureSet().getSize(); |
414 |
} |
415 |
} catch (final DataException e) { |
416 |
Sextante.addErrorToLog(e); |
417 |
return 0; |
418 |
} |
419 |
} |
420 |
return 0; |
421 |
} |
422 |
|
423 |
public int getShapeType() { |
424 |
if (featureStore != null) { |
425 |
return getShapeTypeFromGvSIGShapeType(((FeatureAttributeDescriptor) featureType
|
426 |
.get(featureType.getDefaultGeometryAttributeIndex())) |
427 |
.getGeomType().getType()); |
428 |
} |
429 |
return 0; |
430 |
} |
431 |
|
432 |
private int getShapeTypeFromGvSIGShapeType(final int shapeType) { |
433 |
|
434 |
if( geometryManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.SURFACE, shapeType) ) {
|
435 |
return IVectorLayer.SHAPE_TYPE_POLYGON;
|
436 |
} |
437 |
if( geometryManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTISURFACE, shapeType) ) {
|
438 |
return IVectorLayer.SHAPE_TYPE_POLYGON;
|
439 |
} |
440 |
if( geometryManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.CURVE, shapeType) ) {
|
441 |
return IVectorLayer.SHAPE_TYPE_LINE;
|
442 |
} |
443 |
if( geometryManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE, shapeType) ) {
|
444 |
return IVectorLayer.SHAPE_TYPE_LINE;
|
445 |
} |
446 |
if( geometryManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.POINT, shapeType) ) {
|
447 |
return IVectorLayer.SHAPE_TYPE_POINT;
|
448 |
} |
449 |
if( geometryManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT, shapeType) ) {
|
450 |
return IVectorLayer.SHAPE_TYPE_POINT;
|
451 |
} |
452 |
try {
|
453 |
throw new IllegalArgumentException(); |
454 |
} catch(Exception ex) { |
455 |
logger.warn("Geometry type "+ shapeType + " not supported.", ex); |
456 |
} |
457 |
return IVectorLayer.SHAPE_TYPE_POLYGON;
|
458 |
} |
459 |
|
460 |
private int getgvSIGShapeType(final int shapeType) { |
461 |
switch (shapeType) {
|
462 |
case IVectorLayer.SHAPE_TYPE_POLYGON:
|
463 |
return org.gvsig.fmap.geom.Geometry.TYPES.POLYGON;
|
464 |
case IVectorLayer.SHAPE_TYPE_LINE:
|
465 |
return org.gvsig.fmap.geom.Geometry.TYPES.LINE;
|
466 |
case IVectorLayer.SHAPE_TYPE_POINT:
|
467 |
return org.gvsig.fmap.geom.Geometry.TYPES.POINT;
|
468 |
case IVectorLayer.SHAPE_TYPE_MULTIPOLYGON:
|
469 |
return org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOLYGON;
|
470 |
case IVectorLayer.SHAPE_TYPE_MULTILINE:
|
471 |
return org.gvsig.fmap.geom.Geometry.TYPES.MULTILINE;
|
472 |
case IVectorLayer.SHAPE_TYPE_MULTIPOINT:
|
473 |
return org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT;
|
474 |
default:
|
475 |
return org.gvsig.fmap.geom.Geometry.TYPES.POLYGON;
|
476 |
} |
477 |
} |
478 |
|
479 |
public String getName() { |
480 |
if (m_Layer != null) { |
481 |
return m_Layer.getName();
|
482 |
} else {
|
483 |
return m_sName;
|
484 |
} |
485 |
} |
486 |
|
487 |
public void postProcess() { |
488 |
if (featureStore == null) { |
489 |
return;
|
490 |
} |
491 |
try {
|
492 |
if( featureStore.isEditing() || featureStore.isAppending() ) {
|
493 |
featureStore.finishEditing(); |
494 |
} |
495 |
String name = featureStore.getProviderName();
|
496 |
DataStoreParameters params = featureStore.getParameters(); |
497 |
featureStore.dispose(); |
498 |
featureStore = (FeatureStore) DALLocator.getDataManager().openStore(name, params); |
499 |
featureType = featureStore.getDefaultFeatureType(); |
500 |
} catch (final Exception e) { |
501 |
logger.warn("Can't execute postProcess on '"+this.getName()+"'.",e); |
502 |
} |
503 |
} |
504 |
|
505 |
public String getFilename() { |
506 |
if(fileName != null) |
507 |
return fileName;
|
508 |
else
|
509 |
return m_sName;
|
510 |
} |
511 |
|
512 |
public Object getCRS() { |
513 |
|
514 |
return m_Projection;
|
515 |
|
516 |
} |
517 |
|
518 |
public void setName(final String name) { |
519 |
m_sName = name; |
520 |
} |
521 |
|
522 |
@Override
|
523 |
public Object getBaseDataObject() { |
524 |
return m_Layer;
|
525 |
} |
526 |
|
527 |
public void free() { |
528 |
if( this.featureStore!=null ) { |
529 |
if( featureStore.isEditing() || featureStore.isAppending() ) {
|
530 |
try {
|
531 |
featureStore.finishEditing(); |
532 |
} catch (DataException ex) {
|
533 |
logger.warn("Can't free '"+this.getName()+"'.",ex); |
534 |
} |
535 |
} |
536 |
featureStore.dispose(); |
537 |
featureStore = null;
|
538 |
} |
539 |
if( m_Layer!=null ) { |
540 |
m_Layer.dispose(); |
541 |
m_Layer = null;
|
542 |
} |
543 |
} |
544 |
|
545 |
public IOutputChannel getOutputChannel() {
|
546 |
if (featureStore != null) { |
547 |
final DataStoreParameters dsp = featureStore.getParameters();
|
548 |
if (dsp instanceof FilesystemStoreParameters) { |
549 |
String file =
|
550 |
((FilesystemStoreParameters) dsp).getFile() |
551 |
.getAbsolutePath(); |
552 |
return new FileOutputChannel(file); |
553 |
} else {
|
554 |
return null; |
555 |
} |
556 |
} else {
|
557 |
return m_Channel;
|
558 |
} |
559 |
} |
560 |
|
561 |
public boolean canBeEdited() { |
562 |
// we support only file-based, so we can overwrite
|
563 |
return true; |
564 |
|
565 |
} |
566 |
|
567 |
public FeatureStore getFeatureStore() {
|
568 |
return featureStore;
|
569 |
} |
570 |
|
571 |
|
572 |
public Rectangle2D getFullExtent() { |
573 |
org.gvsig.fmap.geom.primitive.Envelope envelope; |
574 |
try {
|
575 |
envelope = featureStore.getEnvelope(); |
576 |
return new Rectangle2D.Double(envelope.getMinimum(0), envelope.getMinimum(1), envelope.getLength(0), envelope.getLength(1)); |
577 |
} catch (DataException e) {
|
578 |
return null; |
579 |
} |
580 |
} |
581 |
|
582 |
|
583 |
|
584 |
public String[] getFieldNames() { |
585 |
return (String[]) names.toArray(new String[names.size()]); |
586 |
} |
587 |
|
588 |
public int getFieldIndexByName(final String sFieldName) { |
589 |
String[] names = getFieldNames(); |
590 |
for (int i = 0; i < names.length; i++) { |
591 |
if(names[i].equalsIgnoreCase(sFieldName)) {
|
592 |
return i;
|
593 |
} |
594 |
} |
595 |
return -1; |
596 |
} |
597 |
|
598 |
public Class<?>[] getFieldTypes() { |
599 |
return (Class<?>[]) types.toArray(new Class<?>[types.size()]); |
600 |
} |
601 |
|
602 |
private void initializeNamesAndTypes() { |
603 |
|
604 |
int defaultGeometryAttributeIndex = this.featureType.getDefaultGeometryAttributeIndex(); |
605 |
for(int i = 0; i < this.featureType.size(); i++){ |
606 |
if(i!=defaultGeometryAttributeIndex){
|
607 |
FeatureAttributeDescriptor attributeDescriptor = this.featureType.getAttributeDescriptor(i);
|
608 |
names.add(attributeDescriptor.getName()); |
609 |
types.add(attributeDescriptor.getDataType().getDefaultClass()); |
610 |
} |
611 |
} |
612 |
|
613 |
} |
614 |
|
615 |
/* (non-Javadoc)
|
616 |
* @see es.unex.sextante.dataObjects.IVectorLayer#getSubType()
|
617 |
*/
|
618 |
@Override
|
619 |
public int getSubType() throws ReadException { |
620 |
if(m_Layer!=null){ |
621 |
return m_Layer.getGeometryType().getSubType();
|
622 |
} |
623 |
return org.gvsig.fmap.geom.Geometry.SUBTYPES.UNKNOWN;
|
624 |
} |
625 |
} |