svn-gvsig-desktop / trunk / extensions / extAddEventTheme / src / com / iver / gvsig / addeventtheme / AddEventThemeDriver.java @ 10626
History | View | Annotate | Download (10.2 KB)
1 |
/*
|
---|---|
2 |
* Created on 10-nov-2005
|
3 |
*
|
4 |
* gvSIG. Sistema de Informacin Geogrfica 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 Ibez, 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 |
|
45 |
/*
|
46 |
* $log$
|
47 |
*/
|
48 |
package com.iver.gvsig.addeventtheme; |
49 |
|
50 |
import java.awt.geom.Rectangle2D; |
51 |
import java.io.IOException; |
52 |
|
53 |
import com.hardcode.driverManager.DriverLoadException; |
54 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
55 |
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException; |
56 |
import com.hardcode.gdbms.engine.data.DataSource; |
57 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
58 |
import com.hardcode.gdbms.engine.data.IDataSourceListener; |
59 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
60 |
import com.hardcode.gdbms.engine.data.SourceInfo; |
61 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
62 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
63 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
64 |
import com.hardcode.gdbms.engine.values.Value; |
65 |
import com.iver.andami.PluginServices; |
66 |
import com.iver.cit.gvsig.ProjectExtension; |
67 |
import com.iver.cit.gvsig.fmap.core.FGeometry; |
68 |
import com.iver.cit.gvsig.fmap.core.FPoint2D; |
69 |
import com.iver.cit.gvsig.fmap.core.FShape; |
70 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
71 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
72 |
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes; |
73 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
74 |
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver; |
75 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
76 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
77 |
import com.iver.cit.gvsig.fmap.layers.XMLException; |
78 |
import com.iver.utiles.IPersistance; |
79 |
import com.iver.utiles.XMLEntity; |
80 |
|
81 |
/**
|
82 |
* The class AddEventThemeDriver allows to create a new FLayer from a
|
83 |
* gvSIG DataSource.
|
84 |
*
|
85 |
* @author jmorell
|
86 |
*/
|
87 |
public class AddEventThemeDriver implements VectorialDriver, ObjectDriver, BoundedShapes, IPersistance, IDataSourceListener { |
88 |
private Rectangle2D fullExtent = null; |
89 |
private SelectableDataSource ds;
|
90 |
private int xFieldIndex; |
91 |
private int yFieldIndex; |
92 |
|
93 |
/**
|
94 |
* Initializes this.
|
95 |
* @param ds
|
96 |
* @param xFieldIndex
|
97 |
* @param yFieldIndex
|
98 |
* @throws ReadDriverException
|
99 |
* @throws DriverException
|
100 |
*/
|
101 |
public void setData(DataSource ds, int xFieldIndex, int yFieldIndex) throws ReadDriverException { |
102 |
this.ds = new SelectableDataSource(ds); |
103 |
this.ds.addDataSourceListener(this); |
104 |
this.xFieldIndex = xFieldIndex;
|
105 |
this.yFieldIndex = yFieldIndex;
|
106 |
} |
107 |
public int[] getFieldsIndex(){ |
108 |
int[] n=new int[2]; |
109 |
n[0]=xFieldIndex;
|
110 |
n[1]=yFieldIndex;
|
111 |
return n;
|
112 |
} |
113 |
public int getShapeType() { |
114 |
return FShape.POINT;
|
115 |
} |
116 |
|
117 |
public int getShapeCount() throws ReadDriverException { |
118 |
return (int) ds.getRowCount(); |
119 |
} |
120 |
|
121 |
public DriverAttributes getDriverAttributes() {
|
122 |
// TODO Auto-generated method stub
|
123 |
return null; |
124 |
} |
125 |
|
126 |
private double getX(int row) throws ReadDriverException { |
127 |
try {
|
128 |
return (new Double(ds.getFieldValue((int)row, xFieldIndex).toString())).doubleValue(); |
129 |
} catch(NumberFormatException e) { |
130 |
return 0; |
131 |
} |
132 |
} |
133 |
private double getY(int row) throws ReadDriverException { |
134 |
try {
|
135 |
return (new Double(ds.getFieldValue((int)row, yFieldIndex).toString())).doubleValue(); |
136 |
} catch(NumberFormatException e) { |
137 |
return 0; |
138 |
} |
139 |
} |
140 |
|
141 |
public Rectangle2D getFullExtent() throws ReadDriverException { |
142 |
if (fullExtent == null) { |
143 |
for (int i = 0; i < ds.getRowCount(); i++) { |
144 |
double x = this.getX(i); // (new |
145 |
// Double(((Value)ds.getFieldValue((int)i,
|
146 |
// xFieldIndex)).toString())).doubleValue();
|
147 |
double y = this.getY(i); // (new |
148 |
// Double(((Value)ds.getFieldValue((int)i,
|
149 |
// yFieldIndex)).toString())).doubleValue();
|
150 |
FGeometry geometry = ShapeFactory.createGeometry(new FPoint2D(
|
151 |
x, y)); |
152 |
Rectangle2D rect = geometry.getBounds2D();
|
153 |
if (fullExtent == null) { |
154 |
fullExtent = rect; |
155 |
} else {
|
156 |
fullExtent.add(rect); |
157 |
} |
158 |
} |
159 |
} |
160 |
return fullExtent;
|
161 |
} |
162 |
|
163 |
public IGeometry getShape(int index) throws ReadDriverException{ |
164 |
double x;
|
165 |
double y;
|
166 |
try {
|
167 |
x = this.getX(index); // (new |
168 |
// Double(((Value)ds.getFieldValue(index,
|
169 |
// xFieldIndex)).toString())).doubleValue();
|
170 |
y = this.getY(index); //(new Double(((Value)ds.getFieldValue(index, yFieldIndex)).toString())).doubleValue(); |
171 |
//System.err.println("La X = "+x+" , La Y = "+y);
|
172 |
FGeometry geometry = ShapeFactory.createGeometry(new FPoint2D(x, y));
|
173 |
return geometry;
|
174 |
} catch (NumberFormatException e) { |
175 |
// TODO Auto-generated catch block
|
176 |
e.printStackTrace(); |
177 |
} |
178 |
return null; |
179 |
} |
180 |
|
181 |
public String getName() { |
182 |
return "Add Event Layer Driver"; |
183 |
} |
184 |
|
185 |
public int[] getPrimaryKeys() { |
186 |
// TODO Auto-generated method stub
|
187 |
return null; |
188 |
} |
189 |
|
190 |
public void write(DataWare dataWare) { |
191 |
// TODO Auto-generated method stub
|
192 |
|
193 |
} |
194 |
|
195 |
public void setDataSourceFactory(DataSourceFactory arg0) { |
196 |
// TODO Auto-generated method stub
|
197 |
|
198 |
} |
199 |
|
200 |
public Value getFieldValue(long rowIndex, int fieldId) throws ReadDriverException { |
201 |
return ds.getFieldValue(rowIndex, fieldId);
|
202 |
} |
203 |
|
204 |
public int getFieldCount() throws ReadDriverException { |
205 |
return ds.getFieldCount();
|
206 |
} |
207 |
|
208 |
public String getFieldName(int fieldId) throws ReadDriverException { |
209 |
return ds.getFieldName(fieldId);
|
210 |
} |
211 |
|
212 |
public long getRowCount() throws ReadDriverException { |
213 |
return ds.getRowCount();
|
214 |
} |
215 |
|
216 |
public int getFieldType(int i) throws ReadDriverException { |
217 |
return ds.getFieldType(i);
|
218 |
} |
219 |
|
220 |
public String getClassName() { |
221 |
return this.getClass().getName(); |
222 |
} |
223 |
|
224 |
|
225 |
// Para guardar en el xml file
|
226 |
public XMLEntity getXMLEntity() {
|
227 |
XMLEntity xml = new XMLEntity();
|
228 |
xml.putProperty("className", this.getClass().getName()); |
229 |
xml.putProperty("xFieldIndex", xFieldIndex);
|
230 |
xml.putProperty("yFieldIndex", yFieldIndex);
|
231 |
xml.putProperty("tableName", ds.getName());
|
232 |
XMLEntity dsXML = null;
|
233 |
|
234 |
dsXML = this.getDataSourceXML();
|
235 |
if (dsXML != null) { |
236 |
xml.putProperty("hasDSInfo", true); |
237 |
xml.addChild(dsXML); |
238 |
} else {
|
239 |
xml.putProperty("hasDSInfo", false); |
240 |
} |
241 |
return xml;
|
242 |
} |
243 |
|
244 |
// Para recuperar del xml file
|
245 |
public void setXMLEntity(XMLEntity xml) { |
246 |
int xFieldIndex = xml.getIntProperty("xFieldIndex"); |
247 |
int yFieldIndex = xml.getIntProperty("yFieldIndex"); |
248 |
String tableName = xml.getStringProperty("tableName"); |
249 |
DataSource ds;
|
250 |
try {
|
251 |
ds = LayerFactory.getDataSourceFactory().createRandomDataSource(tableName, DataSourceFactory.AUTOMATIC_OPENING); |
252 |
// Intentar reconstruir el DS!!!!!!!!!!
|
253 |
ds = this.getDataSourceFromXML(xml.getChild(0)); |
254 |
setData(ds, xFieldIndex, yFieldIndex); |
255 |
} catch (NoSuchTableException e) {
|
256 |
if (!xml.contains("hasDSInfo") || !xml.getBooleanProperty("hasDSInfo")) { |
257 |
// No esta registrado el DS y no tenemos su informacion en el
|
258 |
// xml (proyecto viejo)... no podemos arreglarlo
|
259 |
throw new RuntimeException(e); |
260 |
} |
261 |
|
262 |
} catch (DriverLoadException e) {
|
263 |
throw new RuntimeException(e); |
264 |
} catch (ReadDriverException e) {
|
265 |
throw new RuntimeException(e); |
266 |
} |
267 |
} |
268 |
|
269 |
|
270 |
public int getFieldWidth(int i) throws ReadDriverException { |
271 |
return ds.getFieldWidth(i);
|
272 |
} |
273 |
|
274 |
public Rectangle2D getShapeBounds(int index) throws ReadDriverException { |
275 |
return getShape(index).getBounds2D();
|
276 |
} |
277 |
|
278 |
public int getShapeType(int index) throws ReadDriverException { |
279 |
return getShape(index).getGeometryType();
|
280 |
} |
281 |
public boolean isWritable() { |
282 |
return true; |
283 |
} |
284 |
|
285 |
public void reloaded(DataSource dataSource) { |
286 |
this.fullExtent = null; |
287 |
|
288 |
} |
289 |
|
290 |
public void reload() throws ReloadDriverException{ |
291 |
this.ds.reload();
|
292 |
|
293 |
} |
294 |
|
295 |
private XMLEntity getDataSourceXML() {
|
296 |
SourceInfo di = this.ds.getSourceInfo();
|
297 |
ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
298 |
|
299 |
return ext.getProject().getSourceInfoXMLEntity(di);
|
300 |
} |
301 |
|
302 |
private DataSource getDataSourceFromXML(XMLEntity xml) throws DriverLoadException, ReadDriverException, NoSuchTableException{ |
303 |
if (xml.getStringProperty("type").equals("otherDriverFile")) { |
304 |
LayerFactory.getDataSourceFactory().addFileDataSource(xml.getStringProperty( |
305 |
"driverName"), xml.getStringProperty("gdbmsname"), |
306 |
xml.getStringProperty("file"));
|
307 |
} else if (xml.getStringProperty("type").equals("sameDriverFile")) { |
308 |
} else if (xml.getStringProperty("type").equals("db")) { |
309 |
LayerFactory.getDataSourceFactory().addDBDataSourceByTable(xml.getStringProperty( |
310 |
"gdbmsname"), xml.getStringProperty("host"), |
311 |
xml.getIntProperty("port"),
|
312 |
xml.getStringProperty("user"),
|
313 |
xml.getStringProperty("password"),
|
314 |
xml.getStringProperty("dbName"),
|
315 |
xml.getStringProperty("tableName"),
|
316 |
xml.getStringProperty("driverInfo"));
|
317 |
} |
318 |
|
319 |
|
320 |
DataSource ds = LayerFactory.getDataSourceFactory().createRandomDataSource(xml.getStringProperty(
|
321 |
"gdbmsname"), DataSourceFactory.AUTOMATIC_OPENING);
|
322 |
|
323 |
return ds;
|
324 |
|
325 |
} |
326 |
|
327 |
} |