Skip to content

Commit

Permalink
Fix gdal and proj errors for map tiles (#918)
Browse files Browse the repository at this point in the history
* Fix gdal and proj errors for map tiles

* Fix h3 error
  • Loading branch information
giswqs authored Oct 14, 2024
1 parent 8ddf6c4 commit f576896
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
25 changes: 14 additions & 11 deletions docs/notebooks/92_maplibre.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -812,16 +812,19 @@
" pitch=40,\n",
" bearing=-27,\n",
")\n",
"data = create_h3_grid()\n",
"m.add_geojson(\n",
" data,\n",
" layer_type=\"fill-extrusion\",\n",
" paint={\n",
" \"fill-extrusion-color\": [\"get\", \"color\"],\n",
" \"fill-extrusion-opacity\": 0.7,\n",
" \"fill-extrusion-height\": [\"*\", 100, [\"get\", \"count\"]],\n",
" },\n",
")\n",
"try:\n",
" data = create_h3_grid()\n",
" m.add_geojson(\n",
" data,\n",
" layer_type=\"fill-extrusion\",\n",
" paint={\n",
" \"fill-extrusion-color\": [\"get\", \"color\"],\n",
" \"fill-extrusion-opacity\": 0.7,\n",
" \"fill-extrusion-height\": [\"*\", 100, [\"get\", \"count\"]],\n",
" },\n",
" )\n",
"except:\n",
" pass\n",
"m"
]
},
Expand Down Expand Up @@ -986,7 +989,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down
17 changes: 14 additions & 3 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9220,7 +9220,7 @@ def map_tiles_to_geotiff(
**kwargs: Additional arguments to pass to gdal.GetDriverByName("GTiff").Create().
"""

import re
import io
import math
import itertools
Expand Down Expand Up @@ -9325,7 +9325,18 @@ def resolution_to_zoom_level(resolution):

gdal.UseExceptions()
web_mercator = osr.SpatialReference()
web_mercator.ImportFromEPSG(3857)
try:
web_mercator.ImportFromEPSG(3857)
except RuntimeError as e:
# https://github.com/PDAL/PDAL/issues/2544#issuecomment-637995923
if "PROJ" in str(e):
pattern = r"/[\w/]+"
match = re.search(pattern, str(e))
if match:
file_path = match.group(0)
os.environ["PROJ_LIB"] = file_path
os.environ["GDAL_DATA"] = file_path.replace("proj", "gdal")
web_mercator.ImportFromEPSG(3857)

WKT_3857 = web_mercator.ExportToWkt()

Expand Down Expand Up @@ -9471,7 +9482,7 @@ def draw_tile(
gtiff.SetGeoTransform((min(xp0, xp1), pwidth, 0, max(yp0, yp1), 0, -pheight))
gtiff.SetProjection(WKT_3857)
for band in range(imgbands):
array = numpy.array(img.getdata(band), dtype="u8")
array = np.array(img.getdata(band), dtype="u8")
array = array.reshape((img.size[1], img.size[0]))
band = gtiff.GetRasterBand(band + 1)
band.WriteArray(array)
Expand Down

0 comments on commit f576896

Please sign in to comment.