Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAddEventTheme / src / com / iver / cit / gvsig / fmap / drivers / addevent / AddEventThemeDriver.java @ 5258

History | View | Annotate | Download (7.8 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.cit.gvsig.fmap.drivers.addevent;
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.BoundedShapes;
64
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
65
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
66
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
67
import com.iver.utiles.IPersistance;
68
import com.iver.utiles.XMLEntity;
69

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

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

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

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

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

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

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

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

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

    
171
    }
172

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

    
176
    }
177

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

    
182
    public int getFieldCount() throws DriverException {
183
        return ds.getFieldCount();
184
    }
185

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

    
190
    public long getRowCount() throws DriverException {
191
        return ds.getRowCount();
192
    }
193

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

    
198
    public String getClassName() {
199
        return this.getClass().getName();
200
    }
201

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

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

    
230
        public void reLoad() throws IOException {
231
                // TODO Auto-generated method stub
232

    
233
        }
234

    
235
        public int getFieldWidth(int i) throws DriverException {
236
                return ds.getFieldWidth(i);
237
        }
238

    
239
        public Rectangle2D getShapeBounds(int index) throws IOException {
240
                return getShape(index).getBounds2D();
241
        }
242

    
243
        public int getShapeType(int index) {
244
                return getShape(index).getGeometryType();
245
        }
246
}