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

Add auto detection of driver for .geojson, .geojsonl and .geojsons files #101

3 changes: 3 additions & 0 deletions pyogrio/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
".gpkg": "GPKG",
".shp": "ESRI Shapefile",
".json": "GeoJSON",
".geojson": "GeoJSON",
".geojsons": "GeoJSONSeq",
".geojsonl": "GeoJSONSeq",
".fgb": "FlatGeobuf",
}

Expand Down
2 changes: 1 addition & 1 deletion pyogrio/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


_data_dir = Path(__file__).parent.resolve() / "fixtures"
ALL_EXTS = [".shp", ".gpkg", ".json"]
ALL_EXTS = [".shp", ".gpkg", ".geojson", ".geojsonl", ".geojsons"]
theroggy marked this conversation as resolved.
Show resolved Hide resolved


def pytest_report_header(config):
Expand Down
29 changes: 22 additions & 7 deletions pyogrio/tests/test_geopandas_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,21 +448,36 @@ def test_write_dataframe(tmpdir, naturalearth_lowres, driver, ext):

@pytest.mark.filterwarnings("ignore:.*Layer .* does not have any features to read")
@pytest.mark.parametrize(
"driver,ext", [("ESRI Shapefile", "shp"), ("GeoJSON", "geojson"), ("GPKG", "gpkg")]
"ext", [ext for ext in ALL_EXTS if ext not in [".geojsonl", ".geojsons"]]
)
def test_write_empty_dataframe(tmpdir, driver, ext):
def test_write_empty_dataframe(tmp_path, ext):
expected = gp.GeoDataFrame(geometry=[], crs=4326)

filename = os.path.join(str(tmpdir), f"test.{ext}")
write_dataframe(expected, filename, driver=driver)

assert os.path.exists(filename)
filename = tmp_path / f"test{ext}"
write_dataframe(expected, filename)

assert filename.exists()
df = read_dataframe(filename)

assert_geodataframe_equal(df, expected)


@pytest.mark.parametrize("ext", [".geojsonl", ".geojsons"])
def test_write_read_empty_dataframe_unsupported(tmp_path, ext):
# Writing empty dataframe to .geojsons or .geojsonl results logically in a 0 byte
# file, but gdal isn't able to read those again at the time of writing.
# Issue logged here: https://github.com/geopandas/pyogrio/issues/94
expected = gp.GeoDataFrame(geometry=[], crs=4326)

filename = tmp_path / f"test{ext}"
write_dataframe(expected, filename)

assert filename.exists()
with pytest.raises(
Exception, match=".* not recognized as a supported file format."
):
_ = read_dataframe(filename)


def test_write_dataframe_gdalparams(tmp_path, naturalearth_lowres):
original_df = read_dataframe(naturalearth_lowres)

Expand Down