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 @ 1259
History | View | Annotate | Download (28 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 |
import java.util.List; |
28 |
|
29 |
import org.gvsig.fmap.dal.exception.DataException; |
30 |
import org.gvsig.fmap.dal.exception.ReadException; |
31 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
32 |
import org.gvsig.fmap.dal.feature.Feature; |
33 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
34 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
35 |
import org.gvsig.fmap.dal.feature.FeatureType; |
36 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
37 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
38 |
import org.gvsig.fmap.geom.GeometryLocator; |
39 |
import org.gvsig.fmap.geom.GeometryManager; |
40 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
41 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
42 |
import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
43 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
44 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
45 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
46 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
47 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
48 |
import org.gvsig.fmap.geom.operation.fromjts.FromJTS; |
49 |
import org.gvsig.fmap.geom.primitive.Curve; |
50 |
import org.gvsig.fmap.geom.primitive.Point; |
51 |
import org.gvsig.fmap.geom.primitive.Primitive; |
52 |
import org.gvsig.fmap.geom.primitive.Surface; |
53 |
import org.gvsig.fmap.geom.type.GeometryType; |
54 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
55 |
|
56 |
import com.vividsolutions.jts.geom.Geometry; |
57 |
import com.vividsolutions.jts.geom.GeometryCollection; |
58 |
|
59 |
import es.unex.sextante.core.Sextante; |
60 |
import org.gvsig.fmap.dal.DataTypes; |
61 |
|
62 |
/**
|
63 |
* Writes features in a FeatureStore
|
64 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
65 |
*/
|
66 |
public class DALFeaturePersister { |
67 |
private FeatureStore store = null; |
68 |
private String[] fieldNames = null; |
69 |
private GeometryManager geometryManager = null; |
70 |
|
71 |
/**
|
72 |
* Sets the output FeatureType
|
73 |
* @param out
|
74 |
* @throws DataException
|
75 |
*/
|
76 |
public DALFeaturePersister(FeatureStore out, String[] fieldNames) { |
77 |
this.store = out;
|
78 |
this.fieldNames = fieldNames;
|
79 |
try {
|
80 |
if(!store.isEditing())
|
81 |
store.edit(); |
82 |
} catch (DataException e) {
|
83 |
} catch (NullPointerException e) { |
84 |
} |
85 |
geometryManager = GeometryLocator.getGeometryManager(); |
86 |
} |
87 |
|
88 |
/**
|
89 |
* Gets the list of field names
|
90 |
* @return
|
91 |
*/
|
92 |
public String[] getFieldNames() { |
93 |
try {
|
94 |
FeatureAttributeDescriptor[] attrs = store.getDefaultFeatureType().getAttributeDescriptors();
|
95 |
String[] fieldNames = new String[attrs.length]; |
96 |
for (int i = 0; i < attrs.length; i++) { |
97 |
fieldNames[i] = attrs[i].getName(); |
98 |
} |
99 |
return fieldNames;
|
100 |
} catch (DataException e) {
|
101 |
Sextante.addErrorToLog(e); |
102 |
} |
103 |
return null; |
104 |
} |
105 |
|
106 |
/**
|
107 |
* Gets the list of field names
|
108 |
* @return
|
109 |
*/
|
110 |
public String[] getFieldNamesWithoutGeom() { |
111 |
try {
|
112 |
FeatureAttributeDescriptor[] attrs = store.getDefaultFeatureType().getAttributeDescriptors();
|
113 |
int nFields = attrs.length;
|
114 |
for (int j = 0; j < nFields; j++) { |
115 |
if(attrs[j].getName().compareTo("GEOMETRY") == 0) { |
116 |
nFields --; |
117 |
break;
|
118 |
} |
119 |
} |
120 |
|
121 |
int cont = 0; |
122 |
String[] fieldNames = new String[nFields]; |
123 |
for (int i = 0; i < attrs.length; i++) { |
124 |
if(attrs[i].getName().compareTo("GEOMETRY") != 0) { |
125 |
fieldNames[cont] = attrs[i].getName(); |
126 |
cont ++; |
127 |
} |
128 |
} |
129 |
return fieldNames;
|
130 |
} catch (DataException e) {
|
131 |
Sextante.addErrorToLog(e); |
132 |
} |
133 |
return null; |
134 |
} |
135 |
|
136 |
/**
|
137 |
* Adds a JTS feature to the FeatureStore
|
138 |
* @param entry
|
139 |
* @param newGeom
|
140 |
* @throws CreateGeometryException
|
141 |
* @throws DataException
|
142 |
*/
|
143 |
public EditableFeature addFeature(Feature feature1, Feature feature2, Geometry newGeom)
|
144 |
throws CreateGeometryException, DataException {
|
145 |
|
146 |
int outPutType = getOutputFeatureStore().getDefaultFeatureType()
|
147 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
148 |
|
149 |
//Si el objeto que viene es un GeometryCollection hay que convertirlo a una lista de geometrias
|
150 |
//solo del tipo que pueda salvarse en la capa eliminando el resto. Esto sucede sobre todo en la intersecci?n
|
151 |
//que devuelve tipos mezclados. Cada uno debe salvarse en el tipo de capa de salida apropiado
|
152 |
ArrayList<org.gvsig.fmap.geom.Geometry> dalGeomList = null; |
153 |
try {
|
154 |
dalGeomList = convertGeometryCollection2GeometryList(newGeom, outPutType); |
155 |
} catch (GeometryOperationNotSupportedException e1) {
|
156 |
Sextante.addErrorToLog(e1); |
157 |
} catch (GeometryOperationException e1) {
|
158 |
Sextante.addErrorToLog(e1); |
159 |
} |
160 |
org.gvsig.fmap.geom.Geometry newDalGeom = null;
|
161 |
|
162 |
//Si lo que venia no era un GeometryCollection se convierte a geometr?a de DAL
|
163 |
if(dalGeomList == null) { |
164 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
165 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
166 |
try {
|
167 |
newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
168 |
} catch (GeometryOperationNotSupportedException e) {
|
169 |
Sextante.addErrorToLog(e); |
170 |
return null; |
171 |
} catch (GeometryOperationException e) {
|
172 |
Sextante.addErrorToLog(e); |
173 |
return null; |
174 |
} |
175 |
} |
176 |
|
177 |
EditableFeature feat = store.createNewFeature(); |
178 |
|
179 |
int globalIndex = 0; |
180 |
int localIndex = 0; |
181 |
Object value = feature1.get(localIndex);
|
182 |
while(value != null && !(value instanceof org.gvsig.fmap.geom.Geometry)) { |
183 |
feat.set(globalIndex, value); |
184 |
value = feature1.get(++localIndex); |
185 |
globalIndex ++; |
186 |
} |
187 |
|
188 |
localIndex = 0;
|
189 |
value = feature2.get(localIndex); |
190 |
while(value != null && !(value instanceof org.gvsig.fmap.geom.Geometry)) { |
191 |
feat.set(globalIndex, value); |
192 |
value = feature2.get(++localIndex); |
193 |
globalIndex ++; |
194 |
} |
195 |
|
196 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
197 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
198 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
199 |
feat.setGeometry("GEOMETRY", multiGeometry);
|
200 |
} else {
|
201 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
202 |
} |
203 |
store.insert(feat); |
204 |
return feat;
|
205 |
} else if(dalGeomList != null) { |
206 |
for (int i = 0; i < dalGeomList.size(); i++) { |
207 |
if(acceptType(store, dalGeomList.get(i))) {
|
208 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(dalGeomList.get(i)); |
209 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
210 |
feat.setGeometry("GEOMETRY", multiGeometry);
|
211 |
} else {
|
212 |
feat.setGeometry("GEOMETRY", dalGeomList.get(i));
|
213 |
} |
214 |
store.insert(feat); |
215 |
} |
216 |
} |
217 |
return feat;
|
218 |
} |
219 |
return null; |
220 |
} |
221 |
|
222 |
public EditableFeature addFeature(Feature feature1, Feature feature2, org.gvsig.fmap.geom.Geometry newDalGeom)
|
223 |
throws CreateGeometryException, DataException {
|
224 |
EditableFeature feat = store.createNewFeature(); |
225 |
|
226 |
int globalIndex = 0; |
227 |
int localIndex = 0; |
228 |
Object value = feature1.get(localIndex);
|
229 |
while(value != null && !(value instanceof org.gvsig.fmap.geom.Geometry)) { |
230 |
feat.set(globalIndex, value); |
231 |
value = feature1.get(++localIndex); |
232 |
globalIndex ++; |
233 |
} |
234 |
|
235 |
localIndex = 0;
|
236 |
value = feature2.get(localIndex); |
237 |
while(value != null && !(value instanceof org.gvsig.fmap.geom.Geometry)) { |
238 |
feat.set(globalIndex, value); |
239 |
value = feature2.get(++localIndex); |
240 |
globalIndex ++; |
241 |
} |
242 |
|
243 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
244 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
245 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
246 |
feat.setGeometry("GEOMETRY", multiGeometry);
|
247 |
} else {
|
248 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
249 |
} |
250 |
store.insert(feat); |
251 |
return feat;
|
252 |
} |
253 |
return null; |
254 |
} |
255 |
|
256 |
/**
|
257 |
* Converts a geometry collection from JTS to a list of geometries of the selected type.
|
258 |
* The GeometryCollection is converted only when it is a heterogeneous collection of geometries
|
259 |
* @param col
|
260 |
* @param type
|
261 |
* @return
|
262 |
* @throws CreateGeometryException
|
263 |
* @throws GeometryOperationException
|
264 |
* @throws GeometryOperationNotSupportedException
|
265 |
*/
|
266 |
public ArrayList<org.gvsig.fmap.geom.Geometry> convertGeometryCollection2GeometryList(Geometry g, int type) throws GeometryOperationNotSupportedException, GeometryOperationException { |
267 |
if( g instanceof GeometryCollection && |
268 |
!(g instanceof com.vividsolutions.jts.geom.MultiLineString) &&
|
269 |
!(g instanceof com.vividsolutions.jts.geom.MultiPoint) &&
|
270 |
!(g instanceof com.vividsolutions.jts.geom.MultiPolygon)) {
|
271 |
GeometryCollection col = (GeometryCollection)g; |
272 |
ArrayList<org.gvsig.fmap.geom.Geometry> geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>(); |
273 |
|
274 |
if(type == TYPES.MULTIPOINT || type == TYPES.POINT) {
|
275 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
276 |
Geometry gx = col.getGeometryN(i); |
277 |
if( gx instanceof com.vividsolutions.jts.geom.Point || |
278 |
gx instanceof com.vividsolutions.jts.geom.MultiPoint) {
|
279 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
280 |
ctx.setAttribute(FromJTS.PARAM, gx); |
281 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
282 |
geometries.add(newDalGeom); |
283 |
} |
284 |
} |
285 |
} |
286 |
|
287 |
if(type == TYPES.MULTICURVE || type == TYPES.CURVE) {
|
288 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
289 |
Geometry gx = col.getGeometryN(i); |
290 |
if( gx instanceof com.vividsolutions.jts.geom.LineString || |
291 |
gx instanceof com.vividsolutions.jts.geom.MultiLineString) {
|
292 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
293 |
ctx.setAttribute(FromJTS.PARAM, gx); |
294 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
295 |
geometries.add(newDalGeom); |
296 |
} |
297 |
} |
298 |
} |
299 |
if(type == TYPES.MULTISURFACE || type == TYPES.SURFACE) {
|
300 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
301 |
Geometry gx = col.getGeometryN(i); |
302 |
if( gx instanceof com.vividsolutions.jts.geom.Polygon || |
303 |
gx instanceof com.vividsolutions.jts.geom.MultiPolygon) {
|
304 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
305 |
ctx.setAttribute(FromJTS.PARAM, gx); |
306 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
307 |
geometries.add(newDalGeom); |
308 |
} |
309 |
} |
310 |
} |
311 |
return geometries;
|
312 |
} |
313 |
return null; |
314 |
} |
315 |
|
316 |
/**
|
317 |
* Converts a geometry into a multigeometry
|
318 |
* @param g
|
319 |
* @return
|
320 |
* @throws CreateGeometryException
|
321 |
*/
|
322 |
private org.gvsig.fmap.geom.Geometry convertGeometryList2MultiGeometry(List<org.gvsig.fmap.geom.Geometry> list) throws CreateGeometryException { |
323 |
if(list.size() == 0) |
324 |
return null; |
325 |
MultiPrimitive primitive = null;
|
326 |
|
327 |
if(list.get(0) instanceof Point || list.get(0) instanceof MultiPoint) { |
328 |
primitive = (MultiPrimitive)geometryManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D); |
329 |
} |
330 |
if(list.get(0) instanceof Curve || list.get(0) instanceof MultiCurve) { |
331 |
primitive = (MultiPrimitive)geometryManager.create(TYPES.MULTICURVE, SUBTYPES.GEOM2D); |
332 |
} |
333 |
if(list.get(0) instanceof Surface || list.get(0) instanceof MultiSurface) { |
334 |
primitive = (MultiPrimitive)geometryManager.create(TYPES.MULTISURFACE, SUBTYPES.GEOM2D); |
335 |
} |
336 |
|
337 |
for (int i = 0; i < list.size(); i++) { |
338 |
org.gvsig.fmap.geom.Geometry geom = list.get(i); |
339 |
if(geom instanceof MultiPrimitive) { |
340 |
MultiPrimitive multi = (MultiPrimitive)geom; |
341 |
for (int j = 0; j < multi.getPrimitivesNumber(); j++) { |
342 |
primitive.addPrimitive((Primitive)multi.getPrimitiveAt(j)); |
343 |
} |
344 |
} else
|
345 |
primitive.addPrimitive((Primitive)list.get(i)); |
346 |
} |
347 |
|
348 |
return primitive;
|
349 |
} |
350 |
|
351 |
/**
|
352 |
* Converts a geometry into a multigeometry
|
353 |
* @param g
|
354 |
* @return
|
355 |
* @throws CreateGeometryException
|
356 |
*/
|
357 |
private org.gvsig.fmap.geom.Geometry convertGeometry2MultiGeometry(org.gvsig.fmap.geom.Geometry g) throws CreateGeometryException { |
358 |
if(g instanceof MultiPrimitive) |
359 |
return g;
|
360 |
if(g instanceof Point) { |
361 |
MultiPoint pNew = (MultiPoint)geometryManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D); |
362 |
pNew.addPoint((Point)g);
|
363 |
return pNew;
|
364 |
} |
365 |
if(g instanceof Curve) { |
366 |
MultiCurve cNew = (MultiCurve)geometryManager.create(TYPES.MULTICURVE, SUBTYPES.GEOM2D); |
367 |
cNew.addCurve((Curve)g); |
368 |
return cNew;
|
369 |
} |
370 |
if(g instanceof Surface) { |
371 |
MultiSurface cNew = (MultiSurface)geometryManager.create(TYPES.MULTISURFACE, SUBTYPES.GEOM2D); |
372 |
cNew.addSurface((Surface)g); |
373 |
return cNew;
|
374 |
} |
375 |
return g;
|
376 |
} |
377 |
|
378 |
/**
|
379 |
* Checks if the type of the feature store and the geometry are compatibles.
|
380 |
* @param store
|
381 |
* @param geom
|
382 |
* @return
|
383 |
* @throws ReadException
|
384 |
*/
|
385 |
private boolean acceptType(FeatureStore store, org.gvsig.fmap.geom.Geometry geom) throws ReadException { |
386 |
int fType = getShapeType(store);
|
387 |
GeometryType geomType = geom.getGeometryType(); |
388 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
389 |
|
390 |
if(geomManager.isSubtype(fType, geomType.getType()))
|
391 |
return true; |
392 |
|
393 |
if( geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE, fType) &&
|
394 |
geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.CURVE, geomType.getType())) |
395 |
return true; |
396 |
if( geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTISURFACE, fType) &&
|
397 |
geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.SURFACE, geomType.getType()) ) |
398 |
return true; |
399 |
if( geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT, fType) &&
|
400 |
geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.POINT, geomType.getType())) |
401 |
return true; |
402 |
if( geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTISOLID, fType) &&
|
403 |
geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.SOLID, geomType.getType())) |
404 |
return true; |
405 |
return false; |
406 |
} |
407 |
|
408 |
/**
|
409 |
* Adds a JTS feature to the FeatureStore
|
410 |
* @param entry
|
411 |
* @param newGeom
|
412 |
* @throws CreateGeometryException
|
413 |
* @throws DataException
|
414 |
*/
|
415 |
public EditableFeature addFeature(Feature feature, Geometry newGeom) throws CreateGeometryException, DataException { |
416 |
int outPutType = getOutputFeatureStore().getDefaultFeatureType()
|
417 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
418 |
//Si el objeto que viene es un GeometryCollection hay que convertirlo a una lista de geometrias
|
419 |
//solo del tipo que pueda salvarse en la capa eliminando el resto. Esto sucede sobre todo en la intersecci?n
|
420 |
//que devuelve tipos mezclados. Cada uno debe salvarse en el tipo de capa de salida apropiado
|
421 |
ArrayList<org.gvsig.fmap.geom.Geometry> dalGeomList = null; |
422 |
try {
|
423 |
dalGeomList = convertGeometryCollection2GeometryList(newGeom, outPutType); |
424 |
} catch (GeometryOperationNotSupportedException e1) {
|
425 |
Sextante.addErrorToLog(e1); |
426 |
} catch (GeometryOperationException e1) {
|
427 |
Sextante.addErrorToLog(e1); |
428 |
} |
429 |
|
430 |
//No era un GeometryCollection
|
431 |
if(dalGeomList == null) { |
432 |
org.gvsig.fmap.geom.Geometry newDalGeom = null;
|
433 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
434 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
435 |
try {
|
436 |
newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
437 |
} catch (GeometryOperationNotSupportedException e) {
|
438 |
Sextante.addErrorToLog(e); |
439 |
} catch (GeometryOperationException e) {
|
440 |
Sextante.addErrorToLog(e); |
441 |
} |
442 |
insertFeatureIntoStore(feature, newDalGeom); |
443 |
} else {
|
444 |
for (int i = 0; i < dalGeomList.size(); i++) { |
445 |
insertFeatureIntoStore(feature, dalGeomList.get(i)); |
446 |
} |
447 |
} |
448 |
return null; |
449 |
} |
450 |
|
451 |
private EditableFeature insertFeatureIntoStore(Feature feature, org.gvsig.fmap.geom.Geometry newDalGeom) throws DataException { |
452 |
if (acceptType(store, newDalGeom)) {
|
453 |
EditableFeature targetFeature = store.createNewFeature(store.getDefaultFeatureType(), true);
|
454 |
for (FeatureAttributeDescriptor attrdesc : targetFeature.getType()) {
|
455 |
if (attrdesc.isComputed()) {
|
456 |
continue;
|
457 |
} |
458 |
if (attrdesc.getType() == DataTypes.GEOMETRY) {
|
459 |
continue;
|
460 |
} |
461 |
String name = attrdesc.getName();
|
462 |
Object value = feature.get(name);
|
463 |
if (value != null) { |
464 |
targetFeature.set(name, value); |
465 |
} |
466 |
} |
467 |
org.gvsig.fmap.geom.Geometry multiGeometry = null;
|
468 |
try {
|
469 |
multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
470 |
} catch (CreateGeometryException e) {
|
471 |
Sextante.addErrorToLog(e); |
472 |
} |
473 |
|
474 |
if (multiGeometry != null && acceptType(store, multiGeometry)) { |
475 |
targetFeature.setGeometry("GEOMETRY", multiGeometry);
|
476 |
} else {
|
477 |
targetFeature.setGeometry("GEOMETRY", newDalGeom);
|
478 |
} |
479 |
store.insert(targetFeature); |
480 |
return targetFeature;
|
481 |
} |
482 |
return null; |
483 |
} |
484 |
|
485 |
/**
|
486 |
* Adds a JTS feature to the FeatureStore
|
487 |
* @param entry
|
488 |
* @param newGeom
|
489 |
* @throws CreateGeometryException
|
490 |
* @throws DataException
|
491 |
*/
|
492 |
public EditableFeature addFeature(Geometry newGeom, int id, double value) throws CreateGeometryException, DataException { |
493 |
org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
494 |
|
495 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
496 |
EditableFeature feat = store.createNewFeature(); |
497 |
feat.set(0, id);
|
498 |
feat.set(1, value);
|
499 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
500 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
501 |
feat.setGeometry("GEOMETRY", multiGeometry);
|
502 |
} else {
|
503 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
504 |
} |
505 |
store.insert(feat); |
506 |
return feat;
|
507 |
} |
508 |
return null; |
509 |
} |
510 |
|
511 |
/**
|
512 |
* Adds a JTS feature to the FeatureStore
|
513 |
* @param entry
|
514 |
* @param newGeom
|
515 |
* @throws CreateGeometryException
|
516 |
* @throws DataException
|
517 |
*/
|
518 |
public EditableFeature addFeature(Feature feature, List<Geometry> newJTSListGeom) throws CreateGeometryException, DataException { |
519 |
List<org.gvsig.fmap.geom.Geometry> newDalListGeom = new ArrayList<org.gvsig.fmap.geom.Geometry>(); |
520 |
|
521 |
for (int i = 0; i < newJTSListGeom.size(); i++) { |
522 |
newDalListGeom.add(GeometryUtil.jtsToGeom(newJTSListGeom.get(i))); |
523 |
} |
524 |
|
525 |
org.gvsig.fmap.geom.Geometry newMultiGeom = convertGeometryList2MultiGeometry(newDalListGeom); |
526 |
insertFeatureIntoStore(feature, newMultiGeom); |
527 |
return null; |
528 |
} |
529 |
|
530 |
/**
|
531 |
* Adds a JTS feature to the FeatureStore
|
532 |
* @param entry
|
533 |
* @param newGeom
|
534 |
* @throws CreateGeometryException
|
535 |
* @throws DataException
|
536 |
*/
|
537 |
public EditableFeature addFeature(Geometry newGeom, String name, Object value) throws CreateGeometryException, DataException { |
538 |
org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
539 |
|
540 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
541 |
EditableFeature feat = store.createNewFeature(); |
542 |
feat.set(name, value); |
543 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
544 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
545 |
feat.setGeometry("GEOMETRY", multiGeometry);
|
546 |
} else {
|
547 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
548 |
} |
549 |
store.insert(feat); |
550 |
return feat;
|
551 |
} |
552 |
return null; |
553 |
} |
554 |
|
555 |
/**
|
556 |
* Adds a geometry feature to the FeatureStore
|
557 |
* @param entry
|
558 |
* @param newGeom
|
559 |
* @throws CreateGeometryException
|
560 |
* @throws DataException
|
561 |
*/
|
562 |
public EditableFeature addFeature(org.gvsig.fmap.geom.Geometry newGeom, String name, Object value) throws CreateGeometryException, DataException { |
563 |
if(newGeom != null && acceptType(store, newGeom)) { |
564 |
EditableFeature feat = store.createNewFeature(); |
565 |
feat.set(name, value); |
566 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
567 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
568 |
feat.setGeometry("GEOMETRY", multiGeometry);
|
569 |
} else {
|
570 |
feat.setGeometry("GEOMETRY", newGeom);
|
571 |
} |
572 |
store.insert(feat); |
573 |
return feat;
|
574 |
} |
575 |
return null; |
576 |
} |
577 |
|
578 |
/**
|
579 |
* Adds a geometry feature to the FeatureStore
|
580 |
* @param entry
|
581 |
* @param newGeom
|
582 |
* @throws CreateGeometryException
|
583 |
* @throws DataException
|
584 |
*/
|
585 |
public EditableFeature addFeature(org.gvsig.fmap.geom.Geometry newGeom, ArrayList<String> names, ArrayList<Object> values) throws CreateGeometryException, DataException { |
586 |
if(newGeom != null && acceptType(store, newGeom)) { |
587 |
EditableFeature feat = store.createNewFeature(); |
588 |
for (int i = 0; i < values.size(); i++) { |
589 |
feat.set(names.get(i), values.get(i)); |
590 |
} |
591 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
592 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
593 |
feat.setGeometry("GEOMETRY", multiGeometry);
|
594 |
} else {
|
595 |
feat.setGeometry("GEOMETRY", newGeom);
|
596 |
} |
597 |
store.insert(feat); |
598 |
return feat;
|
599 |
} |
600 |
return null; |
601 |
} |
602 |
|
603 |
/**
|
604 |
* Adds a JTS feature to the FeatureStore
|
605 |
* @param entry
|
606 |
* @param newGeom
|
607 |
* @throws CreateGeometryException
|
608 |
* @throws DataException
|
609 |
*/
|
610 |
public EditableFeature addFeature(Geometry newGeom, ArrayList<String> names, ArrayList<Object> values) throws CreateGeometryException, DataException { |
611 |
org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
612 |
|
613 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
614 |
EditableFeature feat = store.createNewFeature(); |
615 |
for (int i = 0; i < values.size(); i++) { |
616 |
feat.set(names.get(i), values.get(i)); |
617 |
} |
618 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
619 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
620 |
feat.setDefaultGeometry(multiGeometry); |
621 |
} else {
|
622 |
feat.setDefaultGeometry(newDalGeom); |
623 |
} |
624 |
store.insert(feat); |
625 |
return feat;
|
626 |
} |
627 |
return null; |
628 |
} |
629 |
|
630 |
/**
|
631 |
* Adds a JTS feature to the FeatureStore
|
632 |
* @param entry
|
633 |
* @param newGeom
|
634 |
* @throws CreateGeometryException
|
635 |
* @throws DataException
|
636 |
*/
|
637 |
public EditableFeature addFeature(Geometry newGeom, int id, double value1, double value2) throws CreateGeometryException, DataException { |
638 |
org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
639 |
|
640 |
if(newDalGeom != null && acceptType(store, newDalGeom)) { |
641 |
EditableFeature feat = store.createNewFeature(); |
642 |
feat.set(0, id);
|
643 |
feat.set(1, value1);
|
644 |
feat.set(2, value2);
|
645 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
646 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
647 |
feat.setDefaultGeometry(multiGeometry); |
648 |
} else {
|
649 |
feat.setDefaultGeometry(newDalGeom); |
650 |
} |
651 |
store.insert(feat); |
652 |
return feat;
|
653 |
} |
654 |
return null; |
655 |
} |
656 |
|
657 |
/**
|
658 |
* Adds a DAL feature to the FeatureStore when the field structure in the new feature
|
659 |
* is different to the source field structure.
|
660 |
* @param entry
|
661 |
* @param newGeom
|
662 |
* @throws CreateGeometryException
|
663 |
* @throws DataException
|
664 |
*/
|
665 |
public EditableFeature addDifferentFieldFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
666 |
if(acceptType(store, newGeom)) {
|
667 |
//Builds a new empty feature
|
668 |
EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), true);
|
669 |
|
670 |
//Writes only values with the same name and data type in both features
|
671 |
for (int i = 0; i < fieldNames.length; i++) { |
672 |
if(feature.getType().getIndex(fieldNames[i]) != -1) { |
673 |
Object obj1 = feature.get(fieldNames[i]);
|
674 |
if(feat.getType().getIndex(fieldNames[i]) != -1) { |
675 |
Object obj2 = feat.get(fieldNames[i]);
|
676 |
if(obj1 != null && obj2 != null && obj1.getClass() == obj2.getClass()) |
677 |
feat.set(i, obj1); |
678 |
} |
679 |
} |
680 |
} |
681 |
|
682 |
//Sets the geometry
|
683 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
684 |
if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
685 |
feat.setDefaultGeometry(multiGeometry); |
686 |
} else {
|
687 |
feat.setDefaultGeometry(newGeom); |
688 |
} |
689 |
store.insert(feat); |
690 |
return feat;
|
691 |
} |
692 |
return null; |
693 |
} |
694 |
|
695 |
/**
|
696 |
* Adds a DAL feature to the FeatureStore
|
697 |
* @param entry
|
698 |
* @param newGeom
|
699 |
* @throws CreateGeometryException
|
700 |
* @throws DataException
|
701 |
*/
|
702 |
public EditableFeature addFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
703 |
if(acceptType(store, newGeom)) {
|
704 |
EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), feature); |
705 |
org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
706 |
if (multiGeometry!=null && acceptType(store, multiGeometry)){ |
707 |
feat.setDefaultGeometry(multiGeometry); |
708 |
} else {
|
709 |
feat.setDefaultGeometry(newGeom); |
710 |
} |
711 |
store.insert(feat); |
712 |
return feat;
|
713 |
} |
714 |
return null; |
715 |
} |
716 |
|
717 |
/**
|
718 |
* Adds a DAL feature to the FeatureStore
|
719 |
* @param entry
|
720 |
* @param newGeom
|
721 |
* @throws CreateGeometryException
|
722 |
* @throws DataException
|
723 |
*/
|
724 |
public EditableFeature addGeometryToExistingFeature(EditableFeature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
725 |
//Y esto para que c... sirve?
|
726 |
for (int i = 0; i < fieldNames.length; i++) |
727 |
feature.set(fieldNames[i], feature.get(i)); |
728 |
|
729 |
if(acceptType(store, newGeom)) {
|
730 |
feature.setGeometry(fieldNames.length, newGeom); |
731 |
store.update(feature); |
732 |
return feature;
|
733 |
} |
734 |
return null; |
735 |
} |
736 |
|
737 |
/**
|
738 |
* Ends the edition and closes the FeatureStore
|
739 |
*/
|
740 |
public void end() { |
741 |
try {
|
742 |
store.finishEditing(); |
743 |
} catch (DataException e) {
|
744 |
Sextante.addErrorToLog(e); |
745 |
} |
746 |
// Si destruimos aqui el store ya no estara disponible en el postprocess,
|
747 |
// donde se usa, y se vuelve a destruir.
|
748 |
// store.dispose();
|
749 |
} |
750 |
|
751 |
/**
|
752 |
* Gets the shape type of the selected feature store
|
753 |
* @param FeatureStore source
|
754 |
* @return shape type
|
755 |
* @throws ReadException
|
756 |
*/
|
757 |
@SuppressWarnings("deprecation") |
758 |
public int getShapeType(FeatureStore storeLayer1) throws ReadException { |
759 |
FeatureType featureType; |
760 |
try {
|
761 |
featureType = storeLayer1.getDefaultFeatureType(); |
762 |
} catch (DataException e) {
|
763 |
throw new ReadException(storeLayer1.getName(), e); |
764 |
} |
765 |
int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
766 |
return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
767 |
} |
768 |
|
769 |
@SuppressWarnings("deprecation") |
770 |
public int getType() { |
771 |
if(store == null) |
772 |
return org.gvsig.fmap.geom.Geometry.TYPES.NULL;
|
773 |
FeatureType featureType; |
774 |
try {
|
775 |
featureType = store.getDefaultFeatureType(); |
776 |
} catch (DataException e) {
|
777 |
return org.gvsig.fmap.geom.Geometry.TYPES.NULL;
|
778 |
} |
779 |
int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
780 |
return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
781 |
} |
782 |
|
783 |
public boolean isCompatibleType(org.gvsig.fmap.geom.Geometry geom) { |
784 |
if(store == null) |
785 |
return false; |
786 |
try {
|
787 |
return acceptType(store, geom);
|
788 |
} catch (ReadException e) {
|
789 |
return false; |
790 |
} |
791 |
} |
792 |
|
793 |
/**
|
794 |
* Gets the output FeatureStore
|
795 |
* @return
|
796 |
*/
|
797 |
public FeatureStore getOutputFeatureStore() {
|
798 |
return store;
|
799 |
} |
800 |
} |
801 |
|