Revision 29800

View differences:

trunk/extensions/extGraph/config/config.xml
94 94
			<menu text="Network/Minimum_spanning_tree" icon="shortestpath"
95 95
			 action-command="MST"/> 
96 96
		</extension>
97
		<extension class-name="org.gvsig.graph.ConnectivityExtension"
98
			description="CONNECTIVITY"
99
			active="true">
100
			<menu text="Network/Connectivity" icon="connectivity" action-command="CONNECTIVITY"/> 
101
		</extension>
97 102

  
98 103

  
99 104
		<extension class-name="org.gvsig.graph.ClearRouteExtension"
trunk/extensions/extGraph/src/org/gvsig/graph/gui/ConnectivityControlPanel.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

  
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 Software Colaborativo (www.scolab.es)   development
26
 */
27

  
28
package org.gvsig.graph.gui;
29

  
30
import java.awt.BorderLayout;
31
import java.awt.Color;
32
import java.awt.Dimension;
33
import java.awt.Font;
34
import java.awt.GridBagConstraints;
35
import java.awt.GridBagLayout;
36
import java.awt.Insets;
37
import java.awt.Point;
38
import java.awt.Rectangle;
39
import java.awt.SystemColor;
40

  
41
import javax.swing.BorderFactory;
42
import javax.swing.ButtonGroup;
43
import javax.swing.ComboBoxModel;
44
import javax.swing.DefaultComboBoxModel;
45
import javax.swing.ImageIcon;
46
import javax.swing.JButton;
47
import javax.swing.JCheckBox;
48
import javax.swing.JComboBox;
49
import javax.swing.JLabel;
50
import javax.swing.JPanel;
51
import javax.swing.JRadioButton;
52
import javax.swing.JTextArea;
53
import javax.swing.JTextField;
54
import javax.swing.JToggleButton;
55
import javax.swing.border.EtchedBorder;
56
import javax.swing.border.TitledBorder;
57

  
58
import org.gvsig.graph.core.GraphException;
59
import org.gvsig.graph.core.GvFlag;
60
import org.gvsig.graph.core.Network;
61
import org.gvsig.graph.solvers.IDijkstraListener;
62
import org.gvsig.graph.solvers.ReverseOneToManySolver;
63
import org.gvsig.graph.solvers.SelectDijkstraListener;
64

  
65
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
66
import com.iver.andami.PluginServices;
67
import com.iver.andami.ui.mdiManager.IWindow;
68
import com.iver.andami.ui.mdiManager.WindowInfo;
69
import com.iver.cit.gvsig.fmap.MapContext;
70
import com.iver.cit.gvsig.fmap.MapControl;
71
import com.iver.cit.gvsig.fmap.core.FShape;
72
import com.iver.cit.gvsig.fmap.layers.FLayer;
73
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
74
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
75

  
76
import java.awt.event.ActionEvent;
77
import java.awt.event.ActionListener;
78
import java.awt.event.KeyEvent;
79

  
80
public class ConnectivityControlPanel extends JPanel implements IWindow {
81

  
82
	private static final long serialVersionUID = 1L;
83
	private JPanel jPanelSouth = null;
84
	private JPanel jPanelCenter = null;
85
	private JPanel jPanelNorth = null;
86
	private JLabel jLblConnectivity = null;
87
	private JLabel jLblIcon = null;
88
	private JTextArea jTextArea = null;
89
	private JLabel jLblOriginPoint = null;
90
	private JTextField jTxtOriginPoint = null;
91
	private JToggleButton jToggleButtonSetFlag = null;
92
	private JLabel jLblAssociatedLayer = null;
93
	private JComboBox jCboAssociatedLayer = null;
94
	private JButton jBtnCalculate = null;
95
	private JButton jBtnClose = null;
96
	private JPanel jPanelOptions = null;
97
	private JRadioButton jRadioBtnNormalDirection = null;
98
	private JRadioButton jRadioBtnReverseDirection = null;
99
	private JCheckBox jChkBoxUseMaxDist = null;
100
	private JCheckBox jChkBoxUseMaxCost = null;
101
	private JTextField jTxtMaxDist = null;
102
	private JTextField jTxtMaxCost = null;
103
	private WindowInfo wi;
104
	private MapControl mapCtrl;
105
	
106
	private SelectDijkstraListener selectListener;
107
	/**
108
	 * This is the default constructor
109
	 */
110
	public ConnectivityControlPanel() {
111
		super();
112
		initialize();
113
	}
114

  
115
	/**
116
	 * This method initializes this
117
	 * 
118
	 * @return void
119
	 */
120
	private void initialize() {
121
		this.setSize(343, 346);
122
		this.setLayout(new BorderLayout());
123
		this.add(getJPanelNorth(), BorderLayout.NORTH);
124
		this.add(getJPanelSouth(), BorderLayout.SOUTH);
125
		this.add(getJPanelCenter(), BorderLayout.CENTER);
126
	}
127

  
128
	/**
129
	 * This method initializes jPanelSouth	
130
	 * 	
131
	 * @return javax.swing.JPanel	
132
	 */
133
	private JPanel getJPanelSouth() {
134
		if (jPanelSouth == null) {
135
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
136
			gridBagConstraints.gridx = 1;
137
			gridBagConstraints.gridy = 0;
138
			jPanelSouth = new JPanel();
139
			jPanelSouth.setLayout(new GridBagLayout());
140
			jPanelSouth.setPreferredSize(new Dimension(134, 30));
141
			jPanelSouth.add(getJBtnCalculate(), new GridBagConstraints());
142
			jPanelSouth.add(getJBtnClose(), gridBagConstraints);
143
		}
144
		return jPanelSouth;
145
	}
146

  
147
	/**
148
	 * This method initializes jPanelCenter	
149
	 * 	
150
	 * @return javax.swing.JPanel	
151
	 */
152
	private JPanel getJPanelCenter() {
153
		if (jPanelCenter == null) {
154
			jLblAssociatedLayer = new JLabel();
155
			jLblAssociatedLayer.setBounds(new Rectangle(15, 57, 111, 14));
156
			jLblAssociatedLayer.setText("Capa asociada:");
157
			jLblOriginPoint = new JLabel();
158
			jLblOriginPoint.setBounds(new Rectangle(15, 17, 111, 14));
159
			jLblOriginPoint.setText("Punto or?gen:");
160
			jPanelCenter = new JPanel();
161
			jPanelCenter.setLayout(null);
162
			jPanelCenter.add(jLblOriginPoint, null);
163
			jPanelCenter.add(getJTxtOriginPoint(), null);
164
			jPanelCenter.add(getJToggleButtonSetFlag(), null);
165
			jPanelCenter.add(jLblAssociatedLayer, null);
166
			jPanelCenter.add(getJCboAssociatedLayer(), null);
167
			jPanelCenter.add(getJPanelOptions(), null);
168
		}
169
		return jPanelCenter;
170
	}
171

  
172
	/**
173
	 * This method initializes jPanelNorth	
174
	 * 	
175
	 * @return javax.swing.JPanel	
176
	 */
177
	private JPanel getJPanelNorth() {
178
		if (jPanelNorth == null) {
179
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
180
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
181
			gridBagConstraints1.gridx = 0;
182
			gridBagConstraints1.anchor = GridBagConstraints.SOUTHWEST;
183
			gridBagConstraints1.weightx = 1.0D;
184
			gridBagConstraints1.insets = new Insets(2, 5, 0, 0);
185
			gridBagConstraints1.gridy = 1;
186
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
187
			gridBagConstraints2.gridx = 1;
188
			gridBagConstraints2.anchor = GridBagConstraints.EAST;
189
			gridBagConstraints2.gridy = 1;
190
			jLblIcon = new JLabel();
191
			jLblIcon.setText("");
192
			jLblIcon.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource(
193
					"images/wizard_connectivity.png")));
194
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
195
			gridBagConstraints.gridx = 0;
196
			gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
197
			gridBagConstraints.gridwidth = 1;
198
			gridBagConstraints.ipadx = 7;
199
			gridBagConstraints.gridheight = 0;
200
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
201
			gridBagConstraints.insets = new Insets(2, 5, 0, 0);
202
			gridBagConstraints.ipady = 3;
203
			gridBagConstraints.weightx = 1.0D;
204
			gridBagConstraints.gridy = 0;
205
			jLblConnectivity = new JLabel();
206
			jLblConnectivity.setText(PluginServices.getText(this,"connectivity"));
207
			jLblConnectivity.setFont(new Font("Dialog", Font.BOLD, 14));
208
			jPanelNorth = new JPanel();
209
			jPanelNorth.setLayout(new GridBagLayout());
210
			jPanelNorth.setPreferredSize(new Dimension(0, 70));
211
			jPanelNorth.setBackground(SystemColor.text);
212
			jPanelNorth.setBorder(BorderFactory
213
					.createEtchedBorder(EtchedBorder.LOWERED));
214
			jPanelNorth.add(jLblConnectivity, gridBagConstraints);
215
			jPanelNorth.add(jLblIcon, gridBagConstraints2);
216
			jPanelNorth.add(getJTextArea(), gridBagConstraints1);
217
		}
218
		return jPanelNorth;
219
	}
220
	
221
	private JTextArea getJTextArea() {
222
		if (jTextArea == null) {
223
			jTextArea = new JTextArea();
224
			jTextArea.setPreferredSize(new Dimension(100, 40));
225
			jTextArea.setText("Metadata");
226
			jTextArea.setEditable(false);
227
			jTextArea.setLineWrap(true);
228
		}
229
		return jTextArea;
230
	}
231

  
232

  
233
	/**
234
	 * This method initializes jTxtOriginPoint	
235
	 * 	
236
	 * @return javax.swing.JTextField	
237
	 */
238
	private JTextField getJTxtOriginPoint() {
239
		if (jTxtOriginPoint == null) {
240
			jTxtOriginPoint = new JTextField();
241
			jTxtOriginPoint.setBounds(new Rectangle(137, 16, 124, 20));
242
		}
243
		return jTxtOriginPoint;
244
	}
245

  
246
	/**
247
	 * This method initializes jToggleButtonSetFlag	
248
	 * 	
249
	 * @return javax.swing.JToggleButton	
250
	 */
251
	private JToggleButton getJToggleButtonSetFlag() {
252
		if (jToggleButtonSetFlag == null) {
253
			jToggleButtonSetFlag = new JToggleButton();
254
			jToggleButtonSetFlag.setIcon(new ImageIcon("C:/Users/Fjp/workspace_SVN_HEAD_OFICIAL/extGraph/images/disconnect_co_004.gif"));
255
			jToggleButtonSetFlag.setSize(new Dimension(22, 22));
256
			jToggleButtonSetFlag.setLocation(new Point(275, 16));
257
		}
258
		return jToggleButtonSetFlag;
259
	}
260

  
261
	/**
262
	 * This method initializes jCboAssociatedLayer	
263
	 * 	
264
	 * @return javax.swing.JComboBox	
265
	 */
266
	private JComboBox getJCboAssociatedLayer() {
267
		if (jCboAssociatedLayer == null) {
268
			jCboAssociatedLayer = new JComboBox();
269
			jCboAssociatedLayer.setBounds(new Rectangle(137, 55, 124, 20));
270
		}
271
		return jCboAssociatedLayer;
272
	}
273

  
274
	/**
275
	 * This method initializes jBtnCalculate	
276
	 * 	
277
	 * @return javax.swing.JButton	
278
	 */
279
	private JButton getJBtnCalculate() {
280
		if (jBtnCalculate == null) {
281
			jBtnCalculate = new JButton();
282
			jBtnCalculate.setText("Calcular");
283
			jBtnClose.addActionListener(new ActionListener() {
284
				public void actionPerformed(ActionEvent e) {
285
					doConnectivityAnalisys();
286
				}				
287
			});
288
			
289
		}
290
		return jBtnCalculate;
291
	}
292

  
293
	protected void doConnectivityAnalisys() {
294
		MapContext map = mapCtrl.getMapContext();
295
		SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
296

  
297
		while (it.hasNext())
298
		{
299
			FLayer aux = it.next();
300
			if (!aux.isActive())
301
				continue;
302
			Network net = (Network) aux.getProperty("network");
303
			if ( net != null)
304
			{		
305
				selectListener = new SelectDijkstraListener(net, mapCtrl.getMapContext());
306
				selectListener.startSelection();
307
				try {
308
					if (getJRadioBtnNormalDirection().isSelected())
309
						doNormal(net, selectListener);
310
					else
311
						doReverse(net, selectListener);
312
				} catch (GraphException e) {
313
					// TODO Auto-generated catch block
314
					e.printStackTrace();
315
				}
316
				
317
				selectListener.stopSelection();
318
			}
319
		} //  WHILE
320
	}
321
	
322
	private void doNormal(Network net, SelectDijkstraListener selectListener2) {
323
		// TODO Auto-generated method stub
324
		
325
	}
326

  
327
	private void doReverse(Network net, IDijkstraListener selectListener) throws GraphException {
328
		ReverseOneToManySolver solver = new ReverseOneToManySolver();
329
		solver.setNetwork(net);
330
		solver.addListener(selectListener);
331
		if (getJChkBoxUseMaxCost().isSelected()) {
332
			solver.setMaxCost(Double.parseDouble(getJTxtMaxCost().getText()));
333
		}
334
		if (getJChkBoxUseMaxDist().isSelected()) {
335
			solver.setMaxDistance(Double.parseDouble(getJTxtMaxDist().getText()));
336
		}
337
		
338
		solver.addListener(selectListener);
339
		
340
		//		 Source flag
341
		GvFlag sourceFlag = net.createFlag(441901, 4475977, 10);
342
		solver.setSourceFlag(sourceFlag);
343

  
344
		long t1 = System.currentTimeMillis();
345
		solver.putDestinationsOnNetwork(net.getFlags());
346
		solver.calculate();
347
		solver.removeDestinationsFromNetwork(net.getFlags());
348
		long t2 = System.currentTimeMillis();
349
		System.out.println("tiempo:" + (t2-t1));
350
		
351
		GvFlag flags[] = net.getFlags();
352

  
353
		for (int i=0; i < flags.length; i++)
354
		{
355
			System.out.println("Flag " + i + " " + flags[i].getCost());
356
		}		
357
	}
358
 
359
	/**
360
	 * This method initializes jBtnClose	
361
	 * 	
362
	 * @return javax.swing.JButton	
363
	 */
364
	private JButton getJBtnClose() {
365
		if (jBtnClose == null) {
366
			jBtnClose = new JButton();
367
			jBtnClose.setText("Cerrar");
368
			jBtnClose.addActionListener(new ActionListener() {
369

  
370
				public void actionPerformed(ActionEvent e) {
371
					close();
372
				}
373
				
374
			});
375
		}
376
		return jBtnClose;
377
	}
378

  
379
	protected void close() {
380
		PluginServices.getMDIManager().closeWindow(this);
381
	}
382

  
383
	/**
384
	 * This method initializes jPanelOptions	
385
	 * 	
386
	 * @return javax.swing.JPanel	
387
	 */
388
	private JPanel getJPanelOptions() {
389
		if (jPanelOptions == null) {
390
			jPanelOptions = new JPanel();
391
			jPanelOptions.setLayout(null);
392
			jPanelOptions.setBounds(new Rectangle(15, 87, 315, 140));
393
			jPanelOptions.setBorder(BorderFactory.createTitledBorder(null, "Options", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 11), Color.black));
394
			ButtonGroup group = new ButtonGroup();
395
	        group.add(getJRadioBtnNormalDirection());
396
	        group.add(getJRadioBtnReverseDirection());
397
			jPanelOptions.add(getJRadioBtnNormalDirection(), null);
398
			jPanelOptions.add(getJRadioBtnReverseDirection(), null);
399
			jPanelOptions.add(getJChkBoxUseMaxDist(), null);
400
			jPanelOptions.add(getJChkBoxUseMaxCost(), null);
401
			jPanelOptions.add(getJTxtMaxDist(), null);
402
			jPanelOptions.add(getJTxtMaxCost(), null);
403
		}
404
		return jPanelOptions;
405
	}
406

  
407
	/**
408
	 * This method initializes jRadioBtnNormalDirection	
409
	 * 	
410
	 * @return javax.swing.JRadioButton	
411
	 */
412
	private JRadioButton getJRadioBtnNormalDirection() {
413
		if (jRadioBtnNormalDirection == null) {
414
			jRadioBtnNormalDirection = new JRadioButton();
415
			jRadioBtnNormalDirection.setBounds(new Rectangle(15, 25, 200, 21));
416
			jRadioBtnNormalDirection.setSelected(true);
417
			jRadioBtnNormalDirection.setText("Sentido normal");
418
		}
419
		return jRadioBtnNormalDirection;
420
	}
421

  
422
	/**
423
	 * This method initializes jRadioBtnReverseDirection	
424
	 * 	
425
	 * @return javax.swing.JRadioButton	
426
	 */
427
	private JRadioButton getJRadioBtnReverseDirection() {
428
		if (jRadioBtnReverseDirection == null) {
429
			jRadioBtnReverseDirection = new JRadioButton();
430
			jRadioBtnReverseDirection.setBounds(new Rectangle(15, 53, 180, 23));
431
			jRadioBtnReverseDirection.setText("Sentido inverso");
432
			jRadioBtnReverseDirection.setSelected(false);
433
		}
434
		return jRadioBtnReverseDirection;
435
	}
436

  
437
	/**
438
	 * This method initializes jChkBoxUseMaxDist	
439
	 * 	
440
	 * @return javax.swing.JCheckBox	
441
	 */
442
	private JCheckBox getJChkBoxUseMaxDist() {
443
		if (jChkBoxUseMaxDist == null) {
444
			jChkBoxUseMaxDist = new JCheckBox();
445
			jChkBoxUseMaxDist.setBounds(new Rectangle(15, 85, 168, 21));
446
			jChkBoxUseMaxDist.setText("Usar distancia m?xima:");
447
		}
448
		return jChkBoxUseMaxDist;
449
	}
450

  
451
	/**
452
	 * This method initializes jChkBoxUseMaxCost	
453
	 * 	
454
	 * @return javax.swing.JCheckBox	
455
	 */
456
	private JCheckBox getJChkBoxUseMaxCost() {
457
		if (jChkBoxUseMaxCost == null) {
458
			jChkBoxUseMaxCost = new JCheckBox();
459
			jChkBoxUseMaxCost.setBounds(new Rectangle(15, 112, 167, 23));
460
			jChkBoxUseMaxCost.setText("Usar coste m?ximo:");
461
		}
462
		return jChkBoxUseMaxCost;
463
	}
464

  
465
	/**
466
	 * This method initializes jTxtMaxDist	
467
	 * 	
468
	 * @return javax.swing.JTextField	
469
	 */
470
	private JTextField getJTxtMaxDist() {
471
		if (jTxtMaxDist == null) {
472
			jTxtMaxDist = new JTextField();
473
			jTxtMaxDist.setBounds(new Rectangle(190, 86, 95, 20));
474
		}
475
		return jTxtMaxDist;
476
	}
477

  
478
	/**
479
	 * This method initializes jTxtMaxCost	
480
	 * 	
481
	 * @return javax.swing.JTextField	
482
	 */
483
	private JTextField getJTxtMaxCost() {
484
		if (jTxtMaxCost == null) {
485
			jTxtMaxCost = new JTextField();
486
			jTxtMaxCost.setBounds(new Rectangle(190, 112, 95, 20));
487
		}
488
		return jTxtMaxCost;
489
	}
490

  
491
	public WindowInfo getWindowInfo() {
492
        if (wi==null) {
493
            wi = new WindowInfo(WindowInfo.MODELESSDIALOG | WindowInfo.PALETTE);
494
            wi.setWidth(345);
495
            wi.setHeight(345);
496
            wi.setMinimumSize(new Dimension(345, 345));
497
            wi.setTitle(PluginServices.getText(this, "connectivity_analisys") + "...");
498
        }
499
        return wi;
500

  
501
	}
502
	
503
	public void setMapControl(MapControl mc) throws ReadDriverException {
504
		this.mapCtrl = mc;
505
		SingleLayerIterator it = new SingleLayerIterator(mc.getMapContext().getLayers());
506
		DefaultComboBoxModel model = (DefaultComboBoxModel) getJCboAssociatedLayer().getModel();
507
		model.removeAllElements();
508
		while (it.hasNext()) {
509
			FLayer aux = it.next();
510
			if (aux instanceof FLyrVect) {
511
				FLyrVect lv = (FLyrVect) aux;
512
				if (lv.getShapeType() == FShape.POINT) {
513
					model.addElement(lv.getName());
514
				}
515
			}
516
		}
517
	}
518

  
519
	public Object getWindowProfile() {
520
		// TODO Auto-generated method stub
521
		return null;
522
	}
523

  
524
}  //  @jve:decl-index=0:visual-constraint="10,10"
trunk/extensions/extGraph/src/org/gvsig/graph/gui/RouteControlPanel.java
598 598
				return;
599 599
			}
600 600

  
601
			cost += flags[i].getCost();
601
			cost = flags[i].getCost(); // The last flag shows total cost
602 602
		}
603 603
		NumberFormat nf = NumberFormat.getInstance();
604 604
		nf.setMaximumFractionDigits(2);
trunk/extensions/extGraph/src/org/gvsig/graph/ConnectivityExtension.java
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 org.gvsig.graph;
42

  
43
import java.awt.Color;
44
import java.text.NumberFormat;
45
import java.util.Arrays;
46
import java.util.Random;
47

  
48
import javax.swing.ImageIcon;
49
import javax.swing.JOptionPane;
50

  
51
import org.gvsig.exceptions.BaseException;
52
import org.gvsig.graph.core.GraphException;
53
import org.gvsig.graph.core.GvFlag;
54
import org.gvsig.graph.core.GvNode;
55
import org.gvsig.graph.core.IGraph;
56
import org.gvsig.graph.core.Network;
57
import org.gvsig.graph.core.NetworkUtils;
58
import org.gvsig.graph.gui.ConnectivityControlPanel;
59
import org.gvsig.graph.gui.wizard.servicearea.ServiceAreaWizard;
60
import org.gvsig.graph.solvers.EdgesMemoryDriver;
61
import org.gvsig.graph.solvers.OneToManySolver;
62
import org.gvsig.graph.solvers.ServiceAreaExtractor;
63
import org.gvsig.graph.solvers.ServiceAreaExtractor2;
64

  
65
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
66
import com.hardcode.gdbms.engine.values.DoubleValue;
67
import com.hardcode.gdbms.engine.values.ValueFactory;
68
import com.iver.andami.PluginServices;
69
import com.iver.andami.plugins.Extension;
70
import com.iver.andami.ui.mdiManager.IWindow;
71
import com.iver.cit.gvsig.fmap.MapContext;
72
import com.iver.cit.gvsig.fmap.MapControl;
73
import com.iver.cit.gvsig.fmap.core.FShape;
74
import com.iver.cit.gvsig.fmap.core.IGeometry;
75
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
76
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
77
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
78
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
79
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
80
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
81
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
82
import com.iver.cit.gvsig.fmap.layers.FLayer;
83
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
84
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
85
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
86
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
87
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
88
import com.iver.cit.gvsig.fmap.rendering.FGraphicLabel;
89
import com.iver.cit.gvsig.fmap.rendering.IVectorialUniqueValueLegend;
90
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
91
import com.iver.cit.gvsig.project.documents.view.gui.View;
92

  
93
/**
94
 * @author fjp
95
 * 
96
 * Extension to perform Connectivity calculations. 
97
 * The user can put a flag and select a point associated layer, and set
98
 * if he wants to use a max cost and/or max distance.
99
 * The algorithm will explore the network in reverse order or in normal order and
100
 * the arcs reached will be selected. If the user selects an associated layer, the
101
 * points reached will be selected also.
102
 * This may be useful in the following situations:
103
 * 1.- Searching for connectivity. The unconnected parts of the network will be unselected.
104
 * 2.- Selecting points connected to the network and affected by a cut in the graph
105
 * 3.- Using reverse order, you may find the closest valve to close and avoid leaks.
106
 */
107
public class ConnectivityExtension extends Extension {
108

  
109
	private int idSymbolLine = -1;
110

  
111
	public void initialize() {
112
		PluginServices.getIconTheme().registerDefault(
113
				"connectivity",
114
				this.getClass().getClassLoader().getResource("images/connectivity.gif")
115
			);
116
		
117
	}
118

  
119
	public void execute(String actionCommand) {
120

  
121
		View v = (View) PluginServices.getMDIManager().getActiveWindow();
122
		MapControl mapCtrl = v.getMapControl();
123
		MapContext map = mapCtrl.getMapContext();
124
		SingleLayerIterator it = new SingleLayerIterator(map.getLayers());		
125
		while (it.hasNext()) {
126
			FLayer aux = it.next();
127
			if (!aux.isActive())
128
				continue;
129
			Network net = (Network) aux.getProperty("network");
130

  
131
			if (net != null) {
132
				if(actionCommand.equals("CONNECTIVITY")){
133
					ImageIcon icon = new ImageIcon(this.getClass().getClassLoader()
134
							.getResource("images/wizard_connectivity.png"));
135
					ConnectivityControlPanel w =new ConnectivityControlPanel();
136
					try {
137
						w.setMapControl(mapCtrl);
138
						PluginServices.getMDIManager().addWindow(w);						
139
					} catch (ReadDriverException e) {
140
						// TODO Auto-generated catch block
141
						e.printStackTrace();
142
					}
143
					
144
				}
145
				return;
146
			}
147
		}
148

  
149
	}
150

  
151
	/**
152
	 * @param mapCtrl
153
	 * @param map
154
	 * @param net
155
	 * @param flags
156
	 * @param solver
157
	 * @return
158
	 * @throws GraphException
159
	 */
160
	private void calculateLabels(MapControl mapCtrl, MapContext map, Network net, GvFlag[] flags, OneToManySolver solver) throws GraphException {
161
		GraphicLayer graphicLayer = mapCtrl.getMapContext()
162
				.getGraphicsLayer();
163
		removeOldLabels(graphicLayer);
164
		for (int i = 0; i < flags.length; i++) {
165

  
166
			solver.setSourceFlag(flags[i]);
167
			long t1 = System.currentTimeMillis();
168
			solver.setExploreAllNetwork(true);
169
			solver.calculate();
170
			long t2 = System.currentTimeMillis();
171
			System.out.println("Punto " + i + " de "
172
					+ flags.length + ". " + (t2 - t1)
173
					+ " msecs.");
174
			// Despu?s de esto, los nodos de la red est?n
175
			// etiquetados con los costes al nodo or?gen
176
			EdgesMemoryDriver driver = new EdgesMemoryDriver(net);
177
			FLayer lyr = LayerFactory.createLayer("Edges", driver, null);
178
			map.getLayers().addLayer(lyr);
179
			// doLabeling(mapCtrl, net, flags[i]);
180

  
181
		}
182
	
183
	}
184

  
185
	private void calculateServiceArea(MapContext map, Network net, GvFlag[] flags, OneToManySolver solver) throws BaseException {
186
		ServiceAreaExtractor2 extractor = new ServiceAreaExtractor2(net);
187
		String aux = JOptionPane.showInputDialog("Por favor, introduzca el coste m?ximo del ?rea de servicio:");
188
		if (aux == null)
189
			return;
190
		double[] costs = NetworkUtils.string2doubleArray(aux, ",");
191
		Arrays.sort(costs);
192
		solver.addListener(extractor);
193
		for (int i = 0; i < flags.length; i++) {
194
			solver.setSourceFlag(flags[i]);
195
			long t1 = System.currentTimeMillis();			
196
			solver.setExploreAllNetwork(true);
197
			// TODO: El coste m?ximo deber?a ser un array de costes m?ximos
198
			// para obtener varios pol?gonos por cada intervalo. Y 2 opciones m?s:
199
			// Si se desea ?rea compacta y
200
			// Si los pol?gonos se quieren como anillos conc?ntricos.
201
			// Cada Flag deber?a tener asociado un array de costes m?ximos o distancias
202
			solver.setMaxCost(costs[costs.length -1]);
203
			extractor.setIdFlag(i);
204
			extractor.setCosts(costs);
205
			
206
			extractor.setDoCompactArea(true);
207
			
208
			solver.calculate();
209
			long t2 = System.currentTimeMillis();
210
			System.out.println("Punto " + i + " de "
211
					+ flags.length + ". " + (t2 - t1)
212
					+ " msecs.");
213
			extractor.writeServiceArea();
214
						
215
		}
216
		extractor.closeFiles();
217
		FLyrVect lyrPol = extractor.getPolygonLayer();
218
		lyrPol.setProjection(map.getProjection());
219
		IVectorialUniqueValueLegend defaultLegend = LegendFactory.createVectorialUniqueValueLegend(FShape.POLYGON);
220
		defaultLegend.setClassifyingFieldNames(new String[] {"COST"} );
221
		ISymbol myDefaultSymbol = SymbologyFactory.
222
			createDefaultSymbolByShapeType(FShape.POLYGON);
223

  
224
		defaultLegend.setDefaultSymbol(myDefaultSymbol);
225

  
226
		DoubleValue clave;
227
		IFillSymbol theSymbol = null;
228
		Random rnd = new Random(System.currentTimeMillis());
229

  
230
		for (int j = 0; j < costs.length; j++) {
231
			clave = ValueFactory.createValue(costs[j]);
232
			if (defaultLegend.getSymbolByValue(clave) == null) {
233
				theSymbol =	(IFillSymbol) SymbologyFactory.
234
					createDefaultSymbolByShapeType(FShape.POLYGON);
235
				theSymbol.setDescription(clave.toString());
236
				Color newColor = new Color(rnd.nextFloat(), 
237
						rnd.nextFloat(),
238
						rnd.nextFloat(), 0.7f);
239
				theSymbol.setFillColor(newColor);
240

  
241
				defaultLegend.addSymbol(clave, theSymbol);
242
			}
243

  
244
		} // for
245
		lyrPol.setLegend(defaultLegend);
246
		
247
		FLyrVect lyrLine = extractor.getLineLayer();
248
		lyrLine.setProjection(map.getProjection());
249

  
250
		// provisional
251
		FLyrVect lyrPoints= extractor.getBorderPoints();
252
		lyrPoints.setProjection(map.getProjection());
253
		
254
		map.beginAtomicEvent();
255
		map.getLayers().addLayer(lyrPol);
256
		map.getLayers().addLayer(lyrLine);
257
		map.getLayers().addLayer(lyrPoints);
258
		map.endAtomicEvent();
259

  
260
	
261
	}
262
	
263
	private FSymbol getTextSymbol() {
264
		FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_TEXT);
265
		theSymbol.setColor(Color.RED);
266
		theSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_CIRCLE);
267
		theSymbol.setFontColor(Color.BLACK);
268
		theSymbol.setSizeInPixels(true);
269
		theSymbol.setSize(9);
270
		return theSymbol;
271
	}
272

  
273
	private void removeOldLabels(GraphicLayer gLyr) {
274
		for (int i = gLyr.getNumGraphics() - 1; i >= 0; i--) {
275
			FGraphic gr = gLyr.getGraphic(i);
276
			if (gr.equals("N"))
277
				gLyr.removeGraphic(i);
278

  
279
		}
280
	}
281

  
282
	private void doLabeling(MapControl mapControl, Network net, GvFlag flag) {
283
		GraphicLayer graphicLayer = mapControl.getMapContext()
284
				.getGraphicsLayer();
285
		IGraph g = net.getGraph();
286
		int idSymbol = graphicLayer.addSymbol(getTextSymbol());
287
		String tag = "N";
288
		for (int i = 0; i < g.numVertices(); i++) {
289
			GvNode node = g.getNodeByID(i);
290
			IGeometry geom = ShapeFactory.createPoint2D(node.getX(), node
291
					.getY());
292
			NumberFormat nf = NumberFormat.getInstance();
293
			nf.setMaximumFractionDigits(1);
294
			String aux = "\u221E"; // infinito
295
			if (node.getBestCost() < Double.MAX_VALUE)
296
				aux = nf.format(node.getBestCost()) + " - " + nf.format(node.getAccumulatedLength());
297
			FGraphicLabel theGLabel = new FGraphicLabel(geom, idSymbol, aux);
298
			theGLabel.setObjectTag(tag);
299
			theGLabel.getLabel().setJustification(FLabel.CENTER_TOP);
300
			graphicLayer.addGraphic(theGLabel);
301
		}
302
		mapControl.drawGraphics();
303

  
304
	}
305

  
306
	public boolean isEnabled() {
307
		IWindow window = PluginServices.getMDIManager().getActiveWindow();
308
		if (window instanceof View)
309
		{
310
			View v = (View) window;
311
	        MapControl mapCtrl = v.getMapControl();
312
			MapContext map = mapCtrl.getMapContext();
313
			
314
			SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
315
			while (it.hasNext())
316
			{
317
				FLayer aux = it.next();
318
				if (!aux.isActive())
319
					continue;
320
				Network net = (Network) aux.getProperty("network");
321
				
322
				if ( net != null)
323
				{
324
					if (net.getFlags().length > 0)
325
					{
326
						return true;
327
					}
328
				}
329
			}
330
		}
331
		return false;
332
	}
333

  
334
	public boolean isVisible() {
335
		IWindow f = PluginServices.getMDIManager()
336
		 .getActiveWindow();
337
		if (f == null) {
338
		    return false;
339
		}
340
		if (f instanceof View) {
341
			return true;
342
		}
343
		return false;
344

  
345
	}
346

  
347
}
trunk/extensions/extGraph/src/org/gvsig/graph/solvers/IDijkstraListener.java
48 48
import org.gvsig.graph.core.GvNode;
49 49

  
50 50
public interface IDijkstraListener {
51
	/**
52
	 * @param node
53
	 * @return true if this is the final node you are searching. false otherwise.
54
	 */
51 55
	boolean minimumCostNodeSelected(GvNode node);
52 56
	
57
	/**
58
	 * @param fromNode
59
	 * @param edge
60
	 * @return true if this edge is valid for the solver. false if the algorithm should
61
	 * bypass this edge
62
	 */
53 63
	boolean adjacentEdgeVisited(GvNode fromNode, GvEdge edge);
54 64

  
55 65
}
trunk/extensions/extGraph/src/org/gvsig/graph/solvers/ServiceAreaExtractor2_From_Branch.java
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

  
42
// 18/09/2007 fjp
43
// @author: Fco. Jos? Pe?arrubia	fpenarru@gmail.com
44
package org.gvsig.graph.solvers;
45

  
46
import java.io.File;
47
import java.sql.Types;
48
import java.util.ArrayList;
49
import java.util.HashMap;
50
import java.util.HashSet;
51
import java.util.Iterator;
52
import java.util.Map;
53
import java.util.Set;
54

  
55
import org.gvsig.exceptions.BaseException;
56
import org.gvsig.fmap.algorithm.triangulation.visad.DelaunayWatson;
57
import org.gvsig.graph.core.GvEdge;
58
import org.gvsig.graph.core.GvNode;
59
import org.gvsig.graph.core.IGraph;
60
import org.gvsig.graph.core.Network;
61
import org.gvsig.graph.core.NetworkUtils;
62

  
63
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
64
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException;
65
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
66
import com.hardcode.gdbms.engine.values.Value;
67
import com.hardcode.gdbms.engine.values.ValueFactory;
68
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
69
import com.iver.cit.gvsig.fmap.core.FShape;
70
import com.iver.cit.gvsig.fmap.core.IGeometry;
71
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
72
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
73
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
74
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
75
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
76
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
77
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
78
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
79
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
80
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
81
import com.vividsolutions.jts.algorithm.CGAlgorithms;
82
import com.vividsolutions.jts.algorithm.CentroidPoint;
83
import com.vividsolutions.jts.geom.Coordinate;
84
import com.vividsolutions.jts.geom.Geometry;
85
import com.vividsolutions.jts.geom.GeometryFactory;
86
import com.vividsolutions.jts.geom.LineString;
87
import com.vividsolutions.jts.geom.LinearRing;
88
import com.vividsolutions.jts.geom.Polygon;
89

  
90
/**
91
 * @author fjp
92
 * FROM BRANCH
93
 * 
94
 * This class can label nodes with distances and costs to a flag. You will
95
 * obtain a temp shp layer with fields IdArc, IdEdge, CostOrig, DistOrig,
96
 * CostEnd, DistEnd, IdFlag
97
 * 
98
 * La diferencia con ServiceAreaExtractor es que esta versi?n escucha al
99
 * algoritmo Dijkstra, y va montando el shp de l?neas conforme va siendo
100
 * explorada la red. La gran ventaja de hacerlo as? es que no dependes del
101
 * tama?o de la red. Solo recorres los tramos y nodos que exploras, de forma que
102
 * si limitas el ?rea de servicio a una distancia m?xima, la red solo se explora
103
 * hasta esa distancia / coste.
104
 * 
105
 */
106
public class ServiceAreaExtractor2_From_Branch implements IDijkstraListener {
107
	private static String tempDirectoryPath = System
108
			.getProperty("java.io.tmpdir");
109

  
110
	static FieldDescription[] fields = new FieldDescription[7];
111
	static {
112
		FieldDescription fieldDesc = new FieldDescription();
113
		fieldDesc.setFieldName("IDARC");
114
		fieldDesc.setFieldType(Types.INTEGER);
115
		fieldDesc.setFieldLength(20);
116
		fieldDesc.setFieldDecimalCount(0);
117
		fields[0] = fieldDesc;
118

  
119
		fieldDesc = new FieldDescription();
120
		fieldDesc.setFieldName("IDEDGE");
121
		fieldDesc.setFieldType(Types.INTEGER);
122
		fieldDesc.setFieldLength(20);
123
		fieldDesc.setFieldDecimalCount(0);
124
		fields[1] = fieldDesc;
125

  
126
		fieldDesc = new FieldDescription();
127
		fieldDesc.setFieldName("COSTORIG");
128
		fieldDesc.setFieldType(Types.DOUBLE);
129
		fieldDesc.setFieldLength(20);
130
		fieldDesc.setFieldDecimalCount(5);
131
		fields[2] = fieldDesc;
132

  
133
		fieldDesc = new FieldDescription();
134
		fieldDesc.setFieldName("DISTORIG");
135
		fieldDesc.setFieldType(Types.DOUBLE);
136
		fieldDesc.setFieldLength(20);
137
		fieldDesc.setFieldDecimalCount(5);
138
		fields[3] = fieldDesc;
139

  
140
		fieldDesc = new FieldDescription();
141
		fieldDesc.setFieldName("COSTEND");
142
		fieldDesc.setFieldType(Types.DOUBLE);
143
		fieldDesc.setFieldLength(20);
144
		fieldDesc.setFieldDecimalCount(5);
145
		fields[4] = fieldDesc;
146

  
147
		fieldDesc = new FieldDescription();
148
		fieldDesc.setFieldName("DISTEND");
149
		fieldDesc.setFieldType(Types.DOUBLE);
150
		fieldDesc.setFieldLength(20);
151
		fieldDesc.setFieldDecimalCount(5);
152
		fields[5] = fieldDesc;
153

  
154
		fieldDesc = new FieldDescription();
155
		fieldDesc.setFieldName("IDFLAG");
156
		fieldDesc.setFieldType(Types.INTEGER);
157
		fieldDesc.setFieldLength(20);
158
		fieldDesc.setFieldDecimalCount(5);
159
		fields[6] = fieldDesc;
160

  
161
	}
162
	
163
	static FieldDescription[] fieldsPol = new FieldDescription[2];
164
	static {
165
		FieldDescription fieldDesc = new FieldDescription();
166
		fieldDesc.setFieldName("COST");
167
		fieldDesc.setFieldType(Types.DOUBLE);
168
		fieldDesc.setFieldLength(20);
169
		fieldDesc.setFieldDecimalCount(5);
170
		fieldsPol[0] = fieldDesc;
171
		
172
		fieldDesc = new FieldDescription();
173
		fieldDesc.setFieldName("IDFLAG");
174
		fieldDesc.setFieldType(Types.INTEGER);
175
		fieldDesc.setFieldLength(20);
176
		fieldDesc.setFieldDecimalCount(5);
177
		fieldsPol[1] = fieldDesc;
178

  
179
	}
180
	
181
	static GeometryFactory gf = new GeometryFactory();
182

  
183
	private class VisitedEdge {
184
		private GvEdge edge;
185
		private double percentcost;
186
		public VisitedEdge(GvEdge edge) {
187
			this.edge = edge;
188
			IGraph g = net.getGraph();
189
			GvNode nOrig = g.getNodeByID(edge.getIdNodeOrig());
190
			double maxCost = costs[costs .length-1];
191
			double costCalculated = nOrig.getBestCost() + edge.getWeight();
192
			
193
			if (costCalculated < maxCost)
194
				percentcost = 1.0;
195
			else
196
			{
197
				double percentCostCalculated = (maxCost - nOrig.getBestCost())/ edge.getWeight();
198
				percentcost = percentCostCalculated;
199
			}
200
			
201
		}
202
		public GvEdge getEdge() {
203
			return edge;
204
		}
205
		public double getPercentcost() {
206
			return percentcost;
207
		}
208
		public void setPercentCost(double d) {
209
			this.percentcost = d;
210
			
211
		}
212
	}
213

  
214
	private Network net;
215

  
216
	private ShpWriter shpWriter;
217
	private ShpWriter shpWriterPol;
218
	private ShpWriter shpWriterTri;
219
	private File fTempPol;
220
	private File fTempTri;
221
	private SHPLayerDefinition layerDefPol;
222
	private SHPLayerDefinition layerDefTri;
223
	
224
	
225
	private HashMap<String, VisitedEdge> visitedEdges = new HashMap();
226

  
227
	private File fTemp;
228

  
229
	private SHPLayerDefinition layerDef;
230

  
231
	private int idFlag;
232

  
233
	private ReadableVectorial adapter;
234

  
235
//	private double maxCost;
236

  
237
	private Geometry serviceArea;
238
	private ArrayList <Geometry> serviceAreaPolygons;
239

  
240
	private double[] costs = null;	
241
	
242
	private boolean bDoCompactArea = false;
243
	
244
	private ArrayList borderCoords = new ArrayList();
245

  
246
	private HashSet<Coordinate> nodes;
247
//	DelaunayFast tri2;
248
	DelaunayWatson tri2;
249

  
250
	/**
251
	 * @param net
252
	 * @throws Exception 
253
	 * @throws InitializeWriterException
254
	 * @throws ReadDriverException
255
	 * @throws InitializeDriverException
256
	 */
257
	public ServiceAreaExtractor2_From_Branch(Network net) throws Exception {
258
		this.net = net;
259
		int aux = (int) (Math.random() * 1000);
260
		
261
		nodes = new HashSet<Coordinate>();
262
		
263
		
264
		String nameLine = "tmpServiceAreaLine" + aux + ".shp";
265
		String namePol = "tmpServiceAreaPol" + aux + ".shp";
266
		String nameTri = "tmpTri" + aux + ".shp";
267
		fTemp = new File(tempDirectoryPath + "/" + nameLine );
268
		fTempPol = new File(tempDirectoryPath + "/" + namePol );
269
		fTempTri = new File(tempDirectoryPath + "/" + nameTri );
270
		
271
		layerDef = new SHPLayerDefinition();
272
		layerDef.setFile(fTemp);
273
		layerDef.setName(nameLine);		
274
		layerDef.setFieldsDesc(fields);
275
		layerDef.setShapeType(FShape.LINE);
276

  
277
		layerDefPol = new SHPLayerDefinition();
278
		layerDefPol.setFile(fTempPol);
279
		layerDefPol.setName(namePol);		
280
		layerDefPol.setFieldsDesc(fieldsPol);
281
		layerDefPol.setShapeType(FShape.POLYGON);
282

  
283
		layerDefTri = new SHPLayerDefinition();
284
		layerDefTri.setFile(fTempTri);
285
		layerDefTri.setName(nameTri);		
286
		layerDefTri.setFieldsDesc(fieldsPol);
287
		layerDefTri.setShapeType(FShape.POLYGON);
288
		
289
		shpWriter = new ShpWriter();
290
		shpWriter.setFile(fTemp);
291
		shpWriter.initialize(layerDef);
292

  
293
		shpWriterPol = new ShpWriter();
294
		shpWriterPol.setFile(fTempPol);
295
		shpWriterPol.initialize(layerDefPol);
296
		
297
		shpWriterTri = new ShpWriter();
298
		shpWriterTri.setFile(fTempTri);
299
		shpWriterTri.initialize(layerDefTri);
300
		
301
		shpWriter.preProcess();
302
		shpWriterPol.preProcess();
303
		shpWriterTri.preProcess();
304
		
305
		FLyrVect lyr = net.getLayer();
306
		adapter = lyr.getSource();
307
		adapter.start();
308
		
309
		serviceAreaPolygons = new ArrayList<Geometry>();
310

  
311
	}
312

  
313
	/**
314
	 * Devuelve el ?ndice del intervalo m?s alto que contiene a ese valor.
315
	 * @param bestCost
316
	 * @param costs
317
	 * @return
318
	 */
319
	private int getCostInterval(double bestCost, double[] costs) {
320
		int ret = 0;
321
		if (bestCost > costs[costs.length-1])
322
			return -1;
323
		for (int i=costs.length-1; i>=0; i--) {
324
			if (bestCost > costs[i])
325
			{
326
				ret = i+1;
327
				break;
328
			}
329
		}
330
		return ret;
331
	}
332

  
333
	/**
334
	 * We process each edge and prepare a list of polygons, classified by
335
	 * cost
336
	 * @param edge
337
	 * @param nodeOrig
338
	 * @param nodeEnd
339
	 * @param geom
340
	 * @param costs
341
	 */
342
	private void processEdgeForPolygon(GvEdge edge, GvNode nodeOrig, GvNode nodeEnd, IGeometry geom, double[] costs) {
343
		if (nodeEnd.getBestCost() > nodeOrig.getBestCost())
344
		{		
345
			// miramos en qu? pol?gono cae ese edge POR COMPLETO 
346
			// El coste de su punto final es menor que uno de los costes.
347
			int indexInterval = getCostInterval(nodeEnd.getBestCost(), costs);
348
			// Un pol?gono por cada zona
349
			Geometry jtsGeom = geom.toJTSGeometry();
350
			if (indexInterval != -1)
351
			{
352
				for (int i=costs.length-1; i >= indexInterval; i--) {
353
					calculateConvexHull(jtsGeom, i);
354
				}
355
			}
356
			double maxCost = costs[costs.length-1];
357
			// Es -1 si caso l?mite externo
358
			if (indexInterval < costs.length-1)
359
			{
360
				// Caso l?mite externo
361
				if ((nodeEnd.getBestCost() > maxCost) &&						
362
						(nodeOrig.getBestCost() < maxCost))
363
				{
364
					double pct = (maxCost - nodeOrig.getBestCost())/ edge.getWeight();
365
					LineString partial = NetworkUtils.getPartialLineString(jtsGeom, pct, edge.getDirec());
366
					calculateConvexHull(partial, costs.length-1);
367
					return;
368
				}
369
				// Parcial interno
370
				maxCost = costs[indexInterval+1];
371
				if ((nodeOrig.getBestCost() < maxCost) &&
372
						(nodeEnd.getBestCost() > maxCost)) 
373
				{
374
					// A ese tramo hemos llegado parcialmente
375
					 
376
					double pct = (maxCost - nodeOrig.getBestCost())/ edge.getWeight();
377
					try {
378
						LineString partial = NetworkUtils.getPartialLineString(jtsGeom, pct, edge.getDirec());
379
						calculateConvexHull(partial, indexInterval+1);							
380
					}
381
					catch (Exception e)
382
					{
383
						e.printStackTrace();
384
					}
385
					
386
				}
387
			}
388
		} 
389

  
390
		
391
	}
392

  
393
	/**
394
	 * @param jtsGeom
395
	 * @param i
396
	 */
397
	private void calculateConvexHull(Geometry jtsGeom, int i) {
398
		if (serviceAreaPolygons.size() <= i) { // se crea por primera vez
399
			Geometry gIni = jtsGeom; 
400
			serviceAreaPolygons.add(i, gIni);
401
		}
402
		else
403
		{
404
			Geometry antG = serviceAreaPolygons.get(i);
405
			if (antG == null)
406
				antG = jtsGeom;
407
			else
408
			{
409
				antG = antG.union(jtsGeom);				
410
			}
411
			antG = antG.convexHull();
412
			serviceAreaPolygons.set(i, antG);
413
		}
414
	}
415

  
416

  
417
	private void writePartialEdge(int i, IGeometry geom, GvEdge edge, GvNode nodeOrig, GvNode nodeEnd, int idFlag, double maxCost) throws BaseException {
418
		Geometry jtsGeom = geom.toJTSGeometry();
419
		double pct = (maxCost - nodeOrig.getBestCost())/ edge.getWeight();
420
		if (edge.getDirec() == 0) // Sentido inverso
421
			pct = 1-pct;
422
		LineString partial = NetworkUtils.getPartialLineString(jtsGeom, pct, edge.getDirec());
423
		if (serviceArea == null)
424
			serviceArea = partial;
425
		else
426
		{
427
			serviceArea = serviceArea.union(partial);
428
			serviceArea = serviceArea.convexHull();
429
		}
430
		
431
		IGeometry newGeom = FConverter.jts_to_igeometry(partial);
432

  
433
		Value[] values = new Value[7];
434
		values[0] = ValueFactory.createValue(i);
435
		values[1] = ValueFactory.createValue(edge.getIdEdge());
436
		values[2] = ValueFactory.createValue(nodeOrig.getBestCost());
437
		values[3] = ValueFactory.createValue(nodeOrig.getAccumulatedLength());
438
		values[4] = ValueFactory.createValue(maxCost);
439
		values[5] = ValueFactory.createValue(nodeOrig.getAccumulatedLength() + edge.getDistance()*pct);
440
		values[6] = ValueFactory.createValue(idFlag);
441
		DefaultFeature feat = new DefaultFeature(newGeom, values);
442
		IRowEdited row = new DefaultRowEdited(feat, DefaultRowEdited.STATUS_ADDED, i);
443
		shpWriter.process(row);
444
		
445
//		// TODO: TROZO DEL RESTO
446
//		int direc = 0;
447
//		pct = pct+0.02;
448
//		if (edge.getDirec() == 0)
449
//		{
450
//			direc = 1;
451
//			pct = pct - 0.04;
452
//		}
453
//		LineString partial2 = NetworkUtils.getPartialLineString(jtsGeom, pct, direc);		
454
//		IGeometry newGeom2 = FConverter.jts_to_igeometry(partial2);
455
//		values[6] = ValueFactory.createValue(-2);
456
//		DefaultFeature feat2 = new DefaultFeature(newGeom2, values);
457
//		IRowEdited row2 = new DefaultRowEdited(feat2, DefaultRowEdited.STATUS_ADDED, i);
458
//		shpWriter.process(row2);
459

  
460
		borderCoords.add(partial.getCoordinateN(partial.getNumPoints()-1)); 
461
		
462
		if (bDoCompactArea) {
463
//			processCompact(partial);
464
			Coordinate cLimit = null;
465
			if (edge.getDirec() == 0) // Sentido inverso
466
				cLimit = partial.getCoordinateN(0);
467
			else
468
				cLimit = partial.getCoordinateN(partial.getNumPoints()-1);
469
			processCompact(cLimit.x, cLimit.y);
470
//			processCompact(nodeEnd.getX(), nodeEnd.getY());
471
		}
472
		
473
	}
474
	
475
	public FLyrVect getBorderPoints() throws Exception {
476
		Value[] values = new Value[1];
477
		values[0] = ValueFactory.createValue(costs[costs.length-1]);
478

  
479
		File fTempPoints = new File(tempDirectoryPath + "/borderPoints.shp");
480
		
481
		FieldDescription[] fieldsPoints = new FieldDescription[1];
482
		FieldDescription fieldDesc = new FieldDescription();
483
		fieldDesc.setFieldName("COST");
484
		fieldDesc.setFieldType(Types.DOUBLE);
485
		fieldDesc.setFieldLength(20);
486
		fieldDesc.setFieldDecimalCount(5);
487
		fieldsPoints[0] = fieldDesc;
488

  
489
		SHPLayerDefinition layerDef = new SHPLayerDefinition();
490
		layerDef.setFile(fTempPoints);
491
		layerDef.setName("BorderPoints");		
492
		layerDef.setFieldsDesc(fieldsPoints);
493
		layerDef.setShapeType(FShape.POINT);
494

  
495
		
496
		ShpWriter shpWriter = new ShpWriter();
497
		shpWriter.setFile(fTempPoints);
498
		shpWriter.initialize(layerDef);
499

  
500
		int i=0;
501
		for (Iterator it = borderCoords.iterator(); it.hasNext();) {
502
			Coordinate c = (Coordinate) it.next();
503
			IGeometry geom = ShapeFactory.createPoint2D(c.x, c.y);
504
			DefaultFeature feat = new DefaultFeature(geom, values);
505
			IRowEdited row = new DefaultRowEdited(feat,
506
					DefaultRowEdited.STATUS_ADDED, i++);
507
			shpWriter.process(row);
508
			
509
		}
510
		shpWriter.postProcess();
511
		
512
		FLyrVect lyr = (FLyrVect) LayerFactory.createLayer(layerDef.getName(), "gvSIG shp driver", 
513
				layerDef.getFile(), null);
514
		return lyr;
515

  
516
		
517
	}
518

  
519
	private void processCompact(LineString partial) {
520
		Coordinate cIni = partial.getCoordinateN(0);
521
		Coordinate cEnd = partial.getCoordinateN(partial.getNumPoints()-1);
522
//		System.out.println("PARTIAL c1=" + cIni + " cEnd=" + cEnd);
523
		processCompact(cIni.x, cIni.y);
524
		processCompact(cEnd.x, cEnd.y);
525
	}
526

  
527
	private void writeTotalEdge(int i, IGeometry geom, GvEdge edge,
528
			GvNode nodeOrig, GvNode nodeEnd, int idFlag)
529
			throws Exception {
530
		Value[] values = new Value[7];
531
		values[0] = ValueFactory.createValue(i);
532
		values[1] = ValueFactory.createValue(edge.getIdEdge());
533
		values[2] = ValueFactory.createValue(nodeOrig.getBestCost());
534
		values[3] = ValueFactory.createValue(nodeOrig.getAccumulatedLength());
535
		values[4] = ValueFactory.createValue(nodeEnd.getBestCost());
536
		values[5] = ValueFactory.createValue(nodeEnd.getAccumulatedLength());
537
		values[6] = ValueFactory.createValue(idFlag);
538
		
539
		if (bDoCompactArea) {
540
//			System.out.println(" c1=" + cIni + " cEnd=" + cEnd);
541
//			processCompact(nodeOrig.getX(), nodeOrig.getY());
542
//			processCompact(nodeEnd.getX(), nodeEnd.getY());
543
		}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff