Open simon-tarr opened 3 years ago
I have been able to successfully run compute_class_metrics_df()
by loading in the raster via rioxarrary
:
from pylandstats import Landscape
import rioxarray as rxr
raster = rxr.open_rasterio("/Users/simontarr/Downloads/testraster2.tif", masked=True).squeeze()
raster = raster.astype(int)
# Load in raster as rioxarray
ls=Landscape(raster.values, res=(len(raster.x),len(raster.y)))
x = ls.compute_patch_metrics_df()
print('PATCH METRICS \n', x)
y = ls.compute_class_metrics_df(['total_area', 'proportion_of_landscape', 'number_of_patches']) # Described error here
print('CLASS METRICS \n', y)
Hello @simon-tarr,
thank you for using pylandstats and reporting this. It seems that the problem is that the raster that you have sent does not have a nodata value, hence rasterio assigns a Python None
to such value, which is then problematic when pylandstats deals with the pixel adjacency matrix. From your second message, I suppose that rioxarray deals with it differently, also maybe because of the masked=True
argument.
I will assess whether I should add a patch to pylandstats to avoid this situation, since although the raster's metadata is problematic, pylandstats should be able to at least give a more informative error. In the meantime, you can use the following workaround:
ls = pls.Landscape('testraster.tif', nodata=0)
Thank you again. Best, Martí
Hey @martibosch, sorry about my very slow reply - I got caught up on another project and had to put my pylandstats
work on the back burner. Thanks for your explanation about what was causing the error, makes sense! The workaround is appreciated!
Same errors there. The workaround is nice, and a patch to fix the metadata reading is also much appreciated, thanks.
Hello @simon-tarr,
thank you for using pylandstats and reporting this. It seems that the problem is that the raster that you have sent does not have a nodata value, hence rasterio assigns a Python
None
to such value, which is then problematic when pylandstats deals with the pixel adjacency matrix. From your second message, I suppose that rioxarray deals with it differently, also maybe because of themasked=True
argument.I will assess whether I should add a patch to pylandstats to avoid this situation, since although the raster's metadata is problematic, pylandstats should be able to at least give a more informative error. In the meantime, you can use the following workaround:
ls = pls.Landscape('testraster.tif', nodata=0)
Thank you again. Best, Martí
My problem solved with "nodata=0". Thanks.
Description
I have a small .tif file and I'm trying to compute patch and class-level metrics with
PyLandStats
using the methodscompute_patch_metrics_df
andcompute_class_metrics_df
. I can successfully usecompute_patch_metrics_df
which returns:However I get an error with using
compute_class_metrics_df
(regardless of the input raster). The error is:Is this error user error on my part or some other issue related to the code in
PyLandStats
? I've included one of the rasters which creates the above error.Thank you! testraster.tif.zip