|
7 | 7 | from pyogrio import list_layers, read_info, __gdal_version__, __gdal_geos_version__
|
8 | 8 | from pyogrio.errors import DataLayerError, DataSourceError, FeatureError, GeometryError
|
9 | 9 | from pyogrio.geopandas import read_dataframe, write_dataframe
|
10 |
| -from pyogrio.raw import DRIVERS, DRIVERS_NO_MIXED_SINGLE_MULTI |
| 10 | +from pyogrio.raw import ( |
| 11 | + DRIVERS, |
| 12 | + DRIVERS_NO_MIXED_DIMENSIONS, |
| 13 | + DRIVERS_NO_MIXED_SINGLE_MULTI, |
| 14 | +) |
11 | 15 | from pyogrio.tests.conftest import ALL_EXTS
|
12 | 16 |
|
13 | 17 | try:
|
14 | 18 | import pandas as pd
|
15 | 19 | from pandas.testing import assert_frame_equal, assert_index_equal
|
16 | 20 |
|
17 | 21 | import geopandas as gp
|
| 22 | + from geopandas.array import from_wkt |
18 | 23 | from geopandas.testing import assert_geodataframe_equal
|
19 | 24 |
|
20 | 25 | from shapely.geometry import Point
|
@@ -843,17 +848,93 @@ def test_write_read_null(tmp_path):
|
843 | 848 | ],
|
844 | 849 | )
|
845 | 850 | def test_write_geometry_z_types(tmp_path, wkt, geom_types):
|
846 |
| - from geopandas.array import from_wkt |
847 |
| - |
848 | 851 | filename = tmp_path / "test.fgb"
|
849 |
| - |
850 | 852 | gdf = gp.GeoDataFrame(geometry=from_wkt([wkt]), crs="EPSG:4326")
|
851 | 853 | for geom_type in geom_types:
|
852 | 854 | write_dataframe(gdf, filename, geometry_type=geom_type)
|
853 | 855 | df = read_dataframe(filename)
|
854 | 856 | assert_geodataframe_equal(df, gdf)
|
855 | 857 |
|
856 | 858 |
|
| 859 | +@pytest.mark.parametrize("ext", ALL_EXTS) |
| 860 | +@pytest.mark.parametrize( |
| 861 | + "test_descr, exp_geometry_type, mixed_dimensions, wkt", |
| 862 | + [ |
| 863 | + ("1 Point Z", "2.5D Point", False, ["Point Z (0 0 0)"]), |
| 864 | + ("1 LineString Z", "2.5D LineString", False, ["LineString Z (0 0 0, 1 1 0)"]), |
| 865 | + ( |
| 866 | + "1 Polygon Z", |
| 867 | + "2.5D Polygon", |
| 868 | + False, |
| 869 | + ["Polygon Z ((0 0 0, 0 1 0, 1 1 0, 0 0 0))"], |
| 870 | + ), |
| 871 | + ("1 MultiPoint Z", "2.5D MultiPoint", False, ["MultiPoint Z (0 0 0, 1 1 0)"]), |
| 872 | + ( |
| 873 | + "1 MultiLineString Z", |
| 874 | + "2.5D MultiLineString", |
| 875 | + False, |
| 876 | + ["MultiLineString Z ((0 0 0, 1 1 0), (2 2 2, 3 3 2))"], |
| 877 | + ), |
| 878 | + ( |
| 879 | + "1 MultiLinePolygon Z", |
| 880 | + "2.5D MultiPolygon", |
| 881 | + False, |
| 882 | + [ |
| 883 | + "MultiPolygon Z (((0 0 0, 0 1 0, 1 1 0, 0 0 0)), ((1 1 1, 1 2 1, 2 2 1, 1 1 1)))" # noqa: E501 |
| 884 | + ], |
| 885 | + ), |
| 886 | + ( |
| 887 | + "1 GeometryCollection Z", |
| 888 | + "2.5D GeometryCollection", |
| 889 | + False, |
| 890 | + ["GeometryCollection Z (Point Z (0 0 0))"], |
| 891 | + ), |
| 892 | + ("Point Z + Point", "2.5D Point", True, ["Point Z (0 0 0)", "Point (0 0)"]), |
| 893 | + ("Point Z + None", "2.5D Point", False, ["Point Z (0 0 0)", None]), |
| 894 | + ], |
| 895 | +) |
| 896 | +def test_write_geometry_z_types_auto( |
| 897 | + tmp_path, ext, test_descr, exp_geometry_type, mixed_dimensions, wkt |
| 898 | +): |
| 899 | + # Shapefile has some different behaviour that other file types |
| 900 | + if ext == ".shp": |
| 901 | + if exp_geometry_type == "2.5D GeometryCollection": |
| 902 | + pytest.skip(f"ext {ext} doesn't support {exp_geometry_type}") |
| 903 | + elif exp_geometry_type == "2.5D MultiLineString": |
| 904 | + exp_geometry_type = "2.5D LineString" |
| 905 | + elif exp_geometry_type == "2.5D MultiPolygon": |
| 906 | + exp_geometry_type = "2.5D Polygon" |
| 907 | + |
| 908 | + column_data = {} |
| 909 | + column_data["test_descr"] = [test_descr] * len(wkt) |
| 910 | + column_data["idx"] = [str(idx) for idx in range(len(wkt))] |
| 911 | + gdf = gp.GeoDataFrame(column_data, geometry=from_wkt(wkt), crs="EPSG:4326") |
| 912 | + filename = tmp_path / f"test{ext}" |
| 913 | + |
| 914 | + if mixed_dimensions and DRIVERS[ext] in DRIVERS_NO_MIXED_DIMENSIONS: |
| 915 | + with pytest.raises( |
| 916 | + DataSourceError, |
| 917 | + match=("Mixed 2D and 3D coordinates are not supported by"), |
| 918 | + ): |
| 919 | + write_dataframe(gdf, filename) |
| 920 | + return |
| 921 | + else: |
| 922 | + write_dataframe(gdf, filename) |
| 923 | + |
| 924 | + info = read_info(filename) |
| 925 | + assert info["geometry_type"] == exp_geometry_type |
| 926 | + |
| 927 | + result_gdf = read_dataframe(filename) |
| 928 | + if ext == ".geojsonl": |
| 929 | + result_gdf.crs = "EPSG:4326" |
| 930 | + if ext == ".fgb": |
| 931 | + # When the following gdal issue is released, this if needs to be removed: |
| 932 | + # https://github.com/OSGeo/gdal/issues/7401 |
| 933 | + gdf = gdf.loc[~((gdf.geometry == np.array(None)) | gdf.geometry.is_empty)] |
| 934 | + |
| 935 | + assert_geodataframe_equal(gdf, result_gdf) |
| 936 | + |
| 937 | + |
857 | 938 | def test_read_multisurface(data_dir):
|
858 | 939 | df = read_dataframe(data_dir / "test_multisurface.gpkg")
|
859 | 940 |
|
|
0 commit comments