从地geopandas dataFrame矢量化创建了外形多边形

发布于 2025-02-06 04:48:51 字数 610 浏览 2 评论 0原文

我有一个带有点几何形状的GeodataFrame。 从点几何形状,我想以非常简单的方式定义一个方形多边形几何形状。

鉴于一个点,该点应为正方形的左下角,侧面为250个长度。 即,左下角是当前点,右下角是x轴上的当前点 + 250等。

我的天真方法是这样做: 在GeodataFrame中创建作为新列的角落:

之后,我尝试将新列定义为:

gdf['POLY'] = shapely.Geometry([gdf['BOTTOM_LEFT'], gdf['BOTTOM_RIGHT'], gdf['TOP_LEFT'], gdf['TOP_RIGHT']])

但是,这返回以下错误消息:

AttributeError: 'list' object has no attribute '__array_interface__'

I have a GeoDataFrame with a point geometry.
From the point geometry, I want to define a square polygon geometry in a quite straightforward manner.

Given a point, the point should be the left bottom corner in a square with sides 250 units of length.
I.e, left bottom corner is the current point, right bottom corner is the current point + 250 on the x axis etc.

My naive way of doing this is the following:
Create the corners as new columns in the GeoDataFrame:
enter image description here

After that, I try to define a new columns as:

gdf['POLY'] = shapely.Geometry([gdf['BOTTOM_LEFT'], gdf['BOTTOM_RIGHT'], gdf['TOP_LEFT'], gdf['TOP_RIGHT']])

But this returns the following error message:

AttributeError: 'list' object has no attribute '__array_interface__'

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

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

发布评论

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

评论(3

梦在夏天 2025-02-13 04:48:51

您的实现是接近的,但是您无法将Shapely.deometry.polygon带有一系列点 - 只能一次完成。因此,诀窍是使用 在DataFrame的每一行上调用Polygon:

gdf['geometry'] = gdf.apply(
    lambda s: shapely.geometry.Polygon(
        [s['BOTTOM_LEFT'], s['BOTTOM_RIGHT'], s['TOP_LEFT'], s['TOP_RIGHT']],
        axis=1,
    )
)

您可以使用 translate

gdf['geometry'] = gdf.apply(
    lambda s: shapely.geometry.Polygon(
        [
            s['POINT'],
            s['POINT'].translate(xoff=250),
            s['POINT'].translate(yoff=250, xoff=250),
            s['POINT'].translate(yoff=250),
        ],
        axis=1,
    )
)

Your implementation is close, but you can't call shapely.geometry.Polygon with an array of points - it can only be done one at a time. So the trick is to use df.apply to call Polygon on every row of the DataFrame:

gdf['geometry'] = gdf.apply(
    lambda s: shapely.geometry.Polygon(
        [s['BOTTOM_LEFT'], s['BOTTOM_RIGHT'], s['TOP_LEFT'], s['TOP_RIGHT']],
        axis=1,
    )
)

You could do that with your original point using translate:

gdf['geometry'] = gdf.apply(
    lambda s: shapely.geometry.Polygon(
        [
            s['POINT'],
            s['POINT'].translate(xoff=250),
            s['POINT'].translate(yoff=250, xoff=250),
            s['POINT'].translate(yoff=250),
        ],
        axis=1,
    )
)
ˇ宁静的妩媚 2025-02-13 04:48:51

假设您只有一个单点的GeodataFrame。它称为gdf ​​,看起来如下:

X   Y   geometry
0   5   6   POINT (5.00000 6.00000)

您可以使用以下lambda函数访问点的x和y组件:

#Access x and y components of point geometry
X = gdf.geometry.apply(lambda x: x.x)

Y = gdf.geometry.apply(lambda x: x.y)

现在您可以使用shapely.polygon.polygon创建一个方形对象。。您需要指定广场的四个顶点。您可以使用:

gdf_square = shapely.geometry.Polygon([[X[0], Y[0]],
                                      [X[0]+250, Y[0]],
                                     [X[0]+250, Y[0]+250],
                                     [X[0], Y[0]+250]])

您可以获取一个方形多边形对象,如下所示:

请注意,如果您在GeodataFrame中有很多点,请修改最后一个函数,以使其创建平方多边形,以逐步逐一为点。

Let's assume you have a GeoDataFrame with only single point. It is called gdf and it looks as follows:

X   Y   geometry
0   5   6   POINT (5.00000 6.00000)

You can access the x and y components of the point using the following lambda function:

#Access x and y components of point geometry
X = gdf.geometry.apply(lambda x: x.x)

Y = gdf.geometry.apply(lambda x: x.y)

Now you can create a square object using shapely.geometry.Polygon. You need to specify the four vertices of the square. You can do it using:

gdf_square = shapely.geometry.Polygon([[X[0], Y[0]],
                                      [X[0]+250, Y[0]],
                                     [X[0]+250, Y[0]+250],
                                     [X[0], Y[0]+250]])

You can get a square polygon object as shown below:
enter image description here

Note that if you have many points in the GeoDataFrame, modify the last function such that it creates the square polygon for point in each row one by one.

梦冥 2025-02-13 04:48:51

就我而言,使用列表理解构建三角形的速度比使用geopandas。

polys = [Polygon(((x, y), (x, y+d), (x+d, y+d), (x+d, y))) for x in xs for y in ys]

gdf = gpd.GeoDataFrame(geometry=polys)

In my case it was more than 5 times faster to build the triangles using list comprehension than using geopandas.apply :

polys = [Polygon(((x, y), (x, y+d), (x+d, y+d), (x+d, y))) for x in xs for y in ys]

gdf = gpd.GeoDataFrame(geometry=polys)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文