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