Statistics
| Revision:

root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / core / NetworkRedLoader.java @ 7882

History | View | Annotate | Download (6.83 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 redFile = new File("c:/ejes.red");
69

    
70
        public Graph loadNetworkFjp() {
71
                
72
                long t1 = System.currentTimeMillis();
73
                
74
                int numArcs;
75
                int numEdges;
76
                int numNodes;
77
                
78
                float distance;
79
                short arcType;
80
                int direction;
81
                int idNodo1, idNodo2, nodeCount, edgeCount;
82
                short sentidoDigit; // => 1 en esa direcci?n. 0=> Al contrario. SOLO
83
                // SE UTILIZA PARA LOS CALCULOS POR IDTRAMO Y
84
                // PORCENTAJE
85
                // PARA SABER SI EST? M?S CERCA DE UN NODO O DEL OTRO.
86

    
87

    
88
                        RandomAccessFile file;
89
                        try {
90
                                file = new RandomAccessFile(redFile.getPath(),
91
                                                "r");
92
                                FileChannel channel = file.getChannel();
93
                                MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
94
                                buf.order(ByteOrder.LITTLE_ENDIAN);
95
        
96
                                numArcs = buf.getInt();
97
                                numEdges = buf.getInt();
98
                                numNodes = buf.getInt();
99
                                
100
                                ArrayList nodes = new ArrayList(numNodes);
101
                                ArrayList edges = new ArrayList(numEdges);
102

    
103
                                
104
                                // Arcos
105
                                for (int i=0; i < numEdges; i++)
106
                                {
107
                                        GvEdge edge = readEdge(buf);
108
                                        edges.add(edge);                                        
109
                                }
110
                                int pos = buf.position();
111
                                System.out.println("pos = " + pos + " numEdges=" + numEdges);
112
                                // Nodes
113
                                for (int i=0; i < numEdges; i++)
114
                                {
115
                                        GvNode node = readNode(buf);
116
                                        nodes.add(node);
117
                                }
118
                        
119
                        long t2 = System.currentTimeMillis();
120
                        System.out.println("Tiempo de carga: " + (t2-t1) + " msecs");
121
                        System.out.println("NumEdges = " + edges.size());
122
                        } catch (FileNotFoundException e) {
123
                                // TODO Auto-generated catch block
124
                                e.printStackTrace();
125
                        } catch (IOException e) {
126
                                // TODO Auto-generated catch block
127
                                e.printStackTrace();
128
                        }
129

    
130
                return null;
131
        }
132
        
133
        public Graph loadNetwork()
134
        {
135
                SparseGraph g = new SparseGraph();
136
                long t1 = System.currentTimeMillis();
137
                
138
                RandomAccessFile file;
139
                try {
140
                        file = new RandomAccessFile(redFile.getPath(),
141
                                        "r");
142
                        FileChannel channel = file.getChannel();
143
                        MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
144
                        buf.order(ByteOrder.LITTLE_ENDIAN);
145

    
146
                        int numArcs = buf.getInt();
147
                        int numEdges = buf.getInt();
148
                        int numNodes = buf.getInt();
149
                        
150
                        // Nodes
151
                        buf.position(24*numEdges + 12);
152
                        for (int i=0; i < numNodes; i++)
153
                        {
154
                                GvNode node = readNode(buf);
155
                                
156
                                Vertex v = new DirectedSparseVertex();
157
//                                v.addUserDatum("ID", node.idNode, UserData.CLONE);
158
//                                v.addUserDatum("X", node.x, UserData.CLONE);
159
//                                v.addUserDatum("Y", node.y, UserData.CLONE);
160
        //                        v_locations.setLocation(v, new Point2D.Double(x.doubleValue(),y.doubleValue()));
161
                                g.addVertex(v);                                
162
                        }
163
                        Indexer indexer = Indexer.getIndexer(g);
164
                
165
                        buf.position(12);
166
                        for (int i=0; i < numEdges; i++)
167
                        {
168
                                GvEdge edge = readEdge(buf);
169
                                
170
                                int nodeOrig = edge.idNodeOrig;
171
                                int nodeEnd = edge.idNodeEnd;
172
                                
173
                                Vertex vFrom = (Vertex) indexer.getVertex(nodeOrig);
174
                                Vertex vTo = (Vertex) indexer.getVertex(nodeEnd);
175
                                
176
                                DirectedSparseEdge edgeJ = new DirectedSparseEdge(vFrom, vTo);
177
                                g.addEdge(edgeJ);
178
                        }
179
                        long t2 = System.currentTimeMillis();
180
                        System.out.println("Tiempo de carga: " + (t2-t1) + " msecs");
181
                        return g;
182
                } catch (FileNotFoundException e) {
183
                        // TODO Auto-generated catch block
184
                        e.printStackTrace();
185
                } catch (IOException e) {
186
                        // TODO Auto-generated catch block
187
                        e.printStackTrace();
188
                }
189
                return null;
190
        }
191

    
192
        private GvNode readNode(MappedByteBuffer buf) {
193
                GvNode node = new GvNode();
194
                node.idNode = buf.getInt();
195
                node.x = buf.getDouble();
196
                node.y = buf.getDouble();
197
                
198
                return node;
199
        }
200

    
201
        private GvEdge readEdge(MappedByteBuffer buf) {
202
                GvEdge edge = new GvEdge();
203
                // memcpy(&Arcos[link_num].idTramo,puntero,sizeof(long));
204
                edge.idArc = buf.getInt();
205

    
206
                
207
                // Sentido de digitalizaci?n.Un 1 indica que va en ese sentido, un cero al contrario.
208
                // memcpy(&Arcos[link_num].sentido,puntero,sizeof(int));
209
                edge.direc = buf.getInt();
210

    
211
                // idNodeOrig
212
                edge.idNodeOrig = buf.getInt();
213
                // memcpy(&node_num1,puntero,sizeof(long));
214
                
215
                // idNodeEnd
216
                edge.idNodeEnd = buf.getInt();
217
//                memcpy(&node_num2,puntero,sizeof(long));
218

    
219
                // Read the link costs.
220
                // Type
221
                edge.type = buf.getInt();
222
//                memcpy(&Arcos[link_num].TipoTramo,puntero,sizeof(int));
223

    
224
                // Distance
225
                edge.distance = buf.getFloat();
226
//                memcpy(&Arcos[link_num].Coste2,puntero,sizeof(float));
227

    
228
//                pNodo1 = &Nodos[node_num1];
229
//                Arcos[link_num].idNodo1 = node_num1;
230
//
231
//                Arcos[link_num].idNodo2 = node_num2;
232
                // pNodo2->Enlaces.Add(link_num);
233

    
234
//                // NUEVO 11-JUL-2002
235
//                        if (Arcos[link_num].sentido)
236
//                        IndiceArcos[Arcos[link_num].idTramo].idArco = link_num;
237
//                else
238
//                        IndiceArcos[Arcos[link_num].idTramo].idContraArco = link_num;
239
//
240
//                // NUEVO 27-JUL-2003
241
//                Arcos[link_num].numSoluc = 0;
242
//
243
//                // NUEVO 23_2_2005
244
//                CreaConectores(link_num);
245

    
246
                return edge;
247
        }
248

    
249
        /**
250
         * @param args
251
         */
252
        public static void main(String[] args) {
253
                NetworkRedLoader redLoader = new NetworkRedLoader();
254
                
255
                redLoader.loadNetworkFjp();
256
                redLoader.loadNetwork();
257

    
258
        }
259

    
260
}
261

    
262