Statistics
| Revision:

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

History | View | Annotate | Download (7.52 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.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
import com.iver.cit.gvsig.graph.core.EdgePair;
53
import com.iver.cit.gvsig.graph.core.GvEdge;
54
import com.iver.cit.gvsig.graph.core.GvGraph;
55
import com.iver.cit.gvsig.graph.core.GvNode;
56
import com.iver.cit.gvsig.graph.core.IGraph;
57
import com.iver.cit.gvsig.graph.core.INetworkLoader;
58

    
59
import edu.uci.ics.jung.graph.Graph;
60
import edu.uci.ics.jung.graph.Vertex;
61
import edu.uci.ics.jung.graph.decorators.Indexer;
62
import edu.uci.ics.jung.graph.impl.DirectedSparseEdge;
63
import edu.uci.ics.jung.graph.impl.DirectedSparseVertex;
64
import edu.uci.ics.jung.graph.impl.SparseGraph;
65

    
66
/**
67
 * @author fjp
68
 * 
69
 * Primero vienen los arcos, y luego los nodos. En la cabecera, 3 enteros
70
 * con el numero de tramos, el de arcos y el de nodos.
71
 *
72
 */
73
public class NetworkRedLoader implements INetworkLoader {
74
        
75
        private File netFile = new File("c:/ejes.red");
76

    
77
        public IGraph loadNetwork() {
78
                
79
                long t1 = System.currentTimeMillis();
80
                
81
                int numArcs;
82
                int numEdges;
83
                int numNodes;
84
                
85
                short sentidoDigit; // => 1 en esa direcci?n. 0=> Al contrario. SOLO
86
                // SE UTILIZA PARA LOS CALCULOS POR IDTRAMO Y
87
                // PORCENTAJE
88
                // PARA SABER SI EST? M?S CERCA DE UN NODO O DEL OTRO.
89

    
90

    
91
                        RandomAccessFile file;
92
                        try {
93
                                file = new RandomAccessFile(netFile.getPath(),
94
                                                "r");
95
                                FileChannel channel = file.getChannel();
96
                                MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
97
                                buf.order(ByteOrder.LITTLE_ENDIAN);
98
        
99
                                numArcs = buf.getInt();
100
                                numEdges = buf.getInt();
101
                                numNodes = buf.getInt();
102
                
103
                                GvGraph g = new GvGraph(numArcs, numEdges, numNodes);
104

    
105
                                // Nodes
106
                                buf.position(30*numEdges + 12);
107
                                for (int i=0; i < numNodes; i++)
108
                                {
109
                                        GvNode node = readNode(buf);
110
                                        g.addNode(node);
111
                                }
112
                                // Arcos                        
113
                                buf.position(12);
114
                                for (int i=0; i < numEdges; i++)
115
                                {
116
                                        GvEdge edge = readEdge(buf);
117
                                        edge.setIdEdge(i);
118
                                        g.addEdge(edge);
119
                                        GvNode node = g.getNodeByID(edge.getIdNodeOrig());
120
                                        node.getEnlaces().add(edge);
121
                                        EdgePair edgePair = g.getEdgesByIdArc(edge.getIdArc());
122
                                        if (edgePair == null)
123
                                        {
124
                                                edgePair = new EdgePair();                                                
125
                                                g.addEdgePair(edgePair);
126
                                        }
127
                                        if (edge.getDirec() == 1)
128
                                                edgePair.setIdEdge(i);
129
                                        else
130
                                                edgePair.setIdInverseEdge(i);
131
                                        
132
                                }
133
                        
134
                        long t2 = System.currentTimeMillis();
135
                        System.out.println("Tiempo de carga: " + (t2-t1) + " msecs");
136
                        System.out.println("NumEdges = " + g.numEdges());
137
                        return g;
138
                        } catch (FileNotFoundException e) {
139
                                // TODO Auto-generated catch block
140
                                e.printStackTrace();
141
                        } catch (IOException e) {
142
                                // TODO Auto-generated catch block
143
                                e.printStackTrace();
144
                        }
145

    
146
                return null;
147
        }
148
        
149
        public Graph loadJungNetwork()
150
        {
151
                SparseGraph g = new SparseGraph();
152
                long t1 = System.currentTimeMillis();
153
                
154
                RandomAccessFile file;
155
                try {
156
                        file = new RandomAccessFile(netFile.getPath(),
157
                                        "r");
158
                        FileChannel channel = file.getChannel();
159
                        MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
160
                        buf.order(ByteOrder.LITTLE_ENDIAN);
161

    
162
                        int numArcs = buf.getInt();
163
                        int numEdges = buf.getInt();
164
                        int numNodes = buf.getInt();
165
                        
166
                        // Nodes
167
                        buf.position(24*numEdges + 12);
168
                        for (int i=0; i < numNodes; i++)
169
                        {
170
                                GvNode node = readNode(buf);
171
                                
172
                                Vertex v = new DirectedSparseVertex();
173
//                                v.addUserDatum("ID", node.idNode, UserData.CLONE);
174
//                                v.addUserDatum("X", node.x, UserData.CLONE);
175
//                                v.addUserDatum("Y", node.y, UserData.CLONE);
176
        //                        v_locations.setLocation(v, new Point2D.Double(x.doubleValue(),y.doubleValue()));
177
                                g.addVertex(v);                                
178
                        }
179
                        Indexer indexer = Indexer.getIndexer(g);
180
                
181
                        buf.position(12);
182
                        for (int i=0; i < numEdges; i++)
183
                        {
184
                                GvEdge edge = readEdge(buf);
185
                                
186
                                int nodeOrig = edge.getIdNodeOrig();
187
                                int nodeEnd = edge.getIdNodeEnd();
188
                                
189
                                Vertex vFrom = (Vertex) indexer.getVertex(nodeOrig);
190
                                Vertex vTo = (Vertex) indexer.getVertex(nodeEnd);
191
                                
192
                                DirectedSparseEdge edgeJ = new DirectedSparseEdge(vFrom, vTo);
193
                                g.addEdge(edgeJ);
194
                        }
195
                        long t2 = System.currentTimeMillis();
196
                        System.out.println("Tiempo de carga: " + (t2-t1) + " msecs");
197
                        return g;
198
                } catch (FileNotFoundException e) {
199
                        // TODO Auto-generated catch block
200
                        e.printStackTrace();
201
                } catch (IOException e) {
202
                        // TODO Auto-generated catch block
203
                        e.printStackTrace();
204
                }
205
                return null;
206
        }
207

    
208
        private GvNode readNode(MappedByteBuffer buf) {
209
                GvNode node = new GvNode();
210
                node.setIdNode(buf.getInt());
211
                node.setX(buf.getFloat());
212
                node.setY(buf.getFloat());
213
                return node;
214
        }
215

    
216
        private GvEdge readEdge(MappedByteBuffer buf) {
217
                GvEdge edge = new GvEdge();
218
                // memcpy(&Arcos[link_num].idTramo,puntero,sizeof(long));
219
                edge.setIdArc(buf.getInt());
220

    
221
                
222
                // Sentido de digitalizaci?n.Un 1 indica que va en ese sentido, un cero al contrario.
223
                // memcpy(&Arcos[link_num].sentido,puntero,sizeof(int));
224
                edge.setDirec(buf.getInt());
225

    
226
                // idNodeOrig
227
                edge.setIdNodeOrig(buf.getInt());
228
                // memcpy(&node_num1,puntero,sizeof(long));
229
                
230
                // idNodeEnd
231
                edge.setIdNodeEnd(buf.getInt());
232
//                memcpy(&node_num2,puntero,sizeof(long));
233

    
234
                // Read the link costs.
235
                // Type
236
                edge.setType(buf.getInt());
237
//                memcpy(&Arcos[link_num].TipoTramo,puntero,sizeof(int));
238

    
239
                // Distance
240
                edge.setDistance(buf.getDouble());
241
                edge.setWeight(buf.getDouble());
242
                
243
//                memcpy(&Arcos[link_num].Coste2,puntero,sizeof(float));
244

    
245
//                pNodo1 = &Nodos[node_num1];
246
//                Arcos[link_num].idNodo1 = node_num1;
247
//
248
//                Arcos[link_num].idNodo2 = node_num2;
249
                // pNodo2->Enlaces.Add(link_num);
250

    
251
//                // NUEVO 11-JUL-2002
252
//                        if (Arcos[link_num].sentido)
253
//                        IndiceArcos[Arcos[link_num].idTramo].idArco = link_num;
254
//                else
255
//                        IndiceArcos[Arcos[link_num].idTramo].idContraArco = link_num;
256
//
257
//                // NUEVO 27-JUL-2003
258
//                Arcos[link_num].numSoluc = 0;
259
//
260
//                // NUEVO 23_2_2005
261
//                CreaConectores(link_num);
262
                return edge;
263
        }
264

    
265
        /**
266
         * @param args
267
         */
268
        public static void main(String[] args) {
269
                NetworkRedLoader redLoader = new NetworkRedLoader();
270
                
271
                redLoader.loadNetwork();
272
                redLoader.loadJungNetwork();
273

    
274
        }
275

    
276
        public File getNetFile() {
277
                return netFile;
278
        }
279

    
280
        public void setNetFile(File netFile) {
281
                this.netFile = netFile;
282
        }
283

    
284
}
285

    
286