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 @ 28

History | View | Annotate | Download (5.85 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.util.Converter;
30
import org.gvsig.sextante.app.algorithm.base.core.DALFeaturePersister;
31
import org.gvsig.sextante.app.algorithm.base.util.GeometryUtil;
32

    
33
import com.vividsolutions.jts.geom.Geometry;
34

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

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

    
59
        public ClipOperation(org.gvsig.fmap.geom.Geometry clip) {
60
                this.clippingConvexHull = Converter.geometryToJts(clip);
61
        }
62
        
63
        /**
64
         * Sets the output FeatureType
65
         * @param out
66
         * @throws DataException 
67
         */
68
        public void setFeatureStore(FeatureStore out, String[] attrNames) throws DataException {
69
                persister = new DALFeaturePersister(out, attrNames);
70
        }
71

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

    
174
        public String getProcessDescription() {
175
                return "Clipping features agaisnt a clip geometry";
176
        }
177
}
178