gvsig-geoprocess / 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 @ 336
History | View | Annotate | Download (8.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 gvSIG Association.
|
5 |
*
|
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 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
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 |
*/
|
24 |
package org.gvsig.geoprocess.algorithm.intersection; |
25 |
|
26 |
import java.util.Iterator; |
27 |
import java.util.List; |
28 |
|
29 |
import org.gvsig.fmap.dal.exception.DataException; |
30 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
31 |
import org.gvsig.fmap.dal.feature.Feature; |
32 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
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.exception.CreateGeometryException; |
38 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
39 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
40 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
41 |
import org.gvsig.fmap.geom.operation.fromjts.FromJTS; |
42 |
import org.gvsig.fmap.geom.primitive.Curve; |
43 |
import org.gvsig.fmap.geom.primitive.Surface; |
44 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
45 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
46 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
47 |
import org.gvsig.tools.dispose.DisposableIterator; |
48 |
|
49 |
import com.vividsolutions.jts.geom.Geometry; |
50 |
import com.vividsolutions.jts.geom.GeometryCollection; |
51 |
import com.vividsolutions.jts.geom.LineString; |
52 |
import com.vividsolutions.jts.geom.MultiLineString; |
53 |
import com.vividsolutions.jts.geom.MultiPoint; |
54 |
import com.vividsolutions.jts.geom.MultiPolygon; |
55 |
import com.vividsolutions.jts.geom.Point; |
56 |
import com.vividsolutions.jts.geom.Polygon; |
57 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
58 |
|
59 |
import es.unex.sextante.core.Sextante; |
60 |
|
61 |
/**
|
62 |
* Builds a geometry with the intersection between two layers
|
63 |
*
|
64 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
65 |
*/
|
66 |
public class IntersectionOperation extends GeometryOperation { |
67 |
private FeatureStore storeOverlay = null; |
68 |
protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
69 |
|
70 |
public IntersectionOperation(FeatureStore overlayLayer, AbstractSextanteGeoProcess p) {
|
71 |
super(p);
|
72 |
this.storeOverlay = overlayLayer;
|
73 |
} |
74 |
|
75 |
/**
|
76 |
* Computes intersection between the geometry and the overlay layer. The fields of the
|
77 |
* intersected features will be added.
|
78 |
* @param g
|
79 |
* @param featureInput
|
80 |
* @return
|
81 |
*/
|
82 |
@SuppressWarnings({ "unchecked", "deprecation" }) |
83 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
|
84 |
if(g == null) |
85 |
return lastEditFeature;
|
86 |
|
87 |
FeatureSet features = null;
|
88 |
DisposableIterator it = null;
|
89 |
try {
|
90 |
if(selectedGeomOverlay) {
|
91 |
features = (FeatureSet)storeOverlay.getSelection(); |
92 |
} else {
|
93 |
features = storeOverlay.getFeatureSet(); |
94 |
} |
95 |
it = features.iterator(); |
96 |
} catch (DataException e) {
|
97 |
Sextante.addErrorToLog(e); |
98 |
return lastEditFeature;
|
99 |
} |
100 |
|
101 |
while( it.hasNext() ) {
|
102 |
Feature featureOverlay = (Feature)it.next(); |
103 |
List geomList = featureOverlay.getGeometries();
|
104 |
if(geomList == null) { |
105 |
org.gvsig.fmap.geom.Geometry geom = featureOverlay.getDefaultGeometry(); |
106 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay); |
107 |
continue;
|
108 |
} |
109 |
|
110 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
111 |
while(itGeom.hasNext()) {
|
112 |
org.gvsig.fmap.geom.Geometry geom = itGeom.next(); |
113 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay); |
114 |
} |
115 |
} |
116 |
it.dispose(); |
117 |
return lastEditFeature;
|
118 |
} |
119 |
|
120 |
private EditableFeature intersection( org.gvsig.fmap.geom.Geometry g1,
|
121 |
org.gvsig.fmap.geom.Geometry g2, |
122 |
Feature featureInput, |
123 |
Feature featureOverlay) { |
124 |
Geometry overlaysGeom = null;
|
125 |
Geometry jtsGeom = null;
|
126 |
int outPutType = TYPES.SURFACE;
|
127 |
|
128 |
try {
|
129 |
outPutType = persister.getOutputFeatureStore().getDefaultFeatureType() |
130 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
131 |
} catch (DataException e2) {
|
132 |
Sextante.addErrorToLog(e2); |
133 |
} |
134 |
|
135 |
try {
|
136 |
if( ((g1 instanceof Surface && g2 instanceof Curve) || |
137 |
(g2 instanceof Surface && g1 instanceof Curve)) && |
138 |
outPutType == TYPES.MULTIPOINT) { |
139 |
org.gvsig.fmap.geom.Geometry overGeom = (g1 instanceof Surface) ? g1 : g2;
|
140 |
jtsGeom = (g1 instanceof Surface) ? GeometryUtil.geomToJTS(g2) : GeometryUtil.geomToJTS(g1);
|
141 |
overlaysGeom = (Geometry)overGeom.invokeOperation("toJTSLineString", null); |
142 |
} else {
|
143 |
if( g1 instanceof Surface && |
144 |
g2 instanceof Surface &&
|
145 |
(outPutType == TYPES.MULTIPOINT)) { |
146 |
jtsGeom = (Geometry)g1.invokeOperation("toJTSLineString", null); |
147 |
overlaysGeom = (Geometry)g2.invokeOperation("toJTSLineString", null); |
148 |
} else {
|
149 |
jtsGeom = GeometryUtil.geomToJTS(g1); |
150 |
overlaysGeom = GeometryUtil.geomToJTS(g2); |
151 |
} |
152 |
} |
153 |
|
154 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope()))
|
155 |
return lastEditFeature;
|
156 |
|
157 |
if(jtsGeom.intersects(overlaysGeom)) {
|
158 |
Geometry newGeom = EnhancedPrecisionOp.intersection(jtsGeom, overlaysGeom); |
159 |
if(!newGeom.isEmpty()) {
|
160 |
if(typesMatch(outPutType, newGeom) || newGeom instanceof GeometryCollection) { |
161 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom); |
162 |
} else {
|
163 |
//Para intersecciones entre pol?gonos cuando la salida es de tipo l?nea
|
164 |
//la generamos a partir del pol?gono resultante de la intersecci?n
|
165 |
if( g1 instanceof Surface && |
166 |
g2 instanceof Surface &&
|
167 |
outPutType == TYPES.MULTICURVE && |
168 |
(newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) { |
169 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
170 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
171 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geomManager.invokeOperation(FromJTS.NAME, ctx); |
172 |
newGeom = (Geometry)newDalGeom.invokeOperation("toJTSLineString", null); |
173 |
} |
174 |
|
175 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom); |
176 |
} |
177 |
} |
178 |
} |
179 |
} catch (CreateGeometryException e) {
|
180 |
Sextante.addErrorToLog(e); |
181 |
} catch (DataException e) {
|
182 |
Sextante.addErrorToLog(e); |
183 |
} catch (GeometryOperationNotSupportedException e1) {
|
184 |
Sextante.addErrorToLog(e1); |
185 |
} catch (GeometryOperationException e1) {
|
186 |
Sextante.addErrorToLog(e1); |
187 |
} |
188 |
return lastEditFeature;
|
189 |
} |
190 |
|
191 |
private boolean typesMatch(int dalType, Geometry newGeom) { |
192 |
return
|
193 |
((dalType == TYPES.MULTICURVE && |
194 |
(newGeom instanceof MultiLineString || newGeom instanceof LineString)) || |
195 |
(dalType == TYPES.MULTIPOINT && |
196 |
(newGeom instanceof MultiPoint || newGeom instanceof Point)) || |
197 |
(dalType == TYPES.MULTISURFACE && |
198 |
(newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) || |
199 |
(dalType == TYPES.CURVE && newGeom instanceof LineString) ||
|
200 |
(dalType == TYPES.SURFACE && newGeom instanceof Polygon) || |
201 |
(dalType == TYPES.POINT && newGeom instanceof Point)); |
202 |
|
203 |
} |
204 |
|
205 |
// private MultiCurve convertPolygonJTS2LineJTS(Polygon p) {
|
206 |
//
|
207 |
// }
|
208 |
|
209 |
/**
|
210 |
* clips feature's geometry with the clipping geometry, preserving
|
211 |
* feature's original attributes.
|
212 |
* If feature's geometry doesn't touch clipping geometry, it will be
|
213 |
* ignored.
|
214 |
*/
|
215 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
216 |
/*if(g == null)
|
217 |
return;
|
218 |
|
219 |
Geometry jtsGeom = Converter.geometryToJts(g);
|
220 |
|
221 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope()))
|
222 |
return;
|
223 |
|
224 |
if(jtsGeom.intersects(overlaysGeom)) {
|
225 |
try {
|
226 |
Geometry newGeom = EnhancedPrecisionOp.difference(jtsGeom, overlaysGeom);
|
227 |
persister.addFeature(feature, newGeom);
|
228 |
} catch (CreateGeometryException e) {
|
229 |
Sextante.addErrorToLog(e);
|
230 |
} catch (DataException e) {
|
231 |
Sextante.addErrorToLog(e);
|
232 |
}
|
233 |
} */
|
234 |
} |
235 |
|
236 |
} |
237 |
|