You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

提取栅格值到GeoDataFrame属性列时的Pandas类型不兼容FutureWarning问题求助

提取栅格值到GeoDataFrame属性列时的Pandas类型不兼容FutureWarning问题求助

我写了一段代码,用来从多个栅格文件中提取值并赋值给点要素GeoDataFrame的属性列,但除了DEM列返回int类型没有警告外,其他所有列都会触发FutureWarning,我不想把所有值都强制转成int类型,该怎么解决?

我的代码如下:

import geopandas as gpd  # used to read the shapfile
import rasterio as rio   # used to read the raster (.tif) files
from rasterio.plot import show # used to make plots using rasterio
import matplotlib.pyplot as plt #to make plots using matplotlib

points = gpd.read_file('Points.shp')

points['DEM'] = 0
points['Slope'] = 0
points['Aspect'] = 0
points['RD'] = 0
points['D2Road'] = 0
points['D2Res'] = 0
points['LC'] = 0
points['Temp'] = 0

DEM_raster = rio.open('Elevation.tif')
DEM_arr = DEM_raster.read(1)

Slope_raster = rio.open('Slope.tif')
Slope_arr = Slope_raster.read(1)

Aspect_raster = rio.open('Aspect.tif')
Aspect_arr = Aspect_raster.read(1)

RD_raster = rio.open('RD.tif')
RD_arr = RD_raster.read(1)

Droad_raster = rio.open('D2Road.tif')
Droad_arr = Droad_raster.read(1)

Dres_raster = rio.open('D2Res.tif')
Dres_arr = Dres_raster.read(1)

LC_raster = rio.open('LC.tif')
LC_arr = LC_raster.read(1)

Temp_raster = rio.open('Temp.tif')
Temp_arr = Temp_raster.read(1)

fig, ax = plt.subplots(figsize=(12,12))
points.plot(ax=ax, color = 'orangered')
show(DEM_raster, ax = ax)

for index, row in points.iterrows():
    longitude = row['geometry'].x  
    latitude = row['geometry'].y 

    rowIndex, colIndex=DEM_raster.index(longitude,latitude)
    
    points.loc[index, 'DEM'] = DEM_arr[rowIndex, colIndex]
    points.loc[index, 'Slope'] = float(Slope_arr[rowIndex, colIndex])  
    points.loc[index, 'Aspect'] = float(Aspect_arr[rowIndex, colIndex])  
    points.loc[index, 'RD'] = float(RD_arr[rowIndex, colIndex])  
    points.loc[index, 'D2Road'] = float(Droad_arr[rowIndex, colIndex])  
    points.loc[index, 'D2Res'] = float(Dres_arr[rowIndex, colIndex])  
    points.loc[index, 'LC'] = float(LC_arr[rowIndex, colIndex])  
    points.loc[index, 'Temp'] = float(Temp_arr[rowIndex, colIndex])

points.head()
points.to_file('points_data.shp')

每次运行都会针对除DEM外的所有列弹出以下警告:

FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '168.13560485839844' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.

我试过不强制转成float类型,但还是会出现同样的警告。

备注:内容来源于stack exchange,提问作者Phạm Việt Anh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:39:34