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 @ 252
History | View | Annotate | Download (6.34 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.GeometryOperation; |
52 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
53 |
|
54 |
import com.vividsolutions.jts.geom.Geometry; |
55 |
|
56 |
import es.unex.sextante.core.Sextante; |
57 |
/**
|
58 |
* This class analyzes all features of a layer, clipping its geometries
|
59 |
* with the convex hull of another layer.
|
60 |
* If the geometry of the feature analyzed doesnt intersect with the convex
|
61 |
* hull geometry, the clipvisitor will ignore it.
|
62 |
*
|
63 |
* It intersects, computes intersection and creates a new feature with
|
64 |
* the same attributes and the new intersection geometry.
|
65 |
*
|
66 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
67 |
*/
|
68 |
public class ClipOperation extends GeometryOperation { |
69 |
|
70 |
/**
|
71 |
* Clipping geometry: the convex hull of the
|
72 |
* clipping layer
|
73 |
*/
|
74 |
private Geometry clippingConvexHull = null; |
75 |
|
76 |
public ClipOperation(org.gvsig.fmap.geom.Geometry clip) {
|
77 |
this.clippingConvexHull = GeometryUtil.geomToJTS(clip);
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* clips feature's geometry with the clipping geometry, preserving
|
82 |
* feature's original attributes.
|
83 |
* If feature's geometry doesnt touch clipping geometry, it will be
|
84 |
* ignored.
|
85 |
*/
|
86 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
|
87 |
if(g == null) |
88 |
return lastEditFeature;
|
89 |
|
90 |
Geometry jtsGeom = GeometryUtil.geomToJTS(g); |
91 |
|
92 |
if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
|
93 |
return lastEditFeature;
|
94 |
|
95 |
if(jtsGeom.intersects(clippingConvexHull)) {
|
96 |
try {
|
97 |
Geometry newGeom = jtsGeom.intersection(clippingConvexHull); |
98 |
lastEditFeature = persister.addFeature(feature, newGeom); |
99 |
} catch(com.vividsolutions.jts.geom.TopologyException e){
|
100 |
Sextante.addErrorToLog(e); |
101 |
if(! jtsGeom.isValid()) {
|
102 |
System.out.println("La geometria de entrada no es valida"); |
103 |
jtsGeom = GeometryUtil.removeDuplicatesFrom(jtsGeom); |
104 |
} |
105 |
if(! clippingConvexHull.isValid()) {
|
106 |
System.out.println("La geometria de recorte no es valida"); |
107 |
clippingConvexHull = GeometryUtil.removeDuplicatesFrom(clippingConvexHull); |
108 |
} |
109 |
try {
|
110 |
Geometry newGeom = jtsGeom.intersection(clippingConvexHull); |
111 |
lastEditFeature = persister.addFeature(feature, newGeom); |
112 |
} catch(com.vividsolutions.jts.geom.TopologyException ee){
|
113 |
Sextante.addErrorToLog(ee); |
114 |
} catch (CreateGeometryException ee) {
|
115 |
Sextante.addErrorToLog(ee); |
116 |
} catch (DataException ee) {
|
117 |
Sextante.addErrorToLog(ee); |
118 |
} |
119 |
} catch (CreateGeometryException e) {
|
120 |
Sextante.addErrorToLog(e); |
121 |
} catch (DataException e) {
|
122 |
Sextante.addErrorToLog(e); |
123 |
} |
124 |
} |
125 |
return lastEditFeature;
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* clips feature's geometry with the clipping geometry, preserving
|
130 |
* feature's original attributes.
|
131 |
* If feature's geometry doesnt touch clipping geometry, it will be
|
132 |
* ignored.
|
133 |
*/
|
134 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
135 |
if(g == null) |
136 |
return;
|
137 |
|
138 |
lastEditFeature = feature; |
139 |
|
140 |
Geometry jtsGeom = GeometryUtil.geomToJTS(g); |
141 |
|
142 |
if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
|
143 |
return;
|
144 |
|
145 |
if(jtsGeom.intersects(clippingConvexHull)) {
|
146 |
try {
|
147 |
Geometry newGeom = jtsGeom.intersection(clippingConvexHull); |
148 |
persister.addFeature(feature, newGeom); |
149 |
} catch(com.vividsolutions.jts.geom.TopologyException e){
|
150 |
Sextante.addErrorToLog(e); |
151 |
if(! jtsGeom.isValid()) {
|
152 |
System.out.println("La geometria de entrada no es valida"); |
153 |
jtsGeom = GeometryUtil.removeDuplicatesFrom(jtsGeom); |
154 |
} |
155 |
if(! clippingConvexHull.isValid()) {
|
156 |
System.out.println("La geometria de recorte no es valida"); |
157 |
clippingConvexHull = GeometryUtil.removeDuplicatesFrom(clippingConvexHull); |
158 |
} |
159 |
try {
|
160 |
Geometry newGeom = jtsGeom.intersection(clippingConvexHull); |
161 |
persister.addFeature(feature, newGeom); |
162 |
} catch(com.vividsolutions.jts.geom.TopologyException ee){
|
163 |
Sextante.addErrorToLog(ee); |
164 |
} catch (CreateGeometryException ee) {
|
165 |
Sextante.addErrorToLog(ee); |
166 |
} catch (DataException ee) {
|
167 |
Sextante.addErrorToLog(ee); |
168 |
} |
169 |
} catch (CreateGeometryException e) {
|
170 |
Sextante.addErrorToLog(e); |
171 |
} catch (DataException e) {
|
172 |
Sextante.addErrorToLog(e); |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Ends the edition and closes the FeatureStore
|
179 |
*/
|
180 |
public void end() { |
181 |
persister.end(); |
182 |
} |
183 |
|
184 |
public String getProcessDescription() { |
185 |
return "Clipping features agaisnt a clip geometry"; |
186 |
} |
187 |
} |
188 |
|