简体   繁体   English

如何从 python 中的 bbox 数据创建多边形?

[英]How to create polygon from bbox data in python?

I have created a code in R which extracts bbox from a list of points and then creates a polygon using st_as_sfc .我在 R 中创建了一个代码,它从点列表中提取bbox ,然后使用st_as_sfc创建一个多边形。 Now I am trying to do the same in python where I was able to get the bbox coordinates from the list of points data and then tried to create polygon using the two points by help of shapely.geometry which is throwing error.现在我试图在 python 中做同样的事情,在那里我能够从点数据列表中获取bbox坐标,然后尝试借助抛出错误的shapely.geometry使用这两个点创建多边形。 Is there any alternative for st_as_sfc in python or how can I create a polygon from the obtained bbox coordinates? st_as_sfc中是否有 st_as_sfc 的替代方案,或者我如何根据获得的bbox坐标创建多边形?

R code:- R 代码:-

print(st_as_sfc(st_bbox(sgrid)))
result:-
Geometry set for 1 feature 
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -9574057 ymin: 3590448 xmax: -9494057 ymax: 3670448
Projected CRS: WGS 84 / Pseudo-Mercator
POLYGON ((-9574057 3590448, -9494057 3590448, -...

Python:- data frame from bbox coordinates xmin,ymin and xmax,ymax Python:- 来自 bbox 坐标 xmin、ymin 和 xmax、ymax 的数据框

from shapely import geometry
print(df)
              x             y                               geometry
0  3.418932e+06 -2.088506e+07  POINT (-20885056.99629 3418931.63321)
1  3.478932e+06 -2.082506e+07  POINT (-20825056.99629 3478931.63321)
poly = geometry.Polygon([[p.x, p.y] for p in gdf_p['geometry'].tolist()])
print(poly.wkt)  

error:-错误:-

ValueError: A linearring requires at least 4 coordinates.

If you already have a dataframe, you can achieve this through unary_union & envelope :如果你已经有一个 dataframe,你可以通过unary_union & envelope实现:

import pandas as pd
import geopandas as gpd
from io import StringIO

points = '''geometry
POINT (-20885056.99629 3418931.63321)
POINT (-20825056.99629 3478931.63321)'''
df = pd.read_table(StringIO(points))
gdf = gpd.GeoDataFrame(geometry=gpd.GeoSeries.from_wkt(df['geometry']))

print(gdf)
#                             geometry
# 0  POINT (-20885056.996 3418931.633)
# 1  POINT (-20825056.996 3478931.633)

print(gdf.geometry.unary_union.envelope.wkt)
# POLYGON ((-20885056.99629 3418931.63321, -20825056.99629 3418931.63321, -20825056.99629 3478931.63321, -20885056.99629 3478931.63321, -20885056.99629 3418931.63321))

With (minx, miny, maxx, maxy) tuples you can use shapely.geometry.box() :对于(minx, miny, maxx, maxy)元组,您可以使用shapely.geometry.box()

from shapely import geometry
bbox_tuple = gdf.total_bounds
print(bbox_tuple)
# [-20885056.99629   3418931.63321 -20825056.99629   3478931.63321]

bbox_polygon = geometry.box(*bbox_tuple)
print(bbox_polygon.wkt)
# POLYGON ((-20825056.99629 3418931.63321, -20825056.99629 3478931.63321, -20885056.99629 3478931.63321, -20885056.99629 3418931.63321, -20825056.99629 3418931.63321))

No need to first produce point geometries from your coordinates.无需首先从您的坐标生成点几何图形。 Simply provide all 4 corners like so:只需像这样提供所有 4 个角:

poly = geometry.Polygon(((xmin,ymin), (xmin,ymax), (xmax,ymax), (xmax,ymin)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM