From aac2d92968a27f5cdb74c01e4494e188a87a87de Mon Sep 17 00:00:00 2001 From: Terry Brown <42387258+brownterryn@users.noreply.github.com> Date: Thu, 23 Jan 2025 13:31:43 -0600 Subject: [PATCH] Fix path interpretation for landscape layers. (#141) StreamCat was failing with a path to categorical image data outside the layers folder, either joining paths when it shouldn't, e.g. E:\path\to\layersE:/data/landcover.img, or failing to make a valid DBF name, e.g. ...\DBFStash\E:\data\landcover.img. Changes make it work regardless of / or \ delimiters, previously it failed one way or the other whichever you used. --- StreamCat.py | 4 ++-- StreamCat_functions.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/StreamCat.py b/StreamCat.py index 6fd0cc6..80e4469 100644 --- a/StreamCat.py +++ b/StreamCat.py @@ -90,7 +90,7 @@ mask_dir = "" layer = ( row.LandscapeLayer - if os.sep in row.LandscapeLayer + if "/" in row.LandscapeLayer or "\\" in row.LandscapeLayer else (f"{LYR_DIR}/{row.LandscapeLayer}") ) # use abspath if isinstance(row.summaryfield, str): @@ -191,4 +191,4 @@ "Be sure to delete the associated files in your `OUTDIR` to rerun:" f"\n\t> {OUT_DIR}\n\n!!! `$OUT_DIR/DBF_stash/*` " f"output used in 'Continuous' and 'Categorical' metrics!!!" - ) \ No newline at end of file + ) diff --git a/StreamCat_functions.py b/StreamCat_functions.py index 773ad08..c714c40 100644 --- a/StreamCat_functions.py +++ b/StreamCat_functions.py @@ -21,6 +21,7 @@ import sys import time from collections import OrderedDict, defaultdict, deque +from pathlib import Path from typing import Generator import numpy as np @@ -965,16 +966,18 @@ def createCatStats( arcpy.env.snapRaster = inZoneData if by_RPU == 0: if LandscapeLayer.count(".tif") or LandscapeLayer.count(".img"): + landscape_layer = Path(LandscapeLayer).stem # / vs. \ agnostic outTable = "%s/DBF_stash/zonalstats_%s%s%s.dbf" % ( out_dir, - LandscapeLayer.split("/")[-1].split(".")[0], + landscape_layer, appendMetric, zone, ) else: + landscape_layer = Path(LandscapeLayer).name # / vs. \ agnostic outTable = "%s/DBF_stash/zonalstats_%s%s%s.dbf" % ( out_dir, - LandscapeLayer.split("/")[-1], + landscape_layer, appendMetric, zone, )