svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / PostGisDriver.java @ 5901
History | View | Annotate | Download (22.5 KB)
1 |
/*
|
---|---|
2 |
* Created on 04-mar-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.jdbc.postgis; |
45 |
|
46 |
import java.awt.geom.Rectangle2D; |
47 |
import java.math.BigDecimal; |
48 |
import java.nio.ByteBuffer; |
49 |
import java.sql.Connection; |
50 |
import java.sql.ResultSet; |
51 |
import java.sql.SQLException; |
52 |
import java.sql.Statement; |
53 |
import java.sql.Types; |
54 |
import java.util.Hashtable; |
55 |
import java.util.Properties; |
56 |
|
57 |
import org.apache.log4j.Logger; |
58 |
import org.postgis.PGbox2d; |
59 |
import org.postgis.PGbox3d; |
60 |
|
61 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
62 |
import com.hardcode.gdbms.engine.values.Value; |
63 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
64 |
import com.iver.andami.messages.NotificationManager; |
65 |
import com.iver.cit.gvsig.fmap.DriverException; |
66 |
import com.iver.cit.gvsig.fmap.core.FShape; |
67 |
import com.iver.cit.gvsig.fmap.core.ICanReproject; |
68 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
69 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
70 |
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver; |
71 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
72 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
73 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
74 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
75 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
76 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
77 |
import com.iver.cit.gvsig.fmap.edition.ISpatialWriter; |
78 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
79 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
80 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
81 |
|
82 |
/**
|
83 |
* @author FJP
|
84 |
*
|
85 |
* TODO To change the template for this generated type comment go to Window -
|
86 |
* Preferences - Java - Code Generation - Code and Comments
|
87 |
*/
|
88 |
public class PostGisDriver extends DefaultDBDriver implements ICanReproject, IWriteable { |
89 |
private static Logger logger = Logger.getLogger(PostGisDriver.class |
90 |
.getName()); |
91 |
|
92 |
private static int FETCH_SIZE = 5000; |
93 |
|
94 |
private PostGISWriter writer = new PostGISWriter(); |
95 |
|
96 |
private WKBParser2 parser = new WKBParser2(); |
97 |
|
98 |
private int fetch_min = -1; |
99 |
|
100 |
private int fetch_max = -1; |
101 |
|
102 |
private String sqlOrig; |
103 |
|
104 |
/**
|
105 |
* Used by setAbsolutePosition
|
106 |
*/
|
107 |
private String sqlTotal; |
108 |
|
109 |
private String strEPSG = null; |
110 |
|
111 |
private String originalEPSG = null; |
112 |
|
113 |
private Rectangle2D fullExtent = null; |
114 |
|
115 |
private String strAux; |
116 |
|
117 |
private String completeWhere; |
118 |
|
119 |
private String provCursorName = null; |
120 |
|
121 |
int numProvCursors = 0; |
122 |
|
123 |
boolean bShapeTypeRevised = false; |
124 |
|
125 |
static {
|
126 |
try {
|
127 |
Class.forName("org.postgresql.Driver"); |
128 |
} catch (ClassNotFoundException e) { |
129 |
throw new RuntimeException(e); |
130 |
} |
131 |
} |
132 |
|
133 |
/**
|
134 |
*
|
135 |
*/
|
136 |
public PostGisDriver() {
|
137 |
|
138 |
} |
139 |
|
140 |
/*
|
141 |
* (non-Javadoc)
|
142 |
*
|
143 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
144 |
*/
|
145 |
public DriverAttributes getDriverAttributes() {
|
146 |
return null; |
147 |
} |
148 |
|
149 |
/*
|
150 |
* (non-Javadoc)
|
151 |
*
|
152 |
* @see com.hardcode.driverManager.Driver#getName()
|
153 |
*/
|
154 |
public String getName() { |
155 |
return "PostGIS JDBC Driver"; |
156 |
} |
157 |
|
158 |
/**
|
159 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
160 |
*/
|
161 |
public IGeometry getShape(int index) { |
162 |
IGeometry geom = null;
|
163 |
boolean resul;
|
164 |
try {
|
165 |
setAbsolutePosition(index); |
166 |
// strAux = rs.getString(1);
|
167 |
// geom = parser.read(strAux);
|
168 |
if (rs != null) { |
169 |
byte[] data = rs.getBytes(1); |
170 |
geom = parser.parse(data); |
171 |
} |
172 |
} catch (SQLException e) { |
173 |
e.printStackTrace(); |
174 |
} |
175 |
|
176 |
return geom;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* First, the geometry field. After, the rest of fields
|
181 |
*
|
182 |
* @return
|
183 |
*/
|
184 |
/*
|
185 |
* public String getTotalFields() { String strAux = "AsBinary(" +
|
186 |
* getLyrDef().getFieldGeometry() + ")"; String[] fieldNames =
|
187 |
* getLyrDef().getFieldNames(); for (int i=0; i< fieldNames.length; i++) {
|
188 |
* strAux = strAux + ", " + fieldNames[i]; } return strAux; }
|
189 |
*/
|
190 |
|
191 |
/**
|
192 |
* Antes de llamar a esta funci?n hay que haber fijado el workingArea si se
|
193 |
* quiere usar.
|
194 |
*
|
195 |
* @param conn
|
196 |
*/
|
197 |
public void setData(Connection conn, DBLayerDefinition lyrDef) { |
198 |
this.conn = conn;
|
199 |
// TODO: Deber?amos poder quitar Conneciton de la llamada y meterlo
|
200 |
// en lyrDef desde el principio.
|
201 |
|
202 |
lyrDef.setConnection(conn); |
203 |
setLyrDef(lyrDef); |
204 |
|
205 |
getTableEPSG_and_shapeType(); |
206 |
|
207 |
try {
|
208 |
conn.setAutoCommit(false);
|
209 |
sqlOrig = "SELECT " + getTotalFields() + " FROM " |
210 |
+ getLyrDef().getTableName() + " "
|
211 |
+ getLyrDef().getWhereClause(); |
212 |
if (canReproject(strEPSG)) {
|
213 |
completeWhere = getCompoundWhere(sqlOrig, workingArea, strEPSG); |
214 |
} else {
|
215 |
completeWhere = getCompoundWhere(sqlOrig, workingArea, |
216 |
originalEPSG); |
217 |
} |
218 |
String sqlAux = sqlOrig + completeWhere + " ORDER BY " |
219 |
+ getLyrDef().getFieldID(); |
220 |
completeWhere = getLyrDef().getWhereClause() + completeWhere; |
221 |
|
222 |
sqlTotal = sqlAux; |
223 |
logger.info("Cadena SQL:" + sqlAux);
|
224 |
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
|
225 |
ResultSet.CONCUR_READ_ONLY);
|
226 |
// st.setFetchSize(FETCH_SIZE);
|
227 |
st.execute("declare wkb_cursor binary cursor for " + sqlAux);
|
228 |
rs = st.executeQuery("fetch forward " + FETCH_SIZE
|
229 |
+ " in wkb_cursor");
|
230 |
// st.execute("begin");
|
231 |
// bCursorActivo = true;
|
232 |
// rs = st.executeQuery(sqlOrig);
|
233 |
fetch_min = 0;
|
234 |
fetch_max = FETCH_SIZE - 1;
|
235 |
metaData = rs.getMetaData(); |
236 |
doRelateID_FID(); |
237 |
|
238 |
writer.setCreateTable(false);
|
239 |
writer.setWriteAll(false);
|
240 |
writer.initialize(lyrDef); |
241 |
|
242 |
|
243 |
} catch (SQLException e) { |
244 |
NotificationManager.addError( |
245 |
"Error al conectar a la base de datos.", e);
|
246 |
} catch (EditionException e) {
|
247 |
e.printStackTrace(); |
248 |
NotificationManager.addError( |
249 |
"Error inicializando PosGIS Writer.", e);
|
250 |
|
251 |
} |
252 |
} |
253 |
|
254 |
/**
|
255 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
256 |
*/
|
257 |
public Rectangle2D getFullExtent() { |
258 |
if (fullExtent == null) { |
259 |
try {
|
260 |
Statement s = conn.createStatement();
|
261 |
ResultSet r = s.executeQuery("SELECT extent(" |
262 |
+ getLyrDef().getFieldGeometry() |
263 |
+ ") AS FullExtent FROM " + getLyrDef().getTableName()
|
264 |
+ " " + getCompleteWhere());
|
265 |
r.next(); |
266 |
String strAux = r.getString(1); |
267 |
System.out.println("fullExtent = " + strAux); |
268 |
if (strAux.startsWith("BOX3D")) { |
269 |
PGbox3d regeom = new PGbox3d(strAux);
|
270 |
double x = regeom.getLLB().x;
|
271 |
double y = regeom.getLLB().y;
|
272 |
double w = regeom.getURT().x - x;
|
273 |
double h = regeom.getURT().y - y;
|
274 |
fullExtent = new Rectangle2D.Double(x, y, w, h); |
275 |
} else {
|
276 |
PGbox2d regeom = new PGbox2d(strAux);
|
277 |
double x = regeom.getLLB().x;
|
278 |
double y = regeom.getLLB().y;
|
279 |
double w = regeom.getURT().x - x;
|
280 |
double h = regeom.getURT().y - y;
|
281 |
fullExtent = new Rectangle2D.Double(x, y, w, h); |
282 |
} |
283 |
} catch (SQLException e) { |
284 |
System.err.println(e.getMessage());
|
285 |
} |
286 |
|
287 |
} |
288 |
return fullExtent;
|
289 |
} |
290 |
|
291 |
/*
|
292 |
* (non-Javadoc)
|
293 |
*
|
294 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
|
295 |
*/
|
296 |
public IFeatureIterator getFeatureIterator(String sql) |
297 |
throws com.iver.cit.gvsig.fmap.DriverException {
|
298 |
PostGisFeatureIterator geomIterator = null;
|
299 |
try {
|
300 |
// st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
|
301 |
// ResultSet.CONCUR_READ_ONLY);
|
302 |
|
303 |
|
304 |
if (provCursorName != null) { |
305 |
/* st.execute("BEGIN");
|
306 |
st.execute("CLOSE " + provCursorName);
|
307 |
bCursorActivo = false;
|
308 |
st.execute("COMMIT"); */
|
309 |
numProvCursors++; |
310 |
} |
311 |
// st.execute("BEGIN");
|
312 |
provCursorName = "wkb_cursor_prov_" + System.currentTimeMillis() + "" + numProvCursors; |
313 |
|
314 |
// st.execute("BEGIN");
|
315 |
bCursorActivo = true;
|
316 |
// ResultSet rs = st.executeQuery(sql);
|
317 |
geomIterator = new PostGisFeatureIterator(conn, provCursorName, sql);
|
318 |
geomIterator.setLyrDef(getLyrDef()); |
319 |
} catch (SQLException e) { |
320 |
e.printStackTrace(); |
321 |
|
322 |
throw new DriverException(e); |
323 |
// return null;
|
324 |
} |
325 |
|
326 |
return geomIterator;
|
327 |
} |
328 |
|
329 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
330 |
throws DriverException {
|
331 |
if (workingArea != null) |
332 |
r = r.createIntersection(workingArea); |
333 |
|
334 |
String sqlAux;
|
335 |
if (canReproject(strEPSG)) {
|
336 |
sqlAux = sqlOrig + getCompoundWhere(sqlOrig, r, strEPSG); |
337 |
} else {
|
338 |
sqlAux = sqlOrig + getCompoundWhere(sqlOrig, r, originalEPSG); |
339 |
} |
340 |
|
341 |
System.out.println("SqlAux getFeatureIterator = " + sqlAux); |
342 |
|
343 |
return getFeatureIterator(sqlAux);
|
344 |
} |
345 |
|
346 |
/**
|
347 |
* Le pasas el rect?ngulo que quieres pedir. La primera vez es el
|
348 |
* workingArea, y las siguientes una interseccion de este rectangulo con el
|
349 |
* workingArea
|
350 |
*
|
351 |
* @param r
|
352 |
* @param strEPSG
|
353 |
* @return
|
354 |
*/
|
355 |
private String getCompoundWhere(String sql, Rectangle2D r, String strEPSG) { |
356 |
if (r == null) |
357 |
return ""; |
358 |
|
359 |
double xMin = r.getMinX();
|
360 |
double yMin = r.getMinY();
|
361 |
double xMax = r.getMaxX();
|
362 |
double yMax = r.getMaxY();
|
363 |
String wktBox = "GeometryFromText('LINESTRING(" + xMin + " " + yMin |
364 |
+ ", " + xMax + " " + yMin + ", " + xMax + " " + yMax + ", " |
365 |
+ xMin + " " + yMax + ")', " + strEPSG + ")"; |
366 |
String sqlAux;
|
367 |
if (getWhereClause().indexOf("WHERE") != -1) |
368 |
sqlAux = getLyrDef().getFieldGeometry() + " && " + wktBox;
|
369 |
else
|
370 |
sqlAux = "WHERE " + getLyrDef().getFieldGeometry() + " && " |
371 |
+ wktBox; |
372 |
return sqlAux;
|
373 |
} |
374 |
|
375 |
/**
|
376 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getConnectionStringBeginning()
|
377 |
*/
|
378 |
public String getConnectionStringBeginning() { |
379 |
return "jdbc:postgresql:"; |
380 |
} |
381 |
|
382 |
/*
|
383 |
* (non-Javadoc)
|
384 |
*
|
385 |
* @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#getFieldValue(long,
|
386 |
* int)
|
387 |
*/
|
388 |
public Value getFieldValue(long rowIndex, int idField) |
389 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
390 |
boolean resul;
|
391 |
// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
|
392 |
// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
|
393 |
// LO HAGA
|
394 |
// System.out.println("getShape " + index);
|
395 |
int index = (int) (rowIndex); |
396 |
try {
|
397 |
setAbsolutePosition(index); |
398 |
int fieldId = idField + 2; |
399 |
byte[] byteBuf = rs.getBytes(fieldId); |
400 |
if (byteBuf == null) |
401 |
return ValueFactory.createNullValue();
|
402 |
else {
|
403 |
ByteBuffer buf = ByteBuffer.wrap(byteBuf); |
404 |
if (metaData.getColumnType(fieldId) == Types.VARCHAR) |
405 |
return ValueFactory.createValue(rs.getString(fieldId));
|
406 |
if (metaData.getColumnType(fieldId) == Types.FLOAT) |
407 |
return ValueFactory.createValue(buf.getFloat());
|
408 |
if (metaData.getColumnType(fieldId) == Types.DOUBLE) |
409 |
return ValueFactory.createValue(buf.getDouble());
|
410 |
if (metaData.getColumnType(fieldId) == Types.INTEGER) |
411 |
return ValueFactory.createValue(buf.getInt());
|
412 |
if (metaData.getColumnType(fieldId) == Types.BIGINT) |
413 |
return ValueFactory.createValue(buf.getLong());
|
414 |
if (metaData.getColumnType(fieldId) == Types.BIT) |
415 |
// TODO
|
416 |
return ValueFactory.createValue(rs.getBoolean(fieldId));
|
417 |
if (metaData.getColumnType(fieldId) == Types.DATE) |
418 |
// TODO
|
419 |
return ValueFactory.createValue(rs.getDate(fieldId));
|
420 |
if (metaData.getColumnType(fieldId) == Types.NUMERIC) { |
421 |
// System.out.println(metaData.getColumnName(fieldId) + " "
|
422 |
// + metaData.getColumnClassName(fieldId));
|
423 |
short ndigits = buf.getShort();
|
424 |
short weight = buf.getShort();
|
425 |
short sign = buf.getShort();
|
426 |
short dscale = buf.getShort();
|
427 |
String strAux;
|
428 |
if (sign == 0) |
429 |
strAux = "+";
|
430 |
else
|
431 |
strAux = "-";
|
432 |
|
433 |
for (int iDigit = 0; iDigit < ndigits; iDigit++) { |
434 |
short digit = buf.getShort();
|
435 |
strAux = strAux + digit; |
436 |
if (iDigit == weight)
|
437 |
strAux = strAux + ".";
|
438 |
|
439 |
} |
440 |
strAux = strAux + "0";
|
441 |
String str2;
|
442 |
BigDecimal dec;
|
443 |
dec = new BigDecimal(strAux); |
444 |
// System.out.println(ndigits + "_" + weight + "_" + dscale
|
445 |
// + "_" + strAux);
|
446 |
// System.out.println(strAux + " Big= " + dec);
|
447 |
return ValueFactory.createValue(dec.doubleValue());
|
448 |
} |
449 |
|
450 |
} |
451 |
} catch (SQLException e) { |
452 |
throw new com.hardcode.gdbms.engine.data.driver.DriverException(e |
453 |
.getMessage()); |
454 |
} |
455 |
return ValueFactory.createNullValue();
|
456 |
|
457 |
} |
458 |
|
459 |
public void open() throws com.iver.cit.gvsig.fmap.DriverException { |
460 |
/*
|
461 |
* try { st = conn.createStatement(); st.setFetchSize(2000); if
|
462 |
* (bCursorActivo) close(); st.execute("declare wkb_cursor binary cursor
|
463 |
* for " + sqlOrig); rs = st.executeQuery("fetch forward all in
|
464 |
* wkb_cursor"); // st.execute("BEGIN"); bCursorActivo = true; } catch
|
465 |
* (SQLException e) { e.printStackTrace(); throw new
|
466 |
* com.iver.cit.gvsig.fmap.DriverException(e); }
|
467 |
*/
|
468 |
|
469 |
} |
470 |
|
471 |
private void setAbsolutePosition(int index) throws SQLException { |
472 |
// TODO: USAR LIMIT Y ORDER BY, Y HACERLO TAMBI?N PARA
|
473 |
// MYSQL
|
474 |
|
475 |
// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
|
476 |
// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
|
477 |
// LO HAGA
|
478 |
// System.out.println("getShape " + index + " fetchMin=" + fetch_min + "
|
479 |
// fetchMax=" + fetch_max);
|
480 |
if (index < fetch_min) {
|
481 |
// rs.close();
|
482 |
st.execute("CLOSE wkb_cursor");
|
483 |
st.execute("declare wkb_cursor binary cursor for " + sqlTotal);
|
484 |
rs = st.executeQuery("fetch forward " + FETCH_SIZE
|
485 |
+ " in wkb_cursor");
|
486 |
|
487 |
// rs.beforeFirst();
|
488 |
|
489 |
// rs = st.executeQuery(sqlOrig);
|
490 |
fetch_min = 0;
|
491 |
fetch_max = FETCH_SIZE - 1;
|
492 |
} |
493 |
while (index > fetch_max && index != 0) { |
494 |
rs = st.executeQuery("fetch forward " + FETCH_SIZE
|
495 |
+ " in wkb_cursor");
|
496 |
// rs.next();
|
497 |
/*
|
498 |
* rs.afterLast(); // forzamos una carga rs.next();
|
499 |
*/
|
500 |
fetch_min = fetch_max + 1;
|
501 |
fetch_max = fetch_min + FETCH_SIZE - 1;
|
502 |
// System.out.println("fetchSize = " + rs.getFetchSize() + " " +
|
503 |
// fetch_min + "-" + fetch_max);
|
504 |
} |
505 |
if (rs != null) |
506 |
rs.absolute(index - fetch_min + 1);
|
507 |
|
508 |
// rs.absolute(index+1);
|
509 |
|
510 |
} |
511 |
|
512 |
/**
|
513 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String)
|
514 |
*/
|
515 |
public String getGeometryField(String fieldName) { |
516 |
return "ASBINARY(" + fieldName + ", 'XDR')"; |
517 |
} |
518 |
|
519 |
/**
|
520 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
521 |
*/
|
522 |
public int[] getPrimaryKeys() |
523 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
524 |
// TODO Auto-generated method stub
|
525 |
return null; |
526 |
} |
527 |
|
528 |
/**
|
529 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getDefaultPort()
|
530 |
*/
|
531 |
public int getDefaultPort() { |
532 |
return 5432; |
533 |
} |
534 |
|
535 |
/**
|
536 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
537 |
*/
|
538 |
public void write(DataWare arg0) |
539 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
540 |
// TODO Auto-generated method stub
|
541 |
|
542 |
} |
543 |
|
544 |
/*
|
545 |
* (non-Javadoc)
|
546 |
*
|
547 |
* @see com.iver.cit.gvsig.fmap.core.ICanReproject#getSourceProjection()
|
548 |
*/
|
549 |
public String getSourceProjection() { |
550 |
if (originalEPSG == null) |
551 |
getTableEPSG_and_shapeType(); |
552 |
return originalEPSG;
|
553 |
} |
554 |
|
555 |
/**
|
556 |
* Las tablas con geometr?as est?n en la tabla GEOMETRY_COLUMNS y de
|
557 |
* ah? sacamos en qu? proyecci?n est?n.
|
558 |
* El problema es que si el usuario hace una vista de esa
|
559 |
* tabla, no estar? dada de alta aqu? y entonces gvSIG
|
560 |
* no se entera en qu? proyecci?n est? trabajando (y le
|
561 |
* ponemos un -1 como mal menor). El -1 implica que luego
|
562 |
* no podremos reproyectar al vuelo desde la base de datos.
|
563 |
*/
|
564 |
private void getTableEPSG_and_shapeType() { |
565 |
try {
|
566 |
Statement stAux = conn.createStatement();
|
567 |
|
568 |
String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '" |
569 |
+ getTableName() + "';";
|
570 |
ResultSet rs = stAux.executeQuery(sql);
|
571 |
rs.next(); |
572 |
originalEPSG = "" + rs.getInt("SRID"); |
573 |
String geometryType = rs.getString("TYPE"); |
574 |
int shapeType = FShape.MULTI;
|
575 |
if (geometryType.compareToIgnoreCase("POINT") == 0) |
576 |
shapeType = FShape.POINT; |
577 |
if (geometryType.compareToIgnoreCase("LINESTRING") == 0) |
578 |
shapeType = FShape.LINE; |
579 |
if (geometryType.compareToIgnoreCase("POLYGON") == 0) |
580 |
shapeType = FShape.POLYGON; |
581 |
if (geometryType.compareToIgnoreCase("MULTIPOINT") == 0) |
582 |
shapeType = FShape.POINT; |
583 |
if (geometryType.compareToIgnoreCase("MULTILINESTRING") == 0) |
584 |
shapeType = FShape.LINE; |
585 |
if (geometryType.compareToIgnoreCase("MULTIPOLYGON") == 0) |
586 |
shapeType = FShape.POLYGON; |
587 |
|
588 |
getLyrDef().setShapeType(shapeType); |
589 |
rs.close(); |
590 |
} catch (SQLException e) { |
591 |
// TODO Auto-generated catch block
|
592 |
originalEPSG = "-1";
|
593 |
logger.error(e); |
594 |
e.printStackTrace(); |
595 |
} |
596 |
|
597 |
} |
598 |
|
599 |
/*
|
600 |
* (non-Javadoc)
|
601 |
*
|
602 |
* @see com.iver.cit.gvsig.fmap.core.ICanReproject#getDestProjection()
|
603 |
*/
|
604 |
public String getDestProjection() { |
605 |
return strEPSG;
|
606 |
} |
607 |
|
608 |
/*
|
609 |
* (non-Javadoc)
|
610 |
*
|
611 |
* @see com.iver.cit.gvsig.fmap.core.ICanReproject#setDestProjection(java.lang.String)
|
612 |
*/
|
613 |
public void setDestProjection(String toEPSG) { |
614 |
this.strEPSG = toEPSG;
|
615 |
} |
616 |
|
617 |
/*
|
618 |
* (non-Javadoc)
|
619 |
*
|
620 |
* @see com.iver.cit.gvsig.fmap.core.ICanReproject#canReproject(java.lang.String)
|
621 |
*/
|
622 |
public boolean canReproject(String toEPSGdestinyProjection) { |
623 |
// TODO POR AHORA, REPROYECTA SIEMPRE gvSIG.
|
624 |
return false; |
625 |
} |
626 |
|
627 |
/*
|
628 |
* (non-Javadoc)
|
629 |
*
|
630 |
* @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#doRelateID_FID()
|
631 |
*/
|
632 |
protected void doRelateID_FID() { |
633 |
hashRelate = new Hashtable(); |
634 |
try {
|
635 |
String strSQL = "SELECT " + getLyrDef().getFieldID() + " FROM " |
636 |
+ getLyrDef().getTableName() + " "
|
637 |
+ getLyrDef().getWhereClause(); |
638 |
if (canReproject(strEPSG)) {
|
639 |
strSQL = strSQL |
640 |
+ getCompoundWhere(strSQL, workingArea, strEPSG); |
641 |
} else {
|
642 |
strSQL = strSQL |
643 |
+ getCompoundWhere(strSQL, workingArea, originalEPSG); |
644 |
} |
645 |
strSQL = strSQL + " ORDER BY " + getLyrDef().getFieldID();
|
646 |
Statement s = getConnection().createStatement(
|
647 |
ResultSet.TYPE_SCROLL_INSENSITIVE,
|
648 |
ResultSet.CONCUR_READ_ONLY);
|
649 |
int fetchSize = 5000; |
650 |
ResultSet r = s.executeQuery(strSQL);
|
651 |
int id = 0; |
652 |
String gid;
|
653 |
while (r.next()) {
|
654 |
gid = r.getString(1);
|
655 |
Value aux = ValueFactory.createValue(gid); |
656 |
hashRelate.put(aux, new Integer(id)); |
657 |
// System.out.println("ASOCIANDO CLAVE " + aux + " CON VALOR " + id);
|
658 |
id++; |
659 |
// System.out.println("Row " + id + ":" + strAux);
|
660 |
} |
661 |
s.close(); |
662 |
numReg = id; |
663 |
|
664 |
/*
|
665 |
* for (int index = 0; index < getShapeCount(); index++) { Value aux =
|
666 |
* getFieldValue(index, idFID_FieldName-2); hashRelate.put(aux, new
|
667 |
* Integer(index)); // System.out.println("Row " + index + " clave=" +
|
668 |
* aux); }
|
669 |
*/
|
670 |
/*
|
671 |
* int index = 0;
|
672 |
*
|
673 |
* while (rs.next()) { Value aux = getFieldValue(index,
|
674 |
* idFID_FieldName-2); hashRelate.put(aux, new Integer(index));
|
675 |
* index++; System.out.println("Row " + index + " clave=" + aux); }
|
676 |
* numReg = index;
|
677 |
*/
|
678 |
// rs.beforeFirst();
|
679 |
/*
|
680 |
* } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) { //
|
681 |
* TODO Auto-generated catch block e.printStackTrace();
|
682 |
*/
|
683 |
} catch (SQLException e) { |
684 |
// TODO Auto-generated catch block
|
685 |
e.printStackTrace(); |
686 |
} |
687 |
} |
688 |
|
689 |
public String getSqlTotal() { |
690 |
return sqlTotal;
|
691 |
} |
692 |
|
693 |
/**
|
694 |
* @return Returns the completeWhere.
|
695 |
*/
|
696 |
public String getCompleteWhere() { |
697 |
return completeWhere;
|
698 |
} |
699 |
|
700 |
/*
|
701 |
* (non-Javadoc)
|
702 |
*
|
703 |
* @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#close()
|
704 |
*/
|
705 |
public void close() { |
706 |
super.close();
|
707 |
/*
|
708 |
* if (bCursorActivo) { try { // st =
|
709 |
* conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
|
710 |
* ResultSet.CONCUR_READ_ONLY); st.execute("CLOSE wkb_cursor_prov"); //
|
711 |
* st.close(); } catch (SQLException e) { // TODO Auto-generated catch
|
712 |
* block e.printStackTrace(); } bCursorActivo = false; }
|
713 |
*/
|
714 |
|
715 |
} |
716 |
|
717 |
/*
|
718 |
* (non-Javadoc)
|
719 |
*
|
720 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getFeatureIterator(java.awt.geom.Rectangle2D,
|
721 |
* java.lang.String, java.lang.String[])
|
722 |
*/
|
723 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, |
724 |
String[] alphaNumericFieldsNeeded) throws DriverException { |
725 |
try {
|
726 |
if (workingArea != null) |
727 |
r = r.createIntersection(workingArea); |
728 |
// if (getLyrDef()==null){
|
729 |
// load();
|
730 |
// throw new DriverException("Fallo de la conexi?n");
|
731 |
// }
|
732 |
String strAux = getGeometryField(getLyrDef().getFieldGeometry());
|
733 |
|
734 |
boolean found = false; |
735 |
if (alphaNumericFieldsNeeded != null) { |
736 |
for (int i = 0; i < alphaNumericFieldsNeeded.length; i++) { |
737 |
strAux = strAux + ", " + alphaNumericFieldsNeeded[i];
|
738 |
if (alphaNumericFieldsNeeded[i].equals(getLyrDef()
|
739 |
.getFieldID())) |
740 |
found = true;
|
741 |
} |
742 |
} |
743 |
// Nos aseguramos de pedir siempre el campo ID
|
744 |
if (found == false) |
745 |
strAux = strAux + ", " + getLyrDef().getFieldID();
|
746 |
|
747 |
String sqlProv = "SELECT " + strAux + " FROM " |
748 |
+ getLyrDef().getTableName() + " "
|
749 |
+ getLyrDef().getWhereClause(); |
750 |
|
751 |
String sqlAux;
|
752 |
if (canReproject(strEPSG)) {
|
753 |
sqlAux = sqlProv + getCompoundWhere(sqlProv, r, strEPSG); |
754 |
} else {
|
755 |
sqlAux = sqlProv + getCompoundWhere(sqlProv, r, originalEPSG); |
756 |
} |
757 |
|
758 |
System.out.println("SqlAux getFeatureIterator = " + sqlAux); |
759 |
|
760 |
return getFeatureIterator(sqlAux);
|
761 |
} catch (Exception e) { |
762 |
throw new DriverException(e); |
763 |
} |
764 |
} |
765 |
|
766 |
/* public void preProcess() throws EditionException {
|
767 |
writer.preProcess();
|
768 |
}
|
769 |
|
770 |
public void process(IRowEdited row) throws EditionException {
|
771 |
writer.process(row);
|
772 |
}
|
773 |
|
774 |
public void postProcess() throws EditionException {
|
775 |
writer.postProcess();
|
776 |
}
|
777 |
|
778 |
public String getCapability(String capability) {
|
779 |
return writer.getCapability(capability);
|
780 |
}
|
781 |
|
782 |
public void setCapabilities(Properties capabilities) {
|
783 |
writer.setCapabilities(capabilities);
|
784 |
}
|
785 |
|
786 |
public boolean canWriteAttribute(int sqlType) {
|
787 |
return writer.canWriteAttribute(sqlType);
|
788 |
}
|
789 |
|
790 |
public boolean canWriteGeometry(int gvSIGgeometryType) {
|
791 |
return writer.canWriteGeometry(gvSIGgeometryType);
|
792 |
}
|
793 |
|
794 |
public void initialize(ITableDefinition layerDef) throws EditionException {
|
795 |
writer.setCreateTable(false);
|
796 |
writer.setWriteAll(false);
|
797 |
// Obtenemos el DBLayerDefinition a partir del driver
|
798 |
|
799 |
DBLayerDefinition dbLyrDef = getLyrDef();
|
800 |
|
801 |
|
802 |
writer.initialize(dbLyrDef);
|
803 |
}
|
804 |
*/
|
805 |
public boolean isWritable() { |
806 |
return true; |
807 |
} |
808 |
|
809 |
public IWriter getWriter() {
|
810 |
return writer;
|
811 |
} |
812 |
|
813 |
|
814 |
} |