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 @ 994
History | View | Annotate | Download (27.3 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 | 994 | fdiaz | GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
388 | 545 | fdiaz | |
389 | 807 | fdiaz | if(geomType.isTypeOf(fType))
|
390 | 175 | cordinyana | return true; |
391 | 994 | fdiaz | |
392 | if( geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE, fType) &&
|
||
393 | 807 | fdiaz | geomType.isTypeOf(org.gvsig.fmap.geom.Geometry.TYPES.CURVE)) |
394 | 175 | cordinyana | return true; |
395 | 994 | fdiaz | if( geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTISURFACE, fType) &&
|
396 | 807 | fdiaz | geomType.isTypeOf(org.gvsig.fmap.geom.Geometry.TYPES.SURFACE)) |
397 | 175 | cordinyana | return true; |
398 | 994 | fdiaz | if( geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT, fType) &&
|
399 | 807 | fdiaz | geomType.isTypeOf(org.gvsig.fmap.geom.Geometry.TYPES.POINT)) |
400 | 175 | cordinyana | return true; |
401 | 994 | fdiaz | if( geomManager.isSubtype(org.gvsig.fmap.geom.Geometry.TYPES.MULTISOLID, fType) &&
|
402 | 807 | fdiaz | geomType.isTypeOf(org.gvsig.fmap.geom.Geometry.TYPES.SOLID)) |
403 | 175 | cordinyana | return true; |
404 | return false; |
||
405 | } |
||
406 | |||
407 | /**
|
||
408 | * Adds a JTS feature to the FeatureStore
|
||
409 | * @param entry
|
||
410 | * @param newGeom
|
||
411 | * @throws CreateGeometryException
|
||
412 | * @throws DataException
|
||
413 | */
|
||
414 | public EditableFeature addFeature(Feature feature, Geometry newGeom) throws CreateGeometryException, DataException { |
||
415 | 308 | nbrodin | int outPutType = getOutputFeatureStore().getDefaultFeatureType()
|
416 | .getDefaultGeometryAttribute().getGeomType().getType(); |
||
417 | //Si el objeto que viene es un GeometryCollection hay que convertirlo a una lista de geometrias
|
||
418 | //solo del tipo que pueda salvarse en la capa eliminando el resto. Esto sucede sobre todo en la intersecci?n
|
||
419 | //que devuelve tipos mezclados. Cada uno debe salvarse en el tipo de capa de salida apropiado
|
||
420 | ArrayList<org.gvsig.fmap.geom.Geometry> dalGeomList = null; |
||
421 | 175 | cordinyana | try {
|
422 | 308 | nbrodin | dalGeomList = convertGeometryCollection2GeometryList(newGeom, outPutType); |
423 | } catch (GeometryOperationNotSupportedException e1) {
|
||
424 | Sextante.addErrorToLog(e1); |
||
425 | } catch (GeometryOperationException e1) {
|
||
426 | Sextante.addErrorToLog(e1); |
||
427 | 175 | cordinyana | } |
428 | 545 | fdiaz | |
429 | 308 | nbrodin | //No era un GeometryCollection
|
430 | if(dalGeomList == null) { |
||
431 | org.gvsig.fmap.geom.Geometry newDalGeom = null;
|
||
432 | GeometryOperationContext ctx = new GeometryOperationContext();
|
||
433 | ctx.setAttribute(FromJTS.PARAM, newGeom); |
||
434 | try {
|
||
435 | newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
||
436 | } catch (GeometryOperationNotSupportedException e) {
|
||
437 | Sextante.addErrorToLog(e); |
||
438 | } catch (GeometryOperationException e) {
|
||
439 | Sextante.addErrorToLog(e); |
||
440 | } |
||
441 | insertFeatureIntoStore(feature, newDalGeom); |
||
442 | } else {
|
||
443 | for (int i = 0; i < dalGeomList.size(); i++) { |
||
444 | insertFeatureIntoStore(feature, dalGeomList.get(i)); |
||
445 | } |
||
446 | } |
||
447 | return null; |
||
448 | } |
||
449 | 545 | fdiaz | |
450 | 308 | nbrodin | private EditableFeature insertFeatureIntoStore(Feature feature, org.gvsig.fmap.geom.Geometry newDalGeom) throws DataException { |
451 | 175 | cordinyana | if(acceptType(store, newDalGeom)) {
|
452 | 265 | nbrodin | EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), true);
|
453 | FeatureAttributeDescriptor[] attrDesc = feature.getType().getAttributeDescriptors();
|
||
454 | for (int i = 0; i < attrDesc.length; i++) { |
||
455 | String name = attrDesc[i].getName();
|
||
456 | if(name.compareTo("GEOMETRY") != 0) { |
||
457 | Object value = feature.get(name);
|
||
458 | if(value != null) { |
||
459 | feat.set(name, value); |
||
460 | } |
||
461 | } |
||
462 | } |
||
463 | 650 | fdiaz | |
464 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = null;
|
465 | try {
|
||
466 | multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
||
467 | } catch (CreateGeometryException e) {
|
||
468 | Sextante.addErrorToLog(e); |
||
469 | } |
||
470 | 650 | fdiaz | |
471 | 896 | fdiaz | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
472 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
473 | } else {
|
||
474 | feat.setGeometry("GEOMETRY", newDalGeom);
|
||
475 | 650 | fdiaz | } |
476 | 175 | cordinyana | store.insert(feat); |
477 | return feat;
|
||
478 | } |
||
479 | return null; |
||
480 | } |
||
481 | 545 | fdiaz | |
482 | 175 | cordinyana | /**
|
483 | * Adds a JTS feature to the FeatureStore
|
||
484 | * @param entry
|
||
485 | * @param newGeom
|
||
486 | * @throws CreateGeometryException
|
||
487 | * @throws DataException
|
||
488 | */
|
||
489 | public EditableFeature addFeature(Geometry newGeom, int id, double value) throws CreateGeometryException, DataException { |
||
490 | org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
||
491 | |||
492 | if(newDalGeom != null && acceptType(store, newDalGeom)) { |
||
493 | EditableFeature feat = store.createNewFeature(); |
||
494 | feat.set(0, id);
|
||
495 | feat.set(1, value);
|
||
496 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
497 | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
498 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
499 | } else {
|
||
500 | feat.setGeometry("GEOMETRY", newDalGeom);
|
||
501 | } |
||
502 | 175 | cordinyana | store.insert(feat); |
503 | return feat;
|
||
504 | } |
||
505 | return null; |
||
506 | } |
||
507 | 545 | fdiaz | |
508 | 175 | cordinyana | /**
|
509 | * Adds a JTS feature to the FeatureStore
|
||
510 | * @param entry
|
||
511 | * @param newGeom
|
||
512 | * @throws CreateGeometryException
|
||
513 | * @throws DataException
|
||
514 | */
|
||
515 | 439 | nbrodin | public EditableFeature addFeature(Feature feature, List<Geometry> newJTSListGeom) throws CreateGeometryException, DataException { |
516 | List<org.gvsig.fmap.geom.Geometry> newDalListGeom = new ArrayList<org.gvsig.fmap.geom.Geometry>(); |
||
517 | 545 | fdiaz | |
518 | 439 | nbrodin | for (int i = 0; i < newJTSListGeom.size(); i++) { |
519 | newDalListGeom.add(GeometryUtil.jtsToGeom(newJTSListGeom.get(i))); |
||
520 | } |
||
521 | 545 | fdiaz | |
522 | 439 | nbrodin | org.gvsig.fmap.geom.Geometry newMultiGeom = convertGeometryList2MultiGeometry(newDalListGeom); |
523 | insertFeatureIntoStore(feature, newMultiGeom); |
||
524 | return null; |
||
525 | } |
||
526 | 545 | fdiaz | |
527 | 439 | nbrodin | /**
|
528 | * Adds a JTS feature to the FeatureStore
|
||
529 | * @param entry
|
||
530 | * @param newGeom
|
||
531 | * @throws CreateGeometryException
|
||
532 | * @throws DataException
|
||
533 | */
|
||
534 | 272 | nbrodin | public EditableFeature addFeature(Geometry newGeom, String name, Object value) throws CreateGeometryException, DataException { |
535 | org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
||
536 | |||
537 | if(newDalGeom != null && acceptType(store, newDalGeom)) { |
||
538 | EditableFeature feat = store.createNewFeature(); |
||
539 | feat.set(name, value); |
||
540 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
541 | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
542 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
543 | } else {
|
||
544 | feat.setGeometry("GEOMETRY", newDalGeom);
|
||
545 | } |
||
546 | 272 | nbrodin | store.insert(feat); |
547 | return feat;
|
||
548 | } |
||
549 | return null; |
||
550 | } |
||
551 | 545 | fdiaz | |
552 | 272 | nbrodin | /**
|
553 | 350 | nbrodin | * Adds a geometry feature to the FeatureStore
|
554 | * @param entry
|
||
555 | * @param newGeom
|
||
556 | * @throws CreateGeometryException
|
||
557 | * @throws DataException
|
||
558 | */
|
||
559 | public EditableFeature addFeature(org.gvsig.fmap.geom.Geometry newGeom, String name, Object value) throws CreateGeometryException, DataException { |
||
560 | if(newGeom != null && acceptType(store, newGeom)) { |
||
561 | EditableFeature feat = store.createNewFeature(); |
||
562 | feat.set(name, value); |
||
563 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
564 | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
565 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
566 | } else {
|
||
567 | feat.setGeometry("GEOMETRY", newGeom);
|
||
568 | } |
||
569 | 350 | nbrodin | store.insert(feat); |
570 | return feat;
|
||
571 | } |
||
572 | return null; |
||
573 | } |
||
574 | 545 | fdiaz | |
575 | 350 | nbrodin | /**
|
576 | * Adds a geometry feature to the FeatureStore
|
||
577 | * @param entry
|
||
578 | * @param newGeom
|
||
579 | * @throws CreateGeometryException
|
||
580 | * @throws DataException
|
||
581 | */
|
||
582 | public EditableFeature addFeature(org.gvsig.fmap.geom.Geometry newGeom, ArrayList<String> names, ArrayList<Object> values) throws CreateGeometryException, DataException { |
||
583 | if(newGeom != null && acceptType(store, newGeom)) { |
||
584 | EditableFeature feat = store.createNewFeature(); |
||
585 | for (int i = 0; i < values.size(); i++) { |
||
586 | 545 | fdiaz | feat.set(names.get(i), values.get(i)); |
587 | 350 | nbrodin | } |
588 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
589 | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
590 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
591 | } else {
|
||
592 | feat.setGeometry("GEOMETRY", newGeom);
|
||
593 | } |
||
594 | 350 | nbrodin | store.insert(feat); |
595 | return feat;
|
||
596 | } |
||
597 | return null; |
||
598 | } |
||
599 | 545 | fdiaz | |
600 | 350 | nbrodin | /**
|
601 | 272 | nbrodin | * Adds a JTS feature to the FeatureStore
|
602 | * @param entry
|
||
603 | * @param newGeom
|
||
604 | * @throws CreateGeometryException
|
||
605 | * @throws DataException
|
||
606 | */
|
||
607 | 278 | nbrodin | public EditableFeature addFeature(Geometry newGeom, ArrayList<String> names, ArrayList<Object> values) throws CreateGeometryException, DataException { |
608 | org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
||
609 | |||
610 | if(newDalGeom != null && acceptType(store, newDalGeom)) { |
||
611 | EditableFeature feat = store.createNewFeature(); |
||
612 | for (int i = 0; i < values.size(); i++) { |
||
613 | 545 | fdiaz | feat.set(names.get(i), values.get(i)); |
614 | 278 | nbrodin | } |
615 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
616 | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
617 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
618 | } else {
|
||
619 | feat.setGeometry("GEOMETRY", newDalGeom);
|
||
620 | } |
||
621 | 278 | nbrodin | store.insert(feat); |
622 | return feat;
|
||
623 | } |
||
624 | return null; |
||
625 | } |
||
626 | 545 | fdiaz | |
627 | 278 | nbrodin | /**
|
628 | * Adds a JTS feature to the FeatureStore
|
||
629 | * @param entry
|
||
630 | * @param newGeom
|
||
631 | * @throws CreateGeometryException
|
||
632 | * @throws DataException
|
||
633 | */
|
||
634 | 175 | cordinyana | public EditableFeature addFeature(Geometry newGeom, int id, double value1, double value2) throws CreateGeometryException, DataException { |
635 | org.gvsig.fmap.geom.Geometry newDalGeom = GeometryUtil.jtsToGeom(newGeom); |
||
636 | |||
637 | if(newDalGeom != null && acceptType(store, newDalGeom)) { |
||
638 | EditableFeature feat = store.createNewFeature(); |
||
639 | feat.set(0, id);
|
||
640 | feat.set(1, value1);
|
||
641 | feat.set(2, value2);
|
||
642 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newDalGeom); |
643 | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
644 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
645 | } else {
|
||
646 | feat.setGeometry("GEOMETRY", newDalGeom);
|
||
647 | } |
||
648 | 175 | cordinyana | store.insert(feat); |
649 | return feat;
|
||
650 | } |
||
651 | return null; |
||
652 | } |
||
653 | 545 | fdiaz | |
654 | 175 | cordinyana | /**
|
655 | 545 | fdiaz | * Adds a DAL feature to the FeatureStore when the field structure in the new feature
|
656 | 175 | cordinyana | * is different to the source field structure.
|
657 | * @param entry
|
||
658 | * @param newGeom
|
||
659 | * @throws CreateGeometryException
|
||
660 | * @throws DataException
|
||
661 | */
|
||
662 | public EditableFeature addDifferentFieldFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
||
663 | if(acceptType(store, newGeom)) {
|
||
664 | //Builds a new empty feature
|
||
665 | EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), true);
|
||
666 | 545 | fdiaz | |
667 | 175 | cordinyana | //Writes only values with the same name and data type in both features
|
668 | for (int i = 0; i < fieldNames.length; i++) { |
||
669 | if(feature.getType().getIndex(fieldNames[i]) != -1) { |
||
670 | Object obj1 = feature.get(fieldNames[i]);
|
||
671 | if(feat.getType().getIndex(fieldNames[i]) != -1) { |
||
672 | Object obj2 = feat.get(fieldNames[i]);
|
||
673 | if(obj1 != null && obj2 != null && obj1.getClass() == obj2.getClass()) |
||
674 | feat.set(i, obj1); |
||
675 | } |
||
676 | } |
||
677 | } |
||
678 | 545 | fdiaz | |
679 | 175 | cordinyana | //Sets the geometry
|
680 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
681 | if(multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
682 | feat.setGeometry("GEOMETRY", multiGeometry);
|
||
683 | } else {
|
||
684 | feat.setGeometry("GEOMETRY", newGeom);
|
||
685 | } |
||
686 | 175 | cordinyana | store.insert(feat); |
687 | return feat;
|
||
688 | } |
||
689 | return null; |
||
690 | } |
||
691 | 545 | fdiaz | |
692 | 175 | cordinyana | /**
|
693 | * Adds a DAL feature to the FeatureStore
|
||
694 | * @param entry
|
||
695 | * @param newGeom
|
||
696 | * @throws CreateGeometryException
|
||
697 | * @throws DataException
|
||
698 | */
|
||
699 | public EditableFeature addFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
||
700 | if(acceptType(store, newGeom)) {
|
||
701 | EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), feature); |
||
702 | 896 | fdiaz | org.gvsig.fmap.geom.Geometry multiGeometry = convertGeometry2MultiGeometry(newGeom); |
703 | if (multiGeometry!=null && acceptType(store, multiGeometry)){ |
||
704 | feat.setGeometry(fieldNames.length, multiGeometry); |
||
705 | 545 | fdiaz | } else {
|
706 | feat.setGeometry(fieldNames.length, newGeom); |
||
707 | } |
||
708 | 175 | cordinyana | store.insert(feat); |
709 | return feat;
|
||
710 | } |
||
711 | return null; |
||
712 | } |
||
713 | 545 | fdiaz | |
714 | 175 | cordinyana | /**
|
715 | * Adds a DAL feature to the FeatureStore
|
||
716 | * @param entry
|
||
717 | * @param newGeom
|
||
718 | * @throws CreateGeometryException
|
||
719 | * @throws DataException
|
||
720 | */
|
||
721 | public EditableFeature addGeometryToExistingFeature(EditableFeature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
||
722 | //Y esto para que c... sirve?
|
||
723 | 545 | fdiaz | for (int i = 0; i < fieldNames.length; i++) |
724 | 175 | cordinyana | feature.set(fieldNames[i], feature.get(i)); |
725 | 545 | fdiaz | |
726 | 175 | cordinyana | if(acceptType(store, newGeom)) {
|
727 | feature.setGeometry(fieldNames.length, newGeom); |
||
728 | store.update(feature); |
||
729 | return feature;
|
||
730 | } |
||
731 | return null; |
||
732 | } |
||
733 | 545 | fdiaz | |
734 | 175 | cordinyana | /**
|
735 | * Ends the edition and closes the FeatureStore
|
||
736 | */
|
||
737 | public void end() { |
||
738 | try {
|
||
739 | store.finishEditing(); |
||
740 | } catch (DataException e) {
|
||
741 | Sextante.addErrorToLog(e); |
||
742 | } |
||
743 | 988 | jjdelcerro | // Si destruimos aqui el store ya no estara disponible en el postprocess,
|
744 | // donde se usa, y se vuelve a destruir.
|
||
745 | // store.dispose();
|
||
746 | 175 | cordinyana | } |
747 | 545 | fdiaz | |
748 | 175 | cordinyana | /**
|
749 | * Gets the shape type of the selected feature store
|
||
750 | * @param FeatureStore source
|
||
751 | * @return shape type
|
||
752 | * @throws ReadException
|
||
753 | */
|
||
754 | 278 | nbrodin | @SuppressWarnings("deprecation") |
755 | 175 | cordinyana | public int getShapeType(FeatureStore storeLayer1) throws ReadException { |
756 | FeatureType featureType; |
||
757 | try {
|
||
758 | featureType = storeLayer1.getDefaultFeatureType(); |
||
759 | } catch (DataException e) {
|
||
760 | throw new ReadException(storeLayer1.getName(), e); |
||
761 | } |
||
762 | int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
||
763 | return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
||
764 | } |
||
765 | 545 | fdiaz | |
766 | 329 | nbrodin | @SuppressWarnings("deprecation") |
767 | public int getType() { |
||
768 | if(store == null) |
||
769 | return org.gvsig.fmap.geom.Geometry.TYPES.NULL;
|
||
770 | FeatureType featureType; |
||
771 | try {
|
||
772 | featureType = store.getDefaultFeatureType(); |
||
773 | } catch (DataException e) {
|
||
774 | return org.gvsig.fmap.geom.Geometry.TYPES.NULL;
|
||
775 | } |
||
776 | int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
||
777 | return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
||
778 | } |
||
779 | 545 | fdiaz | |
780 | 329 | nbrodin | public boolean isCompatibleType(org.gvsig.fmap.geom.Geometry geom) { |
781 | if(store == null) |
||
782 | return false; |
||
783 | try {
|
||
784 | return acceptType(store, geom);
|
||
785 | } catch (ReadException e) {
|
||
786 | return false; |
||
787 | } |
||
788 | } |
||
789 | 545 | fdiaz | |
790 | 175 | cordinyana | /**
|
791 | * Gets the output FeatureStore
|
||
792 | * @return
|
||
793 | */
|
||
794 | public FeatureStore getOutputFeatureStore() {
|
||
795 | return store;
|
||
796 | } |
||
797 | } |