Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gdal and proj errors for map tiles #918

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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