在geopandas df中找到所有线串交点的每条线的最近点

如何解决在geopandas df中找到所有线串交点的每条线的最近点

我有一个geopandas数据框,其中包含从纬度,经点数据创建的几个线串。对于所有线相交,我需要找到每个线串中距该相交的最近点。

因此,如果数据框中的两条线相交,则需要最接近每个线串中该点的交点。我已经使用itertools查找了所有可能的相交点,类似于本文中的已接受答案:https://gis.stackexchange.com/questions/137909/intersecting-lines-to-get-crossings-using-python-with-qgis

对于geopandas数据框中的所有线相交点,是否有一种更简单的方法来找到每个线串中最接近交点的点?

我的数据框看起来像这样,每个文件名都包含自己的线串:

                                                            geometry
file                                                            
2015_may14_10  LINESTRING (-140.43855 59.80302,-140.44101 59...
2015_may14_11  LINESTRING (-140.84909 59.83433,-140.84758 59...
2015_may14_12  LINESTRING (-140.66859 59.79890,-140.66600 59...
2015_may14_15  LINESTRING (-140.19642 59.86655,-140.19795 59...
2015_may14_16  LINESTRING (-141.08783 59.94741,-141.08610 59...

解决方法

让我们创建 n 条随机线:

import geopandas as gpd
from shapely.geometry import LineString,Point,Polygon
from shapely import wkt
import numpy as np
xmin,xmax,ymin,ymax = 0,10000,10000
n = 100
xa = (xmax - xmin) * np.random.random(n) + xmin
ya = (ymax - ymin) * np.random.random(n) + ymin
xb = (xmax - xmin) * np.random.random(n) + xmin
yb = (ymax - ymin) * np.random.random(n) + ymin

lines = gpd.GeoDataFrame({'index':range(n),'geometry':[LineString([(a,b),(k,l)]) for a,b,k,l in zip(xa,ya,xb,yb)]})

这给出:

>>> lines
index   geometry
0   0   LINESTRING (4444.630 3081.439,6132.674 5849.463)
1   1   LINESTRING (7015.940 6378.245,4568.386 757.205)
2   2   LINESTRING (8766.417 6070.131,690.359 7511.385)
3   3   LINESTRING (4245.544 4009.196,8496.307 1557.175)
4   4   LINESTRING (1489.436 9364.784,2109.740 5923.480)
...     ...     ...
95  95  LINESTRING (4783.454 7840.857,1935.396 2435.260)
96  96  LINESTRING (1884.455 4982.662,6257.958 3580.912)
97  97  LINESTRING (7072.811 7843.319,4811.589 2486.040)
98  98  LINESTRING (6933.272 6427.046,7528.579 2064.067)
99  99  LINESTRING (3876.400 5183.790,5360.753 1901.207)

让我们得到我们的交点:

res = []
for i in lines.loc[:,'geometry']:
    for j in lines.loc[:,'geometry']:
        inter = i.intersection(j)
        if inter.geom_type != 'LineString':
            res.append(inter)

这里我有点误解,有时 inter = i.intersection(j) 返回一个 LineString 对象,我不知道两条不同的线如何作为交点输出另一条线(除非它们相同) .我把这留给你。

现在,我们可以使用结果点创建我们的 df

points = gpd.GeoDataFrame({'geometry':res})
>>>points

    geometry
0   POINT (4811.366 3682.806)
1   POINT (5149.727 4237.644)
2   POINT (4607.312 3348.202)
3   POINT (6026.639 5675.588)
4   POINT (4514.359 3195.779)
...     ...
2215    POINT (4788.793 3166.070)
2216    POINT (4704.895 3351.608)
2217    POINT (4581.390 3624.734)
2218    POINT (4320.392 4201.921)
2219    POINT (4949.041 2811.691)

2220 rows × 1 columns

我们可以看到我们更多地使用线段而不是纯线,因为交叉点(即点)的数量是 2220。我不同意认为我们很幸运有7880平行线。

然后,我们为操作导入我们最好的朋友:

from shapely.ops import nearest_points

然后我们计算所需的输出:

intersection = []
line = []
my_point = []

for i in points.index:
    for j in lines.index:
        intersection.append(points.loc[i,'geometry'])
        line.append(lines.loc[j,'geometry'])
        my_point.append([p.wkt for p in nearest_points(points.loc[i,'geometry'],lines.loc[j,'geometry'])][1])


result = gpd.GeoDataFrame({'intersection':intersection,'line':line,'nearest_point':my_point})

result.geometry = result.loc[:,'nearest_point'].apply(wkt.loads)
result.drop(columns=['nearest_point'],inplace=True)

>>>result

intersection    line    geometry
0   POINT (4811.365980053641 3682.805619834874)     LINESTRING (4444.630325108094 3081.43918610815...   POINT (4811.366 3682.806)
1   POINT (4811.365980053641 3682.805619834874)     LINESTRING (7015.939846319573 6378.24453843603...   POINT (5677.967 3305.464)
2   POINT (4811.365980053641 3682.805619834874)     LINESTRING (8766.416847858662 6070.13073873083...   POINT (5346.331 6680.480)
3   POINT (4811.365980053641 3682.805619834874)     LINESTRING (4245.544341245415 4009.19558793877...   POINT (4811.366 3682.806)
4   POINT (4811.365980053641 3682.805619834874)     LINESTRING (1489.4355376526 9364.784164867619,...   POINT (2109.740 5923.480)
...     ...     ...     ...
221995  POINT (4949.040525093341 2811.690701237854)     LINESTRING (4783.453909575222 7840.85687296287...   POINT (2745.435 3972.709)
221996  POINT (4949.040525093341 2811.690701237854)     LINESTRING (1884.454611847149 4982.66168904636...   POINT (5294.551 3889.693)
221997  POINT (4949.040525093341 2811.690701237854)     LINESTRING (7072.811488307434 7843.31900543939...   POINT (4949.041 2811.691)
221998  POINT (4949.040525093341 2811.690701237854)     LINESTRING (6933.272054846982 6427.04550331467...   POINT (7381.288 3143.559)
221999  POINT (4949.040525093341 2811.690701237854)     LINESTRING (3876.399925481877 5183.78974899146...   POINT (4949.041 2811.691)

222000 rows × 3 columns

希望这能回答您的问题,如果您有更好的答案,请告诉我。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-