svn-gvsig-desktop / tags / PilotoRedes_Build_3 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / TopologyExtension.java @ 11678
History | View | Annotate | Download (12.7 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")).remove("FLyrVectEditProperties"); |
117 |
((ExtensionPoint) extensionPoints.get("View_TocActions")).remove("FLyrVectEditProperties2"); |
118 |
((ExtensionPoint) extensionPoints.get("View_TocActions")).put("FLyrVectEditProperties",new FLyrVectEditPropertiesTocMenuEntry2()); |
119 |
//extensionPoints.add("View_TocActions","FLyrVectEditProperties2",new FLyrVectEditPropertiesTocMenuEntry2());
|
120 |
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
125 |
*/
|
126 |
public void execute(String s) { |
127 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
128 |
MapControl mc = v.getMapControl(); |
129 |
FLayer[] actives = mc.getMapContext().getLayers().getActives();
|
130 |
try {
|
131 |
for (int i = 0; i < actives.length; i++) { |
132 |
if (actives[i] instanceof FLyrVect) { |
133 |
FLyrVect lv = (FLyrVect) actives[i]; |
134 |
if (s.compareTo("CLEAN") == 0) |
135 |
doClean(lv); |
136 |
if (s.compareTo("TRIANGULATION") == 0) |
137 |
doTriangulation(lv); |
138 |
if (s.compareTo("SHOW_ERRORS") == 0) |
139 |
doShowNodeErrors(lv); |
140 |
|
141 |
} |
142 |
} |
143 |
} catch (DriverException e) {
|
144 |
e.printStackTrace(); |
145 |
NotificationManager.addError(e); |
146 |
} catch (DriverIOException e) {
|
147 |
e.printStackTrace(); |
148 |
NotificationManager.addError(e); |
149 |
} |
150 |
|
151 |
} |
152 |
|
153 |
private void doTriangulation(FLyrVect lv) throws DriverException, |
154 |
DriverIOException { |
155 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
156 |
MapControl mc = v.getMapControl(); |
157 |
|
158 |
FBitSet bitSet = lv.getRecordset().getSelection(); |
159 |
Rectangle2D fullExtent = lv.getFullExtent();
|
160 |
Simplex initialTriangle = new Simplex(new Pnt[] { |
161 |
new Pnt(fullExtent.getX(), fullExtent.getY()),
|
162 |
new Pnt(fullExtent.getMaxX(), fullExtent.getY()),
|
163 |
new Pnt(fullExtent.getCenterX(), fullExtent.getMaxY())});
|
164 |
|
165 |
DelaunayTriangulation triangulator = new DelaunayTriangulation(initialTriangle);
|
166 |
|
167 |
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet.nextSetBit(i + 1)) { |
168 |
IGeometry g = lv.getSource().getShape(i); |
169 |
Geometry jtsG = g.toJTSGeometry(); |
170 |
Coordinate[] coords = jtsG.getCoordinates();
|
171 |
if (jtsG.isEmpty())
|
172 |
continue;
|
173 |
Coordinate[] linePts = CoordinateArrays
|
174 |
.removeRepeatedPoints(coords); |
175 |
Coordinate startPt = linePts[0];
|
176 |
Coordinate endPt = linePts[linePts.length - 1];
|
177 |
triangulator.delaunayPlace(startPt); |
178 |
triangulator.delaunayPlace(endPt); |
179 |
|
180 |
} |
181 |
|
182 |
// Ahora recorremos todos los nodos y los que solo hayan sido
|
183 |
// a?adidos una vez, son dangle o fuzzy.
|
184 |
// TODO: Poner una tolerancia para que las coordinate cercanas
|
185 |
// formen un solo nodo.
|
186 |
GraphicLayer graphicLayer = mc.getMapContext().getGraphicsLayer(); |
187 |
FSymbol simNodeError = new FSymbol(FShape.POINT, Color.RED); |
188 |
simNodeError.setSize(10);
|
189 |
simNodeError.setStyle(FConstant.SYMBOL_STYLE_MARKER_SQUARE); |
190 |
simNodeError.setFill(null);
|
191 |
simNodeError.setOutlined(true);
|
192 |
simNodeError.setOutlineColor(Color.RED);
|
193 |
|
194 |
int idSymbolPoint = graphicLayer.addSymbol(simNodeError);
|
195 |
|
196 |
Iterator it = triangulator.iterator();
|
197 |
|
198 |
while (it.hasNext()) {
|
199 |
Simplex triangle = (Simplex) it.next(); |
200 |
for (Iterator otherIt = triangle.facets().iterator(); otherIt |
201 |
.hasNext();) { |
202 |
Set facet = (Set) otherIt.next(); |
203 |
Pnt[] endpoint = (Pnt[]) facet.toArray(new Pnt[2]); |
204 |
GeneralPathX gp = new GeneralPathX();
|
205 |
System.err.println(endpoint[0].toString()); |
206 |
gp.moveTo(endpoint[0].coord(0), endpoint[0].coord(1)); |
207 |
gp.lineTo(endpoint[1].coord(0), endpoint[1].coord(1)); |
208 |
IGeometry gAux = ShapeFactory.createPolyline2D(gp); |
209 |
FGraphic graphic = new FGraphic(gAux, idSymbolPoint);
|
210 |
graphicLayer.addGraphic(graphic); |
211 |
} |
212 |
} |
213 |
mc.drawGraphics(); |
214 |
|
215 |
} |
216 |
|
217 |
/**
|
218 |
* @param lv
|
219 |
* FLayerVect de l?neas para convertir a pol?gonos.
|
220 |
*/
|
221 |
private void doClean(FLyrVect lv) { |
222 |
TopologyBuilder topologyBuilder = new TopologyBuilder(0, 0); |
223 |
topologyBuilder.setInputLayer(lv); |
224 |
|
225 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
226 |
MapControl mc = v.getMapControl(); |
227 |
|
228 |
try {
|
229 |
FBitSet bitSet = lv.getRecordset().getSelection(); |
230 |
|
231 |
// // First, we need to do "noding"
|
232 |
// // TODO: This step must be optional
|
233 |
// ArrayList lineStrings = new ArrayList();
|
234 |
// for(int i=bitSet.nextSetBit(0); i>=0; i=bitSet.nextSetBit(i+1)) {
|
235 |
// IGeometry g = lv.getSource().getShape(i);
|
236 |
// lineStrings.add(g.toJTSGeometry());
|
237 |
// }
|
238 |
//
|
239 |
// Geometry nodedLineStrings = (Geometry) lineStrings.get(0);
|
240 |
// for (int i = 1; i < lineStrings.size(); i++) {
|
241 |
// nodedLineStrings =
|
242 |
// nodedLineStrings.union((Geometry)lineStrings.get(i));
|
243 |
// }
|
244 |
//
|
245 |
// // FIN noding
|
246 |
//
|
247 |
//
|
248 |
// polygonizer.add(nodedLineStrings);
|
249 |
|
250 |
Collection polygons = topologyBuilder.buildPolygons();
|
251 |
Iterator it = polygons.iterator();
|
252 |
GraphicLayer graphicLayer = mc.getMapContext().getGraphicsLayer(); |
253 |
int idSymbolPol = graphicLayer
|
254 |
.addSymbol(new FSymbol(FShape.POLYGON));
|
255 |
int idSymbolCutEdge = graphicLayer.addSymbol(new FSymbol( |
256 |
FShape.LINE, Color.BLUE));
|
257 |
int idSymbolDangle = graphicLayer.addSymbol(new FSymbol( |
258 |
FShape.LINE, Color.RED));
|
259 |
while (it.hasNext()) {
|
260 |
Polygon pol = (Polygon) it.next(); |
261 |
IGeometry gAux = FConverter.jts_to_igeometry(pol); |
262 |
FGraphic graphic = new FGraphic(gAux, idSymbolPol);
|
263 |
graphicLayer.addGraphic(graphic); |
264 |
} |
265 |
|
266 |
// LINEAS QUE PARTEN POLIGONOS
|
267 |
Collection cutEdges = topologyBuilder.getCutEdges();
|
268 |
it = cutEdges.iterator(); |
269 |
while (it.hasNext()) {
|
270 |
LineString lin = (LineString) it.next(); |
271 |
IGeometry gAux = FConverter.jts_to_igeometry(lin); |
272 |
FGraphic graphic = new FGraphic(gAux, idSymbolCutEdge);
|
273 |
graphicLayer.addGraphic(graphic); |
274 |
} |
275 |
|
276 |
// LINEAS COLGANTES, QUE NO FORMAN POLIGONO
|
277 |
Collection dangles = topologyBuilder.getDangles();
|
278 |
it = dangles.iterator(); |
279 |
while (it.hasNext()) {
|
280 |
LineString lin = (LineString) it.next(); |
281 |
IGeometry gAux = FConverter.jts_to_igeometry(lin); |
282 |
FGraphic graphic = new FGraphic(gAux, idSymbolDangle);
|
283 |
graphicLayer.addGraphic(graphic); |
284 |
} |
285 |
|
286 |
mc.drawGraphics(); |
287 |
} catch (DriverException e) {
|
288 |
e.printStackTrace(); |
289 |
NotificationManager.addError(e); |
290 |
} |
291 |
|
292 |
} |
293 |
|
294 |
/**
|
295 |
* We search for origin-endpoints in LineString. Each one will generate a
|
296 |
* Node. We also fill a map Node-numOccurrences. Dangle and Fuzzy nodes will
|
297 |
* be those that have an occurrence count = 1. (Node with degree cero in
|
298 |
* graph's language)
|
299 |
*
|
300 |
* @param lyr
|
301 |
* @throws DriverException
|
302 |
* @throws DriverIOException
|
303 |
*/
|
304 |
private void doShowNodeErrors(FLyrVect lv) throws DriverException, |
305 |
DriverIOException { |
306 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
307 |
MapControl mc = v.getMapControl(); |
308 |
|
309 |
// ArrayList nodeErrors = new ArrayList();
|
310 |
NodeMap nodeMap = new NodeMap();
|
311 |
FBitSet bitSet = lv.getRecordset().getSelection(); |
312 |
|
313 |
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet.nextSetBit(i + 1)) { |
314 |
IGeometry g = lv.getSource().getShape(i); |
315 |
Geometry jtsG = g.toJTSGeometry(); |
316 |
Coordinate[] coords = jtsG.getCoordinates();
|
317 |
if (jtsG.isEmpty())
|
318 |
continue;
|
319 |
Coordinate[] linePts = CoordinateArrays
|
320 |
.removeRepeatedPoints(coords); |
321 |
Coordinate startPt = linePts[0];
|
322 |
Coordinate endPt = linePts[linePts.length - 1];
|
323 |
|
324 |
MyNode nStart = (MyNode) nodeMap.find(startPt); |
325 |
MyNode nEnd = (MyNode) nodeMap.find(endPt); |
326 |
if (nStart == null) { |
327 |
nStart = new MyNode(startPt);
|
328 |
nodeMap.add(nStart); |
329 |
} else
|
330 |
nStart.setOccurrences(nStart.getOccurrences() + 1);
|
331 |
if (nEnd == null) { |
332 |
nEnd = new MyNode(endPt);
|
333 |
nodeMap.add(nEnd); |
334 |
} else
|
335 |
nEnd.setOccurrences(nEnd.getOccurrences() + 1);
|
336 |
|
337 |
} |
338 |
|
339 |
// Ahora recorremos todos los nodos y los que solo hayan sido
|
340 |
// a?adidos una vez, son dangle o fuzzy.
|
341 |
// TODO: Poner una tolerancia para que las coordinate cercanas
|
342 |
// formen un solo nodo.
|
343 |
GraphicLayer graphicLayer = mc.getMapContext().getGraphicsLayer(); |
344 |
FSymbol simNodeError = new FSymbol(FShape.POINT, Color.RED); |
345 |
simNodeError.setSize(10);
|
346 |
simNodeError.setStyle(FConstant.SYMBOL_STYLE_MARKER_SQUARE); |
347 |
simNodeError.setFill(null);
|
348 |
simNodeError.setOutlined(true);
|
349 |
simNodeError.setOutlineColor(Color.RED);
|
350 |
|
351 |
int idSymbolPoint = graphicLayer.addSymbol(simNodeError);
|
352 |
|
353 |
Iterator it = nodeMap.iterator();
|
354 |
while (it.hasNext()) {
|
355 |
MyNode node = (MyNode) it.next(); |
356 |
if (node.getOccurrences() == 1) { |
357 |
FPoint2D p = FConverter.coordinate2FPoint2D(node |
358 |
.getCoordinate()); |
359 |
IGeometry gAux = ShapeFactory.createPoint2D(p); |
360 |
FGraphic graphic = new FGraphic(gAux, idSymbolPoint);
|
361 |
graphicLayer.addGraphic(graphic); |
362 |
|
363 |
} |
364 |
} |
365 |
mc.drawGraphics(); |
366 |
} |
367 |
|
368 |
/**
|
369 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
370 |
*/
|
371 |
public boolean isEnabled() { |
372 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
373 |
MapControl mc = v.getMapControl(); |
374 |
if (mc.getMapContext().getLayers().getActives().length > 0) |
375 |
return true; |
376 |
return false; |
377 |
} |
378 |
|
379 |
/**
|
380 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
381 |
*/
|
382 |
public boolean isVisible() { |
383 |
// IWindow v = PluginServices.getMDIManager().getActiveWindow();
|
384 |
// if (v instanceof View)
|
385 |
// return true;
|
386 |
return false; |
387 |
} |
388 |
|
389 |
} |