|
| 1 | +From 8e7ad6c60a5052f29a95def2709ad4075f0d1aa1 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Julien Jerphanion <git@jjerphan.xyz> |
| 3 | +Date: Fri, 19 Dec 2025 16:19:35 +0100 |
| 4 | +Subject: [PATCH] Set GDAL_DATA environment variable and remove ValueError |
| 5 | + checks |
| 6 | + |
| 7 | +Set GDAL_DATA to ${PREFIX}/share/gdal before importing pyogrio.core |
| 8 | +to ensure GDAL data files are found correctly. Also remove ValueError |
| 9 | +checks in init_gdal_data to allow graceful handling when data files |
| 10 | +are not present. Patch get_gdal_data_path to return GDAL_DATA env var. |
| 11 | + |
| 12 | +Signed-off-by: Julien Jerphanion <git@jjerphan.xyz> |
| 13 | +--- |
| 14 | + pyogrio/__init__.py | 7 +++++++ |
| 15 | + pyogrio/_ogr.pyx | 16 +++------------- |
| 16 | + 2 files changed, 10 insertions(+), 13 deletions(-) |
| 17 | + |
| 18 | +diff --git a/pyogrio/__init__.py b/pyogrio/__init__.py |
| 19 | +index 17db833..9bc18b1 100644 |
| 20 | +--- a/pyogrio/__init__.py |
| 21 | ++++ b/pyogrio/__init__.py |
| 22 | +@@ -1,5 +1,12 @@ |
| 23 | + """Vectorized vector I/O using OGR.""" |
| 24 | + |
| 25 | ++import os |
| 26 | ++ |
| 27 | ++# Set GDAL_DATA environment variable before importing GDAL-dependent modules |
| 28 | ++if 'GDAL_DATA' not in os.environ: |
| 29 | ++ __prefix = os.environ.get('PREFIX', "/") |
| 30 | ++ os.environ['GDAL_DATA'] = os.path.join(__prefix, 'share', 'gdal') |
| 31 | ++ |
| 32 | + try: |
| 33 | + # we try importing shapely, to ensure it is imported (and it can load its |
| 34 | + # own GEOS copy) before we load GDAL and its linked GEOS |
| 35 | +diff --git a/pyogrio/_ogr.pyx b/pyogrio/_ogr.pyx |
| 36 | +index 2109125..e58ee53 100644 |
| 37 | +--- a/pyogrio/_ogr.pyx |
| 38 | ++++ b/pyogrio/_ogr.pyx |
| 39 | +@@ -159,10 +159,9 @@ def get_gdal_data_path(): |
| 40 | + """ |
| 41 | + Get the path to the directory GDAL uses to read data files. |
| 42 | + """ |
| 43 | +- cdef const char *path_c = CPLFindFile("gdal", "header.dxf") |
| 44 | +- if path_c != NULL: |
| 45 | +- return get_string(path_c).replace("header.dxf", "") |
| 46 | +- return None |
| 47 | ++ __prefix = os.environ.get('PREFIX', "/") |
| 48 | ++ __fallback = os.path.join(__prefix, 'share', 'gdal') |
| 49 | ++ return os.environ.get('GDAL_DATA', __fallback) |
| 50 | + |
| 51 | + |
| 52 | + def has_proj_data(): |
| 53 | +@@ -204,10 +203,6 @@ def init_gdal_data(): |
| 54 | + wheel_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "gdal_data")) |
| 55 | + if os.path.exists(wheel_path): |
| 56 | + set_gdal_config_options({"GDAL_DATA": wheel_path}) |
| 57 | +- if not has_gdal_data(): |
| 58 | +- raise ValueError( |
| 59 | +- "Could not correctly detect GDAL data files installed by pyogrio wheel" |
| 60 | +- ) |
| 61 | + return |
| 62 | + |
| 63 | + # GDAL correctly found data files from GDAL_DATA or compiled-in paths |
| 64 | +@@ -217,11 +212,6 @@ def init_gdal_data(): |
| 65 | + wk_path = os.path.join(sys.prefix, "share", "gdal") |
| 66 | + if os.path.exists(wk_path): |
| 67 | + set_gdal_config_options({"GDAL_DATA": wk_path}) |
| 68 | +- if not has_gdal_data(): |
| 69 | +- raise ValueError( |
| 70 | +- f"Found GDAL data directory at {wk_path} but it does not appear to " |
| 71 | +- "correctly contain GDAL data files" |
| 72 | +- ) |
| 73 | + return |
| 74 | + |
| 75 | + warnings.warn( |
| 76 | +-- |
| 77 | +2.52.0 |
| 78 | + |
0 commit comments