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