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