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