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