gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.intersection / src / main / java / org / gvsig / sextante / app / algorithm / intersection / IntersectionOperation.java @ 30
History | View | Annotate | Download (5.66 KB)
1 |
/*
|
---|---|
2 |
|
3 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
4 |
*
|
5 |
* Copyright (C) 2010 Generalitat Valenciana.
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU General Public License
|
9 |
* as published by the Free Software Foundation; either version 2
|
10 |
* of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License
|
18 |
* along with this program; if not, write to the Free Software
|
19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
20 |
*/
|
21 |
|
22 |
package org.gvsig.sextante.app.algorithm.intersection; |
23 |
|
24 |
import java.util.Iterator; |
25 |
import java.util.List; |
26 |
|
27 |
import org.gvsig.fmap.dal.exception.DataException; |
28 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
29 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
30 |
import org.gvsig.fmap.dal.feature.Feature; |
31 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
34 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
35 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
36 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
37 |
import org.gvsig.sextante.app.algorithm.base.core.DALFeaturePersister; |
38 |
|
39 |
import com.vividsolutions.jts.geom.Geometry; |
40 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
41 |
|
42 |
import es.unex.sextante.core.Sextante; |
43 |
/**
|
44 |
*
|
45 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
46 |
*/
|
47 |
public class IntersectionOperation { |
48 |
|
49 |
private FeatureStore storeOverlay = null; |
50 |
private DALFeaturePersister persister = null; |
51 |
private EditableFeature lastEditFeature = null; |
52 |
|
53 |
public IntersectionOperation(FeatureStore overlayLayer) {
|
54 |
this.storeOverlay = overlayLayer;
|
55 |
} |
56 |
|
57 |
/**
|
58 |
* Sets the output FeatureType
|
59 |
* @param out
|
60 |
* @throws DataException
|
61 |
*/
|
62 |
public void setFeatureStore(FeatureStore out, String[] attrNames) throws DataException { |
63 |
persister = new DALFeaturePersister(out, attrNames);
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* Computes intersection between the geometry and the overlay layer. The fields of the
|
68 |
* intersected features will be added.
|
69 |
* @param g
|
70 |
* @param featureInput
|
71 |
* @return
|
72 |
*/
|
73 |
@SuppressWarnings("unchecked") |
74 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
|
75 |
if(g == null) |
76 |
return lastEditFeature;
|
77 |
|
78 |
Geometry jtsGeom = null;
|
79 |
try {
|
80 |
jtsGeom = (Geometry)g.invokeOperation(ToJTS.CODE, null);
|
81 |
} catch (GeometryOperationNotSupportedException e1) {
|
82 |
Sextante.addErrorToLog(e1); |
83 |
} catch (GeometryOperationException e1) {
|
84 |
Sextante.addErrorToLog(e1); |
85 |
} |
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(jtsGeom, 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(jtsGeom, geom, featureInput, featureOverlay); |
110 |
} |
111 |
} |
112 |
it.dispose(); |
113 |
return lastEditFeature;
|
114 |
} |
115 |
|
116 |
private EditableFeature intersection(Geometry jtsGeom,
|
117 |
org.gvsig.fmap.geom.Geometry overGeom, |
118 |
Feature featureInput, |
119 |
Feature featureOverlay) { |
120 |
Geometry overlaysGeom = null;
|
121 |
try {
|
122 |
overlaysGeom = (Geometry)overGeom.invokeOperation(ToJTS.CODE, null);
|
123 |
} catch (GeometryOperationNotSupportedException e1) {
|
124 |
Sextante.addErrorToLog(e1); |
125 |
return null; |
126 |
} catch (GeometryOperationException e1) {
|
127 |
Sextante.addErrorToLog(e1); |
128 |
return null; |
129 |
} |
130 |
|
131 |
try {
|
132 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope()))
|
133 |
return lastEditFeature;
|
134 |
|
135 |
if(jtsGeom.intersects(overlaysGeom)) {
|
136 |
Geometry newGeom = EnhancedPrecisionOp.intersection(jtsGeom, overlaysGeom); |
137 |
if(!newGeom.isEmpty())
|
138 |
lastEditFeature = persister.addFeature(featureInput, featureOverlay, newGeom); |
139 |
} |
140 |
} catch (CreateGeometryException e) {
|
141 |
Sextante.addErrorToLog(e); |
142 |
} catch (DataException e) {
|
143 |
Sextante.addErrorToLog(e); |
144 |
} |
145 |
return lastEditFeature;
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* clips feature's geometry with the clipping geometry, preserving
|
150 |
* feature's original attributes.
|
151 |
* If feature's geometry doesn't touch clipping geometry, it will be
|
152 |
* ignored.
|
153 |
*/
|
154 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
155 |
/*if(g == null)
|
156 |
return;
|
157 |
|
158 |
Geometry jtsGeom = Converter.geometryToJts(g);
|
159 |
|
160 |
if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope()))
|
161 |
return;
|
162 |
|
163 |
if(jtsGeom.intersects(overlaysGeom)) {
|
164 |
try {
|
165 |
Geometry newGeom = EnhancedPrecisionOp.difference(jtsGeom, overlaysGeom);
|
166 |
persister.addFeature(feature, newGeom);
|
167 |
} catch (CreateGeometryException e) {
|
168 |
Sextante.addErrorToLog(e);
|
169 |
} catch (DataException e) {
|
170 |
Sextante.addErrorToLog(e);
|
171 |
}
|
172 |
} */
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Ends the edition and closes the FeatureStore
|
177 |
*/
|
178 |
public void end() { |
179 |
persister.end(); |
180 |
} |
181 |
|
182 |
public String getProcessDescription() { |
183 |
return "Clipping features agaisnt a clip geometry"; |
184 |
} |
185 |
} |
186 |
|