Skip to content

Commit

Permalink
ENH: add get_gdal_data_path to query GDAL data directory (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Sep 30, 2022
1 parent 04d5da8 commit b1bbecd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## O.4.2

### Improvements

- new `get_gdal_data_path()` utility funtion to check the path of the data
directory detected by GDAL (#160)

### Bug fixes

- register GDAL drivers during initial import of pyogrio (#145)
Expand Down
2 changes: 2 additions & 0 deletions pyogrio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
read_info,
set_gdal_config_options,
get_gdal_config_option,
get_gdal_data_path,
__gdal_version__,
__gdal_version_string__,
__gdal_geos_version__,
Expand All @@ -23,6 +24,7 @@
"read_info",
"set_gdal_config_options",
"get_gdal_config_option",
"get_gdal_data_path",
"read_dataframe",
"write_dataframe",
"__gdal_version__",
Expand Down
10 changes: 10 additions & 0 deletions pyogrio/_ogr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ def has_gdal_data():
return False


def get_gdal_data_path():
"""
Get the path to the directory GDAL uses to read data files.
"""
cdef const char *path_c = CPLFindFile("gdal", "header.dxf")
if path_c != NULL:
return get_string(path_c).rstrip("header.dxf")
return None


def has_proj_data():
"""Verify that PROJ library data files are correctly found.
Expand Down
11 changes: 11 additions & 0 deletions pyogrio/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ogr_list_drivers,
set_gdal_config_options as _set_gdal_config_options,
get_gdal_config_option as _get_gdal_config_option,
get_gdal_data_path as _get_gdal_data_path,
init_gdal_data as _init_gdal_data,
init_proj_data as _init_proj_data,
remove_virtual_file,
Expand Down Expand Up @@ -221,3 +222,13 @@ def get_gdal_config_option(name):
"""

return _get_gdal_config_option(name)


def get_gdal_data_path():
"""Get the path to the directory GDAL uses to read data files.
Returns
-------
str, or None if data directory was not found
"""
return _get_gdal_data_path()
7 changes: 7 additions & 0 deletions pyogrio/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
read_info,
set_gdal_config_options,
get_gdal_config_option,
get_gdal_data_path,
)

from pyogrio._env import GDALEnv
Expand All @@ -32,6 +33,12 @@ def test_proj_data():
assert has_proj_data()


def test_get_gdal_data_path():
# test will fail if the function returns None, which means that GDAL
# cannot find data files, indicating an installation error
assert isinstance(get_gdal_data_path(), str)


def test_gdal_geos_version():
assert __gdal_geos_version__ is None or isinstance(__gdal_geos_version__, tuple)

Expand Down

0 comments on commit b1bbecd

Please sign in to comment.