Revision 1292
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 | ||
---|---|---|
65 | 65 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a> |
66 | 66 |
*/ |
67 | 67 |
public class IntersectionOperation extends GeometryOperation { |
68 |
private FeatureStore storeOverlay = null; |
|
69 |
protected GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
70 |
private Logger log = LoggerFactory.getLogger(IntersectionOperation.class); |
|
71 |
private boolean errorInfo = false; |
|
72 | 68 |
|
73 |
public IntersectionOperation(FeatureStore overlayLayer, AbstractSextanteGeoProcess p) {
|
|
74 |
super(p);
|
|
75 |
this.storeOverlay = overlayLayer;
|
|
76 |
}
|
|
69 |
private FeatureStore storeOverlay = null;
|
|
70 |
protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
|
71 |
private Logger log = LoggerFactory.getLogger(IntersectionOperation.class);
|
|
72 |
private boolean errorInfo = false;
|
|
77 | 73 |
|
78 |
public boolean getErrorInfo() { |
|
79 |
return errorInfo; |
|
80 |
} |
|
74 |
public IntersectionOperation(FeatureStore overlayLayer, AbstractSextanteGeoProcess p) { |
|
75 |
super(p); |
|
76 |
this.storeOverlay = overlayLayer; |
|
77 |
} |
|
81 | 78 |
|
82 |
/** |
|
83 |
* Computes intersection between the geometry and the overlay layer. The fields of the |
|
84 |
* intersected features will be added. |
|
85 |
* @param g |
|
86 |
* @param featureInput |
|
87 |
* @return |
|
88 |
*/ |
|
89 |
@SuppressWarnings({ "unchecked", "deprecation" }) |
|
90 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) { |
|
91 |
if(g == null) |
|
92 |
return lastEditFeature; |
|
79 |
public boolean getErrorInfo() { |
|
80 |
return errorInfo; |
|
81 |
} |
|
93 | 82 |
|
94 |
FeatureSet features = null;
|
|
95 |
Iterator it = null;
|
|
96 |
try {
|
|
97 |
if(selectedGeomOverlay) {
|
|
98 |
features = (FeatureSet)storeOverlay.getSelection();
|
|
99 |
} else {
|
|
100 |
features = storeOverlay.getFeatureSet();
|
|
101 |
}
|
|
102 |
it = features.iterator();
|
|
103 |
} catch (DataException e) {
|
|
104 |
Sextante.addErrorToLog(e);
|
|
105 |
return lastEditFeature;
|
|
106 |
}
|
|
83 |
/**
|
|
84 |
* Computes intersection between the geometry and the overlay layer. The
|
|
85 |
* fields of the intersected features will be added.
|
|
86 |
*
|
|
87 |
* @param g
|
|
88 |
* @param featureInput
|
|
89 |
* @return
|
|
90 |
*/
|
|
91 |
@SuppressWarnings({"unchecked", "deprecation"})
|
|
92 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
|
|
93 |
if (g == null) {
|
|
94 |
return lastEditFeature;
|
|
95 |
}
|
|
107 | 96 |
|
108 |
while( it.hasNext() ) { |
|
109 |
Feature featureOverlay = (Feature)it.next(); |
|
110 |
List<org.gvsig.fmap.geom.Geometry> geomList = featureOverlay.getGeometries(); |
|
111 |
if(geomList == null) { |
|
112 |
org.gvsig.fmap.geom.Geometry geom = featureOverlay.getDefaultGeometry(); |
|
113 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay); |
|
114 |
continue; |
|
115 |
} |
|
97 |
FeatureSet features = null; |
|
98 |
Iterator it = null; |
|
99 |
try { |
|
100 |
if (selectedGeomOverlay) { |
|
101 |
features = (FeatureSet) storeOverlay.getSelection(); |
|
102 |
} else { |
|
103 |
features = storeOverlay.getFeatureSet(); |
|
104 |
} |
|
105 |
it = features.iterator(); |
|
106 |
} catch (DataException e) { |
|
107 |
Sextante.addErrorToLog(e); |
|
108 |
return lastEditFeature; |
|
109 |
} |
|
116 | 110 |
|
117 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator(); |
|
118 |
while(itGeom.hasNext()) { |
|
119 |
org.gvsig.fmap.geom.Geometry geom = itGeom.next(); |
|
120 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay); |
|
121 |
} |
|
122 |
} |
|
111 |
while (it.hasNext()) { |
|
112 |
Feature featureOverlay = (Feature) it.next(); |
|
113 |
List<org.gvsig.fmap.geom.Geometry> geomList = featureOverlay.getGeometries(); |
|
114 |
if (geomList == null) { |
|
115 |
org.gvsig.fmap.geom.Geometry geom = featureOverlay.getDefaultGeometry(); |
|
116 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay); |
|
117 |
continue; |
|
118 |
} |
|
119 |
|
|
120 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator(); |
|
121 |
while (itGeom.hasNext()) { |
|
122 |
org.gvsig.fmap.geom.Geometry geom = itGeom.next(); |
|
123 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay); |
|
124 |
} |
|
125 |
} |
|
123 | 126 |
// it.dispose(); |
124 |
return lastEditFeature;
|
|
125 |
}
|
|
127 |
return lastEditFeature;
|
|
128 |
}
|
|
126 | 129 |
|
127 |
private EditableFeature intersection( org.gvsig.fmap.geom.Geometry g1,
|
|
128 |
org.gvsig.fmap.geom.Geometry g2,
|
|
129 |
Feature featureInput,
|
|
130 |
Feature featureOverlay) {
|
|
131 |
if(g1 == null || g2 == null){
|
|
132 |
return null;
|
|
133 |
}
|
|
134 |
Geometry overlaysGeom = null;
|
|
135 |
Geometry jtsGeom = null;
|
|
136 |
int outPutType = TYPES.SURFACE;
|
|
130 |
private EditableFeature intersection(org.gvsig.fmap.geom.Geometry g1,
|
|
131 |
org.gvsig.fmap.geom.Geometry g2,
|
|
132 |
Feature featureInput,
|
|
133 |
Feature featureOverlay) {
|
|
134 |
if (g1 == null || g2 == null) {
|
|
135 |
return null;
|
|
136 |
}
|
|
137 |
Geometry overlaysGeom = null;
|
|
138 |
Geometry jtsGeom = null;
|
|
139 |
int outPutType = TYPES.SURFACE;
|
|
137 | 140 |
|
138 |
try {
|
|
139 |
outPutType = persister.getOutputFeatureStore().getDefaultFeatureType()
|
|
140 |
.getDefaultGeometryAttribute().getGeomType().getType();
|
|
141 |
} catch (DataException e2) {
|
|
142 |
Sextante.addErrorToLog(e2);
|
|
143 |
}
|
|
141 |
try {
|
|
142 |
outPutType = persister.getOutputFeatureStore().getDefaultFeatureType()
|
|
143 |
.getDefaultGeometryAttribute().getGeomType().getType();
|
|
144 |
} catch (DataException e2) {
|
|
145 |
Sextante.addErrorToLog(e2);
|
|
146 |
}
|
|
144 | 147 |
|
145 |
try {
|
|
146 |
if( ((g1 instanceof Surface && g2 instanceof Curve) ||
|
|
147 |
(g2 instanceof Surface && g1 instanceof Curve)) &&
|
|
148 |
outPutType == TYPES.MULTIPOINT) {
|
|
149 |
org.gvsig.fmap.geom.Geometry overGeom = (g1 instanceof Surface) ? g1 : g2;
|
|
150 |
jtsGeom = (g1 instanceof Surface) ? GeometryUtil.geomToJTS(g2) : GeometryUtil.geomToJTS(g1);
|
|
151 |
overlaysGeom = (Geometry)overGeom.invokeOperation("toJTSLineString", null);
|
|
152 |
} else {
|
|
153 |
if( g1 instanceof Surface &&
|
|
154 |
g2 instanceof Surface &&
|
|
155 |
(outPutType == TYPES.MULTIPOINT)) {
|
|
156 |
jtsGeom = (Geometry)g1.invokeOperation("toJTSLineString", null);
|
|
157 |
overlaysGeom = (Geometry)g2.invokeOperation("toJTSLineString", null);
|
|
158 |
} else {
|
|
159 |
jtsGeom = GeometryUtil.geomToJTS(g1);
|
|
160 |
overlaysGeom = GeometryUtil.geomToJTS(g2);
|
|
161 |
}
|
|
162 |
}
|
|
148 |
try {
|
|
149 |
if (((g1 instanceof Surface && g2 instanceof Curve)
|
|
150 |
|| (g2 instanceof Surface && g1 instanceof Curve))
|
|
151 |
&& outPutType == TYPES.MULTIPOINT) {
|
|
152 |
org.gvsig.fmap.geom.Geometry overGeom = (g1 instanceof Surface) ? g1 : g2;
|
|
153 |
jtsGeom = (g1 instanceof Surface) ? GeometryUtil.geomToJTS(g2) : GeometryUtil.geomToJTS(g1);
|
|
154 |
overlaysGeom = (Geometry) overGeom.invokeOperation("toJTSLineString", null);
|
|
155 |
} else {
|
|
156 |
if (g1 instanceof Surface
|
|
157 |
&& g2 instanceof Surface
|
|
158 |
&& (outPutType == TYPES.MULTIPOINT)) {
|
|
159 |
jtsGeom = (Geometry) g1.invokeOperation("toJTSLineString", null);
|
|
160 |
overlaysGeom = (Geometry) g2.invokeOperation("toJTSLineString", null);
|
|
161 |
} else {
|
|
162 |
jtsGeom = GeometryUtil.geomToJTS(g1);
|
|
163 |
overlaysGeom = GeometryUtil.geomToJTS(g2);
|
|
164 |
}
|
|
165 |
}
|
|
163 | 166 |
|
164 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope())) |
|
165 |
return lastEditFeature; |
|
167 |
if (!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope())) { |
|
168 |
return lastEditFeature; |
|
169 |
} |
|
166 | 170 |
|
167 |
if(jtsGeom.intersects(overlaysGeom)) {
|
|
168 |
Geometry newGeom = EnhancedPrecisionOp.intersection(jtsGeom, overlaysGeom);
|
|
169 |
if(!newGeom.isEmpty()) {
|
|
170 |
if(typesMatch(outPutType, newGeom) || newGeom instanceof GeometryCollection) {
|
|
171 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom);
|
|
172 |
} else {
|
|
173 |
//Para intersecciones entre pol?gonos cuando la salida es de tipo l?nea
|
|
174 |
//la generamos a partir del pol?gono resultante de la intersecci?n
|
|
175 |
if( g1 instanceof Surface &&
|
|
176 |
g2 instanceof Surface &&
|
|
177 |
outPutType == TYPES.MULTICURVE &&
|
|
178 |
(newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) {
|
|
179 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
|
180 |
ctx.setAttribute(FromJTS.PARAM, newGeom);
|
|
181 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geomManager.invokeOperation(FromJTS.NAME, ctx);
|
|
182 |
newGeom = (Geometry)newDalGeom.invokeOperation("toJTSLineString", null);
|
|
183 |
}
|
|
171 |
if (jtsGeom.intersects(overlaysGeom)) {
|
|
172 |
Geometry newGeom = EnhancedPrecisionOp.intersection(jtsGeom, overlaysGeom);
|
|
173 |
if (!newGeom.isEmpty()) {
|
|
174 |
if (typesMatch(outPutType, newGeom) || newGeom instanceof GeometryCollection) {
|
|
175 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom);
|
|
176 |
} else {
|
|
177 |
//Para intersecciones entre pol?gonos cuando la salida es de tipo l?nea
|
|
178 |
//la generamos a partir del pol?gono resultante de la intersecci?n
|
|
179 |
if (g1 instanceof Surface
|
|
180 |
&& g2 instanceof Surface
|
|
181 |
&& outPutType == TYPES.MULTICURVE
|
|
182 |
&& (newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) {
|
|
183 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
|
184 |
ctx.setAttribute(FromJTS.PARAM, newGeom);
|
|
185 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry) geomManager.invokeOperation(FromJTS.NAME, ctx);
|
|
186 |
newGeom = (Geometry) newDalGeom.invokeOperation("toJTSLineString", null);
|
|
187 |
}
|
|
184 | 188 |
|
185 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom);
|
|
186 |
}
|
|
187 |
}
|
|
188 |
}
|
|
189 |
} catch (CreateGeometryException e) {
|
|
190 |
Sextante.addErrorToLog(e);
|
|
191 |
} catch (DataException e) {
|
|
192 |
Sextante.addErrorToLog(e);
|
|
193 |
} catch (GeometryOperationNotSupportedException e1) {
|
|
194 |
Sextante.addErrorToLog(e1);
|
|
195 |
} catch (GeometryOperationException e1) {
|
|
196 |
Sextante.addErrorToLog(e1);
|
|
197 |
} catch (com.vividsolutions.jts.geom.TopologyException e) {
|
|
198 |
errorInfo = true;
|
|
199 |
log.info("Problems operating intersection: ", e);
|
|
200 |
}
|
|
201 |
return lastEditFeature;
|
|
202 |
}
|
|
189 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom);
|
|
190 |
}
|
|
191 |
}
|
|
192 |
}
|
|
193 |
} catch (CreateGeometryException e) {
|
|
194 |
Sextante.addErrorToLog(e);
|
|
195 |
} catch (DataException e) {
|
|
196 |
Sextante.addErrorToLog(e);
|
|
197 |
} catch (GeometryOperationNotSupportedException e1) {
|
|
198 |
Sextante.addErrorToLog(e1);
|
|
199 |
} catch (GeometryOperationException e1) {
|
|
200 |
Sextante.addErrorToLog(e1);
|
|
201 |
} catch (com.vividsolutions.jts.geom.TopologyException e) {
|
|
202 |
errorInfo = true;
|
|
203 |
log.info("Problems operating intersection: ", e);
|
|
204 |
}
|
|
205 |
return lastEditFeature;
|
|
206 |
}
|
|
203 | 207 |
|
204 |
private boolean typesMatch(int dalType, Geometry newGeom) {
|
|
208 |
private boolean typesMatch(int dalType, Geometry newGeom) {
|
|
205 | 209 |
|
206 |
return |
|
207 |
((geomManager.isSubtype(TYPES.MULTICURVE, dalType) && |
|
208 |
(newGeom instanceof MultiLineString || newGeom instanceof LineString)) || |
|
209 |
(geomManager.isSubtype(TYPES.MULTIPOINT, dalType) && |
|
210 |
(newGeom instanceof MultiPoint || newGeom instanceof Point)) || |
|
211 |
(geomManager.isSubtype(TYPES.MULTISURFACE, dalType) && |
|
212 |
(newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) || |
|
213 |
(geomManager.isSubtype(TYPES.CURVE, dalType) && newGeom instanceof LineString) || |
|
214 |
(geomManager.isSubtype(TYPES.SURFACE, dalType) && newGeom instanceof Polygon) || |
|
215 |
(geomManager.isSubtype(TYPES.POINT, dalType) && newGeom instanceof Point)); |
|
210 |
return ((geomManager.isSubtype(TYPES.MULTICURVE, dalType) |
|
211 |
&& (newGeom instanceof MultiLineString || newGeom instanceof LineString)) |
|
212 |
|| (geomManager.isSubtype(TYPES.MULTIPOINT, dalType) |
|
213 |
&& (newGeom instanceof MultiPoint || newGeom instanceof Point)) |
|
214 |
|| (geomManager.isSubtype(TYPES.MULTISURFACE, dalType) |
|
215 |
&& (newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) |
|
216 |
|| (geomManager.isSubtype(TYPES.CURVE, dalType) && newGeom instanceof LineString) |
|
217 |
|| (geomManager.isSubtype(TYPES.SURFACE, dalType) && newGeom instanceof Polygon) |
|
218 |
|| (geomManager.isSubtype(TYPES.POINT, dalType) && newGeom instanceof Point)); |
|
216 | 219 |
|
217 |
}
|
|
220 |
}
|
|
218 | 221 |
|
219 |
/** |
|
220 |
* clips feature's geometry with the clipping geometry, preserving |
|
221 |
* feature's original attributes. |
|
222 |
* If feature's geometry doesn't touch clipping geometry, it will be |
|
223 |
* ignored. |
|
224 |
*/ |
|
225 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
|
226 |
} |
|
222 |
/** |
|
223 |
* clips feature's geometry with the clipping geometry, preserving feature's |
|
224 |
* original attributes. If feature's geometry doesn't touch clipping |
|
225 |
* geometry, it will be ignored. |
|
226 |
*/ |
|
227 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
|
228 |
} |
|
227 | 229 |
|
228 | 230 |
} |
229 |
|
Also available in: Unified diff