svn-gvsig-desktop / tags / PilotoRedes_Build_1 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / TopologyExtension.java @ 11097
History | View | Annotate | Download (12.5 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.graph; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.geom.Rectangle2D; |
45 |
import java.util.Collection; |
46 |
import java.util.Iterator; |
47 |
import java.util.Set; |
48 |
|
49 |
import com.iver.andami.PluginServices; |
50 |
import com.iver.andami.messages.NotificationManager; |
51 |
import com.iver.andami.plugins.Extension; |
52 |
import com.iver.andami.ui.mdiManager.IWindow; |
53 |
import com.iver.cit.gvsig.fmap.DriverException; |
54 |
import com.iver.cit.gvsig.fmap.MapControl; |
55 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
56 |
import com.iver.cit.gvsig.fmap.core.FShape; |
57 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
58 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
59 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
60 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
61 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
62 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
63 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
64 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
65 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
66 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
67 |
import com.iver.cit.gvsig.fmap.layers.GraphicLayer; |
68 |
import com.iver.cit.gvsig.fmap.rendering.FGraphic; |
69 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
70 |
import com.iver.cit.gvsig.project.documents.view.toc.FLyrVectEditPropertiesTocMenuEntry2; |
71 |
import com.iver.cit.gvsig.topology.TopologyBuilder; |
72 |
import com.iver.cit.gvsig.topology.triangulation.DelaunayTriangulation; |
73 |
import com.iver.cit.gvsig.topology.triangulation.Pnt; |
74 |
import com.iver.cit.gvsig.topology.triangulation.Simplex; |
75 |
import com.iver.utiles.extensionPoints.ExtensionPoint; |
76 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
77 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
78 |
import com.vividsolutions.jts.geom.Coordinate; |
79 |
import com.vividsolutions.jts.geom.CoordinateArrays; |
80 |
import com.vividsolutions.jts.geom.Geometry; |
81 |
import com.vividsolutions.jts.geom.LineString; |
82 |
import com.vividsolutions.jts.geom.Polygon; |
83 |
import com.vividsolutions.jts.planargraph.Node; |
84 |
import com.vividsolutions.jts.planargraph.NodeMap; |
85 |
|
86 |
/**
|
87 |
* @author fjp Primera prueba acerca de la creaci?n de pol?gonos a partir de una
|
88 |
* capa de l?neas
|
89 |
*
|
90 |
*/
|
91 |
public class TopologyExtension extends Extension { |
92 |
|
93 |
private class MyNode extends Node { |
94 |
public MyNode(Coordinate pt) {
|
95 |
super(pt);
|
96 |
occurrences = 1;
|
97 |
} |
98 |
|
99 |
int occurrences;
|
100 |
|
101 |
public int getOccurrences() { |
102 |
return occurrences;
|
103 |
} |
104 |
|
105 |
public void setOccurrences(int occurrences) { |
106 |
this.occurrences = occurrences;
|
107 |
} |
108 |
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
113 |
*/
|
114 |
public void initialize() { |
115 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
116 |
((ExtensionPoint) extensionPoints.get("View_TocActions")).put("FLyrVectEditProperties",new FLyrVectEditPropertiesTocMenuEntry2()); |
117 |
//extensionPoints.add("View_TocActions","FLyrVectEditProperties2",new FLyrVectEditPropertiesTocMenuEntry2());
|
118 |
|
119 |
} |
120 |
|
121 |
/**
|
122 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
123 |
*/
|
124 |
public void execute(String s) { |
125 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
126 |
MapControl mc = v.getMapControl(); |
127 |
FLayer[] actives = mc.getMapContext().getLayers().getActives();
|
128 |
try {
|
129 |
for (int i = 0; i < actives.length; i++) { |
130 |
if (actives[i] instanceof FLyrVect) { |
131 |
FLyrVect lv = (FLyrVect) actives[i]; |
132 |
if (s.compareTo("CLEAN") == 0) |
133 |
doClean(lv); |
134 |
if (s.compareTo("TRIANGULATION") == 0) |
135 |
doTriangulation(lv); |
136 |
if (s.compareTo("SHOW_ERRORS") == 0) |
137 |
doShowNodeErrors(lv); |
138 |
|
139 |
} |
140 |
} |
141 |
} catch (DriverException e) {
|
142 |
e.printStackTrace(); |
143 |
NotificationManager.addError(e); |
144 |
} catch (DriverIOException e) {
|
145 |
e.printStackTrace(); |
146 |
NotificationManager.addError(e); |
147 |
} |
148 |
|
149 |
} |
150 |
|
151 |
private void doTriangulation(FLyrVect lv) throws DriverException, |
152 |
DriverIOException { |
153 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
154 |
MapControl mc = v.getMapControl(); |
155 |
|
156 |
FBitSet bitSet = lv.getRecordset().getSelection(); |
157 |
Rectangle2D fullExtent = lv.getFullExtent();
|
158 |
Simplex initialTriangle = new Simplex(new Pnt[] { |
159 |
new Pnt(fullExtent.getX(), fullExtent.getY()),
|
160 |
new Pnt(fullExtent.getMaxX(), fullExtent.getY()),
|
161 |
new Pnt(fullExtent.getCenterX(), fullExtent.getMaxY())});
|
162 |
|
163 |
DelaunayTriangulation triangulator = new DelaunayTriangulation(initialTriangle);
|
164 |
|
165 |
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet.nextSetBit(i + 1)) { |
166 |
IGeometry g = lv.getSource().getShape(i); |
167 |
Geometry jtsG = g.toJTSGeometry(); |
168 |
Coordinate[] coords = jtsG.getCoordinates();
|
169 |
if (jtsG.isEmpty())
|
170 |
continue;
|
171 |
Coordinate[] linePts = CoordinateArrays
|
172 |
.removeRepeatedPoints(coords); |
173 |
Coordinate startPt = linePts[0];
|
174 |
Coordinate endPt = linePts[linePts.length - 1];
|
175 |
triangulator.delaunayPlace(startPt); |
176 |
triangulator.delaunayPlace(endPt); |
177 |
|
178 |
} |
179 |
|
180 |
// Ahora recorremos todos los nodos y los que solo hayan sido
|
181 |
// a?adidos una vez, son dangle o fuzzy.
|
182 |
// TODO: Poner una tolerancia para que las coordinate cercanas
|
183 |
// formen un solo nodo.
|
184 |
GraphicLayer graphicLayer = mc.getMapContext().getGraphicsLayer(); |
185 |
FSymbol simNodeError = new FSymbol(FShape.POINT, Color.RED); |
186 |
simNodeError.setSize(10);
|
187 |
simNodeError.setStyle(FConstant.SYMBOL_STYLE_MARKER_SQUARE); |
188 |
simNodeError.setFill(null);
|
189 |
simNodeError.setOutlined(true);
|
190 |
simNodeError.setOutlineColor(Color.RED);
|
191 |
|
192 |
int idSymbolPoint = graphicLayer.addSymbol(simNodeError);
|
193 |
|
194 |
Iterator it = triangulator.iterator();
|
195 |
|
196 |
while (it.hasNext()) {
|
197 |
Simplex triangle = (Simplex) it.next(); |
198 |
for (Iterator otherIt = triangle.facets().iterator(); otherIt |
199 |
.hasNext();) { |
200 |
Set facet = (Set) otherIt.next(); |
201 |
Pnt[] endpoint = (Pnt[]) facet.toArray(new Pnt[2]); |
202 |
GeneralPathX gp = new GeneralPathX();
|
203 |
System.err.println(endpoint[0].toString()); |
204 |
gp.moveTo(endpoint[0].coord(0), endpoint[0].coord(1)); |
205 |
gp.lineTo(endpoint[1].coord(0), endpoint[1].coord(1)); |
206 |
IGeometry gAux = ShapeFactory.createPolyline2D(gp); |
207 |
FGraphic graphic = new FGraphic(gAux, idSymbolPoint);
|
208 |
graphicLayer.addGraphic(graphic); |
209 |
} |
210 |
} |
211 |
mc.drawGraphics(); |
212 |
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* @param lv
|
217 |
* FLayerVect de l?neas para convertir a pol?gonos.
|
218 |
*/
|
219 |
private void doClean(FLyrVect lv) { |
220 |
TopologyBuilder topologyBuilder = new TopologyBuilder(0, 0); |
221 |
topologyBuilder.setInputLayer(lv); |
222 |
|
223 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
224 |
MapControl mc = v.getMapControl(); |
225 |
|
226 |
try {
|
227 |
FBitSet bitSet = lv.getRecordset().getSelection(); |
228 |
|
229 |
// // First, we need to do "noding"
|
230 |
// // TODO: This step must be optional
|
231 |
// ArrayList lineStrings = new ArrayList();
|
232 |
// for(int i=bitSet.nextSetBit(0); i>=0; i=bitSet.nextSetBit(i+1)) {
|
233 |
// IGeometry g = lv.getSource().getShape(i);
|
234 |
// lineStrings.add(g.toJTSGeometry());
|
235 |
// }
|
236 |
//
|
237 |
// Geometry nodedLineStrings = (Geometry) lineStrings.get(0);
|
238 |
// for (int i = 1; i < lineStrings.size(); i++) {
|
239 |
// nodedLineStrings =
|
240 |
// nodedLineStrings.union((Geometry)lineStrings.get(i));
|
241 |
// }
|
242 |
//
|
243 |
// // FIN noding
|
244 |
//
|
245 |
//
|
246 |
// polygonizer.add(nodedLineStrings);
|
247 |
|
248 |
Collection polygons = topologyBuilder.buildPolygons();
|
249 |
Iterator it = polygons.iterator();
|
250 |
GraphicLayer graphicLayer = mc.getMapContext().getGraphicsLayer(); |
251 |
int idSymbolPol = graphicLayer
|
252 |
.addSymbol(new FSymbol(FShape.POLYGON));
|
253 |
int idSymbolCutEdge = graphicLayer.addSymbol(new FSymbol( |
254 |
FShape.LINE, Color.BLUE));
|
255 |
int idSymbolDangle = graphicLayer.addSymbol(new FSymbol( |
256 |
FShape.LINE, Color.RED));
|
257 |
while (it.hasNext()) {
|
258 |
Polygon pol = (Polygon) it.next(); |
259 |
IGeometry gAux = FConverter.jts_to_igeometry(pol); |
260 |
FGraphic graphic = new FGraphic(gAux, idSymbolPol);
|
261 |
graphicLayer.addGraphic(graphic); |
262 |
} |
263 |
|
264 |
// LINEAS QUE PARTEN POLIGONOS
|
265 |
Collection cutEdges = topologyBuilder.getCutEdges();
|
266 |
it = cutEdges.iterator(); |
267 |
while (it.hasNext()) {
|
268 |
LineString lin = (LineString) it.next(); |
269 |
IGeometry gAux = FConverter.jts_to_igeometry(lin); |
270 |
FGraphic graphic = new FGraphic(gAux, idSymbolCutEdge);
|
271 |
graphicLayer.addGraphic(graphic); |
272 |
} |
273 |
|
274 |
// LINEAS COLGANTES, QUE NO FORMAN POLIGONO
|
275 |
Collection dangles = topologyBuilder.getDangles();
|
276 |
it = dangles.iterator(); |
277 |
while (it.hasNext()) {
|
278 |
LineString lin = (LineString) it.next(); |
279 |
IGeometry gAux = FConverter.jts_to_igeometry(lin); |
280 |
FGraphic graphic = new FGraphic(gAux, idSymbolDangle);
|
281 |
graphicLayer.addGraphic(graphic); |
282 |
} |
283 |
|
284 |
mc.drawGraphics(); |
285 |
} catch (DriverException e) {
|
286 |
e.printStackTrace(); |
287 |
NotificationManager.addError(e); |
288 |
} |
289 |
|
290 |
} |
291 |
|
292 |
/**
|
293 |
* We search for origin-endpoints in LineString. Each one will generate a
|
294 |
* Node. We also fill a map Node-numOccurrences. Dangle and Fuzzy nodes will
|
295 |
* be those that have an occurrence count = 1. (Node with degree cero in
|
296 |
* graph's language)
|
297 |
*
|
298 |
* @param lyr
|
299 |
* @throws DriverException
|
300 |
* @throws DriverIOException
|
301 |
*/
|
302 |
private void doShowNodeErrors(FLyrVect lv) throws DriverException, |
303 |
DriverIOException { |
304 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
305 |
MapControl mc = v.getMapControl(); |
306 |
|
307 |
// ArrayList nodeErrors = new ArrayList();
|
308 |
NodeMap nodeMap = new NodeMap();
|
309 |
FBitSet bitSet = lv.getRecordset().getSelection(); |
310 |
|
311 |
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet.nextSetBit(i + 1)) { |
312 |
IGeometry g = lv.getSource().getShape(i); |
313 |
Geometry jtsG = g.toJTSGeometry(); |
314 |
Coordinate[] coords = jtsG.getCoordinates();
|
315 |
if (jtsG.isEmpty())
|
316 |
continue;
|
317 |
Coordinate[] linePts = CoordinateArrays
|
318 |
.removeRepeatedPoints(coords); |
319 |
Coordinate startPt = linePts[0];
|
320 |
Coordinate endPt = linePts[linePts.length - 1];
|
321 |
|
322 |
MyNode nStart = (MyNode) nodeMap.find(startPt); |
323 |
MyNode nEnd = (MyNode) nodeMap.find(endPt); |
324 |
if (nStart == null) { |
325 |
nStart = new MyNode(startPt);
|
326 |
nodeMap.add(nStart); |
327 |
} else
|
328 |
nStart.setOccurrences(nStart.getOccurrences() + 1);
|
329 |
if (nEnd == null) { |
330 |
nEnd = new MyNode(endPt);
|
331 |
nodeMap.add(nEnd); |
332 |
} else
|
333 |
nEnd.setOccurrences(nEnd.getOccurrences() + 1);
|
334 |
|
335 |
} |
336 |
|
337 |
// Ahora recorremos todos los nodos y los que solo hayan sido
|
338 |
// a?adidos una vez, son dangle o fuzzy.
|
339 |
// TODO: Poner una tolerancia para que las coordinate cercanas
|
340 |
// formen un solo nodo.
|
341 |
GraphicLayer graphicLayer = mc.getMapContext().getGraphicsLayer(); |
342 |
FSymbol simNodeError = new FSymbol(FShape.POINT, Color.RED); |
343 |
simNodeError.setSize(10);
|
344 |
simNodeError.setStyle(FConstant.SYMBOL_STYLE_MARKER_SQUARE); |
345 |
simNodeError.setFill(null);
|
346 |
simNodeError.setOutlined(true);
|
347 |
simNodeError.setOutlineColor(Color.RED);
|
348 |
|
349 |
int idSymbolPoint = graphicLayer.addSymbol(simNodeError);
|
350 |
|
351 |
Iterator it = nodeMap.iterator();
|
352 |
while (it.hasNext()) {
|
353 |
MyNode node = (MyNode) it.next(); |
354 |
if (node.getOccurrences() == 1) { |
355 |
FPoint2D p = FConverter.coordinate2FPoint2D(node |
356 |
.getCoordinate()); |
357 |
IGeometry gAux = ShapeFactory.createPoint2D(p); |
358 |
FGraphic graphic = new FGraphic(gAux, idSymbolPoint);
|
359 |
graphicLayer.addGraphic(graphic); |
360 |
|
361 |
} |
362 |
} |
363 |
mc.drawGraphics(); |
364 |
} |
365 |
|
366 |
/**
|
367 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
368 |
*/
|
369 |
public boolean isEnabled() { |
370 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
371 |
MapControl mc = v.getMapControl(); |
372 |
if (mc.getMapContext().getLayers().getActives().length > 0) |
373 |
return true; |
374 |
return false; |
375 |
} |
376 |
|
377 |
/**
|
378 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
379 |
*/
|
380 |
public boolean isVisible() { |
381 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
382 |
if (v instanceof View) |
383 |
return true; |
384 |
return false; |
385 |
} |
386 |
|
387 |
} |