Skip to content

Commit

Permalink
fix(multimasks): fix bug on inconsistency between different zoom levels
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Mar 27, 2024
1 parent 8089e4c commit 4cb7264
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
47 changes: 12 additions & 35 deletions hot_fair_utilities/preprocessing/multimasks_from_polygons.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ def multimasks_from_polygons(
in_poly_dir,
in_chip_dir,
out_mask_dir,
input_contact_spacing=0.75,
input_boundary_width=0.5,
input_contact_spacing=8,
input_boundary_width=3,
):
"""
Create multichannel building footprint masks from a folder of geojson files.
This also requires the path to the matching image chips directory.Unit of input_contact_spacing and input_boundary_width is in meter which is :
This also requires the path to the matching image chips directory.Unit of input_contact_spacing and input_boundary_width is in pixel which is :
## Can not use meters for contact spacing and width because it won't maintain consistency in different zoom levels
Real-world width (in meters)= Pixel width×Resolution (meters per pixel)
Expand All @@ -44,7 +46,7 @@ def multimasks_from_polygons(
in_chip_dir (str): Path to directory containing image chip files with names matching geojson files.
out_mask_dir (str): Path to directory containing output SDT masks.
input_contact_spacing (int, optional): Pixels that are closer to two different polygons than contact_spacing will be labeled with the contact mask.
input_boundary_width (int, optional): Width in meters of boundary inner buffer around building footprints
input_boundary_width (int, optional): Width in pixel of boundary inner buffer around building footprints
Example:
multimasks_from_polygons(
Expand All @@ -70,7 +72,6 @@ def multimasks_from_polygons(

# construct a list of full paths to the mask files
json_chip_mask_zips = zip(label_paths, chip_paths, mask_paths)
first_iteration = True
for json_path, chip_path, mask_path in tqdm(
json_chip_mask_zips, desc="Multimasks for input"
):
Expand All @@ -97,38 +98,14 @@ def multimasks_from_polygons(

if crs_is_metric(gdf):
meters = True
boundary_width = input_boundary_width
contact_spacing = input_contact_spacing
if first_iteration:
print(
"Resolution (pixel width) in meter :",
min(reference_im.res),
)
print(
"Multimasks labels , Input boundary_width in meters :",
boundary_width,
)
print(
"Multimasks labels , Input contact_spacing in meters :",
contact_spacing,
)
first_iteration = False
boundary_width = min(reference_im.res) * input_boundary_width
contact_spacing = min(reference_im.res) * input_contact_spacing

else:
meters = False
# convert meter to pixel unit
boundary_width = int(input_boundary_width / min(reference_im.res))
contact_spacing = int(input_contact_spacing / min(reference_im.res))

if first_iteration:
print(
"Multimasks labels , Input boundary_width in pixel :",
boundary_width,
)
print(
"Multimasks labels , Input contact_spacing in pixel :",
contact_spacing,
)
first_iteration = False
boundary_width = input_boundary_width
contact_spacing = input_contact_spacing

# NOTE: solaris does not support multipolygon geodataframes
# So first we call explode() to turn multipolygons into polygon dataframes
# ignore_index=True prevents polygons from the same multipolygon from being grouped into a series. -+
Expand Down
8 changes: 4 additions & 4 deletions hot_fair_utilities/preprocessing/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def preprocess(
rasterize_options=None,
georeference_images=False,
multimasks=False,
input_contact_spacing=0.75, # only required if multimasks is set to true
input_boundary_width=0.5, # only required if mulltimasks is set to true
input_contact_spacing=8, # only required if multimasks is set to true
input_boundary_width=3, # only required if mulltimasks is set to true
) -> None:
"""Fully preprocess the input data.
Expand Down Expand Up @@ -44,9 +44,9 @@ def preprocess(
georeference_images: Whether to georeference the OAM images.
multimasks: Whether to additionally output multimask labels.
input_contact_spacing (int, optional): Pixels that are closer to two different polygons than contact_spacing will be labeled with the contact mask.
input_boundary_width (int, optional): Width in meters of boundary inner buffer around building footprints
input_boundary_width (int, optional): Width in pixel of boundary inner buffer around building footprints
Unit of input_contact_spacing and input_boundary_width is in meter which is :
Unit of input_contact_spacing and input_boundary_width is in pixel, we couldn't use meters to maintain consistency based on different zoom level as pixel resolution will be different which is :
Real-world width (in meters)= Pixel width×Resolution (meters per pixel)
Expand Down

0 comments on commit 4cb7264

Please sign in to comment.