Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAddEventTheme / src / com / iver / gvsig / addeventtheme / AddEventThemeDriver.java @ 6324

History | View | Annotate | Download (8.04 KB)

1
/*
2
 * Created on 10-nov-2005
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
package com.iver.gvsig.addeventtheme;
45

    
46
import java.awt.geom.Rectangle2D;
47
import java.io.IOException;
48

    
49
import com.hardcode.driverManager.DriverLoadException;
50
import com.hardcode.gdbms.engine.data.DataSource;
51
import com.hardcode.gdbms.engine.data.DataSourceFactory;
52
import com.hardcode.gdbms.engine.data.IDataSourceListener;
53
import com.hardcode.gdbms.engine.data.NoSuchTableException;
54
import com.hardcode.gdbms.engine.data.driver.DriverException;
55
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
56
import com.hardcode.gdbms.engine.data.edition.DataWare;
57
import com.hardcode.gdbms.engine.values.StringValue;
58
import com.hardcode.gdbms.engine.values.Value;
59
import com.iver.cit.gvsig.fmap.core.FGeometry;
60
import com.iver.cit.gvsig.fmap.core.FPoint2D;
61
import com.iver.cit.gvsig.fmap.core.FShape;
62
import com.iver.cit.gvsig.fmap.core.IGeometry;
63
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
64
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
65
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
66
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
67
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
68
import com.iver.utiles.IPersistance;
69
import com.iver.utiles.XMLEntity;
70

    
71
/**
72
 * The class AddEventThemeDriver allows to create a new FLayer from a
73
 * gvSIG DataSource.
74
 *
75
 * @author jmorell
76
 */
77
public class AddEventThemeDriver implements VectorialDriver, ObjectDriver, BoundedShapes, IPersistance, IDataSourceListener {
78
    private Rectangle2D fullExtent = null;
79
    private DataSource ds;
80
    private int xFieldIndex;
81
    private int yFieldIndex;
82

    
83
    /**
84
     * Initializes this.
85
     * @param ds
86
     * @param xFieldIndex
87
     * @param yFieldIndex
88
     */
89
    public void setData(DataSource ds, int xFieldIndex, int yFieldIndex) {
90
        this.ds = ds;
91
        this.ds.addDataSourceListener(this);
92
        this.xFieldIndex = xFieldIndex;
93
        this.yFieldIndex = yFieldIndex;
94
    }
95
    public int[] getFieldsIndex(){
96
            int[] n=new int[2];
97
            n[0]=xFieldIndex;
98
            n[1]=yFieldIndex;
99
            return n;
100
    }
101
    public int getShapeType() {
102
        return FShape.POINT;
103
    }
104

    
105
    public int getShapeCount() throws IOException {
106
        try {
107
            return (int) ds.getRowCount();
108
        } catch (DriverException e) {
109
            // TODO Auto-generated catch block
110
            e.printStackTrace();
111
        }
112
        return 0;
113
    }
114

    
115
    public DriverAttributes getDriverAttributes() {
116
        // TODO Auto-generated method stub
117
        return null;
118
    }
119

    
120
    public Rectangle2D getFullExtent() throws IOException {
121
        if (fullExtent == null) {
122
            try {
123
                for (int i=0;i<ds.getRowCount();i++) {
124
                    double x = (new Double(((Value)ds.getFieldValue((int)i, xFieldIndex)).toString())).doubleValue();
125
                    double y = (new Double(((Value)ds.getFieldValue((int)i, yFieldIndex)).toString())).doubleValue();
126
                    FGeometry geometry = ShapeFactory.createGeometry(new FPoint2D(x, y));
127
                    Rectangle2D rect = geometry.getBounds2D();
128
                    if (fullExtent == null) {
129
                        fullExtent = rect;
130
                    } else {
131
                        fullExtent.add(rect);
132
                    }
133
                }
134
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
135
                // TODO Auto-generated catch block
136
                e1.printStackTrace();
137
            }
138
        }
139
        return fullExtent;
140
    }
141

    
142
    public IGeometry getShape(int index){
143
        double x;
144
        double y;
145
        try {
146
            x = (new Double(((Value)ds.getFieldValue(index, xFieldIndex)).toString())).doubleValue();
147
            y = (new Double(((Value)ds.getFieldValue(index, yFieldIndex)).toString())).doubleValue();
148
            //System.err.println("La X = "+x+" , La Y = "+y);
149
            FGeometry geometry = ShapeFactory.createGeometry(new FPoint2D(x, y));
150
            return geometry;
151
        } catch (NumberFormatException e) {
152
            // TODO Auto-generated catch block
153
            e.printStackTrace();
154
        } catch (DriverException e) {
155
            // TODO Auto-generated catch block
156
            e.printStackTrace();
157
        }
158
        return null;
159
    }
160

    
161
    public String getName() {
162
        return "Add Event Layer Driver";
163
    }
164

    
165
    public int[] getPrimaryKeys() throws DriverException {
166
        // TODO Auto-generated method stub
167
        return null;
168
    }
169

    
170
    public void write(DataWare dataWare) throws DriverException {
171
        // TODO Auto-generated method stub
172

    
173
    }
174

    
175
    public void setDataSourceFactory(DataSourceFactory arg0) {
176
        // TODO Auto-generated method stub
177

    
178
    }
179

    
180
    public Value getFieldValue(long rowIndex, int fieldId) throws DriverException {
181
        return ds.getFieldValue(rowIndex, fieldId);
182
    }
183

    
184
    public int getFieldCount() throws DriverException {
185
        return ds.getFieldCount();
186
    }
187

    
188
    public String getFieldName(int fieldId) throws DriverException {
189
        return ds.getFieldName(fieldId);
190
    }
191

    
192
    public long getRowCount() throws DriverException {
193
        return ds.getRowCount();
194
    }
195

    
196
    public int getFieldType(int i) throws DriverException {
197
        return ds.getFieldType(i);
198
    }
199

    
200
    public String getClassName() {
201
        return this.getClass().getName();
202
    }
203

    
204
    // Para guardar en el xml file
205
    public XMLEntity getXMLEntity() {
206
        XMLEntity xml = new XMLEntity();
207
        xml.putProperty("className", this.getClass().getName());
208
        xml.putProperty("xFieldIndex", xFieldIndex);
209
        xml.putProperty("yFieldIndex", yFieldIndex);
210
        xml.putProperty("tableName", ds.getName());
211
        return xml;
212
    }
213

    
214
    // Para recuperar del xml file
215
    public void setXMLEntity(XMLEntity xml) {
216
        int xFieldIndex = xml.getIntProperty("xFieldIndex");
217
        int yFieldIndex = xml.getIntProperty("yFieldIndex");
218
        String tableName = xml.getStringProperty("tableName");
219
        try {
220
            DataSource ds = LayerFactory.getDataSourceFactory().createRandomDataSource(tableName, DataSourceFactory.AUTOMATIC_OPENING);
221
            setData(ds, xFieldIndex, yFieldIndex);
222
        } catch (NoSuchTableException e) {
223
            throw new RuntimeException(e);
224
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
225
            throw new RuntimeException(e);
226
        } catch (DriverLoadException e) {
227
            // TODO Auto-generated catch block
228
            e.printStackTrace();
229
        }
230
    }
231

    
232
        public int getFieldWidth(int i) throws DriverException {
233
                return ds.getFieldWidth(i);
234
        }
235

    
236
        public Rectangle2D getShapeBounds(int index) throws IOException {
237
                return getShape(index).getBounds2D();
238
        }
239

    
240
        public int getShapeType(int index) {
241
                return getShape(index).getGeometryType();
242
        }
243
        public boolean isWritable() {
244
                return true;
245
        }
246
        
247
        public void reloaded(DataSource dataSource) {
248
                this.fullExtent = null;
249
                
250
        }
251
        
252
        public void reload() throws IOException, DriverException{
253
                this.ds.reload();
254
                
255
        }
256

    
257
}