svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgresql / PostgreSQLHelper.java @ 28948
History | View | Annotate | Download (21.7 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
/**
|
29 |
*
|
30 |
*/
|
31 |
package org.gvsig.fmap.dal.store.postgresql; |
32 |
|
33 |
import java.sql.Connection; |
34 |
import java.sql.PreparedStatement; |
35 |
import java.sql.ResultSet; |
36 |
import java.sql.ResultSetMetaData; |
37 |
import java.sql.SQLException; |
38 |
import java.sql.Statement; |
39 |
import java.util.ArrayList; |
40 |
import java.util.Comparator; |
41 |
import java.util.Iterator; |
42 |
import java.util.List; |
43 |
import java.util.Map; |
44 |
import java.util.TreeMap; |
45 |
import java.util.TreeSet; |
46 |
|
47 |
import org.cresques.cts.IProjection; |
48 |
import org.gvsig.fmap.crs.CRSFactory; |
49 |
import org.gvsig.fmap.dal.DALLocator; |
50 |
import org.gvsig.fmap.dal.DataTypes; |
51 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
52 |
import org.gvsig.fmap.dal.exception.DataException; |
53 |
import org.gvsig.fmap.dal.exception.InitializeException; |
54 |
import org.gvsig.fmap.dal.exception.ReadException; |
55 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
56 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
57 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
58 |
import org.gvsig.fmap.dal.feature.FeatureType; |
59 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException; |
60 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedGeometryException; |
61 |
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices; |
62 |
import org.gvsig.fmap.dal.store.jdbc.ConnectionAction; |
63 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
64 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelperUser; |
65 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
66 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException; |
67 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecutePreparedSQLException; |
68 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
69 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
70 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
71 |
import org.gvsig.fmap.geom.Geometry; |
72 |
import org.gvsig.fmap.geom.GeometryLocator; |
73 |
import org.gvsig.fmap.geom.GeometryManager; |
74 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB; |
75 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKBGeometryOperationContext; |
76 |
import org.gvsig.fmap.geom.primitive.Envelope; |
77 |
import org.gvsig.tools.exception.BaseException; |
78 |
import org.postgresql.PGResultSetMetaData; |
79 |
import org.slf4j.Logger; |
80 |
import org.slf4j.LoggerFactory; |
81 |
|
82 |
/**
|
83 |
* @author jmvivo
|
84 |
*
|
85 |
*/
|
86 |
public class PostgreSQLHelper extends JDBCHelper { |
87 |
|
88 |
private static Logger logger = LoggerFactory |
89 |
.getLogger(PostgreSQLHelper.class); |
90 |
|
91 |
private Map pgSR2SRSID = new TreeMap(); |
92 |
private Map srsID2pgSR = new TreeMap(); |
93 |
|
94 |
|
95 |
PostgreSQLHelper(JDBCHelperUser consumer, |
96 |
PostgreSQLConnectionParameters params) |
97 |
throws InitializeException {
|
98 |
|
99 |
super(consumer, params);
|
100 |
} |
101 |
|
102 |
protected void initializeResource() throws InitializeException { |
103 |
ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator |
104 |
.getResourceManager(); |
105 |
PostgreSQLResource resource = (PostgreSQLResource) manager |
106 |
.createResource( |
107 |
PostgreSQLResource.NAME, new Object[] { |
108 |
params.getUrl(), params.getHost(), |
109 |
params.getPort(), params.getDBName(), params.getUser(), |
110 |
params.getPassword(), |
111 |
params.getJDBCDriverClassName(), |
112 |
((PostgreSQLConnectionParameters) params).getUseSSL() }); |
113 |
this.setResource(resource);
|
114 |
} |
115 |
|
116 |
|
117 |
protected String getDefaultSchema(Connection conn) |
118 |
throws JDBCException {
|
119 |
if (defaultSchema == null) { |
120 |
String sql = "Select current_schema()"; |
121 |
ResultSet rs = null; |
122 |
Statement st = null; |
123 |
String schema = null; |
124 |
try {
|
125 |
st = conn.createStatement(); |
126 |
try {
|
127 |
rs = st.executeQuery(sql); |
128 |
} catch (java.sql.SQLException e) {
|
129 |
throw new JDBCExecuteSQLException(sql, e); |
130 |
} |
131 |
rs.next(); |
132 |
schema = rs.getString(1);
|
133 |
} catch (java.sql.SQLException e) {
|
134 |
throw new JDBCSQLException(e); |
135 |
} finally {
|
136 |
try {rs.close();} catch (Exception e) {logger.error("Exception clossing resulset", e);}; |
137 |
try {st.close();} catch (Exception e) {logger.error("Exception clossing statement", e);}; |
138 |
rs = null;
|
139 |
st = null;
|
140 |
} |
141 |
defaultSchema = schema; |
142 |
} |
143 |
|
144 |
return defaultSchema;
|
145 |
} |
146 |
|
147 |
public Envelope getFullEnvelopeOfField(
|
148 |
JDBCStoreParameters storeParams, |
149 |
String geometryAttrName, Envelope limit)
|
150 |
throws DataException {
|
151 |
|
152 |
StringBuilder strb = new StringBuilder(); |
153 |
strb.append("Select asbinary(extent(");
|
154 |
strb.append(geometryAttrName); |
155 |
strb.append(")) from ");
|
156 |
|
157 |
if (storeParams.getSQL() != null |
158 |
&& storeParams.getSQL().trim().length() == 0) {
|
159 |
strb.append('(');
|
160 |
strb.append(storeParams.getSQL()); |
161 |
strb.append(") as __tmp__ ");
|
162 |
} else {
|
163 |
strb.append(storeParams.tableID()); |
164 |
} |
165 |
|
166 |
|
167 |
if (limit != null){ |
168 |
strb.append(" where intersects(GeomFromText('");
|
169 |
strb.append(limit.toString()); |
170 |
strb.append("')), boundary(");
|
171 |
strb.append(geometryAttrName); |
172 |
strb.append(")) ");
|
173 |
} |
174 |
|
175 |
String sql = strb.toString();
|
176 |
|
177 |
|
178 |
ResultSet rs = null; |
179 |
Statement st = null; |
180 |
String schema = null; |
181 |
Connection conn = null; |
182 |
|
183 |
GeometryManager geoMan = GeometryLocator.getGeometryManager(); |
184 |
|
185 |
Envelope fullEnvelope = null;
|
186 |
this.open();
|
187 |
this.begin();
|
188 |
try{
|
189 |
conn = getConnection(); |
190 |
st = conn.createStatement(); |
191 |
try {
|
192 |
rs = st.executeQuery(sql); |
193 |
} catch (java.sql.SQLException e) {
|
194 |
throw new JDBCExecuteSQLException(sql, e); |
195 |
} |
196 |
if (!rs.next()) {
|
197 |
return null; |
198 |
} |
199 |
|
200 |
byte[] data = rs.getBytes(1); |
201 |
if (data == null) { |
202 |
return null; |
203 |
} |
204 |
initializeFromWKBOperation(); |
205 |
fromWKBContext.setData(data); |
206 |
Geometry geom = (Geometry) fromWKB.invoke(null, fromWKBContext);
|
207 |
|
208 |
fullEnvelope = geom.getEnvelope(); |
209 |
|
210 |
return fullEnvelope;
|
211 |
} catch (java.sql.SQLException e) {
|
212 |
throw new JDBCSQLException(e); |
213 |
} catch (BaseException e) {
|
214 |
throw new ReadException(user.getName(), e); |
215 |
} finally {
|
216 |
try{ rs.close(); } catch (Exception e){}; |
217 |
try{ st.close(); } catch (Exception e){}; |
218 |
try{ conn.close(); } catch (Exception e){}; |
219 |
rs = null;
|
220 |
st = null;
|
221 |
conn = null;
|
222 |
end(); |
223 |
} |
224 |
|
225 |
|
226 |
} |
227 |
|
228 |
protected void initializeFromWKBOperation() throws BaseException { |
229 |
if (fromWKB == null) { |
230 |
fromWKB = (FromWKB) GeometryLocator.getGeometryManager() |
231 |
.getGeometryOperation(FromWKB.CODE, |
232 |
Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2D); |
233 |
fromWKBContext = new FromWKBGeometryOperationContext();
|
234 |
|
235 |
} |
236 |
} |
237 |
|
238 |
public Geometry getGeometry(byte[] buffer) throws BaseException { |
239 |
if (buffer == null) { |
240 |
return null; |
241 |
} |
242 |
initializeFromWKBOperation(); |
243 |
Geometry geom; |
244 |
try {
|
245 |
fromWKBContext.setData(buffer); |
246 |
|
247 |
geom = (Geometry) fromWKB.invoke(null, fromWKBContext);
|
248 |
} finally {
|
249 |
fromWKBContext.setData(null);
|
250 |
} |
251 |
return geom;
|
252 |
} |
253 |
|
254 |
/**
|
255 |
* Fill <code>featureType</code> geometry attributes with SRS and ShapeType
|
256 |
* information stored in the table GEOMETRY_COLUMNS
|
257 |
*
|
258 |
* @param conn
|
259 |
* @param rsMetadata
|
260 |
* @param featureType
|
261 |
* @throws ReadException
|
262 |
*/
|
263 |
protected void loadSRS_and_shapeType(Connection conn, |
264 |
ResultSetMetaData rsMetadata, EditableFeatureType featureType,
|
265 |
String baseSchema, String baseTable) |
266 |
throws JDBCException {
|
267 |
|
268 |
Statement st = null; |
269 |
ResultSet rs = null; |
270 |
try {
|
271 |
// Sacamos la lista de los attributos geometricos
|
272 |
EditableFeatureAttributeDescriptor attr; |
273 |
List geoAttrs = new ArrayList(); |
274 |
|
275 |
Iterator iter = featureType.iterator();
|
276 |
while (iter.hasNext()) {
|
277 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
278 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
279 |
geoAttrs.add(attr); |
280 |
} |
281 |
} |
282 |
if (geoAttrs.size() < 1) { |
283 |
return;
|
284 |
} |
285 |
|
286 |
|
287 |
// preparamos un set con las lista de tablas de origen
|
288 |
// de los campos
|
289 |
class TableId { |
290 |
public String schema=null; |
291 |
public String table=null; |
292 |
public String field = null; |
293 |
|
294 |
public void appendToSQL(StringBuilder strb) { |
295 |
if (schema == null || schema.length() == 0) { |
296 |
strb |
297 |
.append("( F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '");
|
298 |
} else {
|
299 |
strb.append("( F_TABLE_SCHEMA = '");
|
300 |
strb.append(schema); |
301 |
strb.append("' AND F_TABLE_NAME = '");
|
302 |
} |
303 |
strb.append(table); |
304 |
strb.append("' AND F_GEOMETRY_COLUMN = '");
|
305 |
strb.append(field); |
306 |
strb.append("' )");
|
307 |
} |
308 |
|
309 |
} |
310 |
Comparator cmp = new Comparator(){ |
311 |
public int compare(Object arg0, Object arg1) { |
312 |
TableId a0 = (TableId) arg0; |
313 |
TableId a1 = (TableId) arg1; |
314 |
|
315 |
if (!a0.field.equals(a1.field)) {
|
316 |
return -1; |
317 |
} |
318 |
if (!a0.table.equals(a1.table)) {
|
319 |
return -1; |
320 |
} |
321 |
if (!a0.schema.equals(a1.schema)) {
|
322 |
return -1; |
323 |
} |
324 |
return 0; |
325 |
} |
326 |
}; |
327 |
TreeSet set = new TreeSet(cmp); |
328 |
TableId tableId; |
329 |
iter = geoAttrs.iterator(); |
330 |
int rsIndex;
|
331 |
while (iter.hasNext()) {
|
332 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
333 |
tableId = new TableId();
|
334 |
rsIndex = attr.getIndex() + 1;
|
335 |
|
336 |
if (baseSchema == null && baseTable == null) { |
337 |
if (rsMetadata instanceof PGResultSetMetaData) { |
338 |
tableId.schema = ((PGResultSetMetaData) rsMetadata) |
339 |
.getBaseSchemaName(rsIndex); |
340 |
tableId.table = ((PGResultSetMetaData) rsMetadata) |
341 |
.getBaseTableName(rsIndex); |
342 |
tableId.field = ((PGResultSetMetaData) rsMetadata) |
343 |
.getBaseColumnName(rsIndex); |
344 |
|
345 |
} else {
|
346 |
tableId.schema = rsMetadata.getSchemaName(rsIndex); |
347 |
tableId.table = rsMetadata.getTableName(rsIndex); |
348 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
349 |
} |
350 |
} else {
|
351 |
tableId.schema = baseSchema; |
352 |
tableId.table = baseTable; |
353 |
tableId.field = rsMetadata.getColumnName(rsIndex); |
354 |
} |
355 |
if (tableId.table == null || tableId.table.length() == 0) { |
356 |
// Si no tiene tabla origen (viene de algun calculo por ej.)
|
357 |
// lo saltamos ya que no estara en la tabla GEOMETRY_COLUMNS
|
358 |
continue;
|
359 |
} |
360 |
set.add(tableId); |
361 |
} |
362 |
|
363 |
if (set.size() == 0) { |
364 |
return;
|
365 |
} |
366 |
|
367 |
// Preparamos una sql para que nos saque el resultado
|
368 |
StringBuilder strb = new StringBuilder(); |
369 |
strb.append("Select geometry_columns.*,auth_name || ':' || auth_srid as SRSID ");
|
370 |
strb.append("from geometry_columns left join spatial_ref_sys on ");
|
371 |
strb.append("geometry_columns.srid = spatial_ref_sys.srid WHERE ");
|
372 |
iter = set.iterator(); |
373 |
for (int i=0;i<set.size()-1;i++) { |
374 |
tableId = (TableId) iter.next(); |
375 |
tableId.appendToSQL(strb); |
376 |
strb.append(" OR ");
|
377 |
} |
378 |
tableId = (TableId) iter.next(); |
379 |
tableId.appendToSQL(strb); |
380 |
String sql = strb.toString();
|
381 |
|
382 |
|
383 |
st = conn.createStatement(); |
384 |
try {
|
385 |
rs = st.executeQuery(sql); |
386 |
} catch (SQLException e) { |
387 |
throw new JDBCExecuteSQLException(sql, e); |
388 |
} |
389 |
String srsID;
|
390 |
int pgSrid;
|
391 |
int geometryType;
|
392 |
int geometrySubtype;
|
393 |
String geomTypeStr;
|
394 |
int dimensions;
|
395 |
IProjection srs; |
396 |
|
397 |
while (rs.next()){
|
398 |
srsID = rs.getString("SRSID");
|
399 |
pgSrid = rs.getInt("SRID");
|
400 |
geomTypeStr = rs.getString("TYPE").toUpperCase();
|
401 |
geometryType = Geometry.TYPES.GEOMETRY; |
402 |
if (geomTypeStr.startsWith("POINT")) { |
403 |
geometryType = Geometry.TYPES.POINT; |
404 |
} else if (geomTypeStr.startsWith("LINESTRING")) { |
405 |
geometryType = Geometry.TYPES.CURVE; |
406 |
} else if (geomTypeStr.startsWith("POLYGON")) { |
407 |
geometryType = Geometry.TYPES.SURFACE; |
408 |
} else if (geomTypeStr.startsWith("MULTIPOINT")) { |
409 |
geometryType = Geometry.TYPES.MULTIPOINT; |
410 |
} else if (geomTypeStr.startsWith("MULTILINESTRING")) { |
411 |
geometryType = Geometry.TYPES.MULTICURVE; |
412 |
} else if (geomTypeStr.startsWith("MULTIPOLYGON")) { |
413 |
geometryType = Geometry.TYPES.MULTISURFACE; |
414 |
} |
415 |
dimensions = rs.getInt("coord_dimension");
|
416 |
geometrySubtype = Geometry.SUBTYPES.GEOM2D; |
417 |
if (dimensions > 2) { |
418 |
if (dimensions == 3) { |
419 |
if (geomTypeStr.endsWith("M")) { |
420 |
geometrySubtype = Geometry.SUBTYPES.GEOM2DM; |
421 |
} else {
|
422 |
geometrySubtype = Geometry.SUBTYPES.GEOM3D; |
423 |
} |
424 |
|
425 |
} else {
|
426 |
geometrySubtype = Geometry.SUBTYPES.GEOM3DM; |
427 |
} |
428 |
} |
429 |
addToPgSRToSRSID(pgSrid, srsID); |
430 |
|
431 |
|
432 |
iter = geoAttrs.iterator(); |
433 |
while (iter.hasNext()) {
|
434 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
435 |
rsIndex = attr.getIndex() + 1;
|
436 |
if (!rsMetadata.getColumnName(rsIndex).equals(
|
437 |
rs.getString("f_geometry_column"))) {
|
438 |
continue;
|
439 |
} |
440 |
|
441 |
if (baseSchema == null && baseTable == null) { |
442 |
|
443 |
if (rsMetadata instanceof PGResultSetMetaData) { |
444 |
if (!((PGResultSetMetaData) rsMetadata)
|
445 |
.getBaseTableName(rsIndex).equals( |
446 |
rs.getString("f_table_name"))) {
|
447 |
continue;
|
448 |
} |
449 |
String curSchema = rs.getString("f_table_schema"); |
450 |
String metaSchema = ((PGResultSetMetaData) rsMetadata)
|
451 |
.getBaseSchemaName(rsIndex); |
452 |
if (!metaSchema.equals(curSchema)) {
|
453 |
if (metaSchema.length() == 0 |
454 |
&& metaSchema == getDefaultSchema(conn)) { |
455 |
} else {
|
456 |
continue;
|
457 |
} |
458 |
} |
459 |
|
460 |
} else {
|
461 |
|
462 |
if (!rsMetadata.getTableName(rsIndex).equals(
|
463 |
rs.getString("f_table_name"))) {
|
464 |
continue;
|
465 |
} |
466 |
String curSchema = rs.getString("f_table_schema"); |
467 |
String metaSchema = rsMetadata
|
468 |
.getSchemaName(rsIndex); |
469 |
if (!metaSchema.equals(curSchema)) {
|
470 |
if (metaSchema.length() == 0 |
471 |
&& metaSchema == getDefaultSchema(conn)) { |
472 |
} else {
|
473 |
continue;
|
474 |
} |
475 |
} |
476 |
} |
477 |
} |
478 |
attr.setGeometryType(geometryType); |
479 |
attr.setGeometrySubType(geometrySubtype); |
480 |
if (srsID != null && srsID.length() > 0) { |
481 |
attr.setSRS(CRSFactory.getCRS(srsID)); |
482 |
} |
483 |
iter.remove(); |
484 |
} |
485 |
iter = geoAttrs.iterator(); |
486 |
while (iter.hasNext()) {
|
487 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
488 |
attr.setSRS(null);
|
489 |
attr.setGeometryType(Geometry.TYPES.GEOMETRY); |
490 |
|
491 |
} |
492 |
} |
493 |
|
494 |
} catch (java.sql.SQLException e) {
|
495 |
throw new JDBCSQLException(e); |
496 |
} finally {
|
497 |
try {rs.close();} catch (Exception e) { }; |
498 |
try {st.close();} catch (Exception e) { }; |
499 |
} |
500 |
|
501 |
} |
502 |
|
503 |
|
504 |
public String getSqlColumnTypeDescription(FeatureAttributeDescriptor attr) { |
505 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
506 |
return "geometry"; |
507 |
} |
508 |
return super.getSqlColumnTypeDescription(attr); |
509 |
} |
510 |
|
511 |
|
512 |
public int getPostgisGeomDimensions(int geometrySubType) { |
513 |
switch (geometrySubType) {
|
514 |
case Geometry.SUBTYPES.GEOM2D:
|
515 |
return 2; |
516 |
case Geometry.SUBTYPES.GEOM2DM:
|
517 |
case Geometry.SUBTYPES.GEOM3D:
|
518 |
return 3; |
519 |
|
520 |
case Geometry.SUBTYPES.GEOM3DM:
|
521 |
return 4; |
522 |
default:
|
523 |
throw new UnsupportedDataTypeException( |
524 |
DataTypes.TYPE_NAMES[DataTypes.GEOMETRY], |
525 |
DataTypes.GEOMETRY); |
526 |
} |
527 |
} |
528 |
|
529 |
public String getPostgisGeomType(int geometryType, int geometrySubType) { |
530 |
String pgGeomType;
|
531 |
switch (geometryType) {
|
532 |
case Geometry.TYPES.GEOMETRY:
|
533 |
pgGeomType = "GEOMETRY";
|
534 |
break;
|
535 |
case Geometry.TYPES.POINT:
|
536 |
pgGeomType = "POINT";
|
537 |
break;
|
538 |
case Geometry.TYPES.CURVE:
|
539 |
pgGeomType = "LINESTRING";
|
540 |
break;
|
541 |
case Geometry.TYPES.SURFACE:
|
542 |
pgGeomType = "POLYGON";
|
543 |
break;
|
544 |
case Geometry.TYPES.MULTIPOINT:
|
545 |
pgGeomType = "MULTIPOINT";
|
546 |
break;
|
547 |
case Geometry.TYPES.MULTICURVE:
|
548 |
pgGeomType = "MULTILINESTRING";
|
549 |
break;
|
550 |
case Geometry.TYPES.MULTISURFACE:
|
551 |
pgGeomType = "MULTIPOLYGON";
|
552 |
break;
|
553 |
default:
|
554 |
throw new UnsupportedGeometryException(geometryType, |
555 |
geometrySubType); |
556 |
} |
557 |
if (geometrySubType == Geometry.SUBTYPES.GEOM2DM
|
558 |
|| geometrySubType == Geometry.SUBTYPES.GEOM3DM) { |
559 |
pgGeomType = pgGeomType + "M";
|
560 |
} else if (geometrySubType == Geometry.SUBTYPES.GEOM2DZ) { |
561 |
throw new UnsupportedGeometryException(geometryType, |
562 |
geometrySubType); |
563 |
} |
564 |
return pgGeomType;
|
565 |
} |
566 |
|
567 |
public int getProviderSRID(String srs) { |
568 |
if (srs != null) { |
569 |
Integer pgSRID = (Integer) srsID2pgSR.get(srs); |
570 |
if (pgSRID != null) { |
571 |
return pgSRID.intValue();
|
572 |
} |
573 |
|
574 |
return searchpgSRID(srs);
|
575 |
|
576 |
} |
577 |
return -1; |
578 |
} |
579 |
|
580 |
|
581 |
public int getProviderSRID(IProjection srs) { |
582 |
if (srs != null) { |
583 |
Integer pgSRID = (Integer) srsID2pgSR.get(srs.getAbrev()); |
584 |
if (pgSRID != null) { |
585 |
return pgSRID.intValue();
|
586 |
} |
587 |
|
588 |
return searchpgSRID(srs);
|
589 |
|
590 |
} |
591 |
return -1; |
592 |
} |
593 |
|
594 |
private int searchpgSRID(final IProjection srs) { |
595 |
if (srs == null) { |
596 |
return -1; |
597 |
} |
598 |
return searchpgSRID(srs.getAbrev());
|
599 |
} |
600 |
|
601 |
private int searchpgSRID(final String srsID) { |
602 |
if (srsID == null) { |
603 |
return -1; |
604 |
} |
605 |
|
606 |
ConnectionAction action = new ConnectionAction(){
|
607 |
|
608 |
public Object action(Connection conn) throws DataException { |
609 |
// select srid from spatial_ref_sys where auth_name = 'EPSG' and
|
610 |
// auth_srid = 23030
|
611 |
String[] abrev = srsID.split(":"); |
612 |
StringBuilder sqlb = new StringBuilder(); |
613 |
sqlb.append("select srid from spatial_ref_sys where ");
|
614 |
if (abrev.length > 1) { |
615 |
sqlb.append("auth_name = ? and ");
|
616 |
} |
617 |
sqlb.append("auth_srid = ?");
|
618 |
|
619 |
String sql = sqlb.toString();
|
620 |
PreparedStatement st;
|
621 |
try {
|
622 |
st = conn.prepareStatement(sql); |
623 |
} catch (SQLException e){ |
624 |
throw new JDBCPreparingSQLException(sql,e); |
625 |
} |
626 |
ResultSet rs = null; |
627 |
try{
|
628 |
int i=0; |
629 |
if (abrev.length > 1){ |
630 |
st.setString(i+1, abrev[i]);
|
631 |
i++; |
632 |
} |
633 |
st.setInt(i + 1, Integer.parseInt(abrev[i])); |
634 |
|
635 |
try{
|
636 |
rs = st.executeQuery(); |
637 |
} catch (SQLException e){ |
638 |
throw new JDBCExecutePreparedSQLException(sql, abrev, e); |
639 |
} |
640 |
|
641 |
if (!rs.next()) {
|
642 |
return null; |
643 |
} |
644 |
|
645 |
return new Integer(rs.getInt(1)); |
646 |
|
647 |
} catch (SQLException e){ |
648 |
throw new JDBCSQLException(e); |
649 |
} finally{
|
650 |
try {rs.close(); } catch (Exception e) {}; |
651 |
try {st.close(); } catch (Exception e) {}; |
652 |
} |
653 |
|
654 |
} |
655 |
|
656 |
}; |
657 |
|
658 |
Integer pgSRSID = null; |
659 |
try {
|
660 |
pgSRSID = (Integer) doConnectionAction(action);
|
661 |
} catch (Exception e) { |
662 |
logger.error("Excetion searching pgSRS", e);
|
663 |
return -1; |
664 |
} |
665 |
|
666 |
if (pgSRSID != null) { |
667 |
addToPgSRToSRSID(pgSRSID.intValue(), srsID); |
668 |
return pgSRSID.intValue();
|
669 |
} |
670 |
return -1; |
671 |
|
672 |
} |
673 |
|
674 |
private void addToPgSRToSRSID(int pgSRID, String srsId) { |
675 |
if (pgSRID < 0 || srsId == null || srsId.length() == 0) { |
676 |
return;
|
677 |
} |
678 |
Integer pgSRIDInteger = new Integer(pgSRID); |
679 |
pgSR2SRSID.put(pgSRIDInteger, srsId); |
680 |
srsID2pgSR.put(srsId, pgSRIDInteger); |
681 |
} |
682 |
|
683 |
public List getSqlGeometyFieldAdd(FeatureAttributeDescriptor attr, |
684 |
String table, String schema) { |
685 |
// SELECT AddGeometryColumn({schema}, {table}, {field}, {srid}(int),
|
686 |
// {geomType}(Str), {dimensions}(int))
|
687 |
|
688 |
// gemoType:
|
689 |
/*
|
690 |
* POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING,
|
691 |
* MULTIPOLYGON, GEOMETRYCOLLECTION POINTM, LINESTRINGM, POLYGONM,
|
692 |
* MULTIPOINTM, MULTILINESTRINGM, MULTIPOLYGONM, GEOMETRYCOLLECTIONM
|
693 |
*/
|
694 |
|
695 |
List sqls = new ArrayList(); |
696 |
|
697 |
StringBuilder strb = new StringBuilder(); |
698 |
strb.append("SELECT AddGeometryColumn('");
|
699 |
if (schema != null && schema.length() > 0) { |
700 |
strb.append(schema); |
701 |
strb.append("', '");
|
702 |
} |
703 |
strb.append(table); |
704 |
strb.append("', '");
|
705 |
strb.append(attr.getName()); |
706 |
strb.append("', ");
|
707 |
// strb.append("-1");
|
708 |
strb.append(getProviderSRID(attr.getSRS())); |
709 |
strb.append(", '");
|
710 |
strb.append(getPostgisGeomType(attr.getGeometryType(), attr |
711 |
.getGeometrySubType())); |
712 |
strb.append("', ");
|
713 |
strb.append(getPostgisGeomDimensions(attr.getGeometrySubType())); |
714 |
strb.append(")");
|
715 |
|
716 |
|
717 |
sqls.add(strb.toString()); |
718 |
|
719 |
/*ALTER TABLE muni10000_peq_test DROP CONSTRAINT enforce_srid_the_geom;*/
|
720 |
/*
|
721 |
strb = new StringBuilder();
|
722 |
strb.append("Alter table ");
|
723 |
if (schema != null && schema.length() > 0) {
|
724 |
strb.append(schema);
|
725 |
strb.append(".");
|
726 |
}
|
727 |
strb.append("f_table_name = '");
|
728 |
strb.append(table);
|
729 |
strb.append("' AND f_geometry_column = '");
|
730 |
strb.append(attr.getName());
|
731 |
strb.append("' AND srid = -1");
|
732 |
|
733 |
|
734 |
sqls.add(strb.toString());
|
735 |
*/
|
736 |
return sqls;
|
737 |
} |
738 |
|
739 |
public String getSqlFieldName(FeatureAttributeDescriptor attribute) { |
740 |
if (attribute.getDataType() == DataTypes.GEOMETRY) {
|
741 |
return "asBinary(" + super.getSqlFieldName(attribute) + ")"; |
742 |
} |
743 |
return super.getSqlFieldName(attribute); |
744 |
} |
745 |
|
746 |
protected EditableFeatureAttributeDescriptor createAttributeFromJDBC(
|
747 |
EditableFeatureType type, Connection conn,
|
748 |
ResultSetMetaData rsMetadata, int colIndex) throws SQLException { |
749 |
if (rsMetadata.getColumnType(colIndex) == java.sql.Types.OTHER) {
|
750 |
if (rsMetadata.getColumnTypeName(colIndex).equalsIgnoreCase(
|
751 |
"geometry")) {
|
752 |
return type.add(rsMetadata.getColumnName(colIndex),
|
753 |
DataTypes.GEOMETRY); |
754 |
|
755 |
} |
756 |
} |
757 |
|
758 |
return super.createAttributeFromJDBC(type, conn, rsMetadata, colIndex); |
759 |
} |
760 |
|
761 |
public String escapeFieldName(String field) { |
762 |
if (field.matches("[a-z][a-z0-9_]*")) { |
763 |
return field;
|
764 |
} |
765 |
return "\"" + field + "\""; |
766 |
} |
767 |
|
768 |
public List getAdditionalSqlToCreate(NewDataStoreParameters ndsp, |
769 |
FeatureType fType) { |
770 |
FeatureAttributeDescriptor attr; |
771 |
Iterator iter = fType.iterator();
|
772 |
List result = new ArrayList(); |
773 |
PostgreSQLNewStoreParameters pgNdsp = (PostgreSQLNewStoreParameters) ndsp; |
774 |
while (iter.hasNext()){
|
775 |
attr = (FeatureAttributeDescriptor) iter.next(); |
776 |
if (attr.getDataType() == DataTypes.GEOMETRY){
|
777 |
result.addAll(getSqlGeometyFieldAdd(attr, pgNdsp.getTable(), |
778 |
pgNdsp |
779 |
.getSchema())); |
780 |
} |
781 |
} |
782 |
|
783 |
return result;
|
784 |
} |
785 |
|
786 |
public String getSqlFieldDescription(FeatureAttributeDescriptor attr) |
787 |
throws DataException {
|
788 |
if (attr.getDataType() == DataTypes.GEOMETRY){
|
789 |
return null; |
790 |
} |
791 |
return super.getSqlFieldDescription(attr); |
792 |
} |
793 |
|
794 |
|
795 |
public boolean allowAutomaticValues() { |
796 |
return Boolean.TRUE; |
797 |
} |
798 |
|
799 |
} |