gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.base / src / main / java / org / gvsig / geoprocess / algorithm / base / core / DALFeaturePersister.java @ 333
History | View | Annotate | Download (19.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.algorithm.base.core; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
|
28 |
import org.gvsig.fmap.dal.exception.DataException; |
29 |
import org.gvsig.fmap.dal.exception.ReadException; |
30 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
31 |
import org.gvsig.fmap.dal.feature.Feature; |
32 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.dal.feature.FeatureType; |
35 |
import org.gvsig.fmap.geom.GeometryLocator; |
36 |
import org.gvsig.fmap.geom.GeometryManager; |
37 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
38 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
39 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
40 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
41 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
42 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
43 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
44 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
45 |
import org.gvsig.fmap.geom.operation.fromjts.FromJTS; |
46 |
import org.gvsig.fmap.geom.primitive.Curve; |
47 |
import org.gvsig.fmap.geom.primitive.Point; |
48 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
49 |
|
50 |
import com.vividsolutions.jts.geom.Geometry; |
51 |
import com.vividsolutions.jts.geom.GeometryCollection; |
52 |
|
53 |
import es.unex.sextante.core.Sextante; |
54 |
|
55 |
/**
|
56 |
* Writes features in a FeatureStore
|
57 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
58 |
*/
|
59 |
public class DALFeaturePersister { |
60 |
private FeatureStore store = null; |
61 |
private String[] fieldNames = null; |
62 |
private GeometryManager geometryManager = null; |
63 |
|
64 |
/**
|
65 |
* Sets the output FeatureType
|
66 |
* @param out
|
67 |
* @throws DataException
|
68 |
*/
|
69 |
public DALFeaturePersister(FeatureStore out, String[] fieldNames) { |
70 |
this.store = out;
|
71 |
this.fieldNames = fieldNames;
|
72 |
try {
|
73 |
if(!store.isEditing())
|
74 |
store.edit(); |
75 |
} catch (DataException e) {
|
76 |
} catch (NullPointerException e) { |
77 |
} |
78 |
geometryManager = GeometryLocator.getGeometryManager(); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Gets the list of field names
|
83 |
* @return
|
84 |
*/
|
85 |
public String[] getFieldNames() { |
86 |
try {
|
87 |
FeatureAttributeDescriptor[] attrs = store.getDefaultFeatureType().getAttributeDescriptors();
|
88 |
String[] fieldNames = new String[attrs.length]; |
89 |
for (int i = 0; i < attrs.length; i++) { |
90 |
fieldNames[i] = attrs[i].getName(); |
91 |
} |
92 |
return fieldNames;
|
93 |
} catch (DataException e) {
|
94 |
Sextante.addErrorToLog(e); |
95 |
} |
96 |
return null; |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Gets the list of field names
|
101 |
* @return
|
102 |
*/
|
103 |
public String[] getFieldNamesWithoutGeom() { |
104 |
try {
|
105 |
FeatureAttributeDescriptor[] attrs = store.getDefaultFeatureType().getAttributeDescriptors();
|
106 |
int nFields = attrs.length;
|
107 |
for (int j = 0; j < nFields; j++) { |
108 |
if(attrs[j].getName().compareTo("GEOMETRY") == 0) { |
109 |
nFields --; |
110 |
break;
|
111 |
} |
112 |
} |
113 |
|
114 |
int cont = 0; |
115 |
String[] fieldNames = new String[nFields]; |
116 |
for (int i = 0; i < attrs.length; i++) { |
117 |
if(attrs[i].getName().compareTo("GEOMETRY") != 0) { |
118 |
fieldNames[cont] = attrs[i].getName(); |
119 |
cont ++; |
120 |
} |
121 |
} |
122 |
return fieldNames;
|
123 |
} catch (DataException e) {
|
124 |
Sextante.addErrorToLog(e); |
125 |
} |
126 |
return null; |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Adds a JTS feature to the FeatureStore
|
131 |
* @param entry
|
132 |
* @param newGeom
|
133 |
* @throws CreateGeometryException
|
134 |
* @throws DataException
|
135 |
*/
|
136 |
public EditableFeature addFeature(Feature feature1, Feature feature2, Geometry newGeom)
|
137 |
throws CreateGeometryException, DataException {
|
138 |
|
139 |
int outPutType = getOutputFeatureStore().getDefaultFeatureType()
|
140 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
141 |
|
142 |
//Si el objeto que viene es un GeometryCollection hay que convertirlo a una lista de geometrias
|
143 |
//solo del tipo que pueda salvarse en la capa eliminando el resto. Esto sucede sobre todo en la intersecci?n
|
144 |
//que devuelve tipos mezclados. Cada uno debe salvarse en el tipo de capa de salida apropiado
|
145 |
ArrayList<org.gvsig.fmap.geom.Geometry> dalGeomList = null; |
146 |
try {
|
147 |
dalGeomList = convertGeometryCollection2GeometryList(newGeom, outPutType); |
148 |
} catch (GeometryOperationNotSupportedException e1) {
|
149 |
Sextante.addErrorToLog(e1); |
150 |
} catch (GeometryOperationException e1) {
|
151 |
Sextante.addErrorToLog(e1); |
152 |
} |
153 |
org.gvsig.fmap.geom.Geometry newDalGeom = null;
|
154 |
|
155 |
//Si lo que venia no era un GeometryCollection se convierte a geometr?a de DAL
|
156 |
if(dalGeomList == null) { |
157 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
158 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
159 |
try {
|
160 |
newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
161 |
} catch (GeometryOperationNotSupportedException e) {
|
162 |
Sextante.addErrorToLog(e); |
163 |
return null; |
164 |
} catch (GeometryOperationException e) {
|
165 |
Sextante.addErrorToLog(e); |
166 |
return null; |
167 |
} |
168 |
} |
169 |
|
170 |
EditableFeature feat = store.createNewFeature(); |
171 |
|
172 |
int globalIndex = 0; |
173 |
int localIndex = 0; |
174 |
Object value = feature1.get(localIndex);
|
175 |
while(value != null && !(value instanceof org.gvsig.fmap.geom.Geometry)) { |
176 |
feat.set(globalIndex, value); |
177 |
value = feature1.get(++localIndex); |
178 |
globalIndex ++; |
179 |
} |
180 |
|
181 |
localIndex = 0;
|
182 |
value = feature2.get(localIndex); |
183 |
while(value != null && !(value instanceof org.gvsig.fmap.geom.Geometry)) { |
184 |
feat.set(globalIndex, value); |
185 |
value = feature2.get(++localIndex); |
186 |
globalIndex ++; |
187 |
} |
188 |
|
189 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
190 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
191 |
store.insert(feat); |
192 |
return feat;
|
193 |
} else if(dalGeomList != null) { |
194 |
for (int i = 0; i < dalGeomList.size(); i++) { |
195 |
if(acceptType(store, dalGeomList.get(i))) {
|
196 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(dalGeomList.get(i)));
|
197 |
store.insert(feat); |
198 |
} |
199 |
} |
200 |
return feat;
|
201 |
} |
202 |
return null; |
203 |
} |
204 |
|
205 |
/**
|
206 |
* Converts a geometry collection from JTS to a list of geometries of the selected type.
|
207 |
* The GeometryCollection is converted only when it is a heterogeneous collection of geometries
|
208 |
* @param col
|
209 |
* @param type
|
210 |
* @return
|
211 |
* @throws CreateGeometryException
|
212 |
* @throws GeometryOperationException
|
213 |
* @throws GeometryOperationNotSupportedException
|
214 |
*/
|
215 |
public ArrayList<org.gvsig.fmap.geom.Geometry> convertGeometryCollection2GeometryList(Geometry g, int type) throws GeometryOperationNotSupportedException, GeometryOperationException { |
216 |
if( g instanceof GeometryCollection && |
217 |
!(g instanceof com.vividsolutions.jts.geom.MultiLineString) &&
|
218 |
!(g instanceof com.vividsolutions.jts.geom.MultiPoint) &&
|
219 |
!(g instanceof com.vividsolutions.jts.geom.MultiPolygon)) {
|
220 |
GeometryCollection col = (GeometryCollection)g; |
221 |
ArrayList<org.gvsig.fmap.geom.Geometry> geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>(); |
222 |
|
223 |
if(type == TYPES.MULTIPOINT || type == TYPES.POINT) {
|
224 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
225 |
Geometry gx = col.getGeometryN(i); |
226 |
if( gx instanceof com.vividsolutions.jts.geom.Point || |
227 |
gx instanceof com.vividsolutions.jts.geom.MultiPoint) {
|
228 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
229 |
ctx.setAttribute(FromJTS.PARAM, gx); |
230 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
231 |
geometries.add(newDalGeom); |
232 |
} |
233 |
} |
234 |
} |
235 |
|
236 |
if(type == TYPES.MULTICURVE || type == TYPES.CURVE) {
|
237 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
238 |
Geometry gx = col.getGeometryN(i); |
239 |
if( gx instanceof com.vividsolutions.jts.geom.LineString || |
240 |
gx instanceof com.vividsolutions.jts.geom.MultiLineString) {
|
241 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
242 |
ctx.setAttribute(FromJTS.PARAM, gx); |
243 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
244 |
geometries.add(newDalGeom); |
245 |
} |
246 |
} |
247 |
} |
248 |
if(type == TYPES.MULTISURFACE || type == TYPES.SURFACE) {
|
249 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
250 |
Geometry gx = col.getGeometryN(i); |
251 |
if( gx instanceof com.vividsolutions.jts.geom.Polygon || |
252 |
gx instanceof com.vividsolutions.jts.geom.MultiPolygon) {
|
253 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
254 |
ctx.setAttribute(FromJTS.PARAM, gx); |
255 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
256 |
geometries.add(newDalGeom); |
257 |
} |
258 |
} |
259 |
} |
260 |
return geometries;
|
261 |
} |
262 |
return null; |
263 |
} |
264 |
|
265 |
/**
|
266 |
* Converts a geometry into a multigeometry
|
267 |
* @param g
|
268 |
* @return
|
269 |
* @throws CreateGeometryException
|
270 |
*/
|
271 |
private org.gvsig.fmap.geom.Geometry convertGeometry2MultiGeometry(org.gvsig.fmap.geom.Geometry g) throws CreateGeometryException { |
272 |
if(g instanceof Point) { |
273 |
MultiPoint pNew = (MultiPoint)geometryManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D); |
274 |
pNew.addPoint((Point)g);
|
275 |
return pNew;
|
276 |
} |
277 |
if(g instanceof Curve) { |
278 |
MultiCurve cNew = (MultiCurve)geometryManager.create(TYPES.MULTICURVE, SUBTYPES.GEOM2D); |
279 |
cNew.addCurve((Curve)g); |
280 |
return cNew;
|
281 |
} |
282 |
return g;
|
283 |
} |
284 |
|
285 |
/**
|
286 |
* Checks if the type of the feature store and the geometry are compatibles.
|
287 |
* @param store
|
288 |
* @param geom
|
289 |
* @return
|
290 |
* @throws ReadException
|
291 |
*/
|
292 |
private boolean acceptType(FeatureStore store, org.gvsig.fmap.geom.Geometry geom) throws ReadException { |
293 |
int fType = getShapeType(store);
|
294 |
int gType = geom.getType();
|
295 |
|
296 |
if(fType == gType)
|
297 |
return true; |
298 |
if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE &&
|
299 |
gType == org.gvsig.fmap.geom.Geometry.TYPES.CURVE) |
300 |
return true; |
301 |
if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTISURFACE &&
|
302 |
gType == org.gvsig.fmap.geom.Geometry.TYPES.SURFACE) |
303 |
return true; |
304 |
if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT &&
|
305 |
gType == org.gvsig.fmap.geom.Geometry.TYPES.POINT) |
306 |
return true; |
307 |
if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTISOLID &&
|
308 |
gType == org.gvsig.fmap.geom.Geometry.TYPES.SOLID) |
309 |
return true; |
310 |
return false; |
311 |
} |
312 |
|
313 |
/**
|
314 |
* Adds a JTS feature to the FeatureStore
|
315 |
* @param entry
|
316 |
* @param newGeom
|
317 |
* @throws CreateGeometryException
|
318 |
* @throws DataException
|
319 |
*/
|
320 |
public EditableFeature addFeature(Feature feature, Geometry newGeom) throws CreateGeometryException, DataException { |
321 |
int outPutType = getOutputFeatureStore().getDefaultFeatureType()
|
322 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
323 |
//Si el objeto que viene es un GeometryCollection hay que convertirlo a una lista de geometrias
|
324 |
//solo del tipo que pueda salvarse en la capa eliminando el resto. Esto sucede sobre todo en la intersecci?n
|
325 |
//que devuelve tipos mezclados. Cada uno debe salvarse en el tipo de capa de salida apropiado
|
326 |
ArrayList<org.gvsig.fmap.geom.Geometry> dalGeomList = null; |
327 |
try {
|
328 |
dalGeomList = convertGeometryCollection2GeometryList(newGeom, outPutType); |
329 |
} catch (GeometryOperationNotSupportedException e1) {
|
330 |
Sextante.addErrorToLog(e1); |
331 |
} catch (GeometryOperationException e1) {
|
332 |
Sextante.addErrorToLog(e1); |
333 |
} |
334 |
|
335 |
//No era un GeometryCollection
|
336 |
if(dalGeomList == null) { |
337 |
org.gvsig.fmap.geom.Geometry newDalGeom = null;
|
338 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
339 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
340 |
try {
|
341 |
newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
342 |
} catch (GeometryOperationNotSupportedException e) {
|
343 |
Sextante.addErrorToLog(e); |
344 |
} catch (GeometryOperationException e) {
|
345 |
Sextante.addErrorToLog(e); |
346 |
} |
347 |
insertFeatureIntoStore(feature, newDalGeom); |
348 |
} else {
|
349 |
for (int i = 0; i < dalGeomList.size(); i++) { |
350 |
insertFeatureIntoStore(feature, dalGeomList.get(i)); |
351 |
} |
352 |
} |
353 |
return null; |
354 |
} |
355 |
|
356 |
private EditableFeature insertFeatureIntoStore(Feature feature, org.gvsig.fmap.geom.Geometry newDalGeom) throws DataException { |
357 |
if(acceptType(store, newDalGeom)) {
|
358 |
EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), true);
|
359 |
FeatureAttributeDescriptor[] attrDesc = feature.getType().getAttributeDescriptors();
|
360 |
for (int i = 0; i < attrDesc.length; i++) { |
361 |
String name = attrDesc[i].getName();
|
362 |
if(name.compareTo("GEOMETRY") != 0) { |
363 |
Object value = feature.get(name);
|
364 |
if(value != null) { |
365 |
feat.set(name, value); |
366 |
} |
367 |
} |
368 |
} |
369 |
try {
|
370 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
371 |
} catch (CreateGeometryException e) {
|
372 |
Sextante.addErrorToLog(e); } |
373 |
store.insert(feat); |
374 |
return feat;
|
375 |
} |
376 |
return null; |
377 |
} |
378 |
|
379 |
/**
|
380 |
* Adds a JTS feature to the FeatureStore
|
381 |
* @param entry
|
382 |
* @param newGeom
|
383 |
* @throws CreateGeometryException
|
384 |
* @throws DataException
|
385 |
*/
|
386 |
public EditableFeature addFeature(Geometry newGeom, int id, double value) throws CreateGeometryException, DataException { |
387 |
org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
388 |
|
389 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
390 |
EditableFeature feat = store.createNewFeature(); |
391 |
feat.set(0, id);
|
392 |
feat.set(1, value);
|
393 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
394 |
store.insert(feat); |
395 |
return feat;
|
396 |
} |
397 |
return null; |
398 |
} |
399 |
|
400 |
/**
|
401 |
* Adds a JTS feature to the FeatureStore
|
402 |
* @param entry
|
403 |
* @param newGeom
|
404 |
* @throws CreateGeometryException
|
405 |
* @throws DataException
|
406 |
*/
|
407 |
public EditableFeature addFeature(Geometry newGeom, String name, Object value) throws CreateGeometryException, DataException { |
408 |
org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
409 |
|
410 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
411 |
EditableFeature feat = store.createNewFeature(); |
412 |
feat.set(name, value); |
413 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
414 |
store.insert(feat); |
415 |
return feat;
|
416 |
} |
417 |
return null; |
418 |
} |
419 |
|
420 |
/**
|
421 |
* Adds a JTS feature to the FeatureStore
|
422 |
* @param entry
|
423 |
* @param newGeom
|
424 |
* @throws CreateGeometryException
|
425 |
* @throws DataException
|
426 |
*/
|
427 |
public EditableFeature addFeature(Geometry newGeom, ArrayList<String> names, ArrayList<Object> values) throws CreateGeometryException, DataException { |
428 |
org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
429 |
|
430 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
431 |
EditableFeature feat = store.createNewFeature(); |
432 |
for (int i = 0; i < values.size(); i++) { |
433 |
feat.set(names.get(i), values.get(i)); |
434 |
} |
435 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
436 |
store.insert(feat); |
437 |
return feat;
|
438 |
} |
439 |
return null; |
440 |
} |
441 |
|
442 |
/**
|
443 |
* Adds a JTS feature to the FeatureStore
|
444 |
* @param entry
|
445 |
* @param newGeom
|
446 |
* @throws CreateGeometryException
|
447 |
* @throws DataException
|
448 |
*/
|
449 |
public EditableFeature addFeature(Geometry newGeom, int id, double value1, double value2) throws CreateGeometryException, DataException { |
450 |
org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
451 |
|
452 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
453 |
EditableFeature feat = store.createNewFeature(); |
454 |
feat.set(0, id);
|
455 |
feat.set(1, value1);
|
456 |
feat.set(2, value2);
|
457 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
458 |
store.insert(feat); |
459 |
return feat;
|
460 |
} |
461 |
return null; |
462 |
} |
463 |
|
464 |
/**
|
465 |
* Adds a DAL feature to the FeatureStore when the field structure in the new feature
|
466 |
* is different to the source field structure.
|
467 |
* @param entry
|
468 |
* @param newGeom
|
469 |
* @throws CreateGeometryException
|
470 |
* @throws DataException
|
471 |
*/
|
472 |
public EditableFeature addDifferentFieldFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
473 |
if(acceptType(store, newGeom)) {
|
474 |
//Builds a new empty feature
|
475 |
EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), true);
|
476 |
|
477 |
//Writes only values with the same name and data type in both features
|
478 |
for (int i = 0; i < fieldNames.length; i++) { |
479 |
if(feature.getType().getIndex(fieldNames[i]) != -1) { |
480 |
Object obj1 = feature.get(fieldNames[i]);
|
481 |
if(feat.getType().getIndex(fieldNames[i]) != -1) { |
482 |
Object obj2 = feat.get(fieldNames[i]);
|
483 |
if(obj1 != null && obj2 != null && obj1.getClass() == obj2.getClass()) |
484 |
feat.set(i, obj1); |
485 |
} |
486 |
} |
487 |
} |
488 |
|
489 |
//Sets the geometry
|
490 |
feat.setGeometry(fieldNames.length, convertGeometry2MultiGeometry(newGeom)); |
491 |
store.insert(feat); |
492 |
return feat;
|
493 |
} |
494 |
return null; |
495 |
} |
496 |
|
497 |
/**
|
498 |
* Adds a DAL feature to the FeatureStore
|
499 |
* @param entry
|
500 |
* @param newGeom
|
501 |
* @throws CreateGeometryException
|
502 |
* @throws DataException
|
503 |
*/
|
504 |
public EditableFeature addFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
505 |
if(acceptType(store, newGeom)) {
|
506 |
EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), feature); |
507 |
feat.setGeometry(fieldNames.length, convertGeometry2MultiGeometry(newGeom)); |
508 |
store.insert(feat); |
509 |
return feat;
|
510 |
} |
511 |
return null; |
512 |
} |
513 |
|
514 |
/**
|
515 |
* Adds a DAL feature to the FeatureStore
|
516 |
* @param entry
|
517 |
* @param newGeom
|
518 |
* @throws CreateGeometryException
|
519 |
* @throws DataException
|
520 |
*/
|
521 |
public EditableFeature addGeometryToExistingFeature(EditableFeature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
522 |
//Y esto para que c... sirve?
|
523 |
for (int i = 0; i < fieldNames.length; i++) |
524 |
feature.set(fieldNames[i], feature.get(i)); |
525 |
|
526 |
if(acceptType(store, newGeom)) {
|
527 |
feature.setGeometry(fieldNames.length, newGeom); |
528 |
store.update(feature); |
529 |
return feature;
|
530 |
} |
531 |
return null; |
532 |
} |
533 |
|
534 |
/**
|
535 |
* Ends the edition and closes the FeatureStore
|
536 |
*/
|
537 |
public void end() { |
538 |
try {
|
539 |
store.finishEditing(); |
540 |
} catch (DataException e) {
|
541 |
Sextante.addErrorToLog(e); |
542 |
} |
543 |
store.dispose(); |
544 |
} |
545 |
|
546 |
/**
|
547 |
* Gets the shape type of the selected feature store
|
548 |
* @param FeatureStore source
|
549 |
* @return shape type
|
550 |
* @throws ReadException
|
551 |
*/
|
552 |
@SuppressWarnings("deprecation") |
553 |
public int getShapeType(FeatureStore storeLayer1) throws ReadException { |
554 |
FeatureType featureType; |
555 |
try {
|
556 |
featureType = storeLayer1.getDefaultFeatureType(); |
557 |
} catch (DataException e) {
|
558 |
throw new ReadException(storeLayer1.getName(), e); |
559 |
} |
560 |
int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
561 |
return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
562 |
} |
563 |
|
564 |
@SuppressWarnings("deprecation") |
565 |
public int getType() { |
566 |
if(store == null) |
567 |
return org.gvsig.fmap.geom.Geometry.TYPES.NULL;
|
568 |
FeatureType featureType; |
569 |
try {
|
570 |
featureType = store.getDefaultFeatureType(); |
571 |
} catch (DataException e) {
|
572 |
return org.gvsig.fmap.geom.Geometry.TYPES.NULL;
|
573 |
} |
574 |
int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
575 |
return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
576 |
} |
577 |
|
578 |
public boolean isCompatibleType(org.gvsig.fmap.geom.Geometry geom) { |
579 |
if(store == null) |
580 |
return false; |
581 |
try {
|
582 |
return acceptType(store, geom);
|
583 |
} catch (ReadException e) {
|
584 |
return false; |
585 |
} |
586 |
} |
587 |
|
588 |
/**
|
589 |
* Gets the output FeatureStore
|
590 |
* @return
|
591 |
*/
|
592 |
public FeatureStore getOutputFeatureStore() {
|
593 |
return store;
|
594 |
} |
595 |
} |
596 |
|