Statistics
| Revision:

root / branches / v10 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / solvers / EdgesMemoryDriver.java @ 12977

History | View | Annotate | Download (7.79 KB)

1
/*
2
 * Created on 06-nov-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: EdgesMemoryDriver.java 12977 2007-08-08 11:43:40Z fjp $
47
 * $Log$
48
 * Revision 1.1.2.1  2007-08-08 11:43:40  fjp
49
 * Principio de giros y area de influencia
50
 *
51
 * Revision 1.2  2006/11/08 19:32:36  azabala
52
 * *** empty log message ***
53
 *
54
 * Revision 1.1  2006/11/07 19:49:28  azabala
55
 * *** empty log message ***
56
 *
57
 *
58
 */
59
package com.iver.cit.gvsig.graph.solvers;
60

    
61
import java.awt.geom.Line2D;
62
import java.awt.geom.Rectangle2D;
63
import java.io.IOException;
64
import java.sql.Types;
65
import java.util.List;
66

    
67
import javax.swing.table.DefaultTableModel;
68

    
69
import com.hardcode.gdbms.engine.data.DataSourceFactory;
70
import com.hardcode.gdbms.engine.data.driver.DriverException;
71
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
72
import com.hardcode.gdbms.engine.data.edition.DataWare;
73
import com.hardcode.gdbms.engine.values.Value;
74
import com.hardcode.gdbms.engine.values.ValueFactory;
75
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
76
import com.iver.cit.gvsig.fmap.core.FShape;
77
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
78
import com.iver.cit.gvsig.fmap.core.IFeature;
79
import com.iver.cit.gvsig.fmap.core.IGeometry;
80
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
81
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
82
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
83
import com.iver.cit.gvsig.fmap.drivers.MemoryDriver;
84
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
85
import com.iver.cit.gvsig.graph.core.GvEdge;
86
import com.iver.cit.gvsig.graph.core.GvNode;
87
import com.iver.cit.gvsig.graph.core.Network;
88

    
89
/**
90
 * Driver wrapper around arcs from a network. It may be useful to show
91
 * the arcs as a layer without consuming more memory. Maybe it could be useful
92
 * also to have an Edges version instead of Arcs.
93
 * 
94
 * @author Fco. Jos? Pe?arrubia
95
 * 
96
 */
97
public class EdgesMemoryDriver implements VectorialDriver, ObjectDriver {
98
        static FieldDescription[] fields = new FieldDescription[6];
99
        static {
100
                FieldDescription fieldDesc = new FieldDescription();
101
                fieldDesc.setFieldName("IDARC");
102
                fieldDesc.setFieldType(Types.INTEGER);
103
                fieldDesc.setFieldLength(20);
104
                fieldDesc.setFieldDecimalCount(0);
105
                fields[0] = fieldDesc;
106

    
107
                fieldDesc = new FieldDescription();
108
                fieldDesc.setFieldName("WEIGHT");
109
                fieldDesc.setFieldType(Types.DOUBLE);
110
                fieldDesc.setFieldLength(20);
111
                fieldDesc.setFieldDecimalCount(5);
112
                fields[1] = fieldDesc;
113

    
114
                fieldDesc = new FieldDescription();
115
                fieldDesc.setFieldName("LENGTH");
116
                fieldDesc.setFieldType(Types.DOUBLE);
117
                fieldDesc.setFieldLength(20);
118
                fieldDesc.setFieldDecimalCount(5);
119
                fields[2] = fieldDesc;
120

    
121
                fieldDesc = new FieldDescription();
122
                fieldDesc.setFieldName("TEXT");
123
                fieldDesc.setFieldType(Types.VARCHAR);
124
                fieldDesc.setFieldLength(25);
125
                fieldDesc.setFieldDecimalCount(0);
126
                fields[3] = fieldDesc;
127
                
128
                fieldDesc = new FieldDescription();
129
                fieldDesc.setFieldName("COSTN1");
130
                fieldDesc.setFieldType(Types.DOUBLE);
131
                fieldDesc.setFieldLength(20);
132
                fieldDesc.setFieldDecimalCount(5);
133
                fields[4] = fieldDesc;
134

    
135
                fieldDesc = new FieldDescription();
136
                fieldDesc.setFieldName("COSTN2");
137
                fieldDesc.setFieldType(Types.DOUBLE);
138
                fieldDesc.setFieldLength(20);
139
                fieldDesc.setFieldDecimalCount(5);
140
                fields[5] = fieldDesc;
141
        
142
        }
143

    
144
        
145
        Network net;
146

    
147
        Rectangle2D fullExtent;
148

    
149
        public EdgesMemoryDriver(Network net) {
150
                this.net = net;
151
        }
152

    
153
        public int getShapeType() {
154
                return FShape.LINE;
155
        }
156

    
157
        public String getName() {
158
                return "EdgeDriver";
159
        }
160

    
161
        public DriverAttributes getDriverAttributes() {
162
                return null;
163
        }
164

    
165
        public boolean isWritable() {
166
                return false;
167
        }
168

    
169
        public int[] getPrimaryKeys()
170
                        throws com.hardcode.gdbms.engine.data.driver.DriverException {
171
                return null;
172
        }
173

    
174
        public void write(DataWare dataWare)
175
                        throws com.hardcode.gdbms.engine.data.driver.DriverException {
176
        }
177

    
178
        /**
179
         * Returns de field type of the specified field index.
180
         * 
181
         * @return field type of i field
182
         */
183
        public int getFieldType(int i) throws DriverException {
184
                // azabala: we overwrite MemoryDriver because type resolution
185
                // is based in first register value (it could be null)
186
                if (i >= fields.length)
187
                        throw new DriverException("Excedido el numero de campos");
188
                return fields[i].getFieldType();
189
        }
190

    
191
        public int getShapeCount() throws IOException {
192
                return net.getGraph().numEdges();
193
        }
194

    
195
        public Rectangle2D getFullExtent() throws IOException {
196
                try {
197
                        return net.getLayer().getFullExtent();
198
                } catch (com.iver.cit.gvsig.fmap.DriverException e) {
199
                        e.printStackTrace();
200
                        throw new IOException(e.getMessage());
201
                }
202
        }
203

    
204
        public IGeometry getShape(int index) {
205
                GvEdge edge = net.getGraph().getEdgeByID(index);
206
                GvNode n1 = net.getGraph().getNodeByID(edge.getIdNodeOrig());
207
                GvNode n2 = net.getGraph().getNodeByID(edge.getIdNodeEnd());
208
                Line2D.Double line = new Line2D.Double(n1.getX(), n1.getY(), n2.getX(), n2.getY());
209
                return ShapeFactory.createPolyline2D(new GeneralPathX(line));
210
        }
211

    
212
        public void reload() throws IOException, DriverException {
213

    
214
        }
215
        
216

    
217
        /* (non-Javadoc)
218
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
219
         */
220
        public Value getFieldValue(long rowIndex, int fieldId)
221
                throws DriverException {
222
                Value val = ValueFactory.createNullValue();
223
                GvEdge edge = net.getGraph().getEdgeByID((int) rowIndex);
224
                switch (fieldId)
225
                {
226
                case 0: // idArc
227
                        return ValueFactory.createValue(edge.getIdArc());
228
                case 1: // weight
229
                        return ValueFactory.createValue(edge.getWeight());
230
                case 2: // length
231
                        return ValueFactory.createValue(edge.getDistance());
232
                case 3:
233
                        return ValueFactory.createValue(edge.getType() + "");
234
                case 4: // cost node1
235
                        GvNode n1 = net.getGraph().getNodeByID(edge.getIdNodeOrig());
236
                        
237
                        if (n1.getBestCost() == Double.MAX_VALUE)
238
                                return ValueFactory.createValue(-1.0);
239
                        else
240
                                return ValueFactory.createValue(n1.getBestCost());
241
                case 5: // cost node2
242
                        GvNode n2 = net.getGraph().getNodeByID(edge.getIdNodeOrig());
243
                        if (n2.getBestCost() == Double.MAX_VALUE)
244
                                return ValueFactory.createValue(-1.0);
245
                        else
246
                                return ValueFactory.createValue(n2.getBestCost());
247

    
248
                        
249
                }
250
                return val;
251
        }
252

    
253
        
254
        public int getFieldCount() throws DriverException {
255
                return fields.length;
256
        }
257

    
258
        
259
        public String getFieldName(int fieldId) throws DriverException {
260
                return fields[fieldId].getFieldName();
261
        }
262

    
263
        /* (non-Javadoc)
264
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
265
         */
266
        public long getRowCount() throws DriverException {
267
                return net.getGraph().numEdges();
268
        }
269
    
270
    /* (non-Javadoc)
271
     * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
272
     */
273
    public void setDataSourceFactory(DataSourceFactory dsf) {
274
    }
275

    
276
    public int getFieldWidth(int fieldId)
277
    {
278
            return fields[fieldId].getFieldLength();
279
    }
280
}