root / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / jdbc / postgis / PostGisDriver.java @ 2023
History | View | Annotate | Download (12.2 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.nio.ByteBuffer; |
48 |
import java.sql.Connection; |
49 |
import java.sql.ResultSet; |
50 |
import java.sql.SQLException; |
51 |
import java.sql.Statement; |
52 |
import java.sql.Types; |
53 |
|
54 |
import org.apache.log4j.Logger; |
55 |
import org.postgis.PGbox2d; |
56 |
import org.postgis.PGbox3d; |
57 |
|
58 |
import com.hardcode.gdbms.engine.values.Value; |
59 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
60 |
import com.iver.cit.gvsig.fmap.DriverException; |
61 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
62 |
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver; |
63 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
64 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
65 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser; |
66 |
|
67 |
/**
|
68 |
* @author FJP
|
69 |
*
|
70 |
* TODO To change the template for this generated type comment go to
|
71 |
* Window - Preferences - Java - Code Generation - Code and Comments
|
72 |
*/
|
73 |
public class PostGisDriver extends DefaultDBDriver { |
74 |
private static Logger logger = Logger.getLogger(PostGisDriver.class.getName()); |
75 |
private WKBParser parser = new WKBParser(); |
76 |
private int fetch_min=-1; |
77 |
private int fetch_max=-1; |
78 |
|
79 |
private Rectangle2D fullExtent = null; |
80 |
private String strAux; |
81 |
|
82 |
static {
|
83 |
try {
|
84 |
Class.forName("org.postgresql.Driver"); |
85 |
} catch (ClassNotFoundException e) { |
86 |
throw new RuntimeException(e); |
87 |
} |
88 |
} |
89 |
|
90 |
/**
|
91 |
*
|
92 |
*/
|
93 |
public PostGisDriver() {
|
94 |
} |
95 |
/* (non-Javadoc)
|
96 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
97 |
*/
|
98 |
public DriverAttributes getDriverAttributes() {
|
99 |
return null; |
100 |
} |
101 |
|
102 |
/* (non-Javadoc)
|
103 |
* @see com.hardcode.driverManager.Driver#getName()
|
104 |
*/
|
105 |
public String getName() { |
106 |
return "PostGIS JDBC Driver"; |
107 |
} |
108 |
|
109 |
/**
|
110 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
111 |
*/
|
112 |
public IGeometry getShape(int index) { |
113 |
IGeometry geom = null;
|
114 |
boolean resul;
|
115 |
try {
|
116 |
setAbsolutePosition(index); |
117 |
// strAux = rs.getString(1);
|
118 |
// geom = parser.read(strAux);
|
119 |
byte[] data = rs.getBytes(1); |
120 |
geom = parser.parse(data); |
121 |
|
122 |
} catch (SQLException e) { |
123 |
e.printStackTrace(); |
124 |
} |
125 |
|
126 |
return geom;
|
127 |
} |
128 |
/**
|
129 |
* @param conn
|
130 |
* @param tableName
|
131 |
* @param fields OJO: EL PRIMER CAMPO HA DE SER EL DE GEOMETRIA
|
132 |
* @param whereClause
|
133 |
*/
|
134 |
public void setData(Connection conn, String tableName, String fields, String whereClause, int id_FID_field) |
135 |
{ |
136 |
this.conn = conn;
|
137 |
this.tableName = tableName;
|
138 |
this.fields = fields;
|
139 |
this.whereClause = whereClause;
|
140 |
this.sqlOrig = "SELECT " + fields + " FROM " + tableName + " " + whereClause; |
141 |
this.idFID_FieldName = id_FID_field;
|
142 |
try {
|
143 |
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
144 |
st.setFetchSize(5000);
|
145 |
logger.info("Cadena SQL:" + this.sqlOrig); |
146 |
st.execute("declare wkb_cursor binary cursor for " + sqlOrig);
|
147 |
rs = st.executeQuery("fetch forward all in wkb_cursor");
|
148 |
// st.execute("begin");
|
149 |
// bCursorActivo = true;
|
150 |
// rs = st.executeQuery(sqlOrig);
|
151 |
fetch_min = 0;
|
152 |
fetch_max = rs.getFetchSize(); |
153 |
metaData = rs.getMetaData(); |
154 |
doRelateID_FID(); |
155 |
|
156 |
} catch (SQLException e) { |
157 |
// TODO Auto-generated catch block
|
158 |
e.printStackTrace(); |
159 |
} |
160 |
} |
161 |
|
162 |
/**
|
163 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
164 |
*/
|
165 |
public Rectangle2D getFullExtent(){ |
166 |
if (fullExtent == null) |
167 |
{ |
168 |
try
|
169 |
{ |
170 |
Statement s = conn.createStatement();
|
171 |
ResultSet r = s.executeQuery("SELECT extent(the_geom) AS FullExtent FROM " + tableName); |
172 |
r.next(); |
173 |
String strAux = r.getString(1); |
174 |
System.out.println("fullExtent = " + strAux); |
175 |
if (strAux.startsWith("BOX3D")) |
176 |
{ |
177 |
PGbox3d regeom = new PGbox3d(strAux);
|
178 |
double x = regeom.getLLB().x;
|
179 |
double y = regeom.getLLB().y;
|
180 |
double w = regeom.getURT().x -x;
|
181 |
double h = regeom.getURT().y - y;
|
182 |
fullExtent = new Rectangle2D.Double(x, y, w, h); |
183 |
} |
184 |
else
|
185 |
{ |
186 |
PGbox2d regeom = new PGbox2d(strAux);
|
187 |
double x = regeom.getLLB().x;
|
188 |
double y = regeom.getLLB().y;
|
189 |
double w = regeom.getURT().x -x;
|
190 |
double h = regeom.getURT().y - y;
|
191 |
fullExtent = new Rectangle2D.Double(x, y, w, h); |
192 |
} |
193 |
} |
194 |
catch (SQLException e) |
195 |
{ |
196 |
System.err.println(e.getMessage());
|
197 |
} |
198 |
|
199 |
} |
200 |
return fullExtent;
|
201 |
} |
202 |
/* (non-Javadoc)
|
203 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
|
204 |
*/
|
205 |
public IFeatureIterator getFeatureIterator(String sql) throws com.iver.cit.gvsig.fmap.DriverException { |
206 |
PostGisFeatureIterator geomIterator = null;
|
207 |
try {
|
208 |
st = conn.createStatement(); |
209 |
st.setFetchSize(2000);
|
210 |
if (bCursorActivo)
|
211 |
{ |
212 |
st.execute("CLOSE wkb_cursor_prov");
|
213 |
bCursorActivo = false;
|
214 |
} |
215 |
|
216 |
st.execute("declare wkb_cursor_prov binary cursor for " + sql);
|
217 |
ResultSet rs = st.executeQuery("fetch forward all in wkb_cursor_prov"); |
218 |
// st.execute("BEGIN");
|
219 |
bCursorActivo = true;
|
220 |
// ResultSet rs = st.executeQuery(sql);
|
221 |
geomIterator = new PostGisFeatureIterator(rs);
|
222 |
} catch (SQLException e) { |
223 |
e.printStackTrace(); |
224 |
throw new com.iver.cit.gvsig.fmap.DriverException(e); |
225 |
} |
226 |
|
227 |
return geomIterator;
|
228 |
} |
229 |
|
230 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException { |
231 |
double xMin = r.getMinX();
|
232 |
double yMin = r.getMinY();
|
233 |
double xMax = r.getMaxX();
|
234 |
double yMax = r.getMaxY();
|
235 |
|
236 |
String wktBox = "GeometryFromText('LINESTRING(" + xMin + " " + yMin + ", " |
237 |
+ xMax + " " + yMin + ", " |
238 |
+ xMax + " " + yMax + ", " |
239 |
+ xMin + " " + yMax + ")', " |
240 |
+ strEPSG + ")";
|
241 |
String sqlAux;
|
242 |
if (getWhereClause().startsWith("WHERE")) |
243 |
sqlAux = sqlOrig + " the_geom && " + wktBox;
|
244 |
else
|
245 |
sqlAux = sqlOrig + "WHERE the_geom && " + wktBox;
|
246 |
|
247 |
|
248 |
return getFeatureIterator(sqlAux);
|
249 |
} |
250 |
/**
|
251 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getConnectionStringBeginning()
|
252 |
*/
|
253 |
public String getConnectionStringBeginning() { |
254 |
return "jdbc:postgresql:"; |
255 |
} |
256 |
/* (non-Javadoc)
|
257 |
* @see com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver#getFieldValue(long, int)
|
258 |
*/
|
259 |
public Value getFieldValue(long rowIndex, int idField) throws com.hardcode.gdbms.engine.data.driver.DriverException { |
260 |
boolean resul;
|
261 |
// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
|
262 |
// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
|
263 |
// LO HAGA
|
264 |
// System.out.println("getShape " + index);
|
265 |
int index = (int) (rowIndex); |
266 |
try
|
267 |
{ |
268 |
setAbsolutePosition(index); |
269 |
int fieldId = idField+2; |
270 |
byte[] byteBuf = rs.getBytes(fieldId); |
271 |
if (byteBuf == null) |
272 |
return ValueFactory.createNullValue();
|
273 |
else
|
274 |
{ |
275 |
ByteBuffer buf = ByteBuffer.wrap(byteBuf); |
276 |
if (metaData.getColumnType(fieldId) == Types.VARCHAR) |
277 |
return ValueFactory.createValue(rs.getString(fieldId));
|
278 |
if (metaData.getColumnType(fieldId) == Types.FLOAT) |
279 |
return ValueFactory.createValue(buf.getFloat());
|
280 |
if (metaData.getColumnType(fieldId) == Types.DOUBLE) |
281 |
return ValueFactory.createValue(buf.getDouble());
|
282 |
if (metaData.getColumnType(fieldId) == Types.INTEGER) |
283 |
return ValueFactory.createValue(buf.getInt());
|
284 |
if (metaData.getColumnType(fieldId) == Types.BIGINT) |
285 |
return ValueFactory.createValue(buf.getLong());
|
286 |
if (metaData.getColumnType(fieldId) == Types.BIT) |
287 |
// TODO
|
288 |
return ValueFactory.createValue(rs.getBoolean(fieldId));
|
289 |
if (metaData.getColumnType(fieldId) == Types.DATE) |
290 |
// TODO
|
291 |
return ValueFactory.createValue(rs.getDate(fieldId));
|
292 |
} |
293 |
} |
294 |
catch(SQLException e) |
295 |
{ |
296 |
throw new com.hardcode.gdbms.engine.data.driver.DriverException(e.getMessage()); |
297 |
} |
298 |
return ValueFactory.createNullValue();
|
299 |
|
300 |
} |
301 |
public void open() throws com.iver.cit.gvsig.fmap.DriverException { |
302 |
/* try {
|
303 |
st = conn.createStatement();
|
304 |
st.setFetchSize(2000);
|
305 |
if (bCursorActivo)
|
306 |
close();
|
307 |
st.execute("declare wkb_cursor binary cursor for " + sqlOrig);
|
308 |
rs = st.executeQuery("fetch forward all in wkb_cursor");
|
309 |
// st.execute("BEGIN");
|
310 |
bCursorActivo = true;
|
311 |
} catch (SQLException e) {
|
312 |
e.printStackTrace();
|
313 |
throw new com.iver.cit.gvsig.fmap.DriverException(e);
|
314 |
} */
|
315 |
|
316 |
} |
317 |
|
318 |
private void setAbsolutePosition(int index) throws SQLException |
319 |
{ |
320 |
// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
|
321 |
// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
|
322 |
// LO HAGA
|
323 |
// System.out.println("getShape " + index);
|
324 |
/* if (index < fetch_min)
|
325 |
{
|
326 |
// rs.close();
|
327 |
st.execute("CLOSE wkb_cursor");
|
328 |
st.execute("declare wkb_cursor binary cursor for " + sqlOrig);
|
329 |
rs = st.executeQuery("fetch forward all in wkb_cursor");
|
330 |
|
331 |
// rs.beforeFirst();
|
332 |
|
333 |
// rs = st.executeQuery(sqlOrig);
|
334 |
fetch_min = 0;
|
335 |
fetch_max = rs.getFetchSize();
|
336 |
}
|
337 |
while (index >= fetch_max)
|
338 |
{
|
339 |
rs.afterLast();
|
340 |
// forzamos una carga
|
341 |
rs.next();
|
342 |
fetch_min = fetch_max;
|
343 |
fetch_max = fetch_max + rs.getFetchSize();
|
344 |
// System.out.println("fetchSize = " + rs.getFetchSize() + " " + fetch_min + "-" + fetch_max);
|
345 |
}
|
346 |
rs.absolute(index+1 - fetch_min);
|
347 |
*/
|
348 |
rs.absolute(index+1);
|
349 |
|
350 |
} |
351 |
/**
|
352 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String)
|
353 |
*/
|
354 |
public String getGeometryField(String fieldName) { |
355 |
return "ASBINARY(" + fieldName + ", 'XDR')"; |
356 |
} |
357 |
/**
|
358 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
359 |
*/
|
360 |
public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException { |
361 |
// TODO Auto-generated method stub
|
362 |
return null; |
363 |
} |
364 |
|
365 |
|
366 |
} |