使用 GeoPandas 将点几何转换为线串 [英] Convert Point Geometries to Linestrings with GeoPandas

查看:80
本文介绍了使用 GeoPandas 将点几何转换为线串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的地理数据框 gdf:

I have a geodataframe gdf that looks like this:

        longitude   latitude    geometry
8628    4.890683    52.372383   POINT (4.89068 52.37238)
8629    4.890500    52.371433   POINT (4.89050 52.37143)
8630    4.889217    52.369469   POINT (4.88922 52.36947)
8631    4.889300    52.369415   POINT (4.88930 52.36942)
8632    4.889100    52.368683   POINT (4.88910 52.36868)
8633    4.889567    52.367416   POINT (4.88957 52.36742)
8634    4.889333    52.367134   POINT (4.88933 52.36713)

我试图将这些点几何图形转换为一条线.但是,下面的代码给出了一个错误:AttributeError: 'Point' object has no attribute 'values'

I was trying to convert these point geometries into a line. However, the following code below gives an error: AttributeError: 'Point' object has no attribute 'values'

line_gdf = gdf['geometry'].apply(lambda x: LineString(x.values.tolist()))
line_gdf = gpd.GeoDataFrame(line_gdf, geometry='geometry')

有什么想法吗?

推荐答案

当您从地理数据框中的所有点创建 LineString 时,您只会得到 1 条线.以下是您可以运行以创建 LineString 的代码:

When you create a LineString from all Points in a geodataframe, you get only 1 line. Here is the code you can run to create the LineString:

from shapely.geometry import LineString

# only relevant code here
# use your gdf that has Point geometry
lineStringObj = LineString( [[a.x, a.y] for a in gdf.geometry.values] )

如果您需要以该线串作为其几何图形的 1 行地理数据框,请继续:

If you need a geodataframe of 1 row with this linestring as its geometry, proceed with this:

import pandas as pd
import geopandas as gpd

line_df = pd.DataFrame()
line_df['Attrib'] = [1,]
line_gdf = gpd.GeoDataFrame(line_df, geometry=[lineStringObj,])

这篇关于使用 GeoPandas 将点几何转换为线串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆