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 @ 307
History | View | Annotate | Download (8.4 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 |
features = storeOverlay.getFeatureSet(); |
91 |
it = features.iterator(); |
92 |
} catch (DataException e) {
|
93 |
Sextante.addErrorToLog(e); |
94 |
return lastEditFeature;
|
95 |
} |
96 |
|
97 |
while( it.hasNext() ) {
|
98 |
Feature featureOverlay = (Feature)it.next(); |
99 |
List geomList = featureOverlay.getGeometries();
|
100 |
if(geomList == null) { |
101 |
org.gvsig.fmap.geom.Geometry geom = featureOverlay.getDefaultGeometry(); |
102 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay); |
103 |
continue;
|
104 |
} |
105 |
|
106 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
107 |
while(itGeom.hasNext()) {
|
108 |
org.gvsig.fmap.geom.Geometry geom = itGeom.next(); |
109 |
lastEditFeature = intersection(g, geom, featureInput, featureOverlay); |
110 |
} |
111 |
} |
112 |
it.dispose(); |
113 |
return lastEditFeature;
|
114 |
} |
115 |
|
116 |
private EditableFeature intersection( org.gvsig.fmap.geom.Geometry g1,
|
117 |
org.gvsig.fmap.geom.Geometry g2, |
118 |
Feature featureInput, |
119 |
Feature featureOverlay) { |
120 |
Geometry overlaysGeom = null;
|
121 |
Geometry jtsGeom = null;
|
122 |
int outPutType = TYPES.SURFACE;
|
123 |
|
124 |
try {
|
125 |
outPutType = persister.getOutputFeatureStore().getDefaultFeatureType() |
126 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
127 |
} catch (DataException e2) {
|
128 |
Sextante.addErrorToLog(e2); |
129 |
} |
130 |
|
131 |
try {
|
132 |
if( ((g1 instanceof Surface && g2 instanceof Curve) || |
133 |
(g2 instanceof Surface && g1 instanceof Curve)) && |
134 |
outPutType == TYPES.MULTIPOINT) { |
135 |
org.gvsig.fmap.geom.Geometry overGeom = (g1 instanceof Surface) ? g1 : g2;
|
136 |
jtsGeom = (g1 instanceof Surface) ? GeometryUtil.geomToJTS(g2) : GeometryUtil.geomToJTS(g1);
|
137 |
overlaysGeom = (Geometry)overGeom.invokeOperation("toJTSLineString", null); |
138 |
} else {
|
139 |
if( g1 instanceof Surface && |
140 |
g2 instanceof Surface &&
|
141 |
(outPutType == TYPES.MULTIPOINT)) { |
142 |
jtsGeom = (Geometry)g1.invokeOperation("toJTSLineString", null); |
143 |
overlaysGeom = (Geometry)g2.invokeOperation("toJTSLineString", null); |
144 |
} else {
|
145 |
jtsGeom = GeometryUtil.geomToJTS(g1); |
146 |
overlaysGeom = GeometryUtil.geomToJTS(g2); |
147 |
} |
148 |
} |
149 |
|
150 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope()))
|
151 |
return lastEditFeature;
|
152 |
|
153 |
if(jtsGeom.intersects(overlaysGeom)) {
|
154 |
Geometry newGeom = EnhancedPrecisionOp.intersection(jtsGeom, overlaysGeom); |
155 |
if(!newGeom.isEmpty()) {
|
156 |
if(typesMatch(outPutType, newGeom) || newGeom instanceof GeometryCollection) { |
157 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom); |
158 |
} else {
|
159 |
//Para intersecciones entre pol?gonos cuando la salida es de tipo l?nea
|
160 |
//la generamos a partir del pol?gono resultante de la intersecci?n
|
161 |
if( g1 instanceof Surface && |
162 |
g2 instanceof Surface &&
|
163 |
outPutType == TYPES.MULTICURVE && |
164 |
(newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) { |
165 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
166 |
ctx.setAttribute(FromJTS.PARAM, newGeom); |
167 |
org.gvsig.fmap.geom.Geometry newDalGeom = (org.gvsig.fmap.geom.Geometry)geomManager.invokeOperation(FromJTS.NAME, ctx); |
168 |
newGeom = (Geometry)newDalGeom.invokeOperation("toJTSLineString", null); |
169 |
} |
170 |
|
171 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom); |
172 |
} |
173 |
} |
174 |
} |
175 |
} catch (CreateGeometryException e) {
|
176 |
Sextante.addErrorToLog(e); |
177 |
} catch (DataException e) {
|
178 |
Sextante.addErrorToLog(e); |
179 |
} catch (GeometryOperationNotSupportedException e1) {
|
180 |
Sextante.addErrorToLog(e1); |
181 |
} catch (GeometryOperationException e1) {
|
182 |
Sextante.addErrorToLog(e1); |
183 |
} |
184 |
return lastEditFeature;
|
185 |
} |
186 |
|
187 |
private boolean typesMatch(int dalType, Geometry newGeom) { |
188 |
return
|
189 |
((dalType == TYPES.MULTICURVE && |
190 |
(newGeom instanceof MultiLineString || newGeom instanceof LineString)) || |
191 |
(dalType == TYPES.MULTIPOINT && |
192 |
(newGeom instanceof MultiPoint || newGeom instanceof Point)) || |
193 |
(dalType == TYPES.MULTISURFACE && |
194 |
(newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) || |
195 |
(dalType == TYPES.CURVE && newGeom instanceof LineString) ||
|
196 |
(dalType == TYPES.SURFACE && newGeom instanceof Polygon) || |
197 |
(dalType == TYPES.POINT && newGeom instanceof Point)); |
198 |
|
199 |
} |
200 |
|
201 |
// private MultiCurve convertPolygonJTS2LineJTS(Polygon p) {
|
202 |
//
|
203 |
// }
|
204 |
|
205 |
/**
|
206 |
* clips feature's geometry with the clipping geometry, preserving
|
207 |
* feature's original attributes.
|
208 |
* If feature's geometry doesn't touch clipping geometry, it will be
|
209 |
* ignored.
|
210 |
*/
|
211 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
212 |
/*if(g == null)
|
213 |
return;
|
214 |
|
215 |
Geometry jtsGeom = Converter.geometryToJts(g);
|
216 |
|
217 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope()))
|
218 |
return;
|
219 |
|
220 |
if(jtsGeom.intersects(overlaysGeom)) {
|
221 |
try {
|
222 |
Geometry newGeom = EnhancedPrecisionOp.difference(jtsGeom, overlaysGeom);
|
223 |
persister.addFeature(feature, newGeom);
|
224 |
} catch (CreateGeometryException e) {
|
225 |
Sextante.addErrorToLog(e);
|
226 |
} catch (DataException e) {
|
227 |
Sextante.addErrorToLog(e);
|
228 |
}
|
229 |
} */
|
230 |
} |
231 |
|
232 |
} |
233 |
|