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 | 237 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 175 | cordinyana | *
|
4 | 245 | cordinyana | * Copyright (C) 2007-2012 gvSIG Association.
|
5 | 175 | cordinyana | *
|
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 | 237 | cordinyana | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 245 | cordinyana | *
|
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 | 175 | cordinyana | */
|
24 | package org.gvsig.geoprocess.algorithm.base.core; |
||
25 | |||
26 | 267 | nbrodin | import java.util.ArrayList; |
27 | 439 | nbrodin | import java.util.List; |
28 | 267 | nbrodin | |
29 | 175 | cordinyana | 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 | 265 | nbrodin | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
34 | 175 | cordinyana | import org.gvsig.fmap.dal.feature.FeatureStore; |
35 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
36 | 439 | nbrodin | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
37 | import org.gvsig.fmap.geom.Geometry.TYPES; |
||
38 | 175 | cordinyana | import org.gvsig.fmap.geom.GeometryLocator; |
39 | import org.gvsig.fmap.geom.GeometryManager; |
||
40 | 266 | nbrodin | import org.gvsig.fmap.geom.aggregate.MultiCurve; |
41 | import org.gvsig.fmap.geom.aggregate.MultiPoint; |
||
42 | 439 | nbrodin | import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
43 | import org.gvsig.fmap.geom.aggregate.MultiSurface; |
||
44 | 175 | cordinyana | 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 | 266 | nbrodin | import org.gvsig.fmap.geom.primitive.Curve; |
50 | import org.gvsig.fmap.geom.primitive.Point; |
||
51 | 439 | nbrodin | import org.gvsig.fmap.geom.primitive.Primitive; |
52 | import org.gvsig.fmap.geom.primitive.Surface; |
||
53 | 807 | fdiaz | import org.gvsig.fmap.geom.type.GeometryType; |
54 | 175 | cordinyana | import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
55 | |||
56 | import com.vividsolutions.jts.geom.Geometry; |
||
57 | 267 | nbrodin | import com.vividsolutions.jts.geom.GeometryCollection; |
58 | 175 | cordinyana | |
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 | 545 | fdiaz | |
70 | 175 | cordinyana | /**
|
71 | * Sets the output FeatureType
|
||
72 | * @param out
|
||
73 | 545 | fdiaz | * @throws DataException
|
74 | 175 | cordinyana | */
|
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 | 545 | fdiaz | |
87 | 175 | cordinyana | /**
|
88 | 545 | fdiaz | * Gets the list of field names
|
89 | 278 | nbrodin | * @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 | 545 | fdiaz | |
105 | 278 | nbrodin | /**
|
106 | 545 | fdiaz | * Gets the list of field names
|
107 | 278 | nbrodin | * @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 | 545 | fdiaz | |
120 | 278 | nbrodin | 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 | 545 | fdiaz | |
135 | 278 | nbrodin | /**
|
136 | 175 | cordinyana | * Adds a JTS feature to the FeatureStore
|
137 | * @param entry
|
||
138 | * @param newGeom
|
||
139 | * @throws CreateGeometryException
|
||
140 | * @throws DataException
|
||
141 | */
|
||
142 | 545 | fdiaz | public EditableFeature addFeature(Feature feature1, Feature feature2, Geometry newGeom)
|
143 | 175 | cordinyana | throws CreateGeometryException, DataException {
|
144 | 545 | fdiaz | |
145 | 267 | nbrodin | int outPutType = getOutputFeatureStore().getDefaultFeatureType()
|
146 | .getDefaultGeometryAttribute().getGeomType().getType(); |
||
147 | 545 | fdiaz | |
148 | 267 | nbrodin | //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 | 175 | cordinyana | try {
|
153 | 267 | nbrodin | dalGeomList = convertGeometryCollection2GeometryList(newGeom, outPutType); |
154 | } catch (GeometryOperationNotSupportedException e1) {
|
||
155 | Sextante.addErrorToLog(e1); |
||
156 | } catch (GeometryOperationException e1) {
|
||
157 | Sextante.addErrorToLog(e1); |
||
158 | 175 | cordinyana | } |
159 | 267 | nbrodin | org.gvsig.fmap.geom.Geometry newDalGeom = null;
|
160 | 545 | fdiaz | |
161 | //Si lo que venia no era un GeometryCollection se convierte a geometr?a de DAL
|
||
162 | 267 | nbrodin | 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 | 545 | fdiaz | |
176 | 175 | cordinyana | EditableFeature feat = store.createNewFeature(); |
177 | 545 | fdiaz | |
178 | 175 | cordinyana | 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 | 545 | fdiaz | |
187 | 175 | cordinyana | 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 | 267 | nbrodin | if(newDalGeom != null && acceptType(store, newDalGeom)) { |
196 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
197 | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
198 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
199 | } else {
|
||
200 | 659 | fdiaz | feat.setGeometry("GEOMETRY", newDalGeom);
|
201 | } |
||
202 | 175 | cordinyana | store.insert(feat); |
203 | return feat;
|
||
204 | 267 | nbrodin | } else if(dalGeomList != null) { |
205 | for (int i = 0; i < dalGeomList.size(); i++) { |
||
206 | if(acceptType(store, dalGeomList.get(i))) {
|
||
207 | 896 | fdiaz | 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 | 659 | fdiaz | feat.setGeometry("GEOMETRY", dalGeomList.get(i));
|
212 | } |
||
213 | store.insert(feat); |
||
214 | 267 | nbrodin | } |
215 | } |
||
216 | return feat;
|
||
217 | 175 | cordinyana | } |
218 | return null; |
||
219 | } |
||
220 | 545 | fdiaz | |
221 | public EditableFeature addFeature(Feature feature1, Feature feature2, org.gvsig.fmap.geom.Geometry newDalGeom)
|
||
222 | 372 | nbrodin | throws CreateGeometryException, DataException {
|
223 | EditableFeature feat = store.createNewFeature(); |
||
224 | 545 | fdiaz | |
225 | 372 | nbrodin | 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 | 545 | fdiaz | |
234 | 372 | nbrodin | 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 | 896 | fdiaz | 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 | 372 | nbrodin | store.insert(feat); |
250 | return feat;
|
||
251 | } |
||
252 | return null; |
||
253 | } |
||
254 | 545 | fdiaz | |
255 | 175 | cordinyana | /**
|
256 | 268 | nbrodin | * Converts a geometry collection from JTS to a list of geometries of the selected type.
|
257 | 545 | fdiaz | * The GeometryCollection is converted only when it is a heterogeneous collection of geometries
|
258 | 267 | nbrodin | * @param col
|
259 | * @param type
|
||
260 | * @return
|
||
261 | 545 | fdiaz | * @throws CreateGeometryException
|
262 | * @throws GeometryOperationException
|
||
263 | * @throws GeometryOperationNotSupportedException
|
||
264 | 267 | nbrodin | */
|
265 | public ArrayList<org.gvsig.fmap.geom.Geometry> convertGeometryCollection2GeometryList(Geometry g, int type) throws GeometryOperationNotSupportedException, GeometryOperationException { |
||
266 | 545 | fdiaz | if( g instanceof GeometryCollection && |
267 | !(g instanceof com.vividsolutions.jts.geom.MultiLineString) &&
|
||
268 | !(g instanceof com.vividsolutions.jts.geom.MultiPoint) &&
|
||
269 | 268 | nbrodin | !(g instanceof com.vividsolutions.jts.geom.MultiPolygon)) {
|
270 | 267 | nbrodin | GeometryCollection col = (GeometryCollection)g; |
271 | ArrayList<org.gvsig.fmap.geom.Geometry> geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>(); |
||
272 | 545 | fdiaz | |
273 | 267 | nbrodin | if(type == TYPES.MULTIPOINT || type == TYPES.POINT) {
|
274 | for (int i = 0; i < col.getNumGeometries(); i++) { |
||
275 | Geometry gx = col.getGeometryN(i); |
||
276 | 545 | fdiaz | if( gx instanceof com.vividsolutions.jts.geom.Point || |
277 | 267 | nbrodin | 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 | 545 | fdiaz | |
286 | 267 | nbrodin | if(type == TYPES.MULTICURVE || type == TYPES.CURVE) {
|
287 | for (int i = 0; i < col.getNumGeometries(); i++) { |
||
288 | Geometry gx = col.getGeometryN(i); |
||
289 | 545 | fdiaz | if( gx instanceof com.vividsolutions.jts.geom.LineString || |
290 | 267 | nbrodin | 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 | 545 | fdiaz | if( gx instanceof com.vividsolutions.jts.geom.Polygon || |
302 | 267 | nbrodin | 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 | 545 | fdiaz | |
315 | 267 | nbrodin | /**
|
316 | 266 | nbrodin | * Converts a geometry into a multigeometry
|
317 | * @param g
|
||
318 | * @return
|
||
319 | * @throws CreateGeometryException
|
||
320 | */
|
||
321 | 439 | nbrodin | 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 | 545 | fdiaz | |
326 | 439 | nbrodin | 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 | 545 | fdiaz | |
336 | 439 | nbrodin | 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 | 545 | fdiaz | } |
346 | |||
347 | 439 | nbrodin | return primitive;
|
348 | } |
||
349 | 545 | fdiaz | |
350 | 439 | nbrodin | /**
|
351 | * Converts a geometry into a multigeometry
|
||
352 | * @param g
|
||
353 | * @return
|
||
354 | * @throws CreateGeometryException
|
||
355 | */
|
||
356 | 266 | nbrodin | private org.gvsig.fmap.geom.Geometry convertGeometry2MultiGeometry(org.gvsig.fmap.geom.Geometry g) throws CreateGeometryException { |
357 | 439 | nbrodin | if(g instanceof MultiPrimitive) |
358 | return g;
|
||
359 | 266 | nbrodin | 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 | 439 | nbrodin | if(g instanceof Surface) { |
370 | MultiSurface cNew = (MultiSurface)geometryManager.create(TYPES.MULTISURFACE, SUBTYPES.GEOM2D); |
||
371 | cNew.addSurface((Surface)g); |
||
372 | return cNew;
|
||
373 | } |
||
374 | 266 | nbrodin | return g;
|
375 | } |
||
376 | 545 | fdiaz | |
377 | 266 | nbrodin | /**
|
378 | 175 | cordinyana | * 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 | 807 | fdiaz | GeometryType geomType = geom.getGeometryType(); |
387 | 545 | fdiaz | |
388 | 807 | fdiaz | if(geomType.isTypeOf(fType))
|
389 | 175 | cordinyana | return true; |
390 | if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE &&
|
||
391 | 807 | fdiaz | geomType.isTypeOf(org.gvsig.fmap.geom.Geometry.TYPES.CURVE)) |
392 | 175 | cordinyana | return true; |
393 | if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTISURFACE &&
|
||
394 | 807 | fdiaz | geomType.isTypeOf(org.gvsig.fmap.geom.Geometry.TYPES.SURFACE)) |
395 | 175 | cordinyana | return true; |
396 | if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT &&
|
||
397 | 807 | fdiaz | geomType.isTypeOf(org.gvsig.fmap.geom.Geometry.TYPES.POINT)) |
398 | 175 | cordinyana | return true; |
399 | if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTISOLID &&
|
||
400 | 807 | fdiaz | geomType.isTypeOf(org.gvsig.fmap.geom.Geometry.TYPES.SOLID)) |
401 | 175 | cordinyana | 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 | 308 | nbrodin | 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 | 175 | cordinyana | try {
|
420 | 308 | nbrodin | dalGeomList = convertGeometryCollection2GeometryList(newGeom, outPutType); |
421 | } catch (GeometryOperationNotSupportedException e1) {
|
||
422 | Sextante.addErrorToLog(e1); |
||
423 | } catch (GeometryOperationException e1) {
|
||
424 | Sextante.addErrorToLog(e1); |
||
425 | 175 | cordinyana | } |
426 | 545 | fdiaz | |
427 | 308 | nbrodin | //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 | 545 | fdiaz | |
448 | 308 | nbrodin | private EditableFeature insertFeatureIntoStore(Feature feature, org.gvsig.fmap.geom.Geometry newDalGeom) throws DataException { |
449 | 175 | cordinyana | if(acceptType(store, newDalGeom)) {
|
450 | 265 | nbrodin | 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 | 650 | fdiaz | |
462 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = null;
|
463 | try {
|
||
464 | multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
||
465 | } catch (CreateGeometryException e) {
|
||
466 | Sextante.addErrorToLog(e); |
||
467 | } |
||
468 | 650 | fdiaz | |
469 | 896 | fdiaz | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
470 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
471 | } else {
|
||
472 | feat.setGeometry("GEOMETRY", newDalGeom);
|
||
473 | 650 | fdiaz | } |
474 | 175 | cordinyana | store.insert(feat); |
475 | return feat;
|
||
476 | } |
||
477 | return null; |
||
478 | } |
||
479 | 545 | fdiaz | |
480 | 175 | cordinyana | /**
|
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 | 896 | fdiaz | 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 | 175 | cordinyana | store.insert(feat); |
501 | return feat;
|
||
502 | } |
||
503 | return null; |
||
504 | } |
||
505 | 545 | fdiaz | |
506 | 175 | cordinyana | /**
|
507 | * Adds a JTS feature to the FeatureStore
|
||
508 | * @param entry
|
||
509 | * @param newGeom
|
||
510 | * @throws CreateGeometryException
|
||
511 | * @throws DataException
|
||
512 | */
|
||
513 | 439 | nbrodin | 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 | 545 | fdiaz | |
516 | 439 | nbrodin | for (int i = 0; i < newJTSListGeom.size(); i++) { |
517 | newDalListGeom.add(GeometryUtil.jtsToGeom(newJTSListGeom.get(i))); |
||
518 | } |
||
519 | 545 | fdiaz | |
520 | 439 | nbrodin | org.gvsig.fmap.geom.Geometry newMultiGeom = convertGeometryList2MultiGeometry(newDalListGeom); |
521 | insertFeatureIntoStore(feature, newMultiGeom); |
||
522 | return null; |
||
523 | } |
||
524 | 545 | fdiaz | |
525 | 439 | nbrodin | /**
|
526 | * Adds a JTS feature to the FeatureStore
|
||
527 | * @param entry
|
||
528 | * @param newGeom
|
||
529 | * @throws CreateGeometryException
|
||
530 | * @throws DataException
|
||
531 | */
|
||
532 | 272 | nbrodin | 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 | 896 | fdiaz | 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 | 272 | nbrodin | store.insert(feat); |
545 | return feat;
|
||
546 | } |
||
547 | return null; |
||
548 | } |
||
549 | 545 | fdiaz | |
550 | 272 | nbrodin | /**
|
551 | 350 | nbrodin | * 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 | 896 | fdiaz | 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 | 350 | nbrodin | store.insert(feat); |
568 | return feat;
|
||
569 | } |
||
570 | return null; |
||
571 | } |
||
572 | 545 | fdiaz | |
573 | 350 | nbrodin | /**
|
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 | 545 | fdiaz | feat.set(names.get(i), values.get(i)); |
585 | 350 | nbrodin | } |
586 | 896 | fdiaz | 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 | 350 | nbrodin | store.insert(feat); |
593 | return feat;
|
||
594 | } |
||
595 | return null; |
||
596 | } |
||
597 | 545 | fdiaz | |
598 | 350 | nbrodin | /**
|
599 | 272 | nbrodin | * Adds a JTS feature to the FeatureStore
|
600 | * @param entry
|
||
601 | * @param newGeom
|
||
602 | * @throws CreateGeometryException
|
||
603 | * @throws DataException
|
||
604 | */
|
||
605 | 278 | nbrodin | 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 | 545 | fdiaz | feat.set(names.get(i), values.get(i)); |
612 | 278 | nbrodin | } |
613 | 896 | fdiaz | 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 | 278 | nbrodin | store.insert(feat); |
620 | return feat;
|
||
621 | } |
||
622 | return null; |
||
623 | } |
||
624 | 545 | fdiaz | |
625 | 278 | nbrodin | /**
|
626 | * Adds a JTS feature to the FeatureStore
|
||
627 | * @param entry
|
||
628 | * @param newGeom
|
||
629 | * @throws CreateGeometryException
|
||
630 | * @throws DataException
|
||
631 | */
|
||
632 | 175 | cordinyana | 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 | 896 | fdiaz | 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 | 175 | cordinyana | store.insert(feat); |
647 | return feat;
|
||
648 | } |
||
649 | return null; |
||
650 | } |
||
651 | 545 | fdiaz | |
652 | 175 | cordinyana | /**
|
653 | 545 | fdiaz | * Adds a DAL feature to the FeatureStore when the field structure in the new feature
|
654 | 175 | cordinyana | * 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 | 545 | fdiaz | |
665 | 175 | cordinyana | //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 | 545 | fdiaz | |
677 | 175 | cordinyana | //Sets the geometry
|
678 | 896 | fdiaz | 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 | 175 | cordinyana | store.insert(feat); |
685 | return feat;
|
||
686 | } |
||
687 | return null; |
||
688 | } |
||
689 | 545 | fdiaz | |
690 | 175 | cordinyana | /**
|
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 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
701 | if (multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
702 | feat.setGeometry(fieldNames.length, multiGeometry); |
||
703 | 545 | fdiaz | } else {
|
704 | feat.setGeometry(fieldNames.length, newGeom); |
||
705 | } |
||
706 | 175 | cordinyana | store.insert(feat); |
707 | return feat;
|
||
708 | } |
||
709 | return null; |
||
710 | } |
||
711 | 545 | fdiaz | |
712 | 175 | cordinyana | /**
|
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 | 545 | fdiaz | for (int i = 0; i < fieldNames.length; i++) |
722 | 175 | cordinyana | feature.set(fieldNames[i], feature.get(i)); |
723 | 545 | fdiaz | |
724 | 175 | cordinyana | if(acceptType(store, newGeom)) {
|
725 | feature.setGeometry(fieldNames.length, newGeom); |
||
726 | store.update(feature); |
||
727 | return feature;
|
||
728 | } |
||
729 | return null; |
||
730 | } |
||
731 | 545 | fdiaz | |
732 | 175 | cordinyana | /**
|
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 | 988 | jjdelcerro | // 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 | 175 | cordinyana | } |
745 | 545 | fdiaz | |
746 | 175 | cordinyana | /**
|
747 | * Gets the shape type of the selected feature store
|
||
748 | * @param FeatureStore source
|
||
749 | * @return shape type
|
||
750 | * @throws ReadException
|
||
751 | */
|
||
752 | 278 | nbrodin | @SuppressWarnings("deprecation") |
753 | 175 | cordinyana | 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 | 545 | fdiaz | |
764 | 329 | nbrodin | @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 | 545 | fdiaz | |
778 | 329 | nbrodin | 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 | 545 | fdiaz | |
788 | 175 | cordinyana | /**
|
789 | * Gets the output FeatureStore
|
||
790 | * @return
|
||
791 | */
|
||
792 | public FeatureStore getOutputFeatureStore() {
|
||
793 | return store;
|
||
794 | } |
||
795 | } |