root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / NetworkExtension.java @ 8100
History | View | Annotate | Download (8.82 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.Component; |
44 |
import java.io.File; |
45 |
|
46 |
import javax.swing.JOptionPane; |
47 |
|
48 |
import com.iver.andami.PluginServices; |
49 |
import com.iver.andami.plugins.Extension; |
50 |
import com.iver.andami.ui.mdiManager.IWindow; |
51 |
import com.iver.cit.gvsig.ProjectExtension; |
52 |
import com.iver.cit.gvsig.fmap.DriverException; |
53 |
import com.iver.cit.gvsig.fmap.MapContext; |
54 |
import com.iver.cit.gvsig.fmap.MapControl; |
55 |
import com.iver.cit.gvsig.fmap.core.FShape; |
56 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
57 |
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter; |
58 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
59 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
60 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
61 |
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator; |
62 |
import com.iver.cit.gvsig.graph.core.IGraph; |
63 |
import com.iver.cit.gvsig.graph.core.Network; |
64 |
import com.iver.cit.gvsig.graph.core.NetworkLoader; |
65 |
import com.iver.cit.gvsig.graph.core.NetworkRedLoader; |
66 |
import com.iver.cit.gvsig.graph.core.NetworkWriter; |
67 |
import com.iver.cit.gvsig.project.Project; |
68 |
import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
69 |
import com.iver.cit.gvsig.project.documents.view.IProjectView; |
70 |
import com.iver.cit.gvsig.project.documents.view.gui.IView; |
71 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
72 |
|
73 |
import edu.uci.ics.jung.graph.Graph; |
74 |
|
75 |
public class NetworkExtension extends Extension { |
76 |
|
77 |
// private String fieldType;
|
78 |
// private String fieldDist;
|
79 |
// private String fieldSense;
|
80 |
|
81 |
public void initialize() { |
82 |
} |
83 |
|
84 |
public void execute(String actionCommand) { |
85 |
IView view = (View) PluginServices.getMDIManager().getActiveWindow();
|
86 |
MapControl mapControl = view.getMapControl(); |
87 |
MapContext map = mapControl.getMapContext(); |
88 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(map
|
89 |
.getLayers()); |
90 |
while (lyrIterator.hasNext()) {
|
91 |
FLayer lyr = lyrIterator.next(); |
92 |
if ((lyr.isActive()) && (lyr instanceof FLyrVect)) |
93 |
{ |
94 |
FLyrVect lyrVect = (FLyrVect) lyr; |
95 |
int shapeType;
|
96 |
try {
|
97 |
shapeType = lyrVect.getShapeType(); |
98 |
if (shapeType == FShape.LINE)
|
99 |
{ |
100 |
if (actionCommand.equalsIgnoreCase("GENERATE_RED")) { |
101 |
generateRedNetwork(lyrVect); |
102 |
} |
103 |
if (actionCommand.equalsIgnoreCase("LOAD_RED")) { |
104 |
loadNetwork(lyrVect); |
105 |
} |
106 |
} |
107 |
} catch (DriverException e) {
|
108 |
// TODO Auto-generated catch block
|
109 |
e.printStackTrace(); |
110 |
} |
111 |
|
112 |
} |
113 |
} |
114 |
|
115 |
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* Suponemos que en el proyecto hay 2 tablas, una con los nodos
|
120 |
* y otro con los edges.
|
121 |
* Cargamos la red a partir de esas tablas y se la
|
122 |
* asociamos a la capa. A partir de ah?, nuestras
|
123 |
* herramientas pueden ver si la capa activa tiene
|
124 |
* asociada o no una red y ponerse visibles / invisibles
|
125 |
* Otra posible soluci?n es llevar nuestra propia lista de capas
|
126 |
* con red (que ser? peque?ita), y as?, en lugar de recorrer
|
127 |
* el MapContext, recorremos nuestra lista para ver la
|
128 |
* capa que est? activa y con red. Me empieza a preocupar
|
129 |
* que todas las herramientas iteren por la colecci?n de
|
130 |
* capas para habilitarse/deshabilitarse:
|
131 |
* 100 herramientas * 100 capas = 10.000 comprobaciones
|
132 |
* Si comprobar algo cuesta 1 mseg => 10 segundos!!!
|
133 |
* @param lyrVect
|
134 |
*/
|
135 |
private void loadNetworkFromTables(FLyrVect lyrVect) { |
136 |
// Aqu? mostrar un di?lgo para seleccionar las tablas
|
137 |
// de nodos y edges
|
138 |
// y hacer un mapping (si es necesario) entre los
|
139 |
// nombres de campos
|
140 |
String tableNodes = "Nodes"; |
141 |
String tableEdges = "Edges"; |
142 |
|
143 |
ProjectExtension projectExt = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
144 |
|
145 |
ProjectTable ptNodes = projectExt.getProject().getTable(tableNodes); |
146 |
ProjectTable ptEdges = projectExt.getProject().getTable(tableEdges); |
147 |
|
148 |
SelectableDataSource sdsNodes = ptNodes.getModelo().getRecordset(); |
149 |
SelectableDataSource sdsEdges = ptEdges.getModelo().getRecordset(); |
150 |
|
151 |
NetworkLoader netLoader = new NetworkLoader();
|
152 |
|
153 |
netLoader.setNodeReader(sdsNodes); |
154 |
netLoader.setEdgeReader(sdsEdges); |
155 |
|
156 |
IGraph g = netLoader.loadNetwork(); |
157 |
|
158 |
System.out.println("Num nodos=" + g.getNumNodes() + " numEdges = " + g.getNumEdges()); |
159 |
|
160 |
lyrVect.setProperty("network", g);
|
161 |
|
162 |
} |
163 |
private void loadNetwork(FLyrVect lyrVect) { |
164 |
// Aqu? mostrar un di?lgo para seleccionar las tablas
|
165 |
// de nodos y edges
|
166 |
// y hacer un mapping (si es necesario) entre los
|
167 |
// nombres de campos
|
168 |
|
169 |
NetworkRedLoader netLoader = new NetworkRedLoader();
|
170 |
File redFile = new File("c:/" + lyrVect.getName().replaceAll("\\Q.shp\\E", ".red")); |
171 |
netLoader.setNetFile(redFile); |
172 |
|
173 |
IGraph g = netLoader.loadNetwork(); |
174 |
|
175 |
System.out.println("Num nodos=" + g.getNumNodes() + " numEdges = " + g.getNumEdges()); |
176 |
|
177 |
// TODO: MOSTRAR UN CUADRO DE DI?LOGO CON UN COMBOBOX PARA QUE ESCOJA EL CAMPO DE NOMBRE DE CALLE.
|
178 |
String fieldStreetName = JOptionPane.showInputDialog((Component) PluginServices.getMainFrame(), |
179 |
"Street-Route field name?", "STREET_NAM"); |
180 |
|
181 |
Network net = new Network();
|
182 |
net.setGraph(g); |
183 |
net.setLayer(lyrVect); |
184 |
ShortestPathExtension.solver.setNetwork(net); |
185 |
ShortestPathExtension.solver.setFielStreetName(fieldStreetName); |
186 |
|
187 |
lyrVect.setProperty("network", net);
|
188 |
|
189 |
} |
190 |
|
191 |
|
192 |
private void generateNetwork(FLyrVect lyr) { |
193 |
NetworkWriter netBuilder = new NetworkWriter();
|
194 |
// Por ahora, a pelo, pero hay que sacar un cuadro
|
195 |
// de di?logo para hecer el mapping.
|
196 |
// Tambi?n un cuadro de di?logo para seleccionar
|
197 |
// en qu? tablas quiere escribir, y su formato
|
198 |
// (dbf, postgres, etc)
|
199 |
String fieldType = "tipored"; String fieldDist = "length"; String fieldSense = "sen"; |
200 |
try {
|
201 |
netBuilder.setLayer(lyr, fieldType, fieldDist, fieldSense); |
202 |
DbfWriter nodeWriter = new DbfWriter();
|
203 |
nodeWriter.setFile(new File("c:/nodes.dbf")); |
204 |
|
205 |
DbfWriter edgeWriter = new DbfWriter();
|
206 |
edgeWriter.setFile(new File("c:/edges.dbf")); |
207 |
|
208 |
netBuilder.setEdgeWriter(edgeWriter); |
209 |
netBuilder.setNodeWriter(nodeWriter); |
210 |
|
211 |
netBuilder.writeNetwork(); |
212 |
} catch (DriverException e1) {
|
213 |
// TODO Auto-generated catch block
|
214 |
e1.printStackTrace(); |
215 |
} catch (EditionException e1) {
|
216 |
// TODO Auto-generated catch block
|
217 |
e1.printStackTrace(); |
218 |
} |
219 |
JOptionPane.showMessageDialog(null, "Hecho. "); |
220 |
|
221 |
|
222 |
} |
223 |
|
224 |
private void generateRedNetwork(FLyrVect lyr) { |
225 |
NetworkWriter netBuilder = new NetworkWriter();
|
226 |
// Por ahora, a pelo, pero hay que sacar un cuadro
|
227 |
// de di?logo para hecer el mapping.
|
228 |
// Tambi?n un cuadro de di?logo para seleccionar
|
229 |
// en qu? tablas quiere escribir, y su formato
|
230 |
// (dbf, postgres, etc)
|
231 |
|
232 |
String fieldType = JOptionPane.showInputDialog(null, "fieldType:", "LPOLY_"); |
233 |
if (fieldType == null) |
234 |
return;
|
235 |
String fieldDist = JOptionPane.showInputDialog(null, "fieldLength:", "length"); |
236 |
if (fieldDist == null) |
237 |
return;
|
238 |
|
239 |
String fieldSense = JOptionPane.showInputDialog(null, "fieldSense:", ""); |
240 |
if (fieldSense == null) |
241 |
return;
|
242 |
|
243 |
|
244 |
try {
|
245 |
netBuilder.setLayer(lyr, fieldType, fieldDist, fieldSense); |
246 |
|
247 |
File redFile = new File("c:/" + lyr.getName().replaceAll("\\Q.shp\\E", ".red")); |
248 |
netBuilder.createRedFile(lyr, redFile); |
249 |
} catch (DriverException e1) {
|
250 |
// TODO Auto-generated catch block
|
251 |
e1.printStackTrace(); |
252 |
} |
253 |
JOptionPane.showMessageDialog(null, "Hecho. "); |
254 |
|
255 |
|
256 |
} |
257 |
|
258 |
|
259 |
public boolean isEnabled() { |
260 |
return true; |
261 |
} |
262 |
|
263 |
public boolean isVisible() { |
264 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
265 |
|
266 |
if (f == null) { |
267 |
return false; |
268 |
} |
269 |
|
270 |
if (f instanceof View) { |
271 |
View vista = (View) f; |
272 |
IProjectView model = vista.getModel(); |
273 |
MapContext mapa = model.getMapContext(); |
274 |
FLayer[] activeLayers = mapa.getLayers().getActives();
|
275 |
if (activeLayers.length > 0) |
276 |
if (activeLayers[0] instanceof FLyrVect) |
277 |
return true; |
278 |
} |
279 |
return false; |
280 |
|
281 |
} |
282 |
|
283 |
} |