2 回答

TA貢獻(xiàn)1796條經(jīng)驗 獲得超7個贊
這與提取多邊形有點不同,因為您想要按照觸摸的順序?qū)€觸摸的每個像素進(jìn)行采樣(多邊形方法不關(guān)心像素順序)。
看起來可以改用這種方法來rasterio
代替使用。geopandas
給定使用或fiona
作為對象從 shapefile 中讀取的一條線shapely
,您可以使用端點導(dǎo)出一個新的等距投影,您可以dst_crs
在WarpedVRT中使用該投影并從中讀取像素值。看起來你需要根據(jù)你想要采樣的像素數(shù)來計算你的線的長度,這是WarpedVRT
.
如果您的線不是端點之間的近似直線,則可能需要進(jìn)一步調(diào)整此方法。
如果您只想獲取線下的原始像素值,您應(yīng)該能夠為每條線使用遮罩rasterio
或直接柵格化。對于線條,您可能想要使用all_touched=True
。

TA貢獻(xiàn)1799條經(jīng)驗 獲得超6個贊
我遇到了類似的問題,并找到了適合我的解決方案。該解決方案用于shapely對一條/多條線上的點進(jìn)行采樣,然后從 GeoTiff 訪問相應(yīng)的值,因此提取的配置文件遵循線的方向。這是我最終得到的方法:
def extract_along_line(xarr, line, n_samples=256):
profile = []
for i in range(n_samples):
# get next point on the line
point = line.interpolate(i / n_samples - 1., normalized=True)
# access the nearest pixel in the xarray
value = xarr.sel(x=point.x, y=point.y, method="nearest").data
profile.append(value)
return profile
這是一個使用數(shù)據(jù)庫中的數(shù)據(jù)的工作示例,copernicus-dem該線是接收到的圖塊的對角線:
import rioxarray
import shapely.geometry
import matplotlib.pyplot as plt
sample_tif = ('https://elevationeuwest.blob.core.windows.net/copernicus-dem/'
'COP30_hh/Copernicus_DSM_COG_10_N35_00_E138_00_DEM.tif')
# Load xarray
tile = rioxarray.open_rasterio(sample_tif).squeeze()
# create a line (here its the diagonal of tile)
line = shapely.geometry.MultiLineString([[
[tile.x[-1],tile.y[-1]],
[tile.x[0], tile.y[0]]]])
# use the method from above to extract the profile
profile = extract_along_line(tile, line)
plt.plot(profile)
plt.show()
添加回答
舉報