root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / core / NetworkRedLoader.java @ 8028
History | View | Annotate | Download (7.35 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.core; |
42 |
|
43 |
import java.io.File; |
44 |
import java.io.FileNotFoundException; |
45 |
import java.io.IOException; |
46 |
import java.io.RandomAccessFile; |
47 |
import java.nio.ByteOrder; |
48 |
import java.nio.MappedByteBuffer; |
49 |
import java.nio.channels.FileChannel; |
50 |
import java.util.ArrayList; |
51 |
|
52 |
import edu.uci.ics.jung.graph.Graph; |
53 |
import edu.uci.ics.jung.graph.Vertex; |
54 |
import edu.uci.ics.jung.graph.decorators.Indexer; |
55 |
import edu.uci.ics.jung.graph.impl.DirectedSparseEdge; |
56 |
import edu.uci.ics.jung.graph.impl.DirectedSparseVertex; |
57 |
import edu.uci.ics.jung.graph.impl.SparseGraph; |
58 |
|
59 |
/**
|
60 |
* @author fjp
|
61 |
*
|
62 |
* Primero vienen los arcos, y luego los nodos. En la cabecera, 3 enteros
|
63 |
* con el numero de tramos, el de arcos y el de nodos.
|
64 |
*
|
65 |
*/
|
66 |
public class NetworkRedLoader implements INetworkLoader { |
67 |
|
68 |
private File netFile = new File("c:/ejes.red"); |
69 |
|
70 |
public IGraph loadNetwork() {
|
71 |
|
72 |
long t1 = System.currentTimeMillis(); |
73 |
|
74 |
int numArcs;
|
75 |
int numEdges;
|
76 |
int numNodes;
|
77 |
|
78 |
short sentidoDigit; // => 1 en esa direcci?n. 0=> Al contrario. SOLO |
79 |
// SE UTILIZA PARA LOS CALCULOS POR IDTRAMO Y
|
80 |
// PORCENTAJE
|
81 |
// PARA SABER SI EST? M?S CERCA DE UN NODO O DEL OTRO.
|
82 |
|
83 |
|
84 |
RandomAccessFile file;
|
85 |
try {
|
86 |
file = new RandomAccessFile(netFile.getPath(), |
87 |
"r");
|
88 |
FileChannel channel = file.getChannel();
|
89 |
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); |
90 |
buf.order(ByteOrder.LITTLE_ENDIAN);
|
91 |
|
92 |
numArcs = buf.getInt(); |
93 |
numEdges = buf.getInt(); |
94 |
numNodes = buf.getInt(); |
95 |
|
96 |
GvGraph g = new GvGraph(numArcs, numEdges, numNodes);
|
97 |
|
98 |
// Nodes
|
99 |
buf.position(24*numEdges + 12); |
100 |
for (int i=0; i < numNodes; i++) |
101 |
{ |
102 |
GvNode node = readNode(buf); |
103 |
g.addNode(node); |
104 |
} |
105 |
// Arcos
|
106 |
buf.position(12);
|
107 |
for (int i=0; i < numEdges; i++) |
108 |
{ |
109 |
GvEdge edge = readEdge(buf); |
110 |
g.addEdge(edge); |
111 |
GvNode node = g.getNodeByID(edge.getIdNodeOrig()); |
112 |
node.getEnlaces().add(edge); |
113 |
EdgePair edgePair = g.getEdgesByIdArc(edge.getIdArc()); |
114 |
if (edgePair == null) |
115 |
{ |
116 |
edgePair = new EdgePair();
|
117 |
g.addEdgePair(edgePair); |
118 |
} |
119 |
if (edge.getDirec() == 1) |
120 |
edgePair.setIdEdge(i); |
121 |
else
|
122 |
edgePair.setIdInverseEdge(i); |
123 |
|
124 |
} |
125 |
|
126 |
long t2 = System.currentTimeMillis(); |
127 |
System.out.println("Tiempo de carga: " + (t2-t1) + " msecs"); |
128 |
System.out.println("NumEdges = " + g.getNumEdges()); |
129 |
return g;
|
130 |
} catch (FileNotFoundException e) { |
131 |
// TODO Auto-generated catch block
|
132 |
e.printStackTrace(); |
133 |
} catch (IOException e) { |
134 |
// TODO Auto-generated catch block
|
135 |
e.printStackTrace(); |
136 |
} |
137 |
|
138 |
return null; |
139 |
} |
140 |
|
141 |
public Graph loadJungNetwork()
|
142 |
{ |
143 |
SparseGraph g = new SparseGraph();
|
144 |
long t1 = System.currentTimeMillis(); |
145 |
|
146 |
RandomAccessFile file;
|
147 |
try {
|
148 |
file = new RandomAccessFile(netFile.getPath(), |
149 |
"r");
|
150 |
FileChannel channel = file.getChannel();
|
151 |
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); |
152 |
buf.order(ByteOrder.LITTLE_ENDIAN);
|
153 |
|
154 |
int numArcs = buf.getInt();
|
155 |
int numEdges = buf.getInt();
|
156 |
int numNodes = buf.getInt();
|
157 |
|
158 |
// Nodes
|
159 |
buf.position(24*numEdges + 12); |
160 |
for (int i=0; i < numNodes; i++) |
161 |
{ |
162 |
GvNode node = readNode(buf); |
163 |
|
164 |
Vertex v = new DirectedSparseVertex();
|
165 |
// v.addUserDatum("ID", node.idNode, UserData.CLONE);
|
166 |
// v.addUserDatum("X", node.x, UserData.CLONE);
|
167 |
// v.addUserDatum("Y", node.y, UserData.CLONE);
|
168 |
// v_locations.setLocation(v, new Point2D.Double(x.doubleValue(),y.doubleValue()));
|
169 |
g.addVertex(v); |
170 |
} |
171 |
Indexer indexer = Indexer.getIndexer(g); |
172 |
|
173 |
buf.position(12);
|
174 |
for (int i=0; i < numEdges; i++) |
175 |
{ |
176 |
GvEdge edge = readEdge(buf); |
177 |
|
178 |
int nodeOrig = edge.getIdNodeOrig();
|
179 |
int nodeEnd = edge.getIdNodeEnd();
|
180 |
|
181 |
Vertex vFrom = (Vertex) indexer.getVertex(nodeOrig); |
182 |
Vertex vTo = (Vertex) indexer.getVertex(nodeEnd); |
183 |
|
184 |
DirectedSparseEdge edgeJ = new DirectedSparseEdge(vFrom, vTo);
|
185 |
g.addEdge(edgeJ); |
186 |
} |
187 |
long t2 = System.currentTimeMillis(); |
188 |
System.out.println("Tiempo de carga: " + (t2-t1) + " msecs"); |
189 |
return g;
|
190 |
} catch (FileNotFoundException e) { |
191 |
// TODO Auto-generated catch block
|
192 |
e.printStackTrace(); |
193 |
} catch (IOException e) { |
194 |
// TODO Auto-generated catch block
|
195 |
e.printStackTrace(); |
196 |
} |
197 |
return null; |
198 |
} |
199 |
|
200 |
private GvNode readNode(MappedByteBuffer buf) { |
201 |
GvNode node = new GvNode();
|
202 |
node.setIdNode(buf.getInt()); |
203 |
node.setX(buf.getDouble()); |
204 |
node.setY(buf.getDouble()); |
205 |
return node;
|
206 |
} |
207 |
|
208 |
private GvEdge readEdge(MappedByteBuffer buf) { |
209 |
GvEdge edge = new GvEdge();
|
210 |
// memcpy(&Arcos[link_num].idTramo,puntero,sizeof(long));
|
211 |
edge.setIdArc(buf.getInt()); |
212 |
|
213 |
|
214 |
// Sentido de digitalizaci?n.Un 1 indica que va en ese sentido, un cero al contrario.
|
215 |
// memcpy(&Arcos[link_num].sentido,puntero,sizeof(int));
|
216 |
edge.setDirec(buf.getInt()); |
217 |
|
218 |
// idNodeOrig
|
219 |
edge.setIdNodeOrig(buf.getInt()); |
220 |
// memcpy(&node_num1,puntero,sizeof(long));
|
221 |
|
222 |
// idNodeEnd
|
223 |
edge.setIdNodeEnd(buf.getInt()); |
224 |
// memcpy(&node_num2,puntero,sizeof(long));
|
225 |
|
226 |
// Read the link costs.
|
227 |
// Type
|
228 |
edge.setType(buf.getInt()); |
229 |
// memcpy(&Arcos[link_num].TipoTramo,puntero,sizeof(int));
|
230 |
|
231 |
// Distance
|
232 |
// TODO: PONERLO COMO DOUBLE DENTRO DEL FICHERO!!!!
|
233 |
edge.setDistance(buf.getFloat()); |
234 |
// TODO: POR AHORA, WEIGHT = LENGTH. Con setVelocities() lo cambiamos.
|
235 |
edge.setWeight(edge.getDistance()); |
236 |
|
237 |
// memcpy(&Arcos[link_num].Coste2,puntero,sizeof(float));
|
238 |
|
239 |
// pNodo1 = &Nodos[node_num1];
|
240 |
// Arcos[link_num].idNodo1 = node_num1;
|
241 |
//
|
242 |
// Arcos[link_num].idNodo2 = node_num2;
|
243 |
// pNodo2->Enlaces.Add(link_num);
|
244 |
|
245 |
// // NUEVO 11-JUL-2002
|
246 |
// if (Arcos[link_num].sentido)
|
247 |
// IndiceArcos[Arcos[link_num].idTramo].idArco = link_num;
|
248 |
// else
|
249 |
// IndiceArcos[Arcos[link_num].idTramo].idContraArco = link_num;
|
250 |
//
|
251 |
// // NUEVO 27-JUL-2003
|
252 |
// Arcos[link_num].numSoluc = 0;
|
253 |
//
|
254 |
// // NUEVO 23_2_2005
|
255 |
// CreaConectores(link_num);
|
256 |
return edge;
|
257 |
} |
258 |
|
259 |
/**
|
260 |
* @param args
|
261 |
*/
|
262 |
public static void main(String[] args) { |
263 |
NetworkRedLoader redLoader = new NetworkRedLoader();
|
264 |
|
265 |
redLoader.loadNetwork(); |
266 |
redLoader.loadJungNetwork(); |
267 |
|
268 |
} |
269 |
|
270 |
public File getNetFile() { |
271 |
return netFile;
|
272 |
} |
273 |
|
274 |
public void setNetFile(File netFile) { |
275 |
this.netFile = netFile;
|
276 |
} |
277 |
|
278 |
} |
279 |
|
280 |
|