pylandstats
pylandstats copied to clipboard
line 611, in _patch_area_ser return self._cached_patch_area_ser AttributeError: 'Landscape' object has no attribute '_cached_patch_area_ser'
- PyLandStats version:pylandstats 3.0.0rc1
- Python version:python3.9
- Operating System:window
Description
D:\anaconda\python.exe "D:\lhflearn\指数计算\landscape level1.py" Traceback (most recent call last): File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\landscape.py", line 611, in _patch_area_ser return self._cached_patch_area_ser AttributeError: 'Landscape' object has no attribute '_cached_patch_area_ser'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\lhflearn\指数计算\landscape level1.py", line 14, in
进程已结束,退出代码1
What I Did
import geopandas as gpd import pandas as pd from pylandstats import ZonalAnalysis
读取矢量区域块数据
gdf = gpd.read_file(r"D;\ .shp") tif_file = r" D;\ .tif"
创建 ZonalAnalysis 实例并指定文件路径
za = ZonalAnalysis(landscape_filepath=tif_file, zones=gdf)
定义要计算的指标
metrics = ['area_mn']
计算景观级别指标
landscape_metrics_df = za.compute_landscape_metrics_df(metrics=metrics)
将结果存储到 Excel 文件
output_file = r"D:\lhf\ybds\景观级别指标.xlsx" landscape_metrics_df.to_excel(output_file, index=False)
print("景观级别指标已保存到 Excel 文件:", output_file)
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
line 611, in _patch_area_ser
return self._cached_patch_area_ser
AttributeError: 'Landscape' object has no attribute '_cached_patch_area_ser'
Hi Marti,
I want to thank you for creating Pylandstats because it is great having a package in Python for calculating landscape metrics.
Nevertheless, ever since I have been using Pylandstats, I have received the error " I would appreciate it if you could help to resolve the error.
Best, Bahareh
Hello Bahareh,
thank you for your kind words and for using pylandstats. The AttributeError
is normal because of the "cache" system that pylandstats Landscape
instances use. However, pylandstats should catch this error and then proceed normally so that the user does not see any error. In your case, this did not happen because of the ValueError: need at least one array to concatenate
, which suggest that there is probably some issue with your raster or shapefile, since no patches could be found. Are you sure that your shapefile and raster dataset intersect?
Best, Martí
Hello, At first I want to thank Dr. Marti Bosch for developing this valuable package. I encountered Bahare's problem yesterday and tried to resolve it, finally by Marti's guide about input layers I found that: 1- The extent of the raster input layer must be larger than the zone layer (in vector format) 2- In the raster layer you must fill NoData values with any number else zero. Best, Hossein