Skip to content

Commit

Permalink
fix: argparse parsing of bbox string
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Mar 5, 2024
1 parent 6f95b8b commit 40e86ae
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions osm_fieldwork/basemapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ def create_basemap_file(
f"tms={tms}"
)

# Validation
if not boundary:
err = "You need to specify a boundary! (file or bbox)"
log.error(err)
raise ValueError(err)

# Get all the zoom levels we want
zoom_levels = list()
if zooms:
Expand Down Expand Up @@ -528,7 +534,15 @@ def main():
"""This main function lets this class be run standalone by a bash script."""
parser = argparse.ArgumentParser(description="Create an tile basemap for ODK Collect")
parser.add_argument("-v", "--verbose", action="store_true", help="verbose output")
parser.add_argument("-b", "--boundary", required=True, help="The boundary for the area you want")
parser.add_argument(
"-b",
"--boundary",
nargs="*",
required=True,
help=(
"The boundary for the area you want. " "Accepts path to geojson file or bbox string. " "Format min_x min_y max_x max_y"
),
)
parser.add_argument("-t", "--tms", help="Custom TMS URL")
parser.add_argument("--xy", default=False, help="Swap the X & Y coordinates when using a custom TMS")
parser.add_argument(
Expand All @@ -555,9 +569,26 @@ def main():
parser.print_help()
quit()

if len(args.boundary) == 1:
if Path(args.boundary[0]).suffix not in [".json", ".geojson"]:
log.error("")
log.error("*Error*: the boundary file must have .json or .geojson extension")
log.error("")
parser.print_help()
quit()
boundary_parsed = args.boundary[0]
elif len(args.boundary) == 4:
boundary_parsed = ",".join(args.boundary)
else:
log.error("")
log.error("*Error*: the bounding box must have 4 coordinates")
log.error("")
parser.print_help()
quit()

create_basemap_file(
verbose=args.verbose,
boundary=args.boundary,
boundary=boundary_parsed,
tms=args.tms,
xy=args.xy,
outfile=args.outfile,
Expand Down

0 comments on commit 40e86ae

Please sign in to comment.