-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(geopandas io): move geopandas utils, add _gis_enabled featur…
…e flag to fix gpd import
- Loading branch information
Showing
2 changed files
with
69 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
try: | ||
from json import loads as load_json | ||
from geopandas import GeoDataFrame | ||
from io import BytesIO | ||
from fnmatch import fnmatch | ||
|
||
_gis_enabled = True | ||
|
||
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 ImportError: | ||
_gis_enabled = False |