root / branches / v2_0_0_prep / libraries / org.gvsig.fmap.dal.db.h2spatial / src / main / java / org / gvsig / fmap / dal / db / h2spatial / impl / H2SpatialHelper.java @ 32679
History | View | Annotate | Download (10.9 KB)
1 |
package org.gvsig.fmap.dal.db.h2spatial.impl; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.PreparedStatement; |
5 |
import java.sql.ResultSet; |
6 |
import java.sql.ResultSetMetaData; |
7 |
import java.sql.SQLException; |
8 |
import java.sql.Statement; |
9 |
import java.util.ArrayList; |
10 |
import java.util.Comparator; |
11 |
import java.util.Iterator; |
12 |
import java.util.List; |
13 |
import java.util.Map; |
14 |
import java.util.TreeMap; |
15 |
import java.util.TreeSet; |
16 |
|
17 |
import net.sourceforge.hatbox.MetaNode; |
18 |
import net.sourceforge.hatbox.RTreeDml; |
19 |
import net.sourceforge.hatbox.RTreeSessionDb; |
20 |
|
21 |
import org.cresques.cts.ICoordTrans; |
22 |
import org.cresques.cts.IProjection; |
23 |
import org.gvsig.fmap.crs.CRSFactory; |
24 |
import org.gvsig.fmap.dal.DALLocator; |
25 |
import org.gvsig.fmap.dal.DataTypes; |
26 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
27 |
import org.gvsig.fmap.dal.exception.DataException; |
28 |
import org.gvsig.fmap.dal.exception.InitializeException; |
29 |
import org.gvsig.fmap.dal.exception.ReadException; |
30 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
31 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
32 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
33 |
import org.gvsig.fmap.dal.feature.FeatureType; |
34 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedDataTypeException; |
35 |
import org.gvsig.fmap.dal.feature.exception.UnsupportedGeometryException; |
36 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
37 |
import org.gvsig.fmap.dal.resource.spi.ResourceManagerProviderServices; |
38 |
import org.gvsig.fmap.dal.store.jdbc.ConnectionAction; |
39 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
40 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
41 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelperUser; |
42 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
43 |
import org.gvsig.fmap.dal.store.jdbc.TransactionalAction; |
44 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCException; |
45 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecutePreparedSQLException; |
46 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
47 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCPreparingSQLException; |
48 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.geom.GeometryLocator; |
51 |
import org.gvsig.fmap.geom.GeometryManager; |
52 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
53 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
54 |
import org.gvsig.fmap.geom.operation.fromwkb.FromWKB; |
55 |
import org.gvsig.fmap.geom.primitive.Envelope; |
56 |
import org.gvsig.fmap.geom.primitive.Point; |
57 |
import org.gvsig.tools.exception.BaseException; |
58 |
import org.postgresql.PGResultSetMetaData; |
59 |
import org.slf4j.Logger; |
60 |
import org.slf4j.LoggerFactory; |
61 |
|
62 |
public class H2SpatialHelper extends JDBCHelper { |
63 |
|
64 |
private static Logger logger = LoggerFactory |
65 |
.getLogger(H2SpatialHelper.class); |
66 |
private String rsid=null; |
67 |
private GeometryManager gm;
|
68 |
|
69 |
public H2SpatialHelper(JDBCHelperUser consumer,
|
70 |
JDBCConnectionParameters params) throws InitializeException {
|
71 |
super(consumer, params);
|
72 |
gm = GeometryLocator.getGeometryManager(); |
73 |
} |
74 |
|
75 |
@Override
|
76 |
protected void initializeResource() throws InitializeException { |
77 |
ResourceManagerProviderServices manager = (ResourceManagerProviderServices) DALLocator |
78 |
.getResourceManager(); |
79 |
H2SpatialResource resource = (H2SpatialResource) manager |
80 |
.createAddResource(H2SpatialResource.NAME, new Object[] { |
81 |
params.getUrl(), params.getDBName(), |
82 |
params.getJDBCDriverClassName(), |
83 |
((H2SpatialConnectionParameters) params).getUseSSL() }); |
84 |
this.setResource(resource);
|
85 |
} |
86 |
|
87 |
public void loadFeatureType(EditableFeatureType featureType, |
88 |
JDBCStoreParameters storeParams) throws DataException {
|
89 |
if (storeParams.getSQL() != null |
90 |
&& storeParams.getSQL().trim().length() == 0) {
|
91 |
loadFeatureType(featureType, storeParams, storeParams.getSQL(), |
92 |
null, null); |
93 |
} else {
|
94 |
String sql = "Select * from " + storeParams.tableID(); |
95 |
loadFeatureType(featureType, storeParams, sql, storeParams |
96 |
.getSchema(), storeParams.getTable()); |
97 |
} |
98 |
} |
99 |
|
100 |
public org.gvsig.fmap.geom.primitive.Envelope getFullEnvelopeOfField(JDBCStoreParameters storeParams,
|
101 |
String geometryAttrName, Envelope limit) throws DataException { |
102 |
|
103 |
Connection con = getConnection();
|
104 |
RTreeDml dml=null;
|
105 |
RTreeSessionDb session=null;
|
106 |
net.sourceforge.hatbox.Envelope hbEnvelope=null;
|
107 |
Envelope gvSigEnvelope = null;
|
108 |
try {
|
109 |
dml = RTreeDml.createDml(con, storeParams.getSchema(), storeParams.getTable().toUpperCase()); |
110 |
session = new RTreeSessionDb(con, dml, false); |
111 |
hbEnvelope = session.getNode(session.getRootId()).getBounds(); |
112 |
|
113 |
gvSigEnvelope = |
114 |
gm.createEnvelope(hbEnvelope.getMinX(), hbEnvelope.getMinY(), hbEnvelope.getMaxX(), hbEnvelope.getMaxY(), SUBTYPES.GEOM2D); |
115 |
} catch (SQLException e2) { |
116 |
e2.printStackTrace(); |
117 |
} catch (CreateEnvelopeException e) {
|
118 |
// TODO Auto-generated catch block
|
119 |
e.printStackTrace(); |
120 |
} |
121 |
|
122 |
session.closeAll(); |
123 |
try {
|
124 |
con.close(); |
125 |
} catch (SQLException e) { |
126 |
e.printStackTrace(); |
127 |
} |
128 |
return gvSigEnvelope;
|
129 |
} |
130 |
|
131 |
public String getSqlColumnTypeDescription(FeatureAttributeDescriptor attr) { |
132 |
|
133 |
switch (attr.getDataType()) {
|
134 |
|
135 |
case DataTypes.BOOLEAN:
|
136 |
return "BOOLEAN"; |
137 |
|
138 |
case DataTypes.BYTE:
|
139 |
return "SMALLINT"; |
140 |
|
141 |
case DataTypes.BYTEARRAY:
|
142 |
return "BYTEA"; |
143 |
|
144 |
case DataTypes.CHAR:
|
145 |
return "CHAR"; |
146 |
|
147 |
case DataTypes.DATE:
|
148 |
return "DATE"; |
149 |
|
150 |
case DataTypes.DOUBLE:
|
151 |
return "DOUBLE"; |
152 |
|
153 |
case DataTypes.FLOAT:
|
154 |
return "FLOAT"; |
155 |
|
156 |
case DataTypes.GEOMETRY:
|
157 |
return "BLOB"; |
158 |
|
159 |
case DataTypes.INT:
|
160 |
return "INTEGER"; |
161 |
|
162 |
case DataTypes.LONG:
|
163 |
return "BIGINT"; |
164 |
|
165 |
case DataTypes.STRING:
|
166 |
return "VARCHAR("+attr.getSize()+")"; |
167 |
|
168 |
case DataTypes.TIME:
|
169 |
return "TIME"; |
170 |
|
171 |
case DataTypes.TIMESTAMP:
|
172 |
return "TIMESTAMP"; |
173 |
|
174 |
default:
|
175 |
String typeName = (String) attr.getAdditionalInfo("SQLTypeName"); |
176 |
if (typeName != null) { |
177 |
return typeName;
|
178 |
} |
179 |
|
180 |
throw new UnsupportedDataTypeException(attr.getDataTypeName(), attr |
181 |
.getDataType()); |
182 |
} |
183 |
} |
184 |
|
185 |
public String getSqlFieldName(FeatureAttributeDescriptor attribute) { |
186 |
// if (attribute.getDataType() == DataTypes.GEOMETRY) {
|
187 |
// return "asBinary(" + super.getSqlFieldName(attribute) + ")";
|
188 |
// }
|
189 |
return super.getSqlFieldName(attribute); |
190 |
} |
191 |
|
192 |
protected EditableFeatureAttributeDescriptor createAttributeFromJDBC(
|
193 |
EditableFeatureType type, Connection conn,
|
194 |
ResultSetMetaData rsMetadata, int colIndex) throws SQLException { |
195 |
// if (rsMetadata.getColumnType(colIndex) == java.sql.Types.OTHER) {
|
196 |
if (rsMetadata.getColumnName(colIndex).equalsIgnoreCase("the_geom")) { |
197 |
EditableFeatureAttributeDescriptor efad = type.add(rsMetadata |
198 |
.getColumnName(colIndex), DataTypes.GEOMETRY); |
199 |
efad.setGeometrySubType(Geometry.SUBTYPES.GEOM2D); |
200 |
efad.setGeometryType(Geometry.TYPES.GEOMETRY); |
201 |
if (rsid!=null) |
202 |
efad.setSRS(CRSFactory.getCRS(rsid)); |
203 |
return efad;
|
204 |
} |
205 |
// }
|
206 |
|
207 |
return super.createAttributeFromJDBC(type, conn, rsMetadata, colIndex); |
208 |
} |
209 |
|
210 |
public boolean allowAutomaticValues() { |
211 |
return Boolean.TRUE; |
212 |
} |
213 |
|
214 |
public String getSqlFieldDescription(FeatureAttributeDescriptor attr) |
215 |
throws DataException {
|
216 |
// return getSqlFieldName(attr)+ " "+ getSqlColumnTypeDescription(attr);
|
217 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
218 |
return null; |
219 |
} |
220 |
return super.getSqlFieldDescription(attr); |
221 |
} |
222 |
|
223 |
public boolean supportOffset() { |
224 |
return true; |
225 |
} |
226 |
|
227 |
public boolean supportsUnion() { |
228 |
return true; |
229 |
} |
230 |
|
231 |
public String getH2SpatialGeomType(int geometryType, int geometrySubType) { |
232 |
String slGeomType = new String(); |
233 |
switch (geometryType) {
|
234 |
case Geometry.TYPES.GEOMETRY:
|
235 |
slGeomType = "GEOMETRY";
|
236 |
break;
|
237 |
case Geometry.TYPES.POINT:
|
238 |
slGeomType = "POINT";
|
239 |
break;
|
240 |
case Geometry.TYPES.CURVE:
|
241 |
slGeomType = "LINESTRING";
|
242 |
break;
|
243 |
case Geometry.TYPES.SURFACE:
|
244 |
slGeomType = "POLYGON";
|
245 |
break;
|
246 |
case Geometry.TYPES.MULTIPOINT:
|
247 |
slGeomType = "MULTIPOINT";
|
248 |
break;
|
249 |
case Geometry.TYPES.MULTICURVE:
|
250 |
slGeomType = "MULTILINESTRING";
|
251 |
break;
|
252 |
case Geometry.TYPES.MULTISURFACE:
|
253 |
slGeomType = "MULTIPOLYGON";
|
254 |
break;
|
255 |
default:
|
256 |
throw new UnsupportedGeometryException(geometryType, |
257 |
geometrySubType); |
258 |
} |
259 |
return slGeomType;
|
260 |
} |
261 |
|
262 |
public int getProviderSRID(String srs) { |
263 |
// if (srs != null) {
|
264 |
// Integer slSRID = (Integer) srsID2slSR.get(srs);
|
265 |
// if (slSRID != null) {
|
266 |
// return slSRID.intValue();
|
267 |
// }
|
268 |
//
|
269 |
// return searchslSRID(srs);
|
270 |
//
|
271 |
// }
|
272 |
return -1; |
273 |
} |
274 |
|
275 |
public int getH2SpatialGeomDimensions(int geometrySubType) { |
276 |
switch (geometrySubType) {
|
277 |
case Geometry.SUBTYPES.GEOM2D:
|
278 |
return 2; |
279 |
case Geometry.SUBTYPES.GEOM2DM:
|
280 |
case Geometry.SUBTYPES.GEOM3D:
|
281 |
return 3; |
282 |
|
283 |
case Geometry.SUBTYPES.GEOM3DM:
|
284 |
return 4; |
285 |
default:
|
286 |
throw new UnsupportedDataTypeException( |
287 |
DataTypes.TYPE_NAMES[DataTypes.GEOMETRY], |
288 |
DataTypes.GEOMETRY); |
289 |
} |
290 |
} |
291 |
|
292 |
public List getSqlGeometyFieldAdd(FeatureAttributeDescriptor attr, |
293 |
String table, String schema) { |
294 |
List sqls = new ArrayList(); |
295 |
return sqls;
|
296 |
} |
297 |
|
298 |
|
299 |
public List getAdditionalSqlToCreate(NewDataStoreParameters ndsp, |
300 |
FeatureType fType) { |
301 |
FeatureAttributeDescriptor attr; |
302 |
Iterator iter = fType.iterator();
|
303 |
List result = new ArrayList(); |
304 |
H2SpatialNewStoreParameters slNdsp = (H2SpatialNewStoreParameters) ndsp; |
305 |
while (iter.hasNext()) {
|
306 |
attr = (FeatureAttributeDescriptor) iter.next(); |
307 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
308 |
result.addAll(getSqlGeometyFieldAdd(attr, slNdsp.getTable(), |
309 |
slNdsp.getSchema())); |
310 |
} |
311 |
} |
312 |
|
313 |
return result;
|
314 |
} |
315 |
|
316 |
/**
|
317 |
* Fill <code>featureType</code> geometry attributes with SRS and ShapeType
|
318 |
* information stored in the table GEOMETRY_COLUMNS
|
319 |
*
|
320 |
* @param conn
|
321 |
* @param rsMetadata
|
322 |
* @param featureType
|
323 |
* @throws ReadException
|
324 |
*/
|
325 |
protected void loadSRS_and_shapeType(Connection conn, |
326 |
ResultSetMetaData rsMetadata, EditableFeatureType featureType,
|
327 |
String baseSchema, String baseTable) |
328 |
throws JDBCException {
|
329 |
|
330 |
Statement st = null; |
331 |
ResultSet rs = null; |
332 |
try {
|
333 |
// Sacamos la lista de los attributos geometricos
|
334 |
EditableFeatureAttributeDescriptor attr; |
335 |
List geoAttrs = new ArrayList(); |
336 |
|
337 |
Iterator iter = featureType.iterator();
|
338 |
while (iter.hasNext()) {
|
339 |
attr = (EditableFeatureAttributeDescriptor) iter.next(); |
340 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
341 |
geoAttrs.add(attr); |
342 |
// Preparamos una sql para que nos saque el resultado
|
343 |
StringBuilder strb = new StringBuilder(); |
344 |
strb.append("select NODE_DATA from \"" + baseSchema + "\".\"" + baseTable.toUpperCase()+"_HATBOX" + "\" where ID = 1"); |
345 |
|
346 |
String sql = strb.toString();
|
347 |
|
348 |
st = conn.createStatement(); |
349 |
try {
|
350 |
rs = st.executeQuery(sql); |
351 |
} catch (SQLException e) { |
352 |
throw new JDBCExecuteSQLException(sql, e); |
353 |
} |
354 |
|
355 |
if (rs.next()) {
|
356 |
MetaNode metaNode = new MetaNode(rs.getBytes(1)); |
357 |
String epsg = "EPSG:"+metaNode.getSrid(); |
358 |
attr.setSRS(CRSFactory.getCRS(epsg)); |
359 |
rsid=epsg; |
360 |
} |
361 |
} |
362 |
} |
363 |
if (geoAttrs.size() < 1) { |
364 |
return;
|
365 |
} |
366 |
} catch (java.sql.SQLException e) {
|
367 |
throw new JDBCSQLException(e); |
368 |
} finally {
|
369 |
try {rs.close();} catch (Exception e) { }; |
370 |
try {st.close();} catch (Exception e) { }; |
371 |
} |
372 |
|
373 |
} |
374 |
} |