-
Notifications
You must be signed in to change notification settings - Fork 51
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 a multilayer reading option for AOI #507
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,11 @@ | |
|
||
import ast | ||
import pyproj | ||
import fiona | ||
from fiona._err import CPLE_OpenFailedError | ||
from fiona.errors import DriverError | ||
import geopandas as gpd | ||
import pandas as pd | ||
import numpy as np | ||
import pystac | ||
import rasterio | ||
|
@@ -174,11 +176,24 @@ def check_rasterio_im_load(im): | |
raise ValueError("{} is not an accepted image format for rasterio.".format(im)) | ||
|
||
|
||
def check_gdf_load(gdf): | ||
def check_gdf_load(gdf: Union[str, Path, gpd.GeoDataFrame]) -> gpd.GeoDataFrame: | ||
""" | ||
Check if `gdf` is already loaded in, if not, load from geojson. | ||
Copied from: https://github.com/CosmiQ/solaris/blob/main/solaris/utils/core.py#L52 | ||
""" | ||
|
||
We added a way to read all the layers from a given GPKG, this | ||
way if the GPKG contain more than one layer, it will stack all | ||
geometry and other information find in all the layers. | ||
|
||
Args: | ||
gdf (Union[str, Path, gpd.GeoDataFrame]): Link or Geodataframe itself. | ||
|
||
Raises: | ||
ValueError: Error if the given `gdf` is not a format supported. | ||
|
||
Returns: | ||
gpd.GeoDataFrame: GeoDataFrame containing all information and geometry. | ||
""" | ||
if isinstance(gdf, (str, Path)): | ||
if not is_url(gdf): | ||
gdf = to_absolute_path(str(gdf)) | ||
|
@@ -190,7 +205,21 @@ def check_gdf_load(gdf): | |
gdf, GEOM_POSSIBLE_NAMES="geometry", KEEP_GEOM_COLUMNS="NO" | ||
) | ||
try: | ||
return gpd.read_file(gdf) | ||
layer_name = fiona.listlayers(gdf) | ||
if len(layer_name) == 1: | ||
return gpd.read_file(gdf) | ||
else: | ||
# multi_layers = gpd.GeoDataFrame() | ||
for count, layername in enumerate(layer_name): | ||
if count == 0: | ||
multi_layers = gpd.read_file(gdf, layer=layername) | ||
else: | ||
geopkg = gpd.read_file(gdf, layer=layername) | ||
multi_layers = pd.concat( | ||
[multi_layers, geopkg], | ||
ignore_index=True | ||
) | ||
return multi_layers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, I understand why it makes sense to know if the label gpkg is a multi-layer or not. But do we have label gpkg in practice? I thought we always have a single-layer single-class gpkg for training? If this is not true, then I need to adjust my tiling script too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We mustly use single-layer single-class, but we want to also support multi-classes and Pierre told me that the GPKG will be multi layer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @CharlesAuthier let's discuss it altogether with Mathieu next week because it will require many potential adjustments to the code. |
||
except (DriverError, CPLE_OpenFailedError): | ||
logging.warning( | ||
f"GeoDataFrame couldn't be loaded: either {gdf} isn't a valid" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good job Charles! I like how you improved this part.