【问题标题】:Convert Points to Lines Geopandas将点转换为线 Geopandas
【发布时间】:2018-12-06 20:32:49
【问题描述】:

您好,我正在尝试将 X 和 Y 坐标列表转换为线条。我想通过groupby ID 和时间映射这些数据。只要我grouby 一列,我的代码就会成功执行,但是两列是我遇到错误的地方。我引用了这个question

以下是一些示例数据:

ID  X           Y           Hour
1   -87.78976   41.97658    16
1   -87.66991   41.92355    16
1   -87.59887   41.708447   17
2   -87.73956   41.876827   16
2   -87.68161   41.79886    16
2   -87.5999    41.7083     16
3   -87.59918   41.708485   17
3   -87.59857   41.708393   17
3   -87.64391   41.675133   17

这是我的代码:

df = pd.read_csv("snow_gps.csv", sep=';')

#zip the coordinates into a point object and convert to a GeoData Frame
geometry = [Point(xy) for xy in zip(df.X, df.Y)]
geo_df = GeoDataFrame(df, geometry=geometry)

# aggregate these points with the GrouBy
geo_df = geo_df.groupby(['track_seg_point_id', 'Hour'])['geometry'].apply(lambda x: LineString(x.tolist()))
geo_df = GeoDataFrame(geo_df, geometry='geometry')

这里是错误:ValueError: LineStrings must have at least 2坐标元组

这是我想要得到的最终结果:

ID          Hour     geometry
1           16       LINESTRING (-87.78976 41.97658, -87.66991 41.9... 
1           17       LINESTRING (-87.78964000000001 41.976634999999... 
1           18       LINESTRING (-87.78958 41.97663499999999, -87.6... 
2           16       LINESTRING (-87.78958 41.976612, -87.669785 41... 
2           17       LINESTRING (-87.78958 41.976624, -87.66978 41.... 
3           16       LINESTRING (-87.78958 41.97666, -87.6695199999... 
3           17       LINESTRING (-87.78954 41.976665, -87.66927 41.... 

请任何关于如何按多个参数分组的建议或想法。

【问题讨论】:

  • 您收到的错误消息似乎表明 LineString 正在尝试从单个点创建,但失败了。是否有可能只有一行组成的组?
  • 如果你尝试 x, y 而不是 xy 会怎样?
  • @joris 我不相信这是问题所在?您是否建议不要将 x 和 y 放在单独的列中来加入它们?
  • 如果我只是获取文件df 并运行grouped = df.groupby(['ID', 'Hour']) grouped.groups 这是结果{(0, 16): Int64Index([5, 14, 16, 55, 130], dtype='int64'), (0, 17): Int64Index([7, 27, 126, 141, 185, 235], dtype='int64'),... 除了现在我没有坐标而且我不知道如何将点转换为线?也许我可以索引每个位置以获取它们的 xy 然后转换?
  • 你能用(df.groupby(['ID', 'Hour']).size() < 2).sum()检查它是否肯定给出0?

标签: python pandas geopandas


【解决方案1】:

你的代码很好,问题是你的数据。

您可以看到,如果您按 ID 和 Hour 分组,则只有 1 个点以 ID 1 和 17 小时分组。 LineString 必须由 1 个或多个 Points 组成(必须至少有2个坐标元组)。我在您的示例数据中添加了另一点:

    ID   X          Y           Hour
    1   -87.78976   41.97658    16
    1   -87.66991   41.92355    16
    1   -87.59887   41.708447   17
    1   -87.48234   41.677342   17
    2   -87.73956   41.876827   16
    2   -87.68161   41.79886    16
    2   -87.5999    41.7083     16
    3   -87.59918   41.708485   17
    3   -87.59857   41.708393   17
    3   -87.64391   41.675133   17

正如您在下面看到的,下面的代码几乎与您的相同:

    import pandas as pd
    import geopandas as gpd
    from shapely.geometry import Point, LineString, shape

    df = pd.read_csv("snow_gps.csv", sep='\s*,\s*')

    #zip the coordinates into a point object and convert to a GeoData Frame
    geometry = [Point(xy) for xy in zip(df.X, df.Y)]
    geo_df = gpd.GeoDataFrame(df, geometry=geometry)

    geo_df2 = geo_df.groupby(['ID', 'Hour'])['geometry'].apply(lambda x:                 LineString(x.tolist()))
    geo_df2 = gpd.GeoDataFrame(geo_df2, geometry='geometry')

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-08-27
  • 2021-08-01
  • 1970-01-01
  • 2020-07-30
  • 2020-08-13
  • 2021-08-27
  • 2021-11-11
  • 1970-01-01
相关资源
最近更新 更多