Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line 611, in _patch_area_ser return self._cached_patch_area_ser AttributeError: 'Landscape' object has no attribute '_cached_patch_area_ser' #49

Open
1810174827 opened this issue Apr 28, 2024 · 3 comments

Comments

@1810174827
Copy link

  • 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
landscape_metrics_df = za.compute_landscape_metrics_df(metrics=metrics)
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\multilandscape.py", line 233, in compute_landscape_metrics_df
] = landscape.compute_landscape_metrics_df(
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\landscape.py", line 3943, in compute_landscape_metrics_df
metrics_dict[metric] = getattr(self, metric)(**metric_kws)
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\landscape.py", line 1711, in area_mn
return self._metric_mn(
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\landscape.py", line 792, in _metric_mn
return self._metric_reduce(
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\landscape.py", line 778, in _metric_reduce
patch_metrics = patch_metric_method(
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\landscape.py", line 882, in area
area_ser = self._get_patch_area_ser(class_val=class_val)
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\landscape.py", line 727, in _get_patch_area_ser
patch_area_ser = self._patch_area_ser
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\pylandstats\landscape.py", line 614, in _patch_area_ser
np.concatenate(
File "<array_function internals>", line 5, in concatenate
ValueError: need at least one array to concatenate

进程已结束,退出代码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'
@1810174827
Copy link
Author

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

@martibosch
Copy link
Owner

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í

@NILICK
Copy link

NILICK commented Aug 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants