【问题标题】:Linestring end coordinates in a .csv file along with source and target id.csv 文件中的线串结束坐标以及源和目标 ID
【发布时间】:2022-01-04 05:59:50
【问题描述】:

我有一个数字路线图数据集,其中包含通过道路网络互连的节点坐标和节点编号。 Dataset in xlsx 数据集包含三列:Col1-**source **、Colm 2 Target 和 Column 3-geometry。几何是具有起点坐标、终点坐标和少量中间点坐标的道路线串。 SourceTarget 列是每个路网的起始节点和结束节点的节点编号。 我只想从每一行中提取起始节点和结束节点的坐标。然后排列过滤后的数据集,使每个源和每个目标旁边都有各自的坐标。 示例输出文件是 desired sample output

我正在寻找匀称的代码,大部分信息都在一个线串上。由于我的数据有超过一百万行,所以我无法找到遍历整个数据集的相关代码。

【问题讨论】:

  • 完全不清楚你在问什么。看起来您想从字符串几何中提取源和目标。但从图片中,尚不清楚源和目标是如何包含在几何体中的。您想要实现什么确切的目标以及给定输入的预期输出是什么?
  • @brb 感谢您的评论。原始数据是关于一个城市的数字路线图形式的道路网络的详细信息。其中每条道路都有一个起点坐标、一个终点坐标和几个中间坐标。我想以仅提取起始坐标和结束坐标的方式过滤数据。(我将使用不同的图像集更新我的问题)
  • 不清楚你想要什么。如果几何包含两个用逗号分隔的坐标,并且您想分配给变量源和目标,则下面的 cookie 解决方案将起作用。只需从几何中删除“LINESTRING”、“(”和“)”并在“,”上分割。然后分配给源和目标而不是开始和结束,你就可以了。但是如果想象给出了预期的输出,那么源和目标与几何的关系就不清楚了。例如,你怎么做 source= 67173 and target= 67196 from geometry= LINESTRING(139.9164... 36.6825... , 139.9164... 36.6822...)?
  • 如果下面的解决方案不是答案,那么您需要给出一个单一的输入并解释您想要从中得到什么输出以及这些输出是如何得出的。
  • 再次感谢。 @brb 再次感谢。同时我尝试了这个并从 Linestring 获得端点:'code' for i, row in nodes_raw.iterrows(): # print(type(row['geometry'])) line = shapely.wkt.loads(row['geometry ']) # print(type(line)) endpoints = line.boundary print(row['source'], '\t', row['target'], '\t', endpoints) if i > 100: break
    到目前为止它工作正常。

标签: pandas dataframe geopandas shapely multilinestring


【解决方案1】:

你的意思是:

df[['Start', 'end']] = df['geometry'].str.split(',', expand=True)

【讨论】:

  • 谢谢。我之前尝试过这个来拆分数据。但它仅有助于拆分列“几何”单元格中的值。我必须以“源”获取第一个坐标而“目标”获取最后一个坐标的方式排列数据。
【解决方案2】:
  • 您的示例数据无法使用,因为它是图像。模拟了一些
  • LINESTRING中选择第一个和最后一个点
  • 结构为列(df
  • df 重塑为 df2 为您想要的结构
import io
import shapely.geometry, shapely.wkt
import pandas as pd
import numpy as np

# sample data...
df = pd.read_csv(
    io.StringIO(
        '''source,target,geometry
0,100,"LINESTRING (5.897759230176348 49.44266714130711, 6.242751092156993 49.90222565367873, 5.674051954784829 49.5294835475575)"
1,101,"LINESTRING (13.59594567226444 48.87717194273715, 12.51844038254671 54.470370591848, 6.658229607783568 49.20195831969157)"
2,102,"LINESTRING (16.71947594571444 50.21574656839354, 23.42650841644439 50.30850576435745, 22.77641889821263 49.02739533140962, 14.60709842291953 51.74518809671997)"
3,103,"LINESTRING (18.62085859546164 54.68260569927078, 23.79919884613338 52.69109935160657, 20.89224450041863 54.31252492941253)"
4,104,"LINESTRING (5.606975945670001 51.03729848896978, 6.589396599970826 51.85202912048339, 3.31501148496416 51.34577662473805, 5.988658074577813 51.85161570902505)"
5,105,"LINESTRING (4.799221632515724 49.98537303323637, 6.043073357781111 50.12805166279423, 3.31501148496416 51.34577662473805, 6.15665815595878 50.80372101501058, 3.314971144228537 51.34575511331991)"
6,106,"LINESTRING (3.31501148496416 51.34577662473805, 3.830288527043137 51.62054454203195, 6.905139601274129 53.48216217713065, 4.705997348661185 53.09179840759776)"
7,107,"LINESTRING (7.092053256873896 53.14404328064489, 3.830288527043137 51.62054454203195, 6.842869500362383 52.22844025329755, 3.31501148496416 51.34577662473805)"
8,108,"LINESTRING (6.589396599970826 51.85202912048339, 6.905139601274129 53.48216217713065, 3.314971144228537 51.34575511331991, 5.988658074577813 51.85161570902505)"
9,109,"LINESTRING (5.606975945670001 51.03729848896978, 4.286022983425084 49.90749664977255)"'''
    )
)

# pick first and last point from each linestring as columns
df = df.join(
    df["geometry"]
    .apply(lambda ls: np.array(shapely.wkt.loads(ls).coords)[[0, -1]])
    .apply(
        lambda x: {
            f"{c}_point": shapely.geometry.Point(x[i])
            for i, c in enumerate(df.columns)
            if c != "geometry"
        }
    )
    .apply(pd.Series)
)

# reshape to row wise
df2 = pd.melt(
    df,
    id_vars=["source", "target"],
    value_vars=["source_point", "target_point"],
    value_name="point",
)
df2["node_number"] = np.where(
    df2["variable"] == "source_point", df2["source"], df2["target"]
)
df2 = df2.drop(columns=["source", "target", "variable"])

输出

point node_number
POINT (5.897759230176348 49.44266714130711) 0
POINT (13.59594567226444 48.87717194273715) 1
POINT (16.71947594571444 50.21574656839354) 2
POINT (18.62085859546164 54.68260569927078) 3
POINT (5.606975945670001 51.03729848896978) 4
POINT (4.799221632515724 49.98537303323637) 5
POINT (3.31501148496416 51.34577662473805) 6
POINT (7.092053256873896 53.14404328064489) 7
POINT (6.589396599970826 51.85202912048339) 8
POINT (5.606975945670001 51.03729848896978) 9
POINT (5.674051954784829 49.5294835475575) 100
POINT (6.658229607783568 49.20195831969157) 101
POINT (14.60709842291953 51.74518809671997) 102
POINT (20.89224450041863 54.31252492941253) 103
POINT (5.988658074577813 51.85161570902505) 104
POINT (3.314971144228537 51.34575511331991) 105
POINT (4.705997348661185 53.09179840759776) 106
POINT (3.31501148496416 51.34577662473805) 107
POINT (5.988658074577813 51.85161570902505) 108
POINT (4.286022983425084 49.90749664977255) 109

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多