Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / dissolve / fmap / AlphanumericDissolveVisitor.java @ 5628

History | View | Annotate | Download (5.16 KB)

1
/*
2
 * Created on 26-abr-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: AlphanumericDissolveVisitor.java 5628 2006-06-02 18:21:28Z azabala $
47
 * $Log$
48
 * Revision 1.2  2006-06-02 18:21:28  azabala
49
 * *** empty log message ***
50
 *
51
 * Revision 1.1  2006/05/24 21:11:14  azabala
52
 * primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
53
 *
54
 * Revision 1.2  2006/05/08 15:37:26  azabala
55
 * added alphanumeric dissolve
56
 *
57
 * Revision 1.1  2006/05/01 19:21:50  azabala
58
 * primera version en cvs (todav?a no funciona)
59
 *
60
 *
61
 */
62
package com.iver.cit.gvsig.geoprocess.dissolve.fmap;
63

    
64
import java.util.List;
65
import java.util.Map;
66
import java.util.Stack;
67

    
68
import com.hardcode.gdbms.engine.data.driver.DriverException;
69
import com.hardcode.gdbms.engine.values.Value;
70
import com.iver.cit.gvsig.fmap.core.IGeometry;
71
import com.iver.cit.gvsig.fmap.layers.FBitSet;
72
import com.iver.cit.gvsig.fmap.layers.FLayer;
73
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
74
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor;
75
import com.vividsolutions.jts.geom.Geometry;
76

    
77
/**
78
 * <p>
79
 * This Visitor generates a dissolve of input layer based
80
 * in alphanumeric criteria: it dissolves two polygons with the same
81
 * dissolve attribute value, and doesnt care if they are adjacent or not.
82
 * </p>
83
 * @author azabala
84
 *
85
 */
86
public class AlphanumericDissolveVisitor extends DissolveVisitor {
87

    
88
        public AlphanumericDissolveVisitor(String dissolveField,
89
                        FeatureProcessor processor) {
90
                super(dissolveField, processor);
91
        }
92

    
93
        protected boolean verifyIfDissolve(DissolvedFeature f1, DissolvedFeature f2) {
94
                // dissolveField is the last
95
                int fieldIndex = 0;
96
                if(numericField_sumarizeFunction != null)
97
                        fieldIndex = numericField_sumarizeFunction.keySet().size();
98
                Value adjacentVal = f1.getAttribute(fieldIndex);
99
                Value val = f2.getAttribute(fieldIndex);
100
                if (adjacentVal.doEquals(val)) {
101
                        return true;
102
                }// if val equals
103

    
104
                return false;
105
        }
106
        
107
        class IndividualAlphaVisitor extends IndividualGeometryDissolveVisitor{
108
                protected IndividualAlphaVisitor(DissolvedFeature feature, FBitSet dissolvedFeatures, Stack featuresToDissolve, Map fields_sumarize) {
109
                        super(feature, dissolvedFeatures, featuresToDissolve, fields_sumarize);
110
                }
111
                
112
                public void visit(IGeometry g, int index) throws VisitException {
113
                        if(g == null)
114
                                return;
115
                        if (index == feature.getIndex())
116
                                return;
117
                        // have we dissolved this feature yet?
118
                        if (dissolvedFeatures.get(index))
119
                                return;
120
                        try {
121
                                DissolvedFeature adjacentFeature = createFeature(g, index);
122
                                Geometry jtsGeo = g.toJTSGeometry();
123
                                adjacentFeature.setJtsGeometry(jtsGeo);
124
                                if (verifyIfDissolve(feature, adjacentFeature)) {
125
                                        dissolvedFeatures.set(index);
126
                                        // we actualize geometry by unioning
127
                                        featuresToDissolve.push(adjacentFeature);
128
                                        applySumarizeFunction(index);
129
                                }
130
                        } catch (DriverException e) {
131
                                throw new VisitException(
132
                                                "Error al cargar los pol?gonos adyacentes durante un dissolve");
133
                        }        
134
                }
135
                public String getProcessDescription() {
136
                        return "";
137
                }
138
                public void stop(FLayer layer) {
139
                }
140
                public boolean start(FLayer layer) {
141
                        return true;
142
                }
143
        }
144
        
145
        protected Value[] dissolveGeometries(Stack toDissol,
146
                        List geometries) throws com.iver.cit.gvsig.fmap.DriverException,
147
                        VisitException {
148
                IndividualAlphaVisitor visitor = null;
149
                DissolvedFeature feature = null;
150
                while (toDissol.size() != 0) {
151
                        feature = (DissolvedFeature) toDissol.pop();
152
                        // flags this idx (to not to process in future)
153
                        dissolvedGeometries.set(feature.getIndex());
154
                        if (visitor == null) {
155
                                visitor = new IndividualAlphaVisitor(feature,
156
                                                dissolvedGeometries, toDissol,
157
                                                numericField_sumarizeFunction);
158
                                visitor.setDissolveField(this.dissolveField);
159
                        } else {
160
                                visitor.setProcessedFeature(feature);
161
                        }
162
                        strategy.process(visitor);
163
                        //al final de toda la pila de llamadas recursivas, 
164
                        //geometries tendr? todas las geometrias que debemos dissolver
165
                        geometries.add(feature.getJtsGeometry());
166
                }// while
167
                Value[] values = visitor.getSumarizedValues2();
168
                return values;
169
        }
170

    
171
}