将点捕捉到形状中最近点处的多边形/线
我有一个点层(点)和一个线层(子网格),它是将多边形边界转换为线。我希望这些点捕捉到线上最近的点(如果在 100m 之内),这样我就可以使用空间连接从中获取属性。
我尝试过使用 shapely.ogr.snap 但它非常关闭,就像这样
绿点应该捕捉到右侧而不是左侧的多边形 我尝试遵循涉及插值/项目的其他答案,但我得到“AttributeError:'GeoDataFrame'对象没有属性'_geom'”。我需要使用 wkt.loads 或 unary_union 吗?到目前为止,这些对我来说都失败了。
point4 = point3.copy()point4['geometry'] = point4['geometry'].astype(str).apply(wkt.loads)
point4 = point4.set_geometry(col='geometry')
subgrid2 = geopandas.read_file(f"id_{545}.gpkg")
subgrid2['gridcell'] = subgrid2.id.astype(int)
subgrid2 = subgrid2[['gridcell', 'geometry']]
subgrid2.set_geometry('geometry', inplace=True)
subgrid2 = subgrid2.to_crs(epsg=2278)
subgrid2['geometry'] = subgrid2.geometry.astype(str).apply(wkt.loads)
#subgrid2 = subgrid2.unary_union
wkt.loads(str(subgrid2.iloc[0, 1])))[2].wkt
#geopandas.GeoDataFrame(subgrid2.interpolate(subgrid2.project(point4.geometry))).to_csv("fff.csv")
I have a point layer (point) and a line layer (subgrid) which is polygon boundaries converted to lines. I want the points to snap to the nearest point on the line (if within 100m) so I can fetch attributes from them using a spatial join.
I have tried using shapely.ogr.snap but it is very off, like this
green point should be snapping to polygon on the right, not left
I have tried following other answers involving interpolate/project but I get "AttributeError: 'GeoDataFrame' object has no attribute '_geom'". Do I need to use wkt.loads or unary_union? so far these have failed for me.
point4 = point3.copy()point4['geometry'] = point4['geometry'].astype(str).apply(wkt.loads)
point4 = point4.set_geometry(col='geometry')
subgrid2 = geopandas.read_file(f"id_{545}.gpkg")
subgrid2['gridcell'] = subgrid2.id.astype(int)
subgrid2 = subgrid2[['gridcell', 'geometry']]
subgrid2.set_geometry('geometry', inplace=True)
subgrid2 = subgrid2.to_crs(epsg=2278)
subgrid2['geometry'] = subgrid2.geometry.astype(str).apply(wkt.loads)
#subgrid2 = subgrid2.unary_union
wkt.loads(str(subgrid2.iloc[0, 1])))[2].wkt
#geopandas.GeoDataFrame(subgrid2.interpolate(subgrid2.project(point4.geometry))).to_csv("fff.csv")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 geopandas.sjoin_nearest 查找 100 以内最近的线路米。
shapely.ops.nearest_points 查找上最近的点最近的线路。
然后 shapely.ops.snap 到它:
最左边和最右边的点被捕捉到线条。其余的都不是因为距离> 100米。
Use geopandas.sjoin_nearest to find nearest line within 100 m.
shapely.ops.nearest_points to find the nearest point on the nearest line.
Then shapely.ops.snap to it:
The leftmost and rightmost points are snapped to the lines. The rest are not because the distance > 100 m.