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 @ 741
History | View | Annotate | Download (8.39 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.slf4j.Logger; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
import com.vividsolutions.jts.geom.Geometry; |
51 |
import com.vividsolutions.jts.geom.GeometryCollection; |
52 |
import com.vividsolutions.jts.geom.LineString; |
53 |
import com.vividsolutions.jts.geom.MultiLineString; |
54 |
import com.vividsolutions.jts.geom.MultiPoint; |
55 |
import com.vividsolutions.jts.geom.MultiPolygon; |
56 |
import com.vividsolutions.jts.geom.Point; |
57 |
import com.vividsolutions.jts.geom.Polygon; |
58 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
59 |
|
60 |
import es.unex.sextante.core.Sextante; |
61 |
|
62 |
/**
|
63 |
* Builds a geometry with the intersection between two layers
|
64 |
*
|
65 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
66 |
*/
|
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 |
|
73 |
public IntersectionOperation(FeatureStore overlayLayer, AbstractSextanteGeoProcess p) {
|
74 |
super(p);
|
75 |
this.storeOverlay = overlayLayer;
|
76 |
} |
77 |
|
78 |
public boolean getErrorInfo() { |
79 |
return errorInfo;
|
80 |
} |
81 |
|
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;
|
93 |
|
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 |
} |
107 |
|
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 |
} |
116 |
|
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 |
} |
123 |
// it.dispose();
|
124 |
return lastEditFeature;
|
125 |
} |
126 |
|
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;
|
137 |
|
138 |
try {
|
139 |
outPutType = persister.getOutputFeatureStore().getDefaultFeatureType() |
140 |
.getDefaultGeometryAttribute().getGeomType().getType(); |
141 |
} catch (DataException e2) {
|
142 |
Sextante.addErrorToLog(e2); |
143 |
} |
144 |
|
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 |
} |
163 |
|
164 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope()))
|
165 |
return lastEditFeature;
|
166 |
|
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 |
} |
184 |
|
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 |
} |
203 |
|
204 |
private boolean typesMatch(int dalType, Geometry newGeom) { |
205 |
return
|
206 |
((dalType == TYPES.MULTICURVE && |
207 |
(newGeom instanceof MultiLineString || newGeom instanceof LineString)) || |
208 |
(dalType == TYPES.MULTIPOINT && |
209 |
(newGeom instanceof MultiPoint || newGeom instanceof Point)) || |
210 |
(dalType == TYPES.MULTISURFACE && |
211 |
(newGeom instanceof Polygon || newGeom instanceof MultiPolygon)) || |
212 |
(dalType == TYPES.CURVE && newGeom instanceof LineString) ||
|
213 |
(dalType == TYPES.SURFACE && newGeom instanceof Polygon) || |
214 |
(dalType == TYPES.POINT && newGeom instanceof Point)); |
215 |
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* clips feature's geometry with the clipping geometry, preserving
|
220 |
* feature's original attributes.
|
221 |
* If feature's geometry doesn't touch clipping geometry, it will be
|
222 |
* ignored.
|
223 |
*/
|
224 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
225 |
} |
226 |
|
227 |
} |
228 |
|