簡體   English   中英

基於 lon、lat 和 0 和 1 的數組使用 rasterio 和 shapely 創建多邊形

[英]Creating polygons using rasterio and shapely based on a lon, lat and an array of 0's and 1's

我有一個 0 和 1 的 numpy 數組。 我正在嘗試將 1 轉換為多邊形。 我已經設法使用 rasterio 和 shapely 來做到這一點,如下面的代碼所示:

im = np.array([[0, 0, 0, 0, 0],
   [0, 1, 1, 1, 0],
   [0, 0, 1, 0, 0],
   [0, 1, 1, 1, 0],
   [0, 0, 0, 0, 0]])
shapes = rasterio.features.shapes(im)
polygons = [shapely.geometry.Polygon(shape[0]["coordinates"][0]) for shape in shapes if shape[1] == 1]
print(polygon[0])

但是,每一行和每一列都表示存儲在不同數組中的經度和緯度坐標。 例如:

lon = np.array([125.  , 125.25, 125.5 , 125.75, 126.  ])
lat = np.array([-35.  , -35.25, -35.5 , -35.75, -36.  ])

有誰知道如何創建與正確坐標關聯的多邊形? 我想我必須使用 rasterio.features.shapes 函數的變換參數。 然而我還沒有弄清楚。

找到了解決方案。 確實需要轉換參數。 該參數是仿射變換,定義為:

xres = (max(lon) - min(lon))/len(lon)
yres = (lat[-1] - lat[0])/len(lat)

transform1 = Affine.translation(min(lon) - xres / 2, lat[0] - yres / 2) * Affine.scale(xres, yres)

可用於創建具有光柵和勻稱的多邊形

shapes = rasterio.features.shapes(im, transform = transform1)
polygons = [shapely.geometry.Polygon(shape[0]["coordinates"][0]) for shape in shapes if shape[1] == 1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM