Skip to content

Commit

Permalink
Make output_path essential in "snail process" features.csv
Browse files Browse the repository at this point in the history
- add optional output_path_column to specify column name to use for output paths, relative to data directory
- default to "output_path"
- add quick examples to integration tests
  • Loading branch information
tomalrussell committed Aug 9, 2024
1 parent e0a1bd4 commit 5fec2f9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
49 changes: 39 additions & 10 deletions src/snail/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ def snail(args=None):
required=True,
help="CSV file with raster layers",
)
parser_process.add_argument(
"-c",
"--output_path_column",
type=str,
default="output_path",
help="Features CSV column to use for path to write output, defaults to 'output_path'",
)
parser_process.set_defaults(func=process)

args = parser.parse_args(args)
Expand Down Expand Up @@ -260,11 +267,36 @@ def process(args):
# data directory
dirname = args.directory

# read rasters and transforms
# read rasters and features
csv_errors = []
rasters = _read_csv_or_quit(args.rasters)
features = _read_csv_or_quit(args.features)

# read networks
vector_layers = _read_csv_or_quit(args.features)
if "path" not in rasters.columns:
csv_errors.append(
("Input path column 'path' not found in CSV: %s", args.rasters)
)

if "path" not in features.columns:
csv_errors.append(
("Input path column 'path' not found in CSV: %s", args.rasters)
)

if args.output_path_column not in features.columns:
csv_errors.append(
(
"Output path column '%s' not found in CSV: %s",
args.output_path_column,
args.features,
)
)
else:
features["output_path"] = features[args.output_path_column]

if csv_errors:
for err in csv_errors:
logging.error(*err)
sys.exit()

# fix up path relative to dirname
rasters.path = rasters.path.apply(_join_dirname, args=(dirname,))
Expand All @@ -282,16 +314,13 @@ def process(args):

rasters, transforms = extend_rasters_metadata(rasters)

# fix up path relative to dirname
vector_layers.path = vector_layers.path.apply(
# fix up paths relative to dirname
features.path = features.path.apply(_join_dirname, args=(dirname,))
features.output_path = features.output_path.apply(
_join_dirname, args=(dirname,)
)
if "output_path" not in vector_layers.columns:
vector_layers["output_path"] = vector_layers.path.apply(
lambda p: f"{p}.processed.parquet"
)

for vector_layer in vector_layers.itertuples():
for vector_layer in features.itertuples():
_process_layer(vector_layer, transforms, rasters, args.experimental)


Expand Down
1 change: 1 addition & 0 deletions tests/integration/empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
4 changes: 4 additions & 0 deletions tests/integration/features_alt_path.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
path,splits
lines.geojson,lines_split.parquet
points.geojson,points_split.parquet
polygons.geojson,polygons_split.parquet
4 changes: 4 additions & 0 deletions tests/integration/features_error.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
path
lines.geojson
points.geojson
polygons.geojson
13 changes: 13 additions & 0 deletions tests/integration/run_examples.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# overlay all rasters with each features layer
snail -vv process -fs features.csv -rs rasters.csv

# split with different output formats
snail -vv split -f lines.geojson -r inunriver_historical_WATCH_1980_rp01000.tif -c inunriver_historical_WATCH_1980_rp01000 -b 1 -a -o tmp.geojson
snail -vv split -f lines.geojson -r inunriver_historical_WATCH_1980_rp01000.tif -c inunriver_historical_WATCH_1980_rp01000 -b 1 -a -o tmp.parquet

# error missing columns
snail -vv process -fs empty.txt -rs empty.txt
snail -vv process -fs empty.txt -rs empty.txt -c other

# error with missing "output_path"
snail -vv process -fs features_error.csv -rs rasters.csv

# success with "splits" column
snail -vv process -fs features_alt_path.csv -rs rasters.csv -c splits

0 comments on commit 5fec2f9

Please sign in to comment.