Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / core / loaders / NetworkRedLoader.java @ 26861

History | View | Annotate | Download (7.95 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.MappedByteBuffer;
49
import java.nio.channels.FileChannel;
50

    
51
import org.gvsig.graph.core.EdgePair;
52
import org.gvsig.graph.core.GvEdge;
53
import org.gvsig.graph.core.GvGraph;
54
import org.gvsig.graph.core.GvNode;
55
import org.gvsig.graph.core.IGraph;
56
import org.gvsig.graph.core.INetworkLoader;
57

    
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
                                
107
                                // NOTE: EDGES ARE WRITEN BEFORE. LOOK TO NetworkFileRedWriter.
108
//                                        output.writeInt(id);                    4
109
//                                        output.writeInt(sense);                        4
110
//
111
//                                        output.writeInt(idNodeOrig);        4
112
//                                        output.writeInt(idNodeEnd);                4
113
//                                        output.writeInt(tipoTramo);                4
114
//                                        output.writeDouble(dist);                8
115
//                                        output.writeDouble(cost);                8
116
                                // TOTAL = 5x4 + 2x8 = 20 + 16 = 36 bytes /edge
117

    
118
                                buf.position(36*numEdges + 12);
119
                                for (int i=0; i < numNodes; i++)
120
                                {
121
                                        GvNode node = readNode(buf);
122
                                        g.addNode(node);
123
                                }
124
                                // Arcos                        
125
                                buf.position(12);
126
                                for (int i=0; i < numEdges; i++)
127
                                {
128
                                        GvEdge edge = readEdge(buf);
129
                                        edge.setIdEdge(i);
130
                                        g.addEdge(edge);
131
                                        GvNode nodeOrig = g.getNodeByID(edge.getIdNodeOrig());
132
                                        nodeOrig.addOutputLink(edge);
133
                                        GvNode nodeEnd = g.getNodeByID(edge.getIdNodeEnd());
134
                                        nodeEnd.addInputLink(edge);
135
                                        
136
                                        EdgePair edgePair = g.getEdgesByIdArc(edge.getIdArc());
137
                                        if (edgePair == null)
138
                                        {
139
                                                edgePair = new EdgePair();                                                
140
                                                g.addEdgePair(edge.getIdArc(), edgePair);
141
                                        }
142
                                        if (edge.getDirec() == 1)
143
                                                edgePair.setIdEdge(i);
144
                                        else
145
                                                edgePair.setIdInverseEdge(i);
146
                                                                                
147
                                }
148
                        
149
                        long t2 = System.currentTimeMillis();
150
                        System.out.println("Tiempo de carga: " + (t2-t1) + " msecs");
151
                        System.out.println("NumEdges = " + g.numEdges());
152
                        return g;
153
                        } catch (FileNotFoundException e) {
154
                                // TODO Auto-generated catch block
155
                                e.printStackTrace();
156
                        } catch (IOException e) {
157
                                // TODO Auto-generated catch block
158
                                e.printStackTrace();
159
                        }
160

    
161
                return null;
162
        }
163
        
164
        /* public Graph loadJungNetwork()
165
        {
166
                SparseGraph g = new SparseGraph();
167
                long t1 = System.currentTimeMillis();
168
                
169
                RandomAccessFile file;
170
                try {
171
                        file = new RandomAccessFile(netFile.getPath(),
172
                                        "r");
173
                        FileChannel channel = file.getChannel();
174
                        MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
175
                        buf.order(ByteOrder.LITTLE_ENDIAN);
176

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

    
223
        private GvNode readNode(MappedByteBuffer buf) {
224
                GvNode node = new GvNode();
225
                node.setIdNode(buf.getInt());
226
                node.setX(buf.getDouble());
227
                node.setY(buf.getDouble());
228
                return node;
229
        }
230

    
231
        private GvEdge readEdge(MappedByteBuffer buf) {
232
                GvEdge edge = new GvEdge();
233
                // memcpy(&Arcos[link_num].idTramo,puntero,sizeof(long));
234
                edge.setIdArc(buf.getInt());
235

    
236
                
237
                // Sentido de digitalizaci?n.Un 1 indica que va en ese sentido, un cero al contrario.
238
                // memcpy(&Arcos[link_num].sentido,puntero,sizeof(int));
239
                edge.setDirec(buf.getInt());
240

    
241
                // idNodeOrig
242
                edge.setIdNodeOrig(buf.getInt());
243
                // memcpy(&node_num1,puntero,sizeof(long));
244
                
245
                // idNodeEnd
246
                edge.setIdNodeEnd(buf.getInt());
247
//                memcpy(&node_num2,puntero,sizeof(long));
248

    
249
                // Read the link costs.
250
                // Type
251
                edge.setType(buf.getInt());
252
//                memcpy(&Arcos[link_num].TipoTramo,puntero,sizeof(int));
253

    
254
                // Distance
255
                edge.setDistance(buf.getDouble());
256
                edge.setWeight(buf.getDouble());
257
                
258
//                memcpy(&Arcos[link_num].Coste2,puntero,sizeof(float));
259

    
260
//                pNodo1 = &Nodos[node_num1];
261
//                Arcos[link_num].idNodo1 = node_num1;
262
//
263
//                Arcos[link_num].idNodo2 = node_num2;
264
                // pNodo2->Enlaces.Add(link_num);
265

    
266
//                // NUEVO 11-JUL-2002
267
//                        if (Arcos[link_num].sentido)
268
//                        IndiceArcos[Arcos[link_num].idTramo].idArco = link_num;
269
//                else
270
//                        IndiceArcos[Arcos[link_num].idTramo].idContraArco = link_num;
271
//
272
//                // NUEVO 27-JUL-2003
273
//                Arcos[link_num].numSoluc = 0;
274
//
275
//                // NUEVO 23_2_2005
276
//                CreaConectores(link_num);
277
                return edge;
278
        }
279

    
280
        /**
281
         * @param args
282
         */
283
        public static void main(String[] args) {
284
                NetworkRedLoader redLoader = new NetworkRedLoader();
285
                
286
                redLoader.loadNetwork();
287
//                redLoader.loadJungNetwork();
288

    
289
        }
290

    
291
        public File getNetFile() {
292
                return netFile;
293
        }
294

    
295
        public void setNetFile(File netFile) {
296
                this.netFile = netFile;
297
        }
298

    
299
}
300

    
301