Revision 266
org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.intersection/src/main/java/org/gvsig/geoprocess/algorithm/intersection/IntersectionOperation.java | ||
---|---|---|
26 | 26 |
import java.util.Iterator; |
27 | 27 |
import java.util.List; |
28 | 28 |
|
29 |
import com.vividsolutions.jts.geom.Geometry; |
|
30 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
|
31 |
|
|
32 |
import es.unex.sextante.core.Sextante; |
|
33 |
|
|
34 | 29 |
import org.gvsig.fmap.dal.exception.DataException; |
35 | 30 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
36 | 31 |
import org.gvsig.fmap.dal.feature.Feature; |
37 | 32 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
38 | 33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
35 |
import org.gvsig.fmap.geom.GeometryManager; |
|
36 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
|
37 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
|
39 | 38 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
39 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
|
40 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
|
41 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
|
42 |
import org.gvsig.fmap.geom.operation.fromjts.FromJTS; |
|
43 |
import org.gvsig.fmap.geom.primitive.Curve; |
|
44 |
import org.gvsig.fmap.geom.primitive.Point; |
|
45 |
import org.gvsig.fmap.geom.primitive.Surface; |
|
40 | 46 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
41 | 47 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
42 | 48 |
import org.gvsig.tools.dispose.DisposableIterator; |
43 | 49 |
|
50 |
import com.vividsolutions.jts.geom.Geometry; |
|
51 |
import com.vividsolutions.jts.geom.Polygon; |
|
52 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
|
53 |
|
|
54 |
import es.unex.sextante.core.Sextante; |
|
55 |
|
|
44 | 56 |
/** |
45 | 57 |
* Builds a geometry with the intersection between two layers |
46 | 58 |
* |
... | ... | |
48 | 60 |
*/ |
49 | 61 |
public class IntersectionOperation extends GeometryOperation { |
50 | 62 |
private FeatureStore storeOverlay = null; |
63 |
protected GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
51 | 64 |
|
52 | 65 |
public IntersectionOperation(FeatureStore overlayLayer) { |
53 | 66 |
this.storeOverlay = overlayLayer; |
... | ... | |
65 | 78 |
if(g == null) |
66 | 79 |
return lastEditFeature; |
67 | 80 |
|
68 |
Geometry jtsGeom = GeometryUtil.geomToJTS(g); |
|
69 |
|
|
70 | 81 |
FeatureSet features = null; |
71 | 82 |
DisposableIterator it = null; |
72 | 83 |
try { |
... | ... | |
82 | 93 |
List geomList = featureOverlay.getGeometries(); |
83 | 94 |
if(geomList == null) { |
84 | 95 |
org.gvsig.fmap.geom.Geometry geom = featureOverlay.getDefaultGeometry(); |
85 |
lastEditFeature = intersection(jtsGeom, geom, featureInput, featureOverlay);
|
|
96 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay);
|
|
86 | 97 |
continue; |
87 | 98 |
} |
88 | 99 |
|
89 | 100 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator(); |
90 | 101 |
while(itGeom.hasNext()) { |
91 | 102 |
org.gvsig.fmap.geom.Geometry geom = itGeom.next(); |
92 |
lastEditFeature = intersection(jtsGeom, geom, featureInput, featureOverlay);
|
|
103 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay);
|
|
93 | 104 |
} |
94 | 105 |
} |
95 | 106 |
it.dispose(); |
96 | 107 |
return lastEditFeature; |
97 | 108 |
} |
98 | 109 |
|
99 |
private EditableFeature intersection(Geometry jtsGeom,
|
|
100 |
org.gvsig.fmap.geom.Geometry overGeom,
|
|
110 |
private EditableFeature intersection( org.gvsig.fmap.geom.Geometry g1,
|
|
111 |
org.gvsig.fmap.geom.Geometry g2,
|
|
101 | 112 |
Feature featureInput, |
102 | 113 |
Feature featureOverlay) { |
103 |
Geometry overlaysGeom = GeometryUtil.geomToJTS(overGeom); |
|
114 |
Geometry overlaysGeom = null; |
|
115 |
Geometry jtsGeom = null; |
|
116 |
int outPutType = TYPES.SURFACE; |
|
104 | 117 |
|
105 | 118 |
try { |
119 |
outPutType = persister.getOutputFeatureStore().getDefaultFeatureType() |
|
120 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
|
121 |
} catch (DataException e2) { |
|
122 |
Sextante.addErrorToLog(e2); |
|
123 |
} |
|
124 |
|
|
125 |
try { |
|
126 |
if( ((g1 instanceof Surface && g2 instanceof Curve) || |
|
127 |
(g2 instanceof Surface && g1 instanceof Curve)) && |
|
128 |
outPutType == TYPES.MULTIPOINT) { |
|
129 |
org.gvsig.fmap.geom.Geometry overGeom = (g1 instanceof Surface) ? g1 : g2; |
|
130 |
jtsGeom = (g1 instanceof Surface) ? GeometryUtil.geomToJTS(g2) : GeometryUtil.geomToJTS(g1); |
|
131 |
overlaysGeom = (Geometry)overGeom.invokeOperation("toJTSLineString", null); |
|
132 |
} else { |
|
133 |
if( g1 instanceof Surface && |
|
134 |
g2 instanceof Surface && |
|
135 |
(outPutType == TYPES.MULTIPOINT)) { |
|
136 |
jtsGeom = (Geometry)g1.invokeOperation("toJTSLineString", null); |
|
137 |
overlaysGeom = (Geometry)g2.invokeOperation("toJTSLineString", null); |
|
138 |
} else { |
|
139 |
jtsGeom = GeometryUtil.geomToJTS(g1); |
|
140 |
overlaysGeom = GeometryUtil.geomToJTS(g2); |
|
141 |
} |
|
142 |
} |
|
143 |
|
|
106 | 144 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope())) |
107 | 145 |
return lastEditFeature; |
108 | 146 |
|
109 | 147 |
if(jtsGeom.intersects(overlaysGeom)) { |
110 | 148 |
Geometry newGeom = EnhancedPrecisionOp.intersection(jtsGeom, overlaysGeom); |
111 |
if(!newGeom.isEmpty()) |
|
149 |
if(!newGeom.isEmpty()) { |
|
150 |
|
|
151 |
//Para intersecciones entre pol?gonos cuando la salida es de tipo l?nea |
|
152 |
//la generamos a partir del pol?gono resultante de la intersecci?n |
|
153 |
if( g1 instanceof Surface && |
|
154 |
g2 instanceof Surface && |
|
155 |
(outPutType == TYPES.MULTICURVE)) { |
|
156 |
GeometryOperationContext ctx = new GeometryOperationContext(); |
|
157 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
|
158 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geomManager.invokeOperation(FromJTS.NAME, ctx); |
|
159 |
newGeom = (Geometry)newDalGeom.invokeOperation("toJTSLineString", null); |
|
160 |
} |
|
161 |
|
|
112 | 162 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom); |
163 |
} |
|
113 | 164 |
} |
114 | 165 |
} catch (CreateGeometryException e) { |
115 | 166 |
Sextante.addErrorToLog(e); |
116 | 167 |
} catch (DataException e) { |
117 | 168 |
Sextante.addErrorToLog(e); |
169 |
} catch (GeometryOperationNotSupportedException e1) { |
|
170 |
Sextante.addErrorToLog(e1); |
|
171 |
} catch (GeometryOperationException e1) { |
|
172 |
Sextante.addErrorToLog(e1); |
|
118 | 173 |
} |
119 | 174 |
return lastEditFeature; |
120 | 175 |
} |
121 | 176 |
|
177 |
// private MultiCurve convertPolygonJTS2LineJTS(Polygon p) { |
|
178 |
// |
|
179 |
// } |
|
180 |
|
|
122 | 181 |
/** |
123 | 182 |
* clips feature's geometry with the clipping geometry, preserving |
124 | 183 |
* feature's original attributes. |
org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.intersection/src/main/java/org/gvsig/geoprocess/algorithm/intersection/IntersectionAlgorithm.java | ||
---|---|---|
38 | 38 |
|
39 | 39 |
/** |
40 | 40 |
* Intersection algorithm |
41 |
* <UL> |
|
42 |
* <LI>Pol-Pol: 3 layers (polygon, point, line)</LI> |
|
43 |
* <LI>Pol-Line: 2 layers (point, line)</LI> |
|
44 |
* <LI>Pol-Point: 1 layer (point)</LI> |
|
45 |
* <LI>Line-Point: 1 layer (point)</LI> |
|
46 |
* <LI>Line-Line: 2 layers (point, line)</LI> |
|
47 |
* <LI>Point-Point: 1 layer (point)</LI> |
|
48 |
* </UL> |
|
41 | 49 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a> |
42 | 50 |
*/ |
43 | 51 |
public class IntersectionAlgorithm extends AbstractSextanteGeoProcess { |
44 | 52 |
|
45 |
public static final String RESULT = "RESULT"; |
|
46 |
public static final String LAYER = "LAYER"; |
|
47 |
public static final String INTER = "INTER"; |
|
48 |
public static final String CHECK = "CHECK"; |
|
53 |
public static final String RESULT_POL = "RESULT_POL"; |
|
54 |
public static final String RESULT_POINT = "RESULT_POINT"; |
|
55 |
public static final String RESULT_LINE = "RESULT_LINE"; |
|
56 |
public static final String LAYER = "LAYER"; |
|
57 |
public static final String INTER = "INTER"; |
|
58 |
public static final String CHECK = "CHECK"; |
|
49 | 59 |
|
50 | 60 |
/* |
51 | 61 |
* (non-Javadoc) |
... | ... | |
70 | 80 |
} catch (RepeatedParameterNameException e) { |
71 | 81 |
Sextante.addErrorToLog(e); |
72 | 82 |
} |
73 |
addOutputVectorLayer(RESULT, |
|
74 |
getTranslation("Intersection"), |
|
75 |
OutputVectorLayer.SHAPE_TYPE_UNDEFINED); |
|
83 |
addOutputVectorLayer(RESULT_POL, getTranslation("Intersection_polygon"), |
|
84 |
OutputVectorLayer.SHAPE_TYPE_POLYGON); |
|
85 |
addOutputVectorLayer(RESULT_LINE, getTranslation("Intersection_line"), |
|
86 |
OutputVectorLayer.SHAPE_TYPE_LINE); |
|
87 |
addOutputVectorLayer(RESULT_POINT, getTranslation("Intersection_point"), |
|
88 |
OutputVectorLayer.SHAPE_TYPE_POINT); |
|
76 | 89 |
} |
77 | 90 |
|
78 | 91 |
/* |
... | ... | |
102 | 115 |
* Output shape type |
103 | 116 |
* @param selectedGeom |
104 | 117 |
* If it's true only selected geometries will be computed |
118 |
* @throws GeoAlgorithmExecutionException |
|
105 | 119 |
*/ |
106 | 120 |
private void computesIntersection(IVectorLayer layer, |
107 | 121 |
IVectorLayer overlay, |
108 | 122 |
int shapeType, |
109 |
boolean selectedGeom) throws DataException { |
|
123 |
boolean selectedGeom) throws DataException, GeoAlgorithmExecutionException {
|
|
110 | 124 |
FeatureStore storeLayer = null; |
111 | 125 |
FeatureStore storeOverlay = null; |
112 | 126 |
if(layer instanceof FlyrVectIVectorLayer && |
... | ... | |
123 | 137 |
FeatureSet features2 = null; |
124 | 138 |
features2 = storeOverlay.getFeatureSet(); |
125 | 139 |
FeatureType featureType2 = features2.getDefaultFeatureType(); |
140 |
|
|
141 |
FeatureStore outFeatStorePol = null; |
|
142 |
FeatureStore outFeatStoreLine = null; |
|
143 |
FeatureStore outFeatStorePoint = null; |
|
126 | 144 |
|
127 |
FeatureStore outFeatStore = null; |
|
128 |
//Si las dos capas son de lineas la resultante es de l?neas |
|
129 |
if (isLine(storeLayer) && isLine(storeOverlay)) |
|
130 |
outFeatStore = |
|
131 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
|
132 |
IVectorLayer.SHAPE_TYPE_POINT, |
|
133 |
getTranslation("Intersection"), RESULT); |
|
134 |
else |
|
135 |
// Si alguna de las dos capas es de puntos la resultante tambi?n lo es |
|
136 |
if (isPoint(storeLayer) || isPoint(storeOverlay)) |
|
137 |
outFeatStore = |
|
138 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
|
139 |
IVectorLayer.SHAPE_TYPE_POINT, |
|
140 |
getTranslation("Intersection"), RESULT); |
|
141 |
else |
|
142 |
outFeatStore = |
|
143 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
|
144 |
shapeType, getTranslation("Intersection"), RESULT); |
|
145 |
IntersectionOperation operation = new IntersectionOperation(storeOverlay); |
|
145 | 146 |
|
146 |
IntersectionOperation operation = new IntersectionOperation(storeOverlay); |
|
147 |
operation.computesGeometryOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true); |
|
147 |
//La de puntos se genera siempre |
|
148 |
outFeatStorePoint = |
|
149 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
|
150 |
IVectorLayer.SHAPE_TYPE_POINT, |
|
151 |
getTranslation("Intersection_point"), RESULT_POINT); |
|
152 |
|
|
153 |
operation.computesGeometryOperation(storeLayer, outFeatStorePoint, attrNames, selectedGeom, true); |
|
154 |
|
|
155 |
//La de pol?gonos solo si es intersecci?n entre pol?gonos |
|
156 |
if (isPolygon(storeLayer) && isPolygon(storeOverlay)) { |
|
157 |
outFeatStorePol = |
|
158 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
|
159 |
IVectorLayer.SHAPE_TYPE_POLYGON, getTranslation("Intersection_polygon"), RESULT_POL); |
|
160 |
operation.computesGeometryOperation(storeLayer, outFeatStorePol, attrNames, selectedGeom, true); |
|
161 |
} |
|
162 |
|
|
163 |
//La capa de l?neas se genera cuando ning?na de las dos es de puntos |
|
164 |
if (!isPoint(storeLayer) && !isPoint(storeOverlay)) { |
|
165 |
outFeatStoreLine = |
|
166 |
buildOutPutStoreFromUnion(featureType1, featureType2, |
|
167 |
IVectorLayer.SHAPE_TYPE_LINE, getTranslation("Intersection_line"), RESULT_LINE); |
|
168 |
operation.computesGeometryOperation(storeLayer, outFeatStoreLine, attrNames, selectedGeom, true); |
|
169 |
} |
|
170 |
|
|
171 |
if(outFeatStorePol == null) { |
|
172 |
getNewVectorLayer(RESULT_POL, getTranslation("Null_polygon"), |
|
173 |
OutputVectorLayer.SHAPE_TYPE_POLYGON, new Class[]{Integer.class}, new String[]{""}); |
|
174 |
} |
|
175 |
|
|
176 |
if(outFeatStoreLine == null) { |
|
177 |
getNewVectorLayer(RESULT_LINE, getTranslation("Null_line"), |
|
178 |
OutputVectorLayer.SHAPE_TYPE_LINE, new Class[]{Integer.class}, new String[]{""}); |
|
179 |
} |
|
180 |
|
|
181 |
if(outFeatStorePoint == null) { |
|
182 |
getNewVectorLayer(RESULT_POINT, getTranslation("Null_point"), |
|
183 |
OutputVectorLayer.SHAPE_TYPE_POINT, new Class[]{Integer.class}, new String[]{""}); |
|
184 |
} |
|
148 | 185 |
} |
149 | 186 |
|
150 | 187 |
} |
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 | ||
---|---|---|
32 | 32 |
import org.gvsig.fmap.dal.feature.FeatureType; |
33 | 33 |
import org.gvsig.fmap.geom.GeometryLocator; |
34 | 34 |
import org.gvsig.fmap.geom.GeometryManager; |
35 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
36 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
|
37 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
|
38 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
|
35 | 39 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
36 | 40 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
37 | 41 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
38 | 42 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
39 | 43 |
import org.gvsig.fmap.geom.operation.fromjts.FromJTS; |
44 |
import org.gvsig.fmap.geom.primitive.Curve; |
|
45 |
import org.gvsig.fmap.geom.primitive.Point; |
|
40 | 46 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
41 | 47 |
|
42 | 48 |
import com.vividsolutions.jts.geom.Geometry; |
... | ... | |
113 | 119 |
} |
114 | 120 |
|
115 | 121 |
if(acceptType(store, newDalGeom)) { |
116 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
|
122 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
|
117 | 123 |
store.insert(feat); |
118 | 124 |
return feat; |
119 | 125 |
} |
... | ... | |
121 | 127 |
} |
122 | 128 |
|
123 | 129 |
/** |
130 |
* Converts a geometry into a multigeometry |
|
131 |
* @param g |
|
132 |
* @return |
|
133 |
* @throws CreateGeometryException |
|
134 |
*/ |
|
135 |
private org.gvsig.fmap.geom.Geometry convertGeometry2MultiGeometry(org.gvsig.fmap.geom.Geometry g) throws CreateGeometryException { |
|
136 |
if(g instanceof Point) { |
|
137 |
MultiPoint pNew = (MultiPoint)geometryManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D); |
|
138 |
pNew.addPoint((Point)g); |
|
139 |
return pNew; |
|
140 |
} |
|
141 |
if(g instanceof Curve) { |
|
142 |
MultiCurve cNew = (MultiCurve)geometryManager.create(TYPES.MULTICURVE, SUBTYPES.GEOM2D); |
|
143 |
cNew.addCurve((Curve)g); |
|
144 |
return cNew; |
|
145 |
} |
|
146 |
return g; |
|
147 |
} |
|
148 |
|
|
149 |
/** |
|
124 | 150 |
* Checks if the type of the feature store and the geometry are compatibles. |
125 | 151 |
* @param store |
126 | 152 |
* @param geom |
... | ... | |
182 | 208 |
} |
183 | 209 |
} |
184 | 210 |
} |
185 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
|
211 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
|
186 | 212 |
store.insert(feat); |
187 | 213 |
return feat; |
188 | 214 |
} |
... | ... | |
203 | 229 |
EditableFeature feat = store.createNewFeature(); |
204 | 230 |
feat.set(0, id); |
205 | 231 |
feat.set(1, value); |
206 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
|
232 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
|
207 | 233 |
store.insert(feat); |
208 | 234 |
return feat; |
209 | 235 |
} |
... | ... | |
225 | 251 |
feat.set(0, id); |
226 | 252 |
feat.set(1, value1); |
227 | 253 |
feat.set(2, value2); |
228 |
feat.setGeometry("GEOMETRY", newDalGeom);
|
|
254 |
feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
|
|
229 | 255 |
store.insert(feat); |
230 | 256 |
return feat; |
231 | 257 |
} |
... | ... | |
275 | 301 |
public EditableFeature addFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException { |
276 | 302 |
if(acceptType(store, newGeom)) { |
277 | 303 |
EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), feature); |
278 |
feat.setGeometry(fieldNames.length, newGeom);
|
|
304 |
feat.setGeometry(fieldNames.length, convertGeometry2MultiGeometry(newGeom));
|
|
279 | 305 |
store.insert(feat); |
280 | 306 |
return feat; |
281 | 307 |
} |
org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.lib/org.gvsig.geoprocess.lib.sextante/src/main/java/org/gvsig/geoprocess/lib/sextante/dataObjects/FlyrVectIVectorLayer.java | ||
---|---|---|
435 | 435 |
case IVectorLayer.SHAPE_TYPE_POLYGON: |
436 | 436 |
return org.gvsig.fmap.geom.Geometry.TYPES.SURFACE; |
437 | 437 |
case IVectorLayer.SHAPE_TYPE_LINE: |
438 |
return org.gvsig.fmap.geom.Geometry.TYPES.CURVE; |
|
438 |
return org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE;
|
|
439 | 439 |
case IVectorLayer.SHAPE_TYPE_POINT: |
440 |
return org.gvsig.fmap.geom.Geometry.TYPES.POINT; |
|
440 |
return org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT;
|
|
441 | 441 |
default: |
442 | 442 |
return org.gvsig.fmap.geom.Geometry.TYPES.SURFACE; |
443 | 443 |
} |
Also available in: Unified diff