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