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 / ClipVisitor.java @ 25

History | View | Annotate | Download (6.02 KB)

1
/*
2
 * Created on 16-feb-2006
3
 *
4
 * gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib��ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44

    
45
package org.gvsig.sextante.app.algorithm.clip;
46

    
47
import org.gvsig.sextante.app.algorithm.base.util.GeometryUtil;
48
import org.gvsig.sextante.app.algorithm.base.visitor.exception.ProcessVisitorException;
49
import org.gvsig.sextante.app.algorithm.base.visitor.exception.StartVisitorException;
50
import org.gvsig.sextante.app.algorithm.base.visitor.exception.VisitorException;
51

    
52
import com.vividsolutions.jts.geom.Geometry;
53
/**
54
 * This class analyzes all features of a layer, clipping its geometries
55
 * with the convex hull of another layer.
56
 * If the geometry of the feature analyzed doesnt intersect with the convex
57
 * hull geometry, the clipvisitor will ignore it.
58
 *
59
 * It intersects, computes intersection and creates a new feature with
60
 * the same attributes and the new intersection geometry.
61
 * @author azabala
62
 *
63
 */
64
public class ClipVisitor implements FeatureVisitor {
65

    
66
        /**
67
         * Clipping geometry: the convex hull of the
68
         * clipping layer
69
         */
70
        private Geometry clippingConvexHull;
71
        /**
72
         * It allows visitor to get attribute values link to geometries
73
         */
74
        private SelectableDataSource recordset;
75
        /**
76
         * Processes individual clip results
77
         */
78
        private FeaturePersisterProcessor resultProcessor;
79

    
80
        /**
81
         * If its different of null manages user selection (discarting any
82
         * feature else)
83
         */
84
        private FBitSet selection;
85
        /**
86
         * Constructor. It requires clipping geometry.
87
         * @param clippingGeometry
88
         */
89
        public ClipVisitor(Geometry clippingGeometry,
90
                                ITableDefinition layerDefinition,
91
                                ISchemaManager schemaManager,
92
                                IWriter writer){
93

    
94
                this.clippingConvexHull = clippingGeometry;
95
                        try {
96
                                this.resultProcessor = new FeaturePersisterProcessor(layerDefinition,
97
                                                schemaManager,writer);
98
                        } catch (SchemaEditionException e) {
99
                                // TODO Auto-generated catch block
100
                                e.printStackTrace();
101
                        } catch (VisitorException e) {
102
                                // TODO Auto-generated catch block
103
                                e.printStackTrace();
104
                        }
105

    
106
        }
107

    
108
        public void setSelection(FBitSet selection){
109
                this.selection = selection;
110
        }
111

    
112
        /**
113
         * clips feature's geometry with the clipping geometry, preserving
114
         * feature's original attributes.
115
         * If feature's geometry doesnt touch clipping geometry, it will be
116
         * ignored.
117
         */
118
        public void visit(IGeometry g, int index) throws VisitorException, ProcessVisitorException {
119

    
120
                //TODO METER EL PRINCIPIO Y EL FINAL EN EL VISITOR ABSTRACTO
121
                //AS�, TENDRIAMOS LA GESTI�N TOPOL�GICA EN CADA VISITOR
122

    
123

    
124
                if(g == null)
125
                        return;
126
                if(selection != null){
127
                        if(!selection.get(index))
128
                                return;
129
                }
130
                Geometry jtsGeom = g.toJTSGeometry();
131
                if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
132
                        return;
133
                if(jtsGeom.intersects(clippingConvexHull)){
134
                        try{
135
                                Geometry newGeom = jtsGeom.intersection(clippingConvexHull);
136
                                resultProcessor.processJtsGeometry(newGeom, index);
137
                        }catch(com.vividsolutions.jts.geom.TopologyException e){
138
                                e.printStackTrace();
139
                                System.out.println(jtsGeom.toText());
140
                                System.out.println(clippingConvexHull.toText());
141
                                /*
142
                                 * TODO En este caso, la de recorte chequearla al principio
143
                                 * (cuando se calcula) y solo chequearemos las de entrada
144
                                 * conforme nos van viniendo.
145
                                 *
146
                                 * HAY QUE CREAR UN FRAMEWORK DE CORRECCI�N TOPOL�GICA DE
147
                                 * GEOMETRIAS:
148
                                 * -Mirando autointersecciones.
149
                                 * -Mirando duplicados.
150
                                 * -Eliminando colineales.
151
                                 * ETC.
152
                                 * */
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
                                        resultProcessor.processJtsGeometry(newGeom, index);
164
                                }catch(com.vividsolutions.jts.geom.TopologyException ee){
165
                                        ee.printStackTrace();
166
                                } catch (ReadDriverException ee) {
167
                                        // TODO Auto-generated catch block
168
                                        ee.printStackTrace();
169
                                };
170
                        } catch (ReadDriverException e) {
171
                                // TODO Auto-generated catch block
172
                                e.printStackTrace();
173
                        }
174
                }//if
175
        }
176

    
177
        public void stop(FLayer layer) throws VisitorException {
178
                resultProcessor.finish();
179
        }
180

    
181
        public boolean start(FLayer layer) throws StartVisitorException {
182
                if(layer instanceof AlphanumericData && layer instanceof VectorialData){
183
                        try {
184
                                this.recordset = ((AlphanumericData)layer).getRecordset();
185
                                this.resultProcessor.setSelectableDataSource(recordset);
186
                        } catch (ReadDriverException e) {
187
                                return false;
188
                        }
189
                        return true;
190
                }
191
                return false;
192
        }
193

    
194
        public SelectableDataSource getRecordset() {
195
                return recordset;
196
        }
197

    
198
        public void setRecordset(SelectableDataSource recordset) {
199
                this.recordset = recordset;
200
        }
201

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