I am trying to generate tiles from a set of rasters with some vectorial assets superimposed (names and areas). I am using Generate XYZ tiles (Directory).

Can anyone see from the output below why the math domain error might be being generated?

I am quite new to QGIS and this is the first time I have tried to generate tiles in any GIS.

QGIS version: 3.32.0-Lima
Qt version: 5.15.10
Python version: 3.11.3
GDAL version: 3.7.0
GEOS version: 3.12.0-CAPI-1.18.0
PROJ version: Rel. 9.2.1, June 1st, 2023
PDAL version: 2.5.5 (git-version: Release)
Algorithm started at: 2023-07-28T02:46:59
Algorithm 'Generate XYZ tiles (Directory)' starting…
Input parameters:
{ 'BACKGROUND_COLOR' : QColor(0, 0, 0, 0), 'DPI' : 96, 'EXTENT' : '-180.000000000,180.000000000,-90.000000000,90.000000000 [EPSG:4326]', 'HTML_ATTRIBUTION' : '', 'HTML_OSM' : False, 'HTML_TITLE' : '', 'METATILESIZE' : 4, 'OUTPUT_DIRECTORY' : 'TEMPORARY_OUTPUT', 'OUTPUT_HTML' : 'TEMPORARY_OUTPUT', 'QUALITY' : 75, 'TILE_FORMAT' : 0, 'TILE_HEIGHT' : 256, 'TILE_WIDTH' : 256, 'TMS_CONVENTION' : False, 'ZOOM_MAX' : 6, 'ZOOM_MIN' : 0 }

Traceback (most recent call last):
File "/usr/share/qgis/python/plugins/processing/algs/qgis/TilesXYZ.py", line 624, in processAlgorithm
self.generate(writer, parameters, context, feedback)
File "/usr/share/qgis/python/plugins/processing/algs/qgis/TilesXYZ.py", line 296, in generate
metatiles = get_metatiles(self.wgs_extent, zoom, self.metatilesize)
File "/usr/share/qgis/python/plugins/processing/algs/qgis/TilesXYZ.py", line 123, in get_metatiles
right_tile, bottom_tile = deg2num(south_edge, east_edge, zoom)
File "/usr/share/qgis/python/plugins/processing/algs/qgis/TilesXYZ.py", line 71, in deg2num
ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)
ValueError: math domain error

Execution failed after 0.03 seconds

Loading resulting layers
Algorithm 'Generate XYZ tiles (Directory)' finished
HTML output has been generated by this algorithm.
Open the results dialog to check it.

Note that this is not a duplicate of Math domain error when generating tiles in QGIS, despite my inherent pun at SO's obsession with duplicate questions. In my case, EPSG 4326 is the correct projection, and I am using "Generate XYZ tiles (Directory)" and not "Generate XYZ tiles (MBTiles)".

1 Answer 1


The problem comes from the extent, it is out of bounds for the resulting Mercator projection (EPSG:3857) link:

  • X goes from 0 (left edge is 180 °W) to 2**zoom − 1 (right edge is 180 °E)
  • Y goes from 0 (top edge is 85.0511 °N) to 2**zoom − 1 (bottom edge is 85.0511 °S) in a Mercator projection

So the maximum extent in EPSG:4326 should be -180.000000000,180.000000000,-85.0511287798066,85.0511287798066 [EPSG:4326]

And your specific error is caused by having -90 which causes the ValueError: math domain error:

explanation step by step (math used in the deg2num function of the processing script):

  • math.radians(-90) = -1.5707963267948966
  • math.tan(math.radians(-90)) = -1.633123935319537e+16
  • math.cos(math.radians(-90)) = 6.123233995736766e-17
  • 1/math.cos(math.radians(-90)) = 1.633123935319537e+16
  • math.tan(math.radians(-90)) + 1/math.cos(math.radians(-90)) = -1.633123935319537e+16 + 1.633123935319537e+16 = 0.0
  • Finally: math.log(math.tan(math.radians(-90)) + 1/math.cos(math.radians(-90))) = math.log(0.0) -> ValueError: math domain error

Meaning having -180.000000000,180.000000000,-89, 89 [EPSG:4326] would "work" but I wouldn't recommend it because it can raise a new error: threadSpecificSettings.setExtent(self.wgs_to_dest.transformBoundingBox(extent)) _core.QgsCsException: Could not transform bounding box to target CRS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.