def test_write_gpkg(tmpdir, naturalearth_lowres): meta, geometry, field_data = read(naturalearth_lowres) filename = os.path.join(str(tmpdir), "test.gpkg") write(filename, geometry, field_data, driver="GPKG", **meta) assert os.path.exists(filename)
def test_write_geojsonseq(tmpdir, naturalearth_lowres): meta, geometry, field_data = read(naturalearth_lowres) filename = os.path.join(str(tmpdir), "test.json") write(filename, geometry, field_data, driver="GeoJSONSeq", **meta) assert os.path.exists(filename)
def test_write_unsupported(tmpdir, naturalearth_lowres): meta, geometry, field_data = read(naturalearth_lowres) filename = os.path.join(str(tmpdir), "test.fgdb") with pytest.raises(DriverError, match="does not support write functionality"): write(filename, geometry, field_data, driver="OpenFileGDB", **meta)
def write_dataframe(df, path, layer=None, driver=None, encoding=None, **kwargs): try: with GDALEnv(): import geopandas as gp from geopandas.array import to_wkb # if geopandas is available so is pyproj from pyproj.enums import WktVersion except ImportError: raise ImportError("geopandas is required to use pyogrio.read_dataframe()") path = str(path) if not isinstance(df, gp.GeoDataFrame): raise ValueError("'df' must be a GeoDataFrame") geometry_columns = df.columns[df.dtypes == "geometry"] if len(geometry_columns) == 0: raise ValueError("'df' does not have a geometry column") if len(geometry_columns) > 1: raise ValueError( "'df' must have only one geometry column. " "Multiple geometry columns are not supported for output using OGR." ) geometry_column = geometry_columns[0] geometry = df[geometry_column] fields = [c for c in df.columns if not c == geometry_column] # TODO: may need to fill in pd.NA, etc field_data = [df[f].values for f in fields] # TODO: validate geometry types, not all combinations are valid geometry_type = geometry.type.unique()[0] crs = None if geometry.crs: # TODO: this may need to be WKT1, due to issues # if possible use EPSG codes instead epsg = geometry.crs.to_epsg() if epsg: crs = f"EPSG:{epsg}" else: crs = geometry.crs.to_wkt(WktVersion.WKT1_GDAL) write( path, layer=layer, driver=driver, geometry=to_wkb(geometry.values), field_data=field_data, fields=fields, crs=crs, geometry_type=geometry_type, encoding=encoding, )
def test_write(tmpdir, naturalearth_lowres): meta, geometry, field_data = read(naturalearth_lowres) filename = os.path.join(str(tmpdir), "test.shp") write(filename, geometry, field_data, **meta) assert os.path.exists(filename) for ext in (".dbf", ".prj"): assert os.path.exists(filename.replace(".shp", ext))
def test_write_supported(tmpdir, naturalearth_lowres, driver): """Test drivers not specifically tested above""" meta, geometry, field_data = read(naturalearth_lowres, columns=["iso_a3"]) # note: naturalearth_lowres contains mixed polygons / multipolygons, which # are not supported in mixed form for all drivers. To get around this here # we take the first record only. meta["geometry_type"] = "MultiPolygon" filename = tmpdir / "test" write(filename, geometry[:1], field_data=[f[:1] for f in field_data], driver=driver, **meta) assert filename.exists()
def test_write_geojson(tmpdir, naturalearth_lowres): meta, geometry, field_data = read(naturalearth_lowres) filename = os.path.join(str(tmpdir), "test.json") write(filename, geometry, field_data, driver="GeoJSON", **meta) assert os.path.exists(filename) data = json.loads(open(filename).read()) assert data["type"] == "FeatureCollection" assert data["name"] == "test" assert "crs" in data assert len(data["features"]) == len(geometry) assert not len( set(meta["fields"]).difference( data["features"][0]["properties"].keys()))