@@ -51,31 +51,13 @@ def __init__(self, uri):
51
51
52
52
def _opendb (self ):
53
53
54
- self .gdal_ds = None
55
- if hasattr (gdal , 'OpenEx' ):
56
- # GDAL >= 2
57
- self .gdal_ds = gdal .OpenEx (self .dbname , gdal .OF_UPDATE )
58
- if self .gdal_ds is None :
59
- self .gdal_ds = gdal .OpenEx (self .dbname )
60
- if self .gdal_ds is None or self .gdal_ds .GetDriver ().ShortName != 'GPKG' :
61
- raise ConnectionError (QApplication .translate ("DBManagerPlugin" , '"{0}" not found' ).format (self .dbname ))
62
- self .has_raster = self .gdal_ds .RasterCount != 0 or self .gdal_ds .GetMetadata ('SUBDATASETS' ) is not None
63
- self .connection = None
64
- self .gdal2 = True
65
- else :
66
- # GDAL 1.X compat. To be removed at some point
67
- self .gdal_ds = ogr .Open (self .dbname , update = 1 )
68
- if self .gdal_ds is None :
69
- self .gdal_ds = ogr .Open (self .dbname )
70
- if self .gdal_ds is None or self .gdal_ds .GetDriver ().GetName () != 'GPKG' :
71
- raise ConnectionError (QApplication .translate ("DBManagerPlugin" , '"{0}" not found' ).format (self .dbname ))
72
- # For GDAL 1.X, we cannot issue direct SQL SELECT to the OGR datasource
73
- # so we need a direct sqlite connection
74
- try :
75
- self .connection = spatialite_connect (str (self .dbname ))
76
- except self .connection_error_types () as e :
77
- raise ConnectionError (e )
78
- self .gdal2 = False
54
+ self .gdal_ds = gdal .OpenEx (self .dbname , gdal .OF_UPDATE )
55
+ if self .gdal_ds is None :
56
+ self .gdal_ds = gdal .OpenEx (self .dbname )
57
+ if self .gdal_ds is None or self .gdal_ds .GetDriver ().ShortName != 'GPKG' :
58
+ raise ConnectionError (QApplication .translate ("DBManagerPlugin" , '"{0}" not found' ).format (self .dbname ))
59
+ self .has_raster = self .gdal_ds .RasterCount != 0 or self .gdal_ds .GetMetadata ('SUBDATASETS' ) is not None
60
+ self .connection = None
79
61
80
62
def unquoteId (self , quotedId ):
81
63
if len (quotedId ) <= 2 or quotedId [0 ] != '"' or quotedId [len (quotedId ) - 1 ] != '"' :
@@ -92,56 +74,40 @@ def unquoteId(self, quotedId):
92
74
return unquoted
93
75
94
76
def _fetchOne (self , sql ):
95
- if not self .gdal2 :
96
- # GDAL 1.X compat. To be removed at some point
97
- c = self ._get_cursor ()
98
- self ._execute (c , sql )
99
- res = c .fetchone ()
100
- if res is not None :
101
- return res
102
- else :
103
- return None
77
+ sql_lyr = self .gdal_ds .ExecuteSQL (sql )
78
+ if sql_lyr is None :
79
+ return None
80
+ f = sql_lyr .GetNextFeature ()
81
+ if f is None :
82
+ ret = None
104
83
else :
105
- sql_lyr = self .gdal_ds .ExecuteSQL (sql )
106
- if sql_lyr is None :
107
- return None
84
+ ret = [f .GetField (i ) for i in range (f .GetFieldCount ())]
85
+ self .gdal_ds .ReleaseResultSet (sql_lyr )
86
+ return ret
87
+
88
+ def _fetchAll (self , sql , include_fid_and_geometry = False ):
89
+ sql_lyr = self .gdal_ds .ExecuteSQL (sql )
90
+ if sql_lyr is None :
91
+ return None
92
+ ret = []
93
+ while True :
108
94
f = sql_lyr .GetNextFeature ()
109
95
if f is None :
110
- ret = None
96
+ break
111
97
else :
112
- ret = [f .GetField (i ) for i in range (f .GetFieldCount ())]
113
- self .gdal_ds .ReleaseResultSet (sql_lyr )
114
- return ret
115
-
116
- def _fetchAll (self , sql , include_fid_and_geometry = False ):
117
- if not self .gdal2 :
118
- # GDAL 1.X compat. To be removed at some point
119
- c = self ._get_cursor ()
120
- self ._execute (c , sql )
121
- return c .fetchall ()
122
- else :
123
- sql_lyr = self .gdal_ds .ExecuteSQL (sql )
124
- if sql_lyr is None :
125
- return None
126
- ret = []
127
- while True :
128
- f = sql_lyr .GetNextFeature ()
129
- if f is None :
130
- break
98
+ if include_fid_and_geometry :
99
+ field_vals = [f .GetFID ()]
100
+ if sql_lyr .GetLayerDefn ().GetGeomType () != ogr .wkbNone :
101
+ geom = f .GetGeometryRef ()
102
+ if geom is not None :
103
+ geom = geom .ExportToWkt ()
104
+ field_vals += [geom ]
105
+ field_vals += [f .GetField (i ) for i in range (f .GetFieldCount ())]
106
+ ret .append (field_vals )
131
107
else :
132
- if include_fid_and_geometry :
133
- field_vals = [f .GetFID ()]
134
- if sql_lyr .GetLayerDefn ().GetGeomType () != ogr .wkbNone :
135
- geom = f .GetGeometryRef ()
136
- if geom is not None :
137
- geom = geom .ExportToWkt ()
138
- field_vals += [geom ]
139
- field_vals += [f .GetField (i ) for i in range (f .GetFieldCount ())]
140
- ret .append (field_vals )
141
- else :
142
- ret .append ([f .GetField (i ) for i in range (f .GetFieldCount ())])
143
- self .gdal_ds .ReleaseResultSet (sql_lyr )
144
- return ret
108
+ ret .append ([f .GetField (i ) for i in range (f .GetFieldCount ())])
109
+ self .gdal_ds .ReleaseResultSet (sql_lyr )
110
+ return ret
145
111
146
112
def _fetchAllFromLayer (self , table ):
147
113
@@ -167,15 +133,12 @@ def _fetchAllFromLayer(self, table):
167
133
return ret
168
134
169
135
def _execute_and_commit (self , sql ):
170
- if not self .gdal2 :
171
- DBConnector ._execute_and_commit (self , sql )
172
- else :
173
- sql_lyr = self .gdal_ds .ExecuteSQL (sql )
174
- self .gdal_ds .ReleaseResultSet (sql_lyr )
136
+ sql_lyr = self .gdal_ds .ExecuteSQL (sql )
137
+ self .gdal_ds .ReleaseResultSet (sql_lyr )
175
138
176
139
def _execute (self , cursor , sql ):
177
140
178
- if self .gdal2 and self . connection is None :
141
+ if self .connection is None :
179
142
# Needed when evaluating a SQL query
180
143
try :
181
144
self .connection = spatialite_connect (str (self .dbname ))
@@ -315,17 +278,16 @@ def getVectorTables(self, schema=None):
315
278
geomname = 'MULTIPOLYGON'
316
279
elif geomtype_flatten == ogr .wkbGeometryCollection :
317
280
geomname = 'GEOMETRYCOLLECTION'
318
- if self .gdal2 :
319
- if geomtype_flatten == ogr .wkbCircularString :
320
- geomname = 'CIRCULARSTRING'
321
- elif geomtype_flatten == ogr .wkbCompoundCurve :
322
- geomname = 'COMPOUNDCURVE'
323
- elif geomtype_flatten == ogr .wkbCurvePolygon :
324
- geomname = 'CURVEPOLYGON'
325
- elif geomtype_flatten == ogr .wkbMultiCurve :
326
- geomname = 'MULTICURVE'
327
- elif geomtype_flatten == ogr .wkbMultiSurface :
328
- geomname = 'MULTISURFACE'
281
+ elif geomtype_flatten == ogr .wkbCircularString :
282
+ geomname = 'CIRCULARSTRING'
283
+ elif geomtype_flatten == ogr .wkbCompoundCurve :
284
+ geomname = 'COMPOUNDCURVE'
285
+ elif geomtype_flatten == ogr .wkbCurvePolygon :
286
+ geomname = 'CURVEPOLYGON'
287
+ elif geomtype_flatten == ogr .wkbMultiCurve :
288
+ geomname = 'MULTICURVE'
289
+ elif geomtype_flatten == ogr .wkbMultiSurface :
290
+ geomname = 'MULTISURFACE'
329
291
geomdim = 'XY'
330
292
if hasattr (ogr , 'GT_HasZ' ) and ogr .GT_HasZ (lyr .GetGeomType ()):
331
293
geomdim += 'Z'
@@ -826,14 +788,13 @@ def hasSpatialIndex(self, table, geom_column):
826
788
if self .isRasterTable (table ) or geom_column is None :
827
789
return False
828
790
_ , tablename = self .getSchemaTableName (table )
829
- if self .gdal2 :
830
- # Only try this for GDAL >= 2 (but only available in >= 2.1.2)
831
- sql = u"SELECT HasSpatialIndex(%s, %s)" % (self .quoteString (tablename ), self .quoteString (geom_column ))
832
- gdal .PushErrorHandler ()
833
- ret = self ._fetchOne (sql )
834
- gdal .PopErrorHandler ()
835
- else :
836
- ret = None
791
+
792
+ # (only available in >= 2.1.2)
793
+ sql = u"SELECT HasSpatialIndex(%s, %s)" % (self .quoteString (tablename ), self .quoteString (geom_column ))
794
+ gdal .PushErrorHandler ()
795
+ ret = self ._fetchOne (sql )
796
+ gdal .PopErrorHandler ()
797
+
837
798
if ret is None :
838
799
# might be the case for GDAL < 2.1.2
839
800
sql = u"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name LIKE %s" % self .quoteString ("%%rtree_" + tablename + "_%%" )
0 commit comments