Skip to content

Commit

Permalink
Merge pull request #6 from mayrajeo/gpkg-fix
Browse files Browse the repository at this point in the history
Gpkg fix
  • Loading branch information
mayrajeo authored Jan 23, 2024
2 parents 68c669c + d3ffa1c commit 2bb5932
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 65 deletions.
28 changes: 15 additions & 13 deletions geo2ml/data/tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
self.rasterized_vector_path = self.outpath / "rasterized_vectors"

def tile_raster(
self, path_to_raster: str, allow_partial_data: bool = False
self, path_to_raster: Path | str, allow_partial_data: bool = False
) -> None:
"Tiles specified raster to `self.gridsize_x` times `self.gridsize_y` grid, with `self.overlap` pixel overlap"
names = []
Expand Down Expand Up @@ -110,7 +110,7 @@ def tile_raster(

def tile_vector(
self,
path_to_vector: str,
path_to_vector: Path | str,
min_area_pct: float = 0.0,
gpkg_layer: str = None,
output_format: str = "geojson",
Expand All @@ -124,7 +124,7 @@ def tile_vector(
"No raster grid specified, use Tiler.tile_raster to determine grid limits"
)

if path_to_vector.endswith(".gpkg") and not gpkg_layer:
if Path(path_to_vector).suffix == ".gpkg" and not gpkg_layer:
raise Exception(
"`sampling_locations` is .gpkg but no `gpkg_layer` specified"
)
Expand Down Expand Up @@ -170,8 +170,8 @@ def tile_vector(

def tile_and_rasterize_vector(
self,
path_to_raster: str,
path_to_vector: str,
path_to_raster: Path | str,
path_to_vector: Path | str,
column: str,
gpkg_layer: str = None,
keep_bg_only: bool = False,
Expand All @@ -186,7 +186,7 @@ def tile_and_rasterize_vector(
"No raster grid specified, use Tiler.tile_raster to determine grid limits"
)

if path_to_vector.endswith(".gpkg") and not gpkg_layer:
if Path(path_to_vector).suffix == ".gpkg" and not gpkg_layer:
raise Exception(
"`sampling_locations` is .gpkg but no `gpkg_layer` specified"
)
Expand Down Expand Up @@ -227,7 +227,9 @@ def tile_and_rasterize_vector(
return

# %% ../../nbs/12_data.tiling.ipynb 30
def untile_raster(path_to_targets: str, outfile: str, method: str = "first"):
def untile_raster(
path_to_targets: Path | str, outfile: Path | str, method: str = "first"
):
"""Merge multiple patches from `path_to_targets` into a single raster`"""

rasters = [
Expand Down Expand Up @@ -269,12 +271,12 @@ def copy_sum(merged_data, new_data, merged_mask, new_mask, **kwargs):


def untile_vector(
path_to_targets: str,
outpath: str,
path_to_targets: Path | str,
outpath: Path | str,
non_max_suppression_thresh: float = 0.0,
nms_criterion: str = "score",
):
"Create single GIS-filie from a directory of predicted .shp or .geojson files"
"Create single GIS-file from a directory of predicted .shp or .geojson files"
if os.path.isdir(path_to_targets): # directory
pred_files = [
f for f in os.listdir(path_to_targets) if f.endswith((".shp", ".geojson"))
Expand All @@ -286,7 +288,7 @@ def untile_vector(
gdf = temp_gdf
else:
gdf = pd.concat((gdf, temp_gdf))
elif path_to_targets.endswith("gpkg"): # geopackage
elif Path(path_to_targets).suffix == ".gpkg": # geopackage
layers = fiona.listlayers(path_to_targets)
gdf = None
for l in tqdm(layers):
Expand All @@ -295,7 +297,7 @@ def untile_vector(
gdf = temp_gdf
else:
gdf = pd.concat((gdf, temp_gdf))
print(f"{len(gdf)} polygons before non-max suppression")
print(f"{len(gdf)} polygons")
if non_max_suppression_thresh != 0:
np_bounding_boxes = np.array([b.bounds for b in gdf.geometry])
scores = gdf.score.values
Expand All @@ -306,7 +308,7 @@ def untile_vector(
sort_criterion=nms_criterion,
)
gdf = gdf.iloc[idxs]
print(f"{len(gdf)} polygons after non-max suppression")
print(f"{len(gdf)} polygons after non-max suppression")
if outpath.endswith("shp"):
gdf.to_file(outpath)
elif outpath.endswith("geojson"):
Expand Down
Loading

0 comments on commit 2bb5932

Please sign in to comment.