gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.clip / src / main / java / org / gvsig / geoprocess / algorithm / clip / ClipOperation.java @ 245
History | View | Annotate | Download (6.48 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 |
/*
|
25 |
|
26 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
27 |
*
|
28 |
* Copyright (C) 2010 Generalitat Valenciana.
|
29 |
*
|
30 |
* This program is free software; you can redistribute it and/or
|
31 |
* modify it under the terms of the GNU General Public License
|
32 |
* as published by the Free Software Foundation; either version 2
|
33 |
* of the License, or (at your option) any later version.
|
34 |
*
|
35 |
* This program is distributed in the hope that it will be useful,
|
36 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
37 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
38 |
* GNU General Public License for more details.
|
39 |
*
|
40 |
* You should have received a copy of the GNU General Public License
|
41 |
* along with this program; if not, write to the Free Software
|
42 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
43 |
*/
|
44 |
|
45 |
package org.gvsig.geoprocess.algorithm.clip; |
46 |
|
47 |
import org.gvsig.fmap.dal.exception.DataException; |
48 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
49 |
import org.gvsig.fmap.dal.feature.Feature; |
50 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
51 |
import org.gvsig.geoprocess.algorithm.base.core.DALFeaturePersister; |
52 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
53 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
54 |
|
55 |
import com.vividsolutions.jts.geom.Geometry; |
56 |
|
57 |
import es.unex.sextante.core.Sextante; |
58 |
/**
|
59 |
* This class analyzes all features of a layer, clipping its geometries
|
60 |
* with the convex hull of another layer.
|
61 |
* If the geometry of the feature analyzed doesnt intersect with the convex
|
62 |
* hull geometry, the clipvisitor will ignore it.
|
63 |
*
|
64 |
* It intersects, computes intersection and creates a new feature with
|
65 |
* the same attributes and the new intersection geometry.
|
66 |
*
|
67 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
68 |
*/
|
69 |
public class ClipOperation extends GeometryOperation { |
70 |
|
71 |
/**
|
72 |
* Clipping geometry: the convex hull of the
|
73 |
* clipping layer
|
74 |
*/
|
75 |
private Geometry clippingConvexHull = null; |
76 |
private DALFeaturePersister persister = null; |
77 |
|
78 |
public ClipOperation(org.gvsig.fmap.geom.Geometry clip) {
|
79 |
this.clippingConvexHull = GeometryUtil.geomToJTS(clip);
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* clips feature's geometry with the clipping geometry, preserving
|
84 |
* feature's original attributes.
|
85 |
* If feature's geometry doesnt touch clipping geometry, it will be
|
86 |
* ignored.
|
87 |
*/
|
88 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
|
89 |
if(g == null) |
90 |
return lastEditFeature;
|
91 |
|
92 |
Geometry jtsGeom = GeometryUtil.geomToJTS(g); |
93 |
|
94 |
if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
|
95 |
return lastEditFeature;
|
96 |
|
97 |
if(jtsGeom.intersects(clippingConvexHull)) {
|
98 |
try {
|
99 |
Geometry newGeom = jtsGeom.intersection(clippingConvexHull); |
100 |
lastEditFeature = persister.addFeature(feature, newGeom); |
101 |
} catch(com.vividsolutions.jts.geom.TopologyException e){
|
102 |
Sextante.addErrorToLog(e); |
103 |
if(! jtsGeom.isValid()) {
|
104 |
System.out.println("La geometria de entrada no es valida"); |
105 |
jtsGeom = GeometryUtil.removeDuplicatesFrom(jtsGeom); |
106 |
} |
107 |
if(! clippingConvexHull.isValid()) {
|
108 |
System.out.println("La geometria de recorte no es valida"); |
109 |
clippingConvexHull = GeometryUtil.removeDuplicatesFrom(clippingConvexHull); |
110 |
} |
111 |
try {
|
112 |
Geometry newGeom = jtsGeom.intersection(clippingConvexHull); |
113 |
lastEditFeature = persister.addFeature(feature, newGeom); |
114 |
} catch(com.vividsolutions.jts.geom.TopologyException ee){
|
115 |
Sextante.addErrorToLog(ee); |
116 |
} catch (CreateGeometryException ee) {
|
117 |
Sextante.addErrorToLog(ee); |
118 |
} catch (DataException ee) {
|
119 |
Sextante.addErrorToLog(ee); |
120 |
} |
121 |
} catch (CreateGeometryException e) {
|
122 |
Sextante.addErrorToLog(e); |
123 |
} catch (DataException e) {
|
124 |
Sextante.addErrorToLog(e); |
125 |
} |
126 |
} |
127 |
return lastEditFeature;
|
128 |
} |
129 |
|
130 |
/**
|
131 |
* clips feature's geometry with the clipping geometry, preserving
|
132 |
* feature's original attributes.
|
133 |
* If feature's geometry doesnt touch clipping geometry, it will be
|
134 |
* ignored.
|
135 |
*/
|
136 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
137 |
if(g == null) |
138 |
return;
|
139 |
|
140 |
lastEditFeature = feature; |
141 |
|
142 |
Geometry jtsGeom = GeometryUtil.geomToJTS(g); |
143 |
|
144 |
if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
|
145 |
return;
|
146 |
|
147 |
if(jtsGeom.intersects(clippingConvexHull)) {
|
148 |
try {
|
149 |
Geometry newGeom = jtsGeom.intersection(clippingConvexHull); |
150 |
persister.addFeature(feature, newGeom); |
151 |
} catch(com.vividsolutions.jts.geom.TopologyException e){
|
152 |
Sextante.addErrorToLog(e); |
153 |
if(! jtsGeom.isValid()) {
|
154 |
System.out.println("La geometria de entrada no es valida"); |
155 |
jtsGeom = GeometryUtil.removeDuplicatesFrom(jtsGeom); |
156 |
} |
157 |
if(! clippingConvexHull.isValid()) {
|
158 |
System.out.println("La geometria de recorte no es valida"); |
159 |
clippingConvexHull = GeometryUtil.removeDuplicatesFrom(clippingConvexHull); |
160 |
} |
161 |
try {
|
162 |
Geometry newGeom = jtsGeom.intersection(clippingConvexHull); |
163 |
persister.addFeature(feature, newGeom); |
164 |
} catch(com.vividsolutions.jts.geom.TopologyException ee){
|
165 |
Sextante.addErrorToLog(ee); |
166 |
} catch (CreateGeometryException ee) {
|
167 |
Sextante.addErrorToLog(ee); |
168 |
} catch (DataException ee) {
|
169 |
Sextante.addErrorToLog(ee); |
170 |
} |
171 |
} catch (CreateGeometryException e) {
|
172 |
Sextante.addErrorToLog(e); |
173 |
} catch (DataException e) {
|
174 |
Sextante.addErrorToLog(e); |
175 |
} |
176 |
} |
177 |
} |
178 |
|
179 |
/**
|
180 |
* Ends the edition and closes the FeatureStore
|
181 |
*/
|
182 |
public void end() { |
183 |
persister.end(); |
184 |
} |
185 |
|
186 |
public String getProcessDescription() { |
187 |
return "Clipping features agaisnt a clip geometry"; |
188 |
} |
189 |
} |
190 |
|