Statistics
| Revision:

root / tags / PilotoRedes_Build_2 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / core / loaders / NetworkRedLoader.java @ 11410

History | View | Annotate | Download (7.97 KB)

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