Skip to content

Commit

Permalink
Merge pull request #7 from lutraconsulting/update_from_Qgis
Browse files Browse the repository at this point in the history
up to date with QGIS (checkbox for TMS)
  • Loading branch information
PeterPetrik authored May 22, 2019
2 parents 12910fa + 0e2cb58 commit 4505a0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion tiles_xyz/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name=Tiles XYZ
qgisMinimumVersion=3.0
qgisMaximumVersion=3.6.99
description=Processing algorithm for generating raster tiles
version=0.2
version=0.3
author=Lutra Consulting
email=info@lutraconsulting.co.uk

Expand Down
36 changes: 18 additions & 18 deletions tiles_xyz/tiles_xyz_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def deg2num(lat_deg, lon_deg, zoom):
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
return (xtile, ytile)


# Math functions taken from https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames #spellok
def num2deg(xtile, ytile, zoom):
n = 2.0 ** zoom
Expand Down Expand Up @@ -194,7 +195,8 @@ def generate(self, writer, parameters, context, feedback):
settings.setBackgroundColor(QColor(Qt.transparent))

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

metatiles_by_zoom = {}
metatiles_count = 0
Expand Down Expand Up @@ -455,39 +457,37 @@ def close(self):


class TilesXYZAlgorithmDirectory(TilesXYZAlgorithmBase):
TILE_CONVENTION = 'TILE_CONVENTION'
TMS_CONVENTION = 'TMS_CONVENTION'
OUTPUT_DIRECTORY = 'OUTPUT_DIRECTORY'
OUTPUT_HTML = 'OUTPUT_HTML'

def initAlgorithm(self, config=None):
super(TilesXYZAlgorithmDirectory, self).initAlgorithm()
self.conventions = ['Slippy Map', 'TMS']
self.addParameter(QgsProcessingParameterEnum(self.TILE_CONVENTION,
self.tr('Tile naming convention'),
self.conventions,
defaultValue=0))
self.addParameter(QgsProcessingParameterBoolean(self.TMS_CONVENTION,
self.tr('Use inverted tile Y axis (TMS convention)'),
defaultValue=False,
optional=True))
self.addParameter(QgsProcessingParameterFolderDestination(self.OUTPUT_DIRECTORY,
self.tr('Output directory'),
optional=True))
self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT_HTML,
self.tr('Output html (Leaflet)'),
self.tr('HTML files (*.html)'),
optional=True))

def name(self):
return 'tilesxyzdirectory'

def displayName(self):
return self.tr('Generate XYZ tiles (Directory)')

def processAlgorithm(self, parameters, context, feedback):
tile_convention = self.conventions[self.parameterAsEnum(parameters, self.TILE_CONVENTION, context)]
is_tms = self.parameterAsBoolean(parameters, self.TMS_CONVENTION, context)
output_html = self.parameterAsString(parameters, self.OUTPUT_HTML, context)
output_dir = self.parameterAsString(parameters, self.OUTPUT_DIRECTORY, context)
if not output_dir:
raise QgsProcessingException(self.tr('You need to specify output directory.'))

is_tms = tile_convention == 'TMS'
writer = DirectoryWriter(output_dir, is_tms)
self.generate(writer, parameters, context, feedback)

Expand All @@ -496,14 +496,14 @@ def processAlgorithm(self, parameters, context, feedback):
if output_html:
output_dir_safe = output_dir.replace('\\', '/')
html_code = LEAFLET_TEMPLATE.format(
tilesetname = "LeafLet Preview",
centerx = self.wgs_extent[0] + (self.wgs_extent[2] - self.wgs_extent[0])/2,
centery = self.wgs_extent[1] + (self.wgs_extent[3] - self.wgs_extent[1])/2,
avgzoom = (self.max_zoom + self.min_zoom) / 2,
tilesource = "'file:///{}/{{z}}/{{x}}/{{y}}.{}'".format(output_dir_safe, self.tile_format.lower()),
minzoom = self.min_zoom,
maxzoom = self.max_zoom,
tms = 'true' if is_tms else 'false'
tilesetname="Leaflet Preview",
centerx=self.wgs_extent[0] + (self.wgs_extent[2] - self.wgs_extent[0]) / 2,
centery=self.wgs_extent[1] + (self.wgs_extent[3] - self.wgs_extent[1]) / 2,
avgzoom=(self.max_zoom + self.min_zoom) / 2,
tilesource="'file:///{}/{{z}}/{{x}}/{{y}}.{}'".format(output_dir_safe, self.tile_format.lower()),
minzoom=self.min_zoom,
maxzoom=self.max_zoom,
tms='true' if is_tms else 'false'
)
with open(output_html, "w") as fh:
fh.write(html_code)
Expand Down

0 comments on commit 4505a0c

Please sign in to comment.