Skip to content

Commit

Permalink
fix(gis)!: NameErrors in gis_interface (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
sqr00t authored Jul 31, 2024
1 parent 572f3e5 commit eb21ba3
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions nesta_ds_utils/loading_saving/gis_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,40 @@

_gis_enabled = True

except ImportError:
def _gdf_to_fileobj(df_data: GeoDataFrame, path_to: str, **kwargs) -> BytesIO:
"""Convert GeoDataFrame into bytes file object.
Args:
df_data (gpd.DataFrame): Dataframe to convert.
path_to (str): Saving file name.
Returns:
io.BytesIO: Bytes file object.
"""
buffer = BytesIO()
if fnmatch(path_to, "*.geojson"):
df_data.to_file(buffer, driver="GeoJSON", **kwargs)
else:
raise NotImplementedError(
"Uploading geodataframe currently supported only for 'geojson'."
)
buffer.seek(0)
return buffer

def _fileobj_to_gdf(fileobj: BytesIO, path_from: str, **kwargs) -> GeoDataFrame:
"""Convert bytes file object into geodataframe.
Args:
fileobj (io.BytesIO): Bytes file object.
path_from (str): Path of loaded data.
Returns:
gpd.DataFrame: Data as geodataframe.
"""
if fnmatch(path_from, "*.geojson"):
return GeoDataFrame.from_features(
load_json(fileobj.getvalue().decode())["features"]
)

except (NameError, ImportError):
_gis_enabled = False


def _gdf_to_fileobj(df_data: GeoDataFrame, path_to: str, **kwargs) -> BytesIO:
"""Convert GeoDataFrame into bytes file object.
Args:
df_data (gpd.DataFrame): Dataframe to convert.
path_to (str): Saving file name.
Returns:
io.BytesIO: Bytes file object.
"""
buffer = BytesIO()
if fnmatch(path_to, "*.geojson"):
df_data.to_file(buffer, driver="GeoJSON", **kwargs)
else:
raise NotImplementedError(
"Uploading geodataframe currently supported only for 'geojson'."
)
buffer.seek(0)
return buffer


def _fileobj_to_gdf(fileobj: BytesIO, path_from: str, **kwargs) -> GeoDataFrame:
"""Convert bytes file object into geodataframe.
Args:
fileobj (io.BytesIO): Bytes file object.
path_from (str): Path of loaded data.
Returns:
gpd.DataFrame: Data as geodataframe.
"""
if fnmatch(path_from, "*.geojson"):
return GeoDataFrame.from_features(
load_json(fileobj.getvalue().decode())["features"]
)

0 comments on commit eb21ba3

Please sign in to comment.