站号 lon lat 海拔高度 cheng 0 59417 106.8540 22.3397 0 0.4 1 59419 106.7400 22.0644 0 0.4 2 53276 112.9010 42.3978 0 0.5 3 59421 107.1660 22.8594 0 0.3 4 59427 107.0390 22.1103 0 0.4 ... ... ... ... ... ... 1165 50658 125.7880 48.0842 0 1.4 1166 50659 126.2400 48.0539 0 1.3 1167 51430 81.1480 43.8317 0 0.5 1168 51431 81.3264 43.9406 0 1.1 1169 56319 95.3175 29.3128 0 0.8 1170 rows × 5 columns 0.0 32.0 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[31], line 63 61 # 对非中国区域的数据进行掩码操作 62 xx, yy = m(grid_lon, grid_lat) # 将经纬度转换为投影坐标 ---> 63 mask = ~m.is_land(xx, yy) | ((xx < m.xmin) | (xx > m.xmax)) | ((yy < m.ymin) | (yy > m.ymax)) 64 masked_data = np.ma.array(interpolated_accuracy, mask=mask) 66 # 绘制等高线填充图 File e:\Aminiconda\envs\tyingb\lib\site-packages\mpl_toolkits\basemap\__init__.py:2051, in Basemap.is_land(self, xpt, ypt) 2049 landpt = False 2050 for poly in self.landpolygons: -> 2051 landpt = _geoslib.Point((xpt,ypt)).within(poly) 2052 if landpt: break 2053 lakept = False File src\\_geoslib.pyx:468, in _geoslib.Point.__init__() TypeError: only size-1 arrays can be converted to Python scalars --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[42], line 2 1 shp = geopandas.read_file('E:/text/长春站点/长春市.shp') ----> 2 interpolated_accuracy.rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True) 3 interpolated_accuracy.rio.write_crs("WGS1984", inplace=True)#视自己的数据坐标系而定,这里是比较广泛的WGS1984 4 d = interpolated_accuracy.rio.clip(shp.geometry.apply(mapping),shp.crs,drop=False) AttributeError: 'numpy.ndarray' object has no attribute 'rio'
时间: 2025-05-12 18:34:48 浏览: 22
从你的问题来看,你在处理地理空间数据分析的过程中遇到了两个主要的问题:
### 第一个问题分析
```python
TypeError: only size-1 arrays can be converted to Python scalars
```
**原因**:
这个错误出现在 `Basemap` 的 `is_land()` 函数中。该函数期望传入的是单一标量值,而不是数组或列表。然而,在你的代码中传递了整个网格点 `(xx, yy)`。
**解决办法**:
可以尝试将 `xx` 和 `yy` 展平为一维数组,并逐一判断每个点是否位于陆地上,之后再重塑回原始形状。例如:
```python
import numpy as np
# 假设 xx 和 yy 是二维数组
shape = xx.shape # 记录原始形状
xx_flat = xx.flatten()
yy_flat = yy.flatten()
# 创建掩码
land_mask = []
for x, y in zip(xx_flat, yy_flat):
land_mask.append(m.is_land(x, y))
# 转换回原来的形状
land_mask = np.array(land_mask).reshape(shape)
```
---
### 第二个问题分析
```python
AttributeError: 'numpy.ndarray' object has no attribute 'rio'
```
**原因**:
这里的报错是因为变量 `interpolated_accuracy` 是一个 NumPy 数组 (`numpy.ndarray`),但它被当作了一个包含 `.rio` 方法的对象(如 `xarray.DataArray`)。`.rio` 是 `rasterio` 库的功能扩展模块,专门用于处理栅格数据的空间维度设置等问题。
**解决办法**:
需要确认 `interpolated_accuracy` 是否是一个支持 `.rio` 功能的类型。如果它是普通的 NumPy 数组,则无法直接使用 `.rio` 方法。你需要先将其转换成适当的对象(如 `xarray.DataArray` 或其他兼容的格式),然后再调用相应的功能。示例如下:
```python
import xarray as xr
# 将 NumPy 数组转为 DataArray 并附加坐标信息
da = xr.DataArray(
interpolated_accuracy,
dims=("y", "x"),
coords={
"lon": (("x"), grid_lon),
"lat": (("y"), grid_lat)
}
)
# 设置空间维度并指定 CRS
da.rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True)
da.rio.write_crs("epsg:4326", inplace=True) # 使用 WGS84 投影系统
# 执行裁剪操作
clipped_da = da.rio.clip(shp.geometry.apply(mapping), shp.crs, drop=False)
```
---
### 总结建议
1. **检查输入数据类型**:确保所有参与运算的数据都匹配预期类型。
2. **逐步调试**:对于复杂任务,尽量分步骤完成,避免一次运行过多逻辑导致定位问题困难。
3. **参考官方文档**:遇到库相关的疑问时,请查阅对应工具包的详细说明文件。
####
阅读全文
相关推荐










