Skip to content

Commit

Permalink
add option to create tiles using TMS naming convention (contributed by
Browse files Browse the repository at this point in the history
Giovanni Allegri)
  • Loading branch information
alexbruy committed May 7, 2013
1 parent 5fe726c commit 6f341b2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion aboutdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def getAboutText(self):
return self.tr("""<p>Generate tiles from QGIS project.</p>
<p>Plugin generates raster tiles from QGIS project corresponding to <a href="http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames">Slippy Map</a>
specification. Output tiles can be saved in directory or as zip archive.</p>
<p><strong>Developers</strong>: Alexander Bruy (<a href="http://nextgis.org">NextGIS</a>), portions of code by Andrew Naplavkov.</p>
<p><strong>Developers</strong>: Alexander Bruy (<a href="http://nextgis.org">NextGIS</a>), portions of code by Andrew Naplavkov and Giovanni Allegri.</p>
<p><strong>Homepage</strong>: <a href="http://hub.qgis.org/projects/qtiles">http://hub.qgis.org/projects/qtiles</a></p>
<p>Please report bugs at <a href="http://hub.qgis.org/projects/qtiles/issues">bugtracker</a></p>
""")
5 changes: 4 additions & 1 deletion qtilesdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions qtilesdialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>434</width>
<height>492</height>
<width>481</width>
<height>521</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -199,6 +199,13 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="3">
<widget class="QCheckBox" name="chkTMSConvention">
<property name="text">
<string>Use TMS tiles convention (OSM by default)</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
5 changes: 3 additions & 2 deletions tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 15 additions & 7 deletions tilingthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 6f341b2

Please sign in to comment.