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