svn-gvsig-desktop / tags / v1_1_Build_1009 / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / dissolve / fmap / AdjacencyDissolveVisitor.java @ 12649
History | View | Annotate | Download (9.55 KB)
1 |
/*
|
---|---|
2 |
* Created on 09-may-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: AdjacencyDissolveVisitor.java 12649 2007-07-17 12:20:01Z $
|
47 |
* $Log$
|
48 |
* Revision 1.2.2.1 2007-07-12 09:28:45 azabala
|
49 |
* optimizations in dissolution of buffered features (we dont use strategies to avoid reading of geometries already processed)
|
50 |
*
|
51 |
* Revision 1.2 2006/07/21 11:06:06 azabala
|
52 |
* fixed bug 604: empty dist field in buffered dissolved features
|
53 |
*
|
54 |
* Revision 1.1 2006/06/20 18:20:45 azabala
|
55 |
* first version in cvs
|
56 |
*
|
57 |
* Revision 1.1 2006/05/24 21:11:14 azabala
|
58 |
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
|
59 |
*
|
60 |
*
|
61 |
*/
|
62 |
package com.iver.cit.gvsig.geoprocess.impl.dissolve.fmap; |
63 |
|
64 |
import java.awt.geom.Rectangle2D; |
65 |
import java.util.List; |
66 |
import java.util.Stack; |
67 |
|
68 |
import org.cresques.cts.ICoordTrans; |
69 |
|
70 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
71 |
import com.hardcode.gdbms.engine.values.DoubleValue; |
72 |
import com.hardcode.gdbms.engine.values.Value; |
73 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
74 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
75 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
76 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
77 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
78 |
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException; |
79 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor; |
80 |
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes; |
81 |
import com.iver.utiles.swing.threads.CancellableMonitorable; |
82 |
import com.vividsolutions.jts.geom.Geometry; |
83 |
import com.vividsolutions.jts.geom.GeometryCollection; |
84 |
import com.vividsolutions.jts.geom.GeometryFactory; |
85 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
86 |
|
87 |
/**
|
88 |
* <p>
|
89 |
* This Visitor generates a dissolve of input layer based in adjacency criteria:
|
90 |
* it dissolves two polygons, if and only if they are adjacent.
|
91 |
* </p>
|
92 |
* TODO: To dissolve buffers from radial rings create a DissolveVisitor
|
93 |
* to compare FROM-TO Fields.
|
94 |
*
|
95 |
* @author azabala
|
96 |
*
|
97 |
*/
|
98 |
public class AdjacencyDissolveVisitor extends DissolveVisitor { |
99 |
|
100 |
/**
|
101 |
* Reference to the buffer distance of the visited buffered feature.
|
102 |
*/
|
103 |
private DoubleValue currentBufferDistance = null; |
104 |
|
105 |
/**
|
106 |
* FIXME REFACTOR THIS!!!!
|
107 |
* This is a workaround to avoid the use of strategies
|
108 |
* and allow the cancelation
|
109 |
*
|
110 |
* To optimize disolution of buffers, we need to avoid the reading
|
111 |
* of geometries that also has been processed. Strategies cant do this
|
112 |
*
|
113 |
*/
|
114 |
CancellableMonitorable cancelMonitor = null;
|
115 |
|
116 |
// FIXME Probe to optimize the union of the features
|
117 |
//(buffer + dissolve when applies to almost all features of the layer
|
118 |
//is very inefficient
|
119 |
private Geometry geometry;
|
120 |
|
121 |
|
122 |
public AdjacencyDissolveVisitor(String dissolveField, |
123 |
FeatureProcessor processor) { |
124 |
super(dissolveField, processor);
|
125 |
} |
126 |
|
127 |
//FIXME REFACTOR THIS!!!
|
128 |
public void setCancelMonitor(CancellableMonitorable cancelMonitor){ |
129 |
this.cancelMonitor = cancelMonitor;
|
130 |
} |
131 |
|
132 |
protected boolean verifyIfDissolve(DissolvedFeature f1, DissolvedFeature f2) { |
133 |
Geometry geo1 = f1.getJtsGeometry(); |
134 |
Geometry geo2 = f2.getJtsGeometry(); |
135 |
return geo1.intersects(geo2);
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Creates a new IFeature with util info for dissolve geoprocess
|
140 |
* (it ignore non numerical values, etc)
|
141 |
*
|
142 |
* @param g
|
143 |
* @param index
|
144 |
* @return
|
145 |
* @throws DriverException
|
146 |
*/
|
147 |
protected DissolvedFeature createFeature(IGeometry g, int index) |
148 |
throws DriverException {
|
149 |
DissolvedFeature solution = new DissolvedFeature(g, null, index); |
150 |
return solution;
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* We overwrite this method because we are not interested in save sumarization
|
155 |
* function values with dissolved features.
|
156 |
* Instead, we want to add buffer distance like an attribute of them.
|
157 |
*/
|
158 |
public void visit(IGeometry g, int index) throws VisitException { |
159 |
if(g == null) |
160 |
return;
|
161 |
if(g.getGeometryType() != XTypes.POLYGON &&
|
162 |
g.getGeometryType() != XTypes.MULTI) |
163 |
return;
|
164 |
if (!dissolvedGeometries.get(index)) {
|
165 |
try {
|
166 |
int fieldIndex = recordset.getFieldIndexByName("DIST"); |
167 |
currentBufferDistance = (DoubleValue) recordset.getFieldValue(index, fieldIndex); |
168 |
// if we havent dissolved this feature
|
169 |
Stack toDissol = new Stack();// stack for adjacent features |
170 |
DissolvedFeature feature = createFeature(g, index); |
171 |
toDissol.push(feature); |
172 |
|
173 |
// ArrayList geometries = new ArrayList();
|
174 |
|
175 |
Value[] values = dissolveGeometries(toDissol/*, geometries*/); |
176 |
|
177 |
// Geometry geometry = union(geometries);
|
178 |
|
179 |
Value[] valuesWithFID = new Value[values.length + 1]; |
180 |
System.arraycopy(values, 0, valuesWithFID, 1, values.length); |
181 |
valuesWithFID[0] = ValueFactory.createValue(fid);
|
182 |
DissolvedFeature dissolved = new DissolvedFeature(null,valuesWithFID, fid/*index*/); |
183 |
dissolved.setJtsGeometry(geometry); |
184 |
this.featureProcessor.processFeature(dissolved);
|
185 |
fid++; |
186 |
resetFunctions(); |
187 |
} catch (DriverException e) {
|
188 |
throw new VisitException( |
189 |
"Error al procesar las geometrias a fusionar durante dissolve");
|
190 |
} catch (com.iver.cit.gvsig.fmap.DriverException e) {
|
191 |
throw new VisitException( |
192 |
"Error al procesar las geometrias a fusionar durante dissolve");
|
193 |
} |
194 |
}// if
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* We overwrite this method to ignore sumarization values and to
|
199 |
* add buffer distance to the attributes of the result features.
|
200 |
*/
|
201 |
protected Value[] dissolveGeometries(Stack toDissol/*, |
202 |
List geometries*/) throws com.iver.cit.gvsig.fmap.DriverException, |
203 |
VisitException { |
204 |
|
205 |
IndividualGeometryDissolveVisitor visitor = null;
|
206 |
DissolvedFeature feature = null;
|
207 |
while (toDissol.size() != 0) { |
208 |
feature = (DissolvedFeature) toDissol.pop(); |
209 |
|
210 |
|
211 |
// flags this idx (to not to process in future)
|
212 |
dissolvedGeometries.set(feature.getIndex()); |
213 |
|
214 |
if (visitor == null) { |
215 |
visitor = new IndividualGeometryDissolveVisitor(feature,
|
216 |
dissolvedGeometries, toDissol, |
217 |
numericField_sumarizeFunction); |
218 |
visitor.setDissolveField(this.dissolveField);
|
219 |
} else {
|
220 |
visitor.setProcessedFeature(feature); |
221 |
} |
222 |
|
223 |
Rectangle2D bounds = feature.getGeometry().getBounds2D();
|
224 |
double xmin = bounds.getMinX();
|
225 |
double ymin = bounds.getMinY();
|
226 |
double xmax = bounds.getMaxX();
|
227 |
double ymax = bounds.getMaxY();
|
228 |
double magnify = 15d; |
229 |
Rectangle2D query = new Rectangle2D.Double(xmin - magnify, ymin |
230 |
- magnify, (xmax - xmin) + magnify, (ymax - ymin) + magnify); |
231 |
|
232 |
if (dissolvedLayer.getISpatialIndex() == null) { |
233 |
strategy.process(visitor, query); |
234 |
}else{
|
235 |
process(visitor, query); |
236 |
} |
237 |
|
238 |
//al final de toda la pila de llamadas recursivas,
|
239 |
//geometries tendr? todas las geometrias que debemos dissolver
|
240 |
// geometries.add(feature.getJtsGeometry());
|
241 |
|
242 |
if(geometry == null){ |
243 |
geometry = feature.getJtsGeometry(); |
244 |
}else{
|
245 |
GeometryFactory factory = geometry.getFactory(); |
246 |
Geometry[] geoms = new Geometry[]{geometry, feature.getJtsGeometry()}; |
247 |
GeometryCollection collection = factory.createGeometryCollection(geoms); |
248 |
geometry = EnhancedPrecisionOp.buffer(collection, 0d);
|
249 |
} |
250 |
|
251 |
|
252 |
|
253 |
}// while
|
254 |
Value[] values = new Value[1]; |
255 |
values[0] = currentBufferDistance;
|
256 |
return values;
|
257 |
} |
258 |
|
259 |
void process(IndividualGeometryDissolveVisitor visitor, Rectangle2D query){ |
260 |
if (visitor.start(dissolvedLayer)) {
|
261 |
ReadableVectorial va = dissolvedLayer.getSource(); |
262 |
ICoordTrans ct = dissolvedLayer.getCoordTrans(); |
263 |
List lstRecs = dissolvedLayer.getISpatialIndex().query(query);
|
264 |
Integer idRec;
|
265 |
int index;
|
266 |
try {
|
267 |
va.start(); |
268 |
DriverAttributes attr = va.getDriverAttributes(); |
269 |
boolean bMustClone = false; |
270 |
if (attr != null) { |
271 |
if (attr.isLoadedInMemory()) {
|
272 |
bMustClone = attr.isLoadedInMemory(); |
273 |
} |
274 |
} |
275 |
|
276 |
for (int i = 0; i < lstRecs.size(); i++) { |
277 |
if(cancelMonitor != null){ |
278 |
if(cancelMonitor.isCanceled())
|
279 |
return;
|
280 |
} |
281 |
idRec = (Integer) lstRecs.get(i);
|
282 |
index = idRec.intValue(); |
283 |
if(getDissolvedGeometries().get(index))
|
284 |
continue;
|
285 |
|
286 |
IGeometry geom = va.getShape(index); |
287 |
if (geom == null)// azabala |
288 |
continue;
|
289 |
if (ct != null) { |
290 |
if (bMustClone)
|
291 |
geom = geom.cloneGeometry(); |
292 |
geom.reProject(ct); |
293 |
} |
294 |
if (geom.intersects(query))
|
295 |
visitor.visit(geom, index); |
296 |
}// for
|
297 |
va.stop(); |
298 |
} catch (DriverIOException e) {
|
299 |
// TODO Auto-generated catch block
|
300 |
e.printStackTrace(); |
301 |
} catch (VisitException e) {
|
302 |
// TODO Auto-generated catch block
|
303 |
e.printStackTrace(); |
304 |
} |
305 |
}// if visitor.start
|
306 |
|
307 |
visitor.stop(dissolvedLayer); |
308 |
} |
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
} |