From 6f341b2b606566c48e4d2e4f3764fbe113ee593c Mon Sep 17 00:00:00 2001 From: Alexander Bruy Date: Tue, 7 May 2013 10:25:49 +0400 Subject: [PATCH] add option to create tiles using TMS naming convention (contributed by Giovanni Allegri) --- aboutdialog.py | 2 +- qtilesdialog.py | 5 ++++- qtilesdialogbase.ui | 11 +++++++++-- tile.py | 5 +++-- tilingthread.py | 22 +++++++++++++++------- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/aboutdialog.py b/aboutdialog.py index 809f6e7..da4a698 100644 --- a/aboutdialog.py +++ b/aboutdialog.py @@ -71,7 +71,7 @@ def getAboutText(self): return self.tr("""

Generate tiles from QGIS project.

Plugin generates raster tiles from QGIS project corresponding to Slippy Map specification. Output tiles can be saved in directory or as zip archive.

-

Developers: Alexander Bruy (NextGIS), portions of code by Andrew Naplavkov.

+

Developers: Alexander Bruy (NextGIS), portions of code by Andrew Naplavkov and Giovanni Allegri.

Homepage: http://hub.qgis.org/projects/qtiles

Please report bugs at bugtracker

""") diff --git a/qtilesdialog.py b/qtilesdialog.py index a2e4266..a701c73 100644 --- a/qtilesdialog.py +++ b/qtilesdialog.py @@ -96,6 +96,7 @@ def manageGui(self): self.spnTileHeight.setValue(settings.value("tileHeight", 256).toInt()[0]) self.chkAntialiasing.setChecked(settings.value("enable_antialiasing", False).toBool()) + self.chkTMSConvention.setChecked(settings.value("use_tms_filenames", False).toBool()) def reject(self): QDialog.reject(self) @@ -147,6 +148,7 @@ def accept(self): settings.setValue("tileHeight", self.spnTileHeight.value()) settings.setValue("enable_antialiasing", self.chkAntialiasing.isChecked()) + settings.setValue("use_tms_filenames", self.chkTMSConvention.isChecked()) canvas = self.iface.mapCanvas() @@ -175,7 +177,8 @@ def accept(self): self.spnTileWidth.value(), self.spnTileHeight.value(), fileInfo, - self.chkAntialiasing.isChecked() + self.chkAntialiasing.isChecked(), + self.chkTMSConvention.isChecked() ) self.workThread.rangeChanged.connect(self.setProgressRange) self.workThread.updateProgress.connect(self.updateProgress) diff --git a/qtilesdialogbase.ui b/qtilesdialogbase.ui index 119fda0..df94c49 100644 --- a/qtilesdialogbase.ui +++ b/qtilesdialogbase.ui @@ -6,8 +6,8 @@ 0 0 - 434 - 492 + 481 + 521 @@ -199,6 +199,13 @@ + + + + Use TMS tiles convention (OSM by default) + + + diff --git a/tile.py b/tile.py index 59334b1..46770c4 100644 --- a/tile.py +++ b/tile.py @@ -30,15 +30,16 @@ from qgis.core import * class Tile: - def __init__(self, x=0, y=0, z=0): + def __init__(self, x=0, y=0, z=0, tms=1): self.x = x self.y = y self.z = z + self.tms = tms def toPoint(self): n = math.pow(2, self.z) longitude = float(self.x) / n * 360.0 - 180.0 - latitude = math.degrees(math.atan(math.sinh(math.pi * (1.0 - 2.0 * float(self.y) / n)))) + latitude = self.tms * math.degrees(math.atan(math.sinh(math.pi * (1.0 - 2.0 * float(self.y) / n)))) return QgsPoint(longitude, latitude) def toRectangle(self): diff --git a/tilingthread.py b/tilingthread.py index a4bb073..92260c7 100644 --- a/tilingthread.py +++ b/tilingthread.py @@ -43,7 +43,7 @@ class TilingThread(QThread): rootDir = "Mapnik" - def __init__(self, layers, extent, minZoom, maxZoom, width, height, outputPath, antialiasing): + def __init__(self, layers, extent, minZoom, maxZoom, width, height, outputPath, antialiasing, tmsConvention): QThread.__init__(self, QThread.currentThread()) self.mutex = QMutex() self.stopMe = 0 @@ -57,6 +57,7 @@ def __init__(self, layers, extent, minZoom, maxZoom, width, height, outputPath, self.width = width self.antialias = antialiasing + self.tmsConvetion = tmsConvention self.interrupted = False self.tiles = [] @@ -102,9 +103,16 @@ def run(self): self.rangeChanged.emit(self.tr("Searching tiles..."), 0) - self.__countTiles(Tile()) + useTMS = 1 + if self.tmsConvention: + useTMS = -1 + + self.countTiles(Tile(tms=useTMS)) if self.interrupted: + del self.tiles[:] + self.tiles = None + if self.zip is not None: self.zip.close() self.zip = None @@ -122,7 +130,7 @@ def run(self): self.painter.setRenderHint(QPainter.Antialiasing) for t in self.tiles: - self.__render(t) + self.render(t) self.updateProgress.emit() @@ -149,7 +157,7 @@ def stop(self): QThread.wait(self) - def __countTiles(self, tile): + def countTiles(self, tile): if self.interrupted or not self.extent.intersects(tile.toRectangle()): return @@ -166,10 +174,10 @@ def __countTiles(self, tile): self.interrupted = True return - subTile = Tile(x, y, tile.z +1) - self.__countTiles(subTile) + subTile = Tile(x, y, tile.z +1, tile.tms) + self.countTiles(subTile) - def __render(self, tile): + def render(self, tile): self.renderer.setExtent(self.projector.transform(tile.toRectangle())) scale = self.scaleCalc.calculate(self.renderer.extent(), self.width) self.renderer.setScale(scale)