This commit is contained in:
2025-01-26 19:24:23 -08:00
parent 32cd60e92b
commit d1dde0dbc6
4155 changed files with 29170 additions and 216373 deletions

View File

@@ -1,15 +1,17 @@
import os
import contextlib
from zipfile import ZipFile, ZIP_DEFLATED
import pytest
import os
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
import pyogrio
import pyogrio.raw
from pyogrio.util import vsi_path
from pyogrio._compat import HAS_PYPROJ
from pyogrio.util import get_vsi_path_or_buffer, vsi_path
import pytest
try:
import geopandas # NOQA
import geopandas # noqa: F401
has_geopandas = True
except ImportError:
@@ -31,9 +33,11 @@ def change_cwd(path):
[
# local file paths that should be passed through as is
("data.gpkg", "data.gpkg"),
(Path("data.gpkg"), "data.gpkg"),
("/home/user/data.gpkg", "/home/user/data.gpkg"),
(r"C:\User\Documents\data.gpkg", r"C:\User\Documents\data.gpkg"),
("file:///home/user/data.gpkg", "/home/user/data.gpkg"),
("/home/folder # with hash/data.gpkg", "/home/folder # with hash/data.gpkg"),
# cloud URIs
("https://testing/data.gpkg", "/vsicurl/https://testing/data.gpkg"),
("s3://testing/data.gpkg", "/vsis3/testing/data.gpkg"),
@@ -82,6 +86,8 @@ def change_cwd(path):
"s3://testing/test.zip!a/b/item.shp",
"/vsizip/vsis3/testing/test.zip/a/b/item.shp",
),
("/vsimem/data.gpkg", "/vsimem/data.gpkg"),
(Path("/vsimem/data.gpkg"), "/vsimem/data.gpkg"),
],
)
def test_vsi_path(path, expected):
@@ -236,6 +242,9 @@ def test_detect_zip_path(tmp_path, naturalearth_lowres):
path = tmp_path / "test.zip"
with ZipFile(path, mode="w", compression=ZIP_DEFLATED, compresslevel=5) as out:
for ext in ["dbf", "prj", "shp", "shx"]:
if not HAS_PYPROJ and ext == "prj":
continue
filename = f"test1.{ext}"
out.write(tmp_path / filename, filename)
@@ -265,7 +274,7 @@ def test_detect_zip_path(tmp_path, naturalearth_lowres):
@pytest.mark.network
def test_url():
url = "https://raw.githubusercontent.com/geopandas/pyogrio/main/pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.shp" # NOQA
url = "https://raw.githubusercontent.com/geopandas/pyogrio/main/pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.shp"
result = pyogrio.raw.read(url)
assert len(result[2]) == 177
@@ -277,9 +286,10 @@ def test_url():
assert len(result[0]) == 177
@pytest.mark.network
@pytest.mark.skipif(not has_geopandas, reason="GeoPandas not available")
def test_url_dataframe():
url = "https://raw.githubusercontent.com/geopandas/pyogrio/main/pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.shp" # NOQA
url = "https://raw.githubusercontent.com/geopandas/pyogrio/main/pyogrio/tests/fixtures/naturalearth_lowres/naturalearth_lowres.shp"
assert len(pyogrio.read_dataframe(url)) == 177
@@ -330,3 +340,25 @@ def test_uri_s3(aws_env_setup):
def test_uri_s3_dataframe(aws_env_setup):
df = pyogrio.read_dataframe("zip+s3://fiona-testing/coutwildrnp.zip")
assert len(df) == 67
@pytest.mark.parametrize(
"path, expected",
[
(Path("/tmp/test.gpkg"), str(Path("/tmp/test.gpkg"))),
(Path("/vsimem/test.gpkg"), "/vsimem/test.gpkg"),
],
)
def test_get_vsi_path_or_buffer_obj_to_string(path, expected):
"""Verify that get_vsi_path_or_buffer retains forward slashes in /vsimem paths.
The /vsimem paths should keep forward slashes for GDAL to recognize them as such.
However, on Windows systems, forward slashes are by default replaced by backslashes,
so this test verifies that this doesn't happen for /vsimem paths.
"""
assert get_vsi_path_or_buffer(path) == expected
def test_get_vsi_path_or_buffer_fixtures_to_string(tmp_path):
path = tmp_path / "test.gpkg"
assert get_vsi_path_or_buffer(path) == str(path)