Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.clip / src / main / java / org / gvsig / sextante / app / algorithm / clip / ClipOperation.java @ 30

History | View | Annotate | Download (6.65 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.clip;
23

    
24
import org.gvsig.fmap.dal.exception.DataException;
25
import org.gvsig.fmap.dal.feature.EditableFeature;
26
import org.gvsig.fmap.dal.feature.Feature;
27
import org.gvsig.fmap.dal.feature.FeatureStore;
28
import org.gvsig.fmap.geom.exception.CreateGeometryException;
29
import org.gvsig.fmap.geom.operation.GeometryOperationException;
30
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
31
import org.gvsig.fmap.geom.operation.tojts.ToJTS;
32
import org.gvsig.sextante.app.algorithm.base.core.DALFeaturePersister;
33
import org.gvsig.sextante.app.algorithm.base.util.GeometryUtil;
34

    
35
import com.vividsolutions.jts.geom.Geometry;
36

    
37
import es.unex.sextante.core.Sextante;
38
/**
39
 * This class analyzes all features of a layer, clipping its geometries
40
 * with the convex hull of another layer.
41
 * If the geometry of the feature analyzed doesnt intersect with the convex
42
 * hull geometry, the clipvisitor will ignore it.
43
 *
44
 * It intersects, computes intersection and creates a new feature with
45
 * the same attributes and the new intersection geometry.
46
 * 
47
 * @author Nacho Brodin (nachobrodin@gmail.com)
48
 */
49
public class ClipOperation {
50

    
51
        /**
52
         * Clipping geometry: the convex hull of the
53
         * clipping layer
54
         */
55
        private Geometry                         clippingConvexHull   = null;
56
        private DALFeaturePersister              persister            = null;
57
        private EditableFeature                  lastEditFeature      = null;
58

    
59
        public ClipOperation(org.gvsig.fmap.geom.Geometry clip) {
60
                try {
61
                        this.clippingConvexHull = (Geometry)clip.invokeOperation(ToJTS.CODE, null);
62
                } catch (GeometryOperationNotSupportedException e1) {
63
                        Sextante.addErrorToLog(e1);
64
                } catch (GeometryOperationException e1) {
65
                        Sextante.addErrorToLog(e1);
66
                }
67
        }
68
        
69
        /**
70
         * Sets the output FeatureType
71
         * @param out
72
         * @throws DataException 
73
         */
74
        public void setFeatureStore(FeatureStore out, String[] attrNames) throws DataException {
75
                persister = new DALFeaturePersister(out, attrNames);
76
        }
77

    
78
        /**
79
         * clips feature's geometry with the clipping geometry, preserving
80
         * feature's original attributes.
81
         * If feature's geometry doesnt touch clipping geometry, it will be
82
         * ignored.
83
         */
84
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
85
                if(g == null)
86
                        return lastEditFeature;
87
                
88
                Geometry jtsGeom = null;
89
                try {
90
                        jtsGeom = (Geometry)g.invokeOperation(ToJTS.CODE, null);
91
                } catch (GeometryOperationNotSupportedException e1) {
92
                        Sextante.addErrorToLog(e1);
93
                        return null;
94
                } catch (GeometryOperationException e1) {
95
                        Sextante.addErrorToLog(e1);
96
                        return null;
97
                }
98
                
99
                if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
100
                        return lastEditFeature;
101
                
102
                if(jtsGeom.intersects(clippingConvexHull)) {
103
                        try {
104
                                Geometry newGeom = jtsGeom.intersection(clippingConvexHull);
105
                                lastEditFeature = persister.addFeature(feature, newGeom);
106
                        } catch(com.vividsolutions.jts.geom.TopologyException e){
107
                                Sextante.addErrorToLog(e);
108
                                if(! jtsGeom.isValid()) {
109
                                        System.out.println("La geometria de entrada no es valida");
110
                                        jtsGeom = GeometryUtil.removeDuplicatesFrom(jtsGeom);
111
                                }
112
                                if(! clippingConvexHull.isValid()) {
113
                                        System.out.println("La geometria de recorte no es valida");
114
                                        clippingConvexHull = GeometryUtil.removeDuplicatesFrom(clippingConvexHull);
115
                                }
116
                                try {
117
                                        Geometry newGeom = jtsGeom.intersection(clippingConvexHull);
118
                                        lastEditFeature = persister.addFeature(feature, newGeom);
119
                                } catch(com.vividsolutions.jts.geom.TopologyException ee){
120
                                        Sextante.addErrorToLog(ee);
121
                                } catch (CreateGeometryException ee) {
122
                                        Sextante.addErrorToLog(ee);
123
                                } catch (DataException ee) {
124
                                        Sextante.addErrorToLog(ee);
125
                                }
126
                        } catch (CreateGeometryException e) {
127
                                Sextante.addErrorToLog(e);
128
                        } catch (DataException e) {
129
                                Sextante.addErrorToLog(e);
130
                        }
131
                }
132
                return lastEditFeature;
133
        }
134
        
135
        /**
136
         * clips feature's geometry with the clipping geometry, preserving
137
         * feature's original attributes.
138
         * If feature's geometry doesnt touch clipping geometry, it will be
139
         * ignored.
140
         */
141
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
142
                if(g == null)
143
                        return;
144
                
145
                lastEditFeature = feature;
146
                
147
                Geometry jtsGeom = null;
148
                try {
149
                        jtsGeom = (Geometry)g.invokeOperation(ToJTS.CODE, null);
150
                } catch (GeometryOperationNotSupportedException e1) {
151
                        Sextante.addErrorToLog(e1);
152
                        return;
153
                } catch (GeometryOperationException e1) {
154
                        Sextante.addErrorToLog(e1);
155
                        return;
156
                }
157
                
158
                if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
159
                        return;
160
                
161
                if(jtsGeom.intersects(clippingConvexHull)) {
162
                        try {
163
                                Geometry newGeom = jtsGeom.intersection(clippingConvexHull);
164
                                persister.addFeature(feature, newGeom);
165
                        } catch(com.vividsolutions.jts.geom.TopologyException e){
166
                                Sextante.addErrorToLog(e);
167
                                if(! jtsGeom.isValid()) {
168
                                        System.out.println("La geometria de entrada no es valida");
169
                                        jtsGeom = GeometryUtil.removeDuplicatesFrom(jtsGeom);
170
                                }
171
                                if(! clippingConvexHull.isValid()) {
172
                                        System.out.println("La geometria de recorte no es valida");
173
                                        clippingConvexHull = GeometryUtil.removeDuplicatesFrom(clippingConvexHull);
174
                                }
175
                                try {
176
                                        Geometry newGeom = jtsGeom.intersection(clippingConvexHull);
177
                                        persister.addFeature(feature, newGeom);
178
                                } catch(com.vividsolutions.jts.geom.TopologyException ee){
179
                                        Sextante.addErrorToLog(ee);
180
                                } catch (CreateGeometryException ee) {
181
                                        Sextante.addErrorToLog(ee);
182
                                } catch (DataException ee) {
183
                                        Sextante.addErrorToLog(ee);
184
                                }
185
                        } catch (CreateGeometryException e) {
186
                                Sextante.addErrorToLog(e);
187
                        } catch (DataException e) {
188
                                Sextante.addErrorToLog(e);
189
                        }
190
                }
191
        }
192
        
193
        /**
194
         * Ends the edition and closes the FeatureStore
195
         */
196
        public void end() {
197
                persister.end();
198
        }
199

    
200
        public String getProcessDescription() {
201
                return "Clipping features agaisnt a clip geometry";
202
        }
203
}
204