Revision 267 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
DALFeaturePersister.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.geoprocess.algorithm.base.core; |
25 | 25 |
|
26 |
import java.util.ArrayList; |
|
27 |
|
|
26 | 28 |
import org.gvsig.fmap.dal.exception.DataException; |
27 | 29 |
import org.gvsig.fmap.dal.exception.ReadException; |
28 | 30 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
... | ... | |
46 | 48 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
47 | 49 |
|
48 | 50 |
import com.vividsolutions.jts.geom.Geometry; |
51 |
import com.vividsolutions.jts.geom.GeometryCollection; |
|
49 | 52 |
|
50 | 53 |
import es.unex.sextante.core.Sextante; |
51 | 54 |
|
... | ... | |
85 | 88 |
public EditableFeature addFeature(Feature feature1, Feature feature2, Geometry newGeom) |
86 | 89 |
throws CreateGeometryException, DataException { |
87 | 90 |
|
88 |
org.gvsig.fmap.geom.Geometry newDalGeom = null; |
|
89 |
|
|
90 |
GeometryOperationContext ctx = new GeometryOperationContext(); |
|
91 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
|
91 |
int outPutType = getOutputFeatureStore().getDefaultFeatureType() |
|
92 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
|
93 |
|
|
94 |
//Si el objeto que viene es un GeometryCollection hay que convertirlo a una lista de geometrias |
|
95 |
//solo del tipo que pueda salvarse en la capa eliminando el resto. Esto sucede sobre todo en la intersecci?n |
|
96 |
//que devuelve tipos mezclados. Cada uno debe salvarse en el tipo de capa de salida apropiado |
|
97 |
ArrayList<org.gvsig.fmap.geom.Geometry> dalGeomList = null; |
|
92 | 98 |
try { |
93 |
newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
|
94 |
} catch (GeometryOperationNotSupportedException e) { |
|
95 |
Sextante.addErrorToLog(e); |
|
96 |
return null; |
|
97 |
} catch (GeometryOperationException e) { |
|
98 |
Sextante.addErrorToLog(e); |
|
99 |
return null; |
|
99 |
dalGeomList = convertGeometryCollection2GeometryList(newGeom, outPutType); |
|
100 |
} catch (GeometryOperationNotSupportedException e1) { |
|
101 |
Sextante.addErrorToLog(e1); |
|
102 |
} catch (GeometryOperationException e1) { |
|
103 |
Sextante.addErrorToLog(e1); |
|
100 | 104 |
} |
105 |
org.gvsig.fmap.geom.Geometry newDalGeom = null; |
|
101 | 106 |
|
107 |
//Si lo que venia no era un GeometryCollection se convierte a geometr?a de DAL |
|
108 |
if(dalGeomList == null) { |
|
109 |
GeometryOperationContext ctx = new GeometryOperationContext(); |
|
110 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
|
111 |
try { |
|
112 |
newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
|
113 |
} catch (GeometryOperationNotSupportedException e) { |
|
114 |
Sextante.addErrorToLog(e); |
|
115 |
return null; |
|
116 |
} catch (GeometryOperationException e) { |
|
117 |
Sextante.addErrorToLog(e); |
|
118 |
return null; |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
102 | 122 |
EditableFeature feat = store.createNewFeature(); |
103 | 123 |
|
104 | 124 |
int globalIndex = 0; |
... | ... | |
118 | 138 |
globalIndex ++; |
119 | 139 |
} |
120 | 140 |
|
121 |
if(acceptType(store, newDalGeom)) { |
|
141 |
if(newDalGeom != null && acceptType(store, newDalGeom)) {
|
|
122 | 142 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom)); |
123 | 143 |
store.insert(feat); |
124 | 144 |
return feat; |
145 |
} else if(dalGeomList != null) { |
|
146 |
for (int i = 0; i < dalGeomList.size(); i++) { |
|
147 |
if(acceptType(store, dalGeomList.get(i))) { |
|
148 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(dalGeomList.get(i))); |
|
149 |
store.insert(feat); |
|
150 |
} |
|
151 |
} |
|
152 |
return feat; |
|
125 | 153 |
} |
126 | 154 |
return null; |
127 | 155 |
} |
128 | 156 |
|
129 | 157 |
/** |
158 |
* Converts a geometry collection from JTS to a list of geometries of the selected type. |
|
159 |
* @param col |
|
160 |
* @param type |
|
161 |
* @return |
|
162 |
* @throws CreateGeometryException |
|
163 |
* @throws GeometryOperationException |
|
164 |
* @throws GeometryOperationNotSupportedException |
|
165 |
*/ |
|
166 |
public ArrayList<org.gvsig.fmap.geom.Geometry> convertGeometryCollection2GeometryList(Geometry g, int type) throws GeometryOperationNotSupportedException, GeometryOperationException { |
|
167 |
if(g instanceof GeometryCollection) { |
|
168 |
GeometryCollection col = (GeometryCollection)g; |
|
169 |
ArrayList<org.gvsig.fmap.geom.Geometry> geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>(); |
|
170 |
|
|
171 |
if(type == TYPES.MULTIPOINT || type == TYPES.POINT) { |
|
172 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
|
173 |
Geometry gx = col.getGeometryN(i); |
|
174 |
if( gx instanceof com.vividsolutions.jts.geom.Point || |
|
175 |
gx instanceof com.vividsolutions.jts.geom.MultiPoint) { |
|
176 |
GeometryOperationContext ctx = new GeometryOperationContext(); |
|
177 |
ctx.setAttribute(FromJTS.PARAM, gx); |
|
178 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
|
179 |
geometries.add(newDalGeom); |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
if(type == TYPES.MULTICURVE || type == TYPES.CURVE) { |
|
185 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
|
186 |
Geometry gx = col.getGeometryN(i); |
|
187 |
if( gx instanceof com.vividsolutions.jts.geom.LineString || |
|
188 |
gx instanceof com.vividsolutions.jts.geom.MultiLineString) { |
|
189 |
GeometryOperationContext ctx = new GeometryOperationContext(); |
|
190 |
ctx.setAttribute(FromJTS.PARAM, gx); |
|
191 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
|
192 |
geometries.add(newDalGeom); |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
196 |
if(type == TYPES.MULTISURFACE || type == TYPES.SURFACE) { |
|
197 |
for (int i = 0; i < col.getNumGeometries(); i++) { |
|
198 |
Geometry gx = col.getGeometryN(i); |
|
199 |
if( gx instanceof com.vividsolutions.jts.geom.Polygon || |
|
200 |
gx instanceof com.vividsolutions.jts.geom.MultiPolygon) { |
|
201 |
GeometryOperationContext ctx = new GeometryOperationContext(); |
|
202 |
ctx.setAttribute(FromJTS.PARAM, gx); |
|
203 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geometryManager.invokeOperation(FromJTS.NAME, ctx); |
|
204 |
geometries.add(newDalGeom); |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
return geometries; |
|
209 |
} |
|
210 |
return null; |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
130 | 214 |
* Converts a geometry into a multigeometry |
131 | 215 |
* @param g |
132 | 216 |
* @return |
Also available in: Unified diff