Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / clip / fmap / ClipVisitor.java @ 10626

History | View | Annotate | Download (8.11 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
/* CVS MESSAGES:
45
*
46
* $Id: ClipVisitor.java 10626 2007-03-06 16:55:54Z caballero $
47
* $Log$
48
* Revision 1.3  2007-03-06 16:47:58  caballero
49
* Exceptions
50
*
51
* Revision 1.2  2006/07/21 09:10:34  azabala
52
* fixed bug 608: user doesnt enter any result file to the geoprocess panel
53
*
54
* Revision 1.1  2006/06/20 18:20:45  azabala
55
* first version in cvs
56
*
57
* Revision 1.4  2006/06/08 18:20:23  azabala
58
* optimizaciones: se usa consulta espacial para solo chequear los elementos de la primera capa que intersecten con la capa de recorte
59
*
60
* Revision 1.3  2006/06/02 18:21:28  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.2  2006/05/31 09:10:12  fjp
64
* Ubicaci?n de IWriter
65
*
66
* Revision 1.1  2006/05/24 21:14:07  azabala
67
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
68
*
69
* Revision 1.6  2006/03/28 16:27:36  azabala
70
* *** empty log message ***
71
*
72
* Revision 1.5  2006/03/17 19:52:43  azabala
73
* *** empty log message ***
74
*
75
* Revision 1.4  2006/03/15 18:30:39  azabala
76
* *** empty log message ***
77
*
78
* Revision 1.3  2006/03/14 18:32:46  fjp
79
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
80
*
81
* Revision 1.2  2006/03/07 21:01:33  azabala
82
* *** empty log message ***
83
*
84
* Revision 1.1  2006/03/06 19:48:39  azabala
85
* *** empty log message ***
86
*
87
* Revision 1.2  2006/02/19 20:56:21  azabala
88
* *** empty log message ***
89
*
90
* Revision 1.1  2006/02/17 16:33:05  azabala
91
* *** empty log message ***
92
*
93
*
94
*/
95
package com.iver.cit.gvsig.geoprocess.impl.clip.fmap;
96

    
97
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
98
import com.hardcode.gdbms.driver.exceptions.SchemaEditionException;
99
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException;
100
import com.iver.cit.gvsig.exceptions.visitors.StartVisitorException;
101
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
102
import com.iver.cit.gvsig.fmap.core.IGeometry;
103
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
104
import com.iver.cit.gvsig.fmap.edition.ISchemaManager;
105
import com.iver.cit.gvsig.fmap.edition.IWriter;
106
import com.iver.cit.gvsig.fmap.layers.FBitSet;
107
import com.iver.cit.gvsig.fmap.layers.FLayer;
108
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
109
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
110
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
111
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
112
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor;
113
import com.iver.cit.gvsig.geoprocess.core.util.GeometryUtil;
114
import com.vividsolutions.jts.geom.Geometry;
115
/**
116
 * This class analyzes all features of a layer, clipping its geometries
117
 * with the convex hull of another layer.
118
 * If the geometry of the feature analyzed doesnt intersect with the convex
119
 * hull geometry, the clipvisitor will ignore it.
120
 *
121
 * It intersects, computes intersection and creates a new feature with
122
 * the same attributes and the new intersection geometry.
123
 * @author azabala
124
 *
125
 */
126
public class ClipVisitor implements FeatureVisitor {
127

    
128
        /**
129
         * Clipping geometry: the convex hull of the
130
         * clipping layer
131
         */
132
        private Geometry clippingConvexHull;
133
        /**
134
         * It allows visitor to get attribute values link to geometries
135
         */
136
        private SelectableDataSource recordset;
137
        /**
138
         * Processes individual clip results
139
         */
140
        private FeaturePersisterProcessor resultProcessor;
141

    
142
        /**
143
         * If its different of null manages user selection (discarting any
144
         * feature else)
145
         */
146
        private FBitSet selection;
147
        /**
148
         * Constructor. It requires clipping geometry.
149
         * @param clippingGeometry
150
         */
151
        public ClipVisitor(Geometry clippingGeometry,
152
                                ITableDefinition layerDefinition,
153
                                ISchemaManager schemaManager,
154
                                IWriter writer){
155

    
156
                this.clippingConvexHull = clippingGeometry;
157
                        try {
158
                                this.resultProcessor = new FeaturePersisterProcessor(layerDefinition,
159
                                                schemaManager,writer);
160
                        } catch (SchemaEditionException e) {
161
                                // TODO Auto-generated catch block
162
                                e.printStackTrace();
163
                        } catch (VisitorException e) {
164
                                // TODO Auto-generated catch block
165
                                e.printStackTrace();
166
                        }
167

    
168
        }
169

    
170
        public void setSelection(FBitSet selection){
171
                this.selection = selection;
172
        }
173

    
174
        /**
175
         * clips feature's geometry with the clipping geometry, preserving
176
         * feature's original attributes.
177
         * If feature's geometry doesnt touch clipping geometry, it will be
178
         * ignored.
179
         */
180
        public void visit(IGeometry g, int index) throws VisitorException, ProcessVisitorException {
181

    
182
                //TODO METER EL PRINCIPIO Y EL FINAL EN EL VISITOR ABSTRACTO
183
                //AS?, TENDRIAMOS LA GESTI?N TOPOL?GICA EN CADA VISITOR
184

    
185

    
186
                if(g == null)
187
                        return;
188
                if(selection != null){
189
                        if(!selection.get(index))
190
                                return;
191
                }
192
                Geometry jtsGeom = g.toJTSGeometry();
193
                if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
194
                        return;
195
                if(jtsGeom.intersects(clippingConvexHull)){
196
                        try{
197
                                Geometry newGeom = jtsGeom.intersection(clippingConvexHull);
198
                                resultProcessor.processJtsGeometry(newGeom, index);
199
                        }catch(com.vividsolutions.jts.geom.TopologyException e){
200
                                e.printStackTrace();
201
                                System.out.println(jtsGeom.toText());
202
                                System.out.println(clippingConvexHull.toText());
203
                                /*
204
                                 * TODO En este caso, la de recorte chequearla al principio
205
                                 * (cuando se calcula) y solo chequearemos las de entrada
206
                                 * conforme nos van viniendo.
207
                                 *
208
                                 * HAY QUE CREAR UN FRAMEWORK DE CORRECCI?N TOPOL?GICA DE
209
                                 * GEOMETRIAS:
210
                                 * -Mirando autointersecciones.
211
                                 * -Mirando duplicados.
212
                                 * -Eliminando colineales.
213
                                 * ETC.
214
                                 * */
215
                                if(! jtsGeom.isValid()){
216
                                        System.out.println("La geometria de entrada no es valida");
217
                                        jtsGeom = GeometryUtil.removeDuplicatesFrom(jtsGeom);
218
                                }
219
                                if(! clippingConvexHull.isValid()){
220
                                        System.out.println("La geometria de recorte no es valida");
221
                                        clippingConvexHull = GeometryUtil.removeDuplicatesFrom(clippingConvexHull);
222
                                }
223
                                try{
224
                                        Geometry newGeom = jtsGeom.intersection(clippingConvexHull);
225
                                        resultProcessor.processJtsGeometry(newGeom, index);
226
                                }catch(com.vividsolutions.jts.geom.TopologyException ee){
227
                                        ee.printStackTrace();
228
                                } catch (ReadDriverException ee) {
229
                                        // TODO Auto-generated catch block
230
                                        ee.printStackTrace();
231
                                };
232
                        } catch (ReadDriverException e) {
233
                                // TODO Auto-generated catch block
234
                                e.printStackTrace();
235
                        }
236
                }//if
237
        }
238

    
239
        public void stop(FLayer layer) throws VisitorException {
240
                resultProcessor.finish();
241
        }
242

    
243
        public boolean start(FLayer layer) throws StartVisitorException {
244
                if(layer instanceof AlphanumericData && layer instanceof VectorialData){
245
                        try {
246
                                this.recordset = ((AlphanumericData)layer).getRecordset();
247
                                this.resultProcessor.setSelectableDataSource(recordset);
248
                        } catch (ReadDriverException e) {
249
                                return false;
250
                        }
251
                        return true;
252
                }
253
                return false;
254
        }
255

    
256
        public SelectableDataSource getRecordset() {
257
                return recordset;
258
        }
259

    
260
        public void setRecordset(SelectableDataSource recordset) {
261
                this.recordset = recordset;
262
        }
263

    
264
        public String getProcessDescription() {
265
                return "Clipping features agaisnt a clip geometry";
266
        }
267
}
268