temp_mean = np.mean(temp_array[city_geom.contains_points(temp_array.lon, temp_array.lat)])这串代码报错AttributeError: 'Polygon' object has no attribute 'contains_points',怎么修改
时间: 2024-04-03 17:32:46 浏览: 14
这个错误提示表明 `city_geom` 对象是一个 `Polygon` 类型的对象,但是它没有 `contains_points` 属性,因此不能调用 `contains_points` 方法。通常情况下,可以通过将 `city_geom` 对象转换成 `shapely.geometry.MultiPolygon` 类型的对象来解决这个问题。
你可以尝试使用以下代码来将 `city_geom` 转换为 `shapely.geometry.MultiPolygon` 对象:
```
from shapely.geometry import MultiPolygon
city_geom = MultiPolygon(city_geom)
```
然后再运行你的代码就应该可以通过了。
相关问题
temp_df = pd.DataFrame(columns=['year', 'month', 'city', 'temperature']) for year in years: for month in months: temp_array = temp_data[(year-1979)*12+month-1, :, :] for i, row in shp_file.iterrows(): city_name = row['市'] city_geom = row['geometry'] temp_mean = np.mean(temp_array[city_geom.contains_points(temp_array.lon, temp_array.lat)]) temp_df = temp_df.append({'year': year, 'month': month, 'city': city_name, 'temperature': temp_mean}, ignore_index=True)这串代码报错 temp_mean = np.mean(temp_array[city_geom.contains_points(temp_array.lon, temp_array.lat)]) AttributeError: 'Polygon' object has no attribute 'contains_points'怎么修改
这个错误是因为 `city_geom` 是一个单个多边形对象,而 `contains_points` 方法是用于点集合的。要解决此错误,可以使用 `contains` 方法来检查 `temp_array` 中的每个点是否在 `city_geom` 中,如下所示:
```
temp_mean = np.mean(temp_array[np.array([city_geom.contains(Point(lon, lat)) for lon, lat in zip(temp_array.lon, temp_array.lat)])])
```
这将使用列表推导式为 `temp_array` 中的每个点创建一个包含 True 或 False 的数组,并将其传递给 `temp_array`,以便获取在 `city_geom` 中的温度值。
import netCDF4 import geopandas as gpd import numpy as np import pandas as pd # 加载nc文件 nc_file = netCDF4.Dataset('E:/data/temp_CMFD_V0106_B-01_01mo_010deg_197901-201812.nc') temp_data = nc_file.variables['temp'][:] # 加载shp文件 shp_file = gpd.read_file('D:/dilidashuju/shijiquhua.shp') # 计算每个市级行政区的平均温度 years = range(1979, 2018) months = range(1, 13) temp_df = pd.DataFrame(columns=['year', 'month', 'city', 'temperature']) for year in years: for month in months: temp_array = temp_data[(year-1979)*12+month-1, :, :] for i, row in shp_file.iterrows(): city_name = row['市'] city_geom = row['geometry'] temp_mean = np.mean(temp_array[np.array([city_geom.contains(Point(lon, lat)) for lon, lat in zip(temp_array.lon, temp_array.lat)])]) temp_df = temp_df.append({'year': year, 'month': month, 'city': city_name, 'temperature': temp_mean}, ignore_index=True) # 将结果保存到CSV文件中 temp_df.to_csv('city_temperature.csv', index=False)这串代码报错Traceback (most recent call last): File "C:\Users\Lenovo\PycharmProjects\pythonditu\main.py", line 24, in <module> temp_mean = np.mean(temp_array[np.array([city_geom.contains(Point(lon, lat)) for lon, lat in zip(temp_array.lon, temp_array.lat)])]) AttributeError: 'MaskedArray' object has no attribute 'lon'怎么修改
这个错误是因为 `temp_array` 是一个 `MaskedArray` 对象,它不能像普通的 numpy 数组那样直接使用索引访问变量。要解决此错误,可以使用 `data` 属性来访问 `temp_array` 的实际数据数组,如下所示:
```
temp_mean = np.mean(temp_array.data[np.array([city_geom.contains(Point(lon, lat)) for lon, lat in zip(temp_array.lon, temp_array.lat)])])
```
这将使用列表推导式为 `temp_array` 中的每个点创建一个包含 True 或 False 的数组,并将其传递给 `temp_array.data`,以便获取在 `city_geom` 中的温度值。