繁体   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