使用rasterio创建多边形,并根据lon,lat和0'和1' s的阵列形成整齐地创建多边形

发布于 2025-01-28 11:48:58 字数 630 浏览 4 评论 0原文

我有一个带有0和1的数组。我试图将1转换为多边形。我已经设法使用rasterio进行操作,并像下面的代码中那样全身:

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.features.shapes函数的转换参数。但是我还没有弄清楚。

I have an numpy array with 0 and 1's. I am trying to convert the 1's into polygons. I have managed to do so using rasterio and shapely as seen in the code below:

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])

however, each row and column reffers to longitude and latitude coordinates stored in different arrays. for example:

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

Does anyone know how to create the polygons associated with the correct coordinates? I think I have to use the transform parameter of the rasterio.features.shapes function. Yet I haven't been able to figure it out yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2025-02-04 11:48:58

找到了解决方案。确实需要转换参数。此参数为仿射截止性,定义为:

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)

可用于用rasterio创建多边形和摇摆

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

Found the solution. The transform parameter indeed is needed. This parameter is and affine trasnformation which is defined as:

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)

that can be used to create the polygons with rasterio and shapely

shapes = rasterio.features.shapes(im, transform = transform1)
polygons = [shapely.geometry.Polygon(shape[0]["coordinates"][0]) for shape in shapes if shape[1] == 1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文