我有一个栅格和一个Shapefile,其中包含许多多边形(建筑物轮廓)。 Shapefile中的每个多边形都有一个关联的值(损坏级别为0-3)。我使用rasterio.mask.mask创建了一个被遮罩的栅格,并将输出转换为包含被遮罩的栅格像素的中心点的纬度/经度坐标,栅格像素的关联值(相干性)的地理数据框(称为“ d”)值,“ cc”)。但是,我希望最终的地理数据框包含用于创建遮罩的Shapefile中的损伤级别和多边形几何。我该怎么做?我制作遮罩的代码如下(基于https://gis.stackexchange.com/questions/260304/extract-raster-values-within-shapefile-with-pygeoprocessing-or-gdal):
import rasterio as rio
cc_path=(r'coherenceND_cut.tif')
cc_data=rio.open(cc_path)
out_image, out_transform=rio.mask.mask(cc_data, shapefile['geometry'], all_touched=True, nodata=999, crop=True)
out_meta = cc_data.meta
nodata=999
# extract the values of the masked array
extractdata = out_image[0,:,:]
# extract the row, columns of the valid values
import numpy as np
row, col = np.where(extractdata != nodata)
cc = np.extract(extractdata != nodata, extractdata)
from rasterio import Affine # or from affine import Affine
T1 = out_transform * Affine.translation(0.5, 0.5) # reference the pixel centre
rc2xy = lambda r, c: (c, r) * T1
d = gpd.GeoDataFrame({'col':col,'row':row,'cc':cc})
# coordinate transformation
d['x'] = d.apply(lambda row: rc2xy(row.row,row.col)[0], axis=1)
d['y'] = d.apply(lambda row: rc2xy(row.row,row.col)[1], axis=1)
# geometry
from shapely.geometry import Point
d['geometry'] =d.apply(lambda row: Point(row['x'], row['y']), axis=1)
输出数据帧(d)的前2行如下:
col row cc x y
222 4 -0.0102040805 13.29583338772 42.77888886942
但是它希望它看起来像这样:
col row cc x y damage level polygon
222 4 -0.0102040805 13.29583338772 42.77888886942 1 ((13.23572745800004 42.73806166700007 0, 13.23560465900005 42.73804756200008 0, 13.23560623600002 42.73808437900004 0, 13.23572871300007 42.73808739300006 0, 13.23572785700003 42.73806985400006 0, 13.23572745800004 42.73806166700007 0))