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