Skip to content

Commit

Permalink
[processing] fix #21958 used sqlite3 module instead of GDAL/ORG's SQL…
Browse files Browse the repository at this point in the history
…ite driver
  • Loading branch information
Marcel Dancak authored and PeterPetrik committed May 3, 2019
1 parent 346cb38 commit b1106b9
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions python/plugins/processing/algs/qgis/TilesXYZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import math
from uuid import uuid4

import ogr
import gdal
import sqlite3
from qgis.PyQt.QtCore import QSize, Qt, QByteArray, QBuffer
from qgis.PyQt.QtGui import QColor, QImage, QPainter
from qgis.core import (QgsProcessingException,
Expand Down Expand Up @@ -153,15 +153,22 @@ def __init__(self, filename, tile_params, extent, min_zoom, max_zoom):
driver = gdal.GetDriverByName('MBTiles')
ds = driver.Create(filename, 1, 1, 1, options=['TILE_FORMAT=%s' % tile_format] + options)
ds = None
sqlite_driver = ogr.GetDriverByName('SQLite')
ds = sqlite_driver.Open(filename, 1)
ds.ExecuteSQL("INSERT INTO metadata(name, value) VALUES ('{}', '{}');".format('minzoom', min_zoom))
ds.ExecuteSQL("INSERT INTO metadata(name, value) VALUES ('{}', '{}');".format('maxzoom', max_zoom))
# will be set properly after writing all tiles
ds.ExecuteSQL("INSERT INTO metadata(name, value) VALUES ('{}', '');".format('bounds'))
ds = None

self._execute_sqlite(
"INSERT INTO metadata(name, value) VALUES ('{}', '{}');".format('minzoom', min_zoom),
"INSERT INTO metadata(name, value) VALUES ('{}', '{}');".format('maxzoom', max_zoom),
# will be set properly after writing all tiles
"INSERT INTO metadata(name, value) VALUES ('{}', '');".format('bounds')
)
self._zoom = None

def _execute_sqlite(self, *commands):
conn = sqlite3.connect(self.filename)
for cmd in commands:
conn.execute(cmd)
conn.commit()
conn.close()

def _initZoomLayer(self, zoom):
west_edge, south_edge, east_edge, north_edge = self.extent
first_tile = Tile(*deg2num(north_edge, west_edge, zoom), zoom)
Expand All @@ -176,11 +183,8 @@ def _initZoomLayer(self, zoom):
first_tile_extent[3]
]

sqlite_driver = ogr.GetDriverByName('SQLite')
ds = sqlite_driver.Open(self.filename, 1)
bounds = ','.join(map(str, zoom_extent))
ds.ExecuteSQL("UPDATE metadata SET value='{}' WHERE name='bounds'".format(bounds))
ds = None
self._execute_sqlite("UPDATE metadata SET value='{}' WHERE name='bounds'".format(bounds))

self._zoomDs = gdal.OpenEx(self.filename, 1, open_options=['ZOOM_LEVEL=%s' % first_tile.z])
self._first_tile = first_tile
Expand All @@ -207,11 +211,8 @@ def writeTile(self, tile, image):

def close(self):
self._zoomDs = None
sqlite_driver = ogr.GetDriverByName('SQLite')
ds = sqlite_driver.Open(self.filename, 1)
bounds = ','.join(map(str, self.extent))
ds.ExecuteSQL("UPDATE metadata SET value='{}' WHERE name='bounds'".format(bounds))
ds = None
self._execute_sqlite("UPDATE metadata SET value='{}' WHERE name='bounds'".format(bounds))


class TilesXYZ(QgisAlgorithm):
Expand Down Expand Up @@ -309,6 +310,8 @@ def processAlgorithm(self, parameters, context, feedback):
settings.setDestinationCrs(dest_crs)
settings.setLayers(self.layers)
settings.setOutputDpi(dpi)
if tile_format == 'PNG':
settings.setBackgroundColor(QColor(Qt.transparent))

wgs_extent = src_to_wgs.transformBoundingBox(extent)
wgs_extent = [wgs_extent.xMinimum(), wgs_extent.yMinimum(), wgs_extent.xMaximum(), wgs_extent.yMaximum()]
Expand Down

0 comments on commit b1106b9

Please sign in to comment.