Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / gvsig / geoprocessing / impl / clip / ClipVisitor.java @ 4447

History | View | Annotate | Download (5.13 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 4447 2006-03-15 18:35:56Z azabala $
47
* $Log$
48
* Revision 1.4  2006-03-15 18:30:39  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.3  2006/03/14 18:32:46  fjp
52
* Cambio con LayerDefinition para que sea compatible con la definici?n de tablas tambi?n.
53
*
54
* Revision 1.2  2006/03/07 21:01:33  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.1  2006/03/06 19:48:39  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.2  2006/02/19 20:56:21  azabala
61
* *** empty log message ***
62
*
63
* Revision 1.1  2006/02/17 16:33:05  azabala
64
* *** empty log message ***
65
*
66
*
67
*/
68
package com.iver.gvsig.geoprocessing.impl.clip;
69

    
70
import com.iver.cit.gvsig.fmap.DriverException;
71
import com.iver.cit.gvsig.fmap.core.IGeometry;
72
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
73
import com.iver.cit.gvsig.fmap.edition.EditionException;
74
import com.iver.cit.gvsig.fmap.edition.ISchemaManager;
75
import com.iver.cit.gvsig.fmap.edition.IWriter;
76
import com.iver.cit.gvsig.fmap.layers.FLayer;
77
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
78
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
79
import com.iver.cit.gvsig.fmap.layers.layerOperations.VectorialData;
80
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
81
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
82
import com.iver.gvsig.geoprocessing.impl.jtsprocessors.FeaturePersisterProcessor;
83
import com.vividsolutions.jts.geom.Geometry;
84
/**
85
 * This class analyzes all features of a layer, clipping its geometries
86
 * with the convex hull of another layer.
87
 * If the geometry of the feature analyzed doesnt intersect with the convex
88
 * hull geometry, the clipvisitor will ignore it.
89
 * 
90
 * It intersects, computes intersection and creates a new feature with
91
 * the same attributes and the new intersection geometry.
92
 * @author azabala
93
 *
94
 */
95
public class ClipVisitor implements FeatureVisitor {
96
        
97
        /**
98
         * Clipping geometry: the convex hull of the
99
         * clipping layer
100
         */
101
        private Geometry clippingConvexHull;
102
        /**
103
         * It allows visitor to get attribute values link to geometries
104
         */
105
        private SelectableDataSource recordset;
106
        /**
107
         * Processes individual clip results
108
         */
109
        private FeaturePersisterProcessor resultProcessor;
110
        /**
111
         * Constructor. It requires clipping geometry.
112
         * @param clippingGeometry
113
         */
114
        public ClipVisitor(Geometry clippingGeometry, 
115
                                ITableDefinition layerDefinition,
116
                                ISchemaManager schemaManager,
117
                                IWriter writer){
118
                
119
                this.clippingConvexHull = clippingGeometry;
120
                try {
121
                        this.resultProcessor = new FeaturePersisterProcessor(layerDefinition,
122
                                        schemaManager,writer);
123
                } catch (EditionException e) {
124
                        // TODO Auto-generated catch block
125
                        e.printStackTrace();
126
                }
127
                
128
        }
129
        
130
        /**
131
         * clips feature's geometry with the clipping geometry, preserving
132
         * feature's original attributes.
133
         * If feature's geometry doesnt touch clipping geometry, it will be
134
         * ignored.
135
         */
136
        public void visit(IGeometry g, int index) throws VisitException {
137
                Geometry jtsGeom = g.toJTSGeometry();
138
                //first of all we check envelopes
139
                if(!jtsGeom.getEnvelope().intersects(clippingConvexHull.getEnvelope()))
140
                        return;
141
                
142
                if(jtsGeom.intersects(clippingConvexHull)){
143
                        Geometry newGeom = jtsGeom.intersection(clippingConvexHull);
144
                        resultProcessor.processJtsGeometry(newGeom, index);
145
                }
146
        }
147

    
148
        public void stop(FLayer layer) {
149
                resultProcessor.finish();
150
        }
151

    
152
        public boolean start(FLayer layer) {
153
                if(layer instanceof AlphanumericData && layer instanceof VectorialData){
154
                        try {
155
                                this.recordset = ((AlphanumericData)layer).getRecordset();
156
                                this.resultProcessor.setSelectableDataSource(recordset);
157
                        } catch (DriverException e) {
158
                                return false;//must we throw an Exception??
159
                        }
160
                        return true;
161
                }
162
                return false;
163
        }
164

    
165
        public SelectableDataSource getRecordset() {
166
                return recordset;
167
        }
168

    
169
        public void setRecordset(SelectableDataSource recordset) {
170
                this.recordset = recordset;
171
        }
172
        
173
        public String getProcessDescription() {
174
                return "Clipping features agaisnt a clip geometry";
175
        }
176
}
177