Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_2_Build_1044 / prototypes / VectorialAvanzado / extensions / extGraph / src / com / iver / cit / gvsig / graph / core / loaders / NetworkRedLoader.java @ 20099

History | View | Annotate | Download (7.73 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

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

    
58
/**
59
 * @author fjp
60
 * 
61
 * Primero vienen los arcos, y luego los nodos. En la cabecera, 3 enteros
62
 * con el numero de tramos, el de arcos y el de nodos.
63
 *
64
 */
65
public class NetworkRedLoader implements INetworkLoader {
66
        
67
        private File netFile = new File("c:/ejes.net");
68

    
69
        public IGraph loadNetwork() {
70
                
71
                long t1 = System.currentTimeMillis();
72
                
73
                int numArcs;
74
                int numEdges;
75
                int numNodes;
76
                
77
                short sentidoDigit; // => 1 en esa direcci?n. 0=> Al contrario. SOLO
78
                // SE UTILIZA PARA LOS CALCULOS POR IDTRAMO Y
79
                // PORCENTAJE
80
                // PARA SABER SI EST? M?S CERCA DE UN NODO O DEL OTRO.
81

    
82

    
83
                        RandomAccessFile file;
84
                        try {
85
                                file = new RandomAccessFile(netFile.getPath(),
86
                                                "r");
87
                                FileChannel channel = file.getChannel();
88
                                MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
89
                                buf.order(ByteOrder.LITTLE_ENDIAN);
90
        
91
                                numArcs = buf.getInt();
92
                                numEdges = buf.getInt();
93
                                numNodes = buf.getInt();
94
                
95
                                GvGraph g = new GvGraph(numArcs, numEdges, numNodes);
96

    
97
                                // Nodes
98
                                
99
                                // NOTE: EDGES ARE WRITEN BEFORE. LOOK TO NetworkFileRedWriter.
100
//                                        output.writeInt(id);                    4
101
//                                        output.writeInt(sense);                        4
102
//
103
//                                        output.writeInt(idNodeOrig);        4
104
//                                        output.writeInt(idNodeEnd);                4
105
//                                        output.writeInt(tipoTramo);                4
106
//                                        output.writeDouble(dist);                8
107
//                                        output.writeDouble(cost);                8
108
                                // TOTAL = 5x4 + 2x8 = 20 + 16 = 36 bytes /edge
109

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

    
150
                return null;
151
        }
152
        
153
//        public Graph loadJungNetwork()
154
//        {
155
//                SparseGraph g = new SparseGraph();
156
//                long t1 = System.currentTimeMillis();
157
//                
158
//                RandomAccessFile file;
159
//                try {
160
//                        file = new RandomAccessFile(netFile.getPath(),
161
//                                        "r");
162
//                        FileChannel channel = file.getChannel();
163
//                        MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
164
//                        buf.order(ByteOrder.LITTLE_ENDIAN);
165
//
166
//                        int numArcs = buf.getInt();
167
//                        int numEdges = buf.getInt();
168
//                        int numNodes = buf.getInt();
169
//                        
170
//                        // Nodes
171
//                        buf.position(24*numEdges + 12);
172
//                        for (int i=0; i < numNodes; i++)
173
//                        {
174
//                                GvNode node = readNode(buf);
175
//                                
176
//                                Vertex v = new DirectedSparseVertex();
177
////                                v.addUserDatum("ID", node.idNode, UserData.CLONE);
178
////                                v.addUserDatum("X", node.x, UserData.CLONE);
179
////                                v.addUserDatum("Y", node.y, UserData.CLONE);
180
//        //                        v_locations.setLocation(v, new Point2D.Double(x.doubleValue(),y.doubleValue()));
181
//                                g.addVertex(v);                                
182
//                        }
183
//                        Indexer indexer = Indexer.getIndexer(g);
184
//                
185
//                        buf.position(12);
186
//                        for (int i=0; i < numEdges; i++)
187
//                        {
188
//                                GvEdge edge = readEdge(buf);
189
//                                
190
//                                int nodeOrig = edge.getIdNodeOrig();
191
//                                int nodeEnd = edge.getIdNodeEnd();
192
//                                
193
//                                Vertex vFrom = (Vertex) indexer.getVertex(nodeOrig);
194
//                                Vertex vTo = (Vertex) indexer.getVertex(nodeEnd);
195
//                                
196
//                                DirectedSparseEdge edgeJ = new DirectedSparseEdge(vFrom, vTo);
197
//                                g.addEdge(edgeJ);
198
//                        }
199
//                        long t2 = System.currentTimeMillis();
200
//                        System.out.println("Tiempo de carga: " + (t2-t1) + " msecs");
201
//                        return g;
202
//                } catch (FileNotFoundException e) {
203
//                        // TODO Auto-generated catch block
204
//                        e.printStackTrace();
205
//                } catch (IOException e) {
206
//                        // TODO Auto-generated catch block
207
//                        e.printStackTrace();
208
//                }
209
//                return null;
210
//        }
211

    
212
        private GvNode readNode(MappedByteBuffer buf) {
213
                GvNode node = new GvNode();
214
                node.setIdNode(buf.getInt());
215
                node.setX(buf.getDouble());
216
                node.setY(buf.getDouble());
217
                return node;
218
        }
219

    
220
        private GvEdge readEdge(MappedByteBuffer buf) {
221
                GvEdge edge = new GvEdge();
222
                // memcpy(&Arcos[link_num].idTramo,puntero,sizeof(long));
223
                edge.setIdArc(buf.getInt());
224

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

    
230
                // idNodeOrig
231
                edge.setIdNodeOrig(buf.getInt());
232
                // memcpy(&node_num1,puntero,sizeof(long));
233
                
234
                // idNodeEnd
235
                edge.setIdNodeEnd(buf.getInt());
236
//                memcpy(&node_num2,puntero,sizeof(long));
237

    
238
                // Read the link costs.
239
                // Type
240
                edge.setType(buf.getInt());
241
//                memcpy(&Arcos[link_num].TipoTramo,puntero,sizeof(int));
242

    
243
                // Distance
244
                edge.setDistance(buf.getDouble());
245
                edge.setWeight(buf.getDouble());
246
                
247
//                memcpy(&Arcos[link_num].Coste2,puntero,sizeof(float));
248

    
249
//                pNodo1 = &Nodos[node_num1];
250
//                Arcos[link_num].idNodo1 = node_num1;
251
//
252
//                Arcos[link_num].idNodo2 = node_num2;
253
                // pNodo2->Enlaces.Add(link_num);
254

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

    
269
        /**
270
         * @param args
271
         */
272
        public static void main(String[] args) {
273
                NetworkRedLoader redLoader = new NetworkRedLoader();
274
                
275
                redLoader.loadNetwork();
276
//                redLoader.loadJungNetwork();
277

    
278
        }
279

    
280
        public File getNetFile() {
281
                return netFile;
282
        }
283

    
284
        public void setNetFile(File netFile) {
285
                this.netFile = netFile;
286
        }
287

    
288
}
289

    
290