Statistics
| Revision:

root / trunk / extensions / extAddEventTheme / src / com / iver / gvsig / addeventtheme / AddEventThemeDriver.java @ 3525

History | View | Annotate | Download (7.2 KB)

1 3525 caballero
/*
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.NoSuchTableException;
53
import com.hardcode.gdbms.engine.data.driver.DriverException;
54
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
55
import com.hardcode.gdbms.engine.data.edition.DataWare;
56
import com.hardcode.gdbms.engine.values.StringValue;
57
import com.hardcode.gdbms.engine.values.Value;
58
import com.iver.cit.gvsig.fmap.core.FGeometry;
59
import com.iver.cit.gvsig.fmap.core.FPoint2D;
60
import com.iver.cit.gvsig.fmap.core.FShape;
61
import com.iver.cit.gvsig.fmap.core.IGeometry;
62
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
63
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
64
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
65
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
66
import com.iver.utiles.IPersistance;
67
import com.iver.utiles.XMLEntity;
68
69
/**
70
 * The class AddEventThemeDriver allows to create a new FLayer from a
71
 * gvSIG DataSource.
72
 *
73
 * @author jmorell
74
 */
75
public class AddEventThemeDriver implements VectorialDriver, ObjectDriver, IPersistance {
76
    private Rectangle2D fullExtent = null;
77
    private DataSource ds;
78
    private int xFieldIndex;
79
    private int yFieldIndex;
80
81
    /**
82
     * Initializes this.
83
     * @param ds
84
     * @param xFieldIndex
85
     * @param yFieldIndex
86
     */
87
    public void setData(DataSource ds, int xFieldIndex, int yFieldIndex) {
88
        this.ds = ds;
89
        this.xFieldIndex = xFieldIndex;
90
        this.yFieldIndex = yFieldIndex;
91
    }
92
93
    public int getShapeType() {
94
        return FShape.POINT;
95
    }
96
97
    public int getShapeCount() throws IOException {
98
        try {
99
            return (int) ds.getRowCount();
100
        } catch (DriverException e) {
101
            // TODO Auto-generated catch block
102
            e.printStackTrace();
103
        }
104
        return 0;
105
    }
106
107
    public DriverAttributes getDriverAttributes() {
108
        // TODO Auto-generated method stub
109
        return null;
110
    }
111
112
    public Rectangle2D getFullExtent() throws IOException {
113
        if (fullExtent == null) {
114
            try {
115
                for (int i=0;i<ds.getRowCount();i++) {
116
                    double x = (new Double(((Value)ds.getFieldValue((int)i, xFieldIndex)).toString())).doubleValue();
117
                    double y = (new Double(((Value)ds.getFieldValue((int)i, yFieldIndex)).toString())).doubleValue();
118
                    FGeometry geometry = ShapeFactory.createGeometry(new FPoint2D(x, y));
119
                    Rectangle2D rect = geometry.getBounds2D();
120
                    if (fullExtent == null) {
121
                        fullExtent = rect;
122
                    } else {
123
                        fullExtent.add(rect);
124
                    }
125
                }
126
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
127
                // TODO Auto-generated catch block
128
                e1.printStackTrace();
129
            }
130
        }
131
        return fullExtent;
132
    }
133
134
    public IGeometry getShape(int index){
135
        double x;
136
        double y;
137
        try {
138
            x = (new Double(((Value)ds.getFieldValue(index, xFieldIndex)).toString())).doubleValue();
139
            y = (new Double(((Value)ds.getFieldValue(index, yFieldIndex)).toString())).doubleValue();
140
            FGeometry geometry = ShapeFactory.createGeometry(new FPoint2D(x, y));
141
            return geometry;
142
        } catch (NumberFormatException e) {
143
            // TODO Auto-generated catch block
144
            e.printStackTrace();
145
        } catch (DriverException e) {
146
            // TODO Auto-generated catch block
147
            e.printStackTrace();
148
        }
149
        return null;
150
    }
151
152
    public String getName() {
153
        return "Add Event Theme Driver";
154
    }
155
156
    public int[] getPrimaryKeys() throws DriverException {
157
        // TODO Auto-generated method stub
158
        return null;
159
    }
160
161
    public void write(DataWare dataWare) throws DriverException {
162
        // TODO Auto-generated method stub
163
164
    }
165
166
    public void setDataSourceFactory(DataSourceFactory arg0) {
167
        // TODO Auto-generated method stub
168
169
    }
170
171
    public Value getFieldValue(long rowIndex, int fieldId) throws DriverException {
172
        return ds.getFieldValue(rowIndex, fieldId);
173
    }
174
175
    public int getFieldCount() throws DriverException {
176
        return ds.getFieldCount();
177
    }
178
179
    public String getFieldName(int fieldId) throws DriverException {
180
        return ds.getFieldName(fieldId);
181
    }
182
183
    public long getRowCount() throws DriverException {
184
        return ds.getRowCount();
185
    }
186
187
    public int getFieldType(int i) throws DriverException {
188
        return ds.getFieldType(i);
189
    }
190
191
    public String getClassName() {
192
        return this.getClass().getName();
193
    }
194
195
    // Para guardar en el xml file
196
    public XMLEntity getXMLEntity() {
197
        XMLEntity xml = new XMLEntity();
198
        xml.putProperty("className", this.getClass().getName());
199
        xml.putProperty("xFieldIndex", xFieldIndex);
200
        xml.putProperty("yFieldIndex", yFieldIndex);
201
        xml.putProperty("tableName", ds.getName());
202
        return xml;
203
    }
204
205
    // Para recuperar del xml file
206
    public void setXMLEntity(XMLEntity xml) {
207
        int xFieldIndex = xml.getIntProperty("xFieldIndex");
208
        int yFieldIndex = xml.getIntProperty("yFieldIndex");
209
        String tableName = xml.getStringProperty("tableName");
210
        try {
211
            DataSource ds = LayerFactory.getDataSourceFactory().createRandomDataSource(tableName, DataSourceFactory.AUTOMATIC_OPENING);
212
            setData(ds, xFieldIndex, yFieldIndex);
213
        } catch (NoSuchTableException e) {
214
            throw new RuntimeException(e);
215
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
216
            throw new RuntimeException(e);
217
        } catch (DriverLoadException e) {
218
            // TODO Auto-generated catch block
219
            e.printStackTrace();
220
        }
221
    }
222
}