【发布时间】:2023-04-05 14:04:02
【问题描述】:
我有 2 个数据框,第一个有线串,第二个有很多点。
我想找到最接近线串的点。我尝试了一些东西,但我想它不起作用。我该怎么做?
这是我的代码:
def find_keybyvalue (value,dict):
for s,d in dict.items():
if value==d:
return s
liste={}
lis={}
for i in range(first.count()):
for n in range(second.count()):
point = second.geometry[n]
liste[n]=point
if len(liste)!=0:
print(liste)
for a, p1 in liste.items():
line = shapely.wkt.loads(first.linstring[i])
p2 = nearest_points(line, p1)[0]
distance = math.sqrt(((p1.x-p2.x)**2)+((p1.y-p2.y)**2))
lis[a]=distance
b=min(lis.values())
print
这是第一个数据帧的示例:
{'lin': {0: 'LINESTRING (32.85619 39.868728, 32.856321 39.868657, 32.856477 39.868615)',
1: 'LINESTRING (32.85619 39.868728, 32.856321 39.868657, 32.856477 39.868615)',
2: 'LINESTRING (32.85619 39.868728, 32.856321 39.868657, 32.856477 39.868615)',
3: 'LINESTRING (32.882227 39.550864, 32.880966 39.551685)',
4: 'LINESTRING (32.882227 39.550864, 32.880966 39.551685)',
5: 'LINESTRING (32.692448 39.885163, 32.692012 39.885222)',
6: 'LINESTRING (32.86239 39.900284, 32.862381 39.900456)',
7: 'LINESTRING (32.812381 39.878539, 32.81255 39.879431)',
8: 'LINESTRING (32.812381 39.878539, 32.81255 39.879431)',
9: 'LINESTRING (32.812381 39.878539, 32.81255 39.879431)',
10: 'LINESTRING (32.712708 40.404688, 32.711973 40.404542, 32.711355 40.404252, 32.710675 40.403367, 32.71017 40.402972, 32.707958 40.402167, 32.705775 40.40114, 32.704602 40.400377, 32.703107 40.399538, 32.7023 40.399137, 32.701442 40.398798, 32.699727 40.398285, 32.698995 40.397953, 32.69837 40.39751, 32.697003 40.396632, 32.696455 40.396157, 32.69596 40.395658, 32.695558 40.395147, 32.695092 40.394375, 32.694597 40.394143, 32.693903 40.394207, 32.693316 40.394351)',
11: 'LINESTRING (32.651653 39.850165, 32.652376 39.850618)',
12: 'LINESTRING (32.678197 39.867385, 32.678069 39.867217)',
13: 'LINESTRING (32.744751 39.782534, 32.744926 39.786808)',
14: 'LINESTRING (32.744751 39.782534, 32.744926 39.786808)',
15: 'LINESTRING (32.887934 39.952421, 32.887946 39.952489)',
16: 'LINESTRING (32.975037 39.971029, 32.974902 39.971053)',
17: 'LINESTRING (32.975037 39.971029, 32.974902 39.971053)',
18: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
19: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
20: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
21: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
22: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
23: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
24: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
25: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
26: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
27: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
28: 'LINESTRING (32.871431 39.979258, 32.871111 39.979476, 32.87096 39.979521)',
29: 'LINESTRING (32.871431 39.979258, 32.871111 39.979476, 32.87096 39.979521)',
30: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
31: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
32: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
33: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
34: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
35: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
36: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
37: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
38: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
39: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
40: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
41: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
42: 'LINESTRING (32.724833 39.996181, 32.724756 39.995943)',
43: 'LINESTRING (32.71277988 39.80887704, 32.71290084 39.80894598, 32.71340196 39.80906001, 32.71361688 39.80909799)',
44: 'LINESTRING (32.76150192 40.014702, 32.76157284 40.01517603)',
45: 'LINESTRING (32.76150192 40.014702, 32.76157284 40.01517603)',
46: 'LINESTRING (32.76150192 40.014702, 32.76157284 40.01517603)',
47: 'LINESTRING (32.707125 40.00806801, 32.70731796 40.00878099, 32.70739392 40.00901598, 32.70750588 40.00922901, 32.70763692 40.00942998, 32.7078 40.00960602, 32.70797604 40.00976901, 32.70823884 40.00998303, 32.70883896 40.010355, 32.70925188 40.010661, 32.70963708 40.01099796, 32.71014 40.01150403, 32.71041792 40.01175396, 32.71069404 40.01193801, 32.71094784 40.01206698, 32.71131396 40.01221602, 32.71158 40.012299, 32.71187592 40.01235903, 32.71401 40.012416)',
48: 'LINESTRING (32.707125 40.00806801, 32.70731796 40.00878099, 32.70739392 40.00901598, 32.70750588 40.00922901, 32.70763692 40.00942998, 32.7078 40.00960602, 32.70797604 40.00976901, 32.70823884 40.00998303, 32.70883896 40.010355, 32.70925188 40.010661, 32.70963708 40.01099796, 32.71014 40.01150403, 32.71041792 40.01175396, 32.71069404 40.01193801, 32.71094784 40.01206698, 32.71131396 40.01221602, 32.71158 40.012299, 32.71187592 40.01235903, 32.71401 40.012416)',
49: 'LINESTRING (32.707125 40.00806801, 32.70731796 40.00878099, 32.70739392 40.00901598, 32.70750588 40.00922901, 32.70763692 40.00942998, 32.7078 40.00960602, 32.70797604 40.00976901, 32.70823884 40.00998303, 32.70883896 40.010355, 32.70925188 40.010661, 32.70963708 40.01099796, 32.71014 40.01150403, 32.71041792 40.01175396, 32.71069404 40.01193801, 32.71094784 40.01206698, 32.71131396 40.01221602, 32.71158 40.012299, 32.71187592 40.01235903, 32.71401 40.012416)'}}
这是第二个数据帧的示例
{'geometry': {0: <shapely.geometry.point.Point at 0x1ca1d6026c8>,
1: <shapely.geometry.point.Point at 0x1ca1d602248>,
2: <shapely.geometry.point.Point at 0x1ca1d602bc8>,
3: <shapely.geometry.point.Point at 0x1ca1d602548>,
4: <shapely.geometry.point.Point at 0x1ca1d602388>,
5: <shapely.geometry.point.Point at 0x1ca1d602208>,
6: <shapely.geometry.point.Point at 0x1ca1d602fc8>,
7: <shapely.geometry.point.Point at 0x1ca1d602648>,
8: <shapely.geometry.point.Point at 0x1ca1d6023c8>,
9: <shapely.geometry.point.Point at 0x1ca1d602788>,
10: <shapely.geometry.point.Point at 0x1ca1d602888>,
11: <shapely.geometry.point.Point at 0x1ca1d602408>,
12: <shapely.geometry.point.Point at 0x1ca1d602808>,
13: <shapely.geometry.point.Point at 0x1ca1d602848>,
14: <shapely.geometry.point.Point at 0x1ca1d602b88>,
15: <shapely.geometry.point.Point at 0x1ca1d602e88>,
16: <shapely.geometry.point.Point at 0x1ca1d602e08>,
17: <shapely.geometry.point.Point at 0x1ca1d602048>,
18: <shapely.geometry.point.Point at 0x1ca1d6020c8>,
19: <shapely.geometry.point.Point at 0x1ca1d602a48>,
20: <shapely.geometry.point.Point at 0x1ca1d602488>,
21: <shapely.geometry.point.Point at 0x1ca1d602f48>,
22: <shapely.geometry.point.Point at 0x1ca1d602ec8>,
23: <shapely.geometry.point.Point at 0x1ca1d6298c8>,
24: <shapely.geometry.point.Point at 0x1ca1d629f08>,
25: <shapely.geometry.point.Point at 0x1ca1d629988>,
26: <shapely.geometry.point.Point at 0x1ca1d629c48>,
27: <shapely.geometry.point.Point at 0x1ca1d629fc8>,
28: <shapely.geometry.point.Point at 0x1ca1d629a88>,
29: <shapely.geometry.point.Point at 0x1ca1d629b08>,
30: <shapely.geometry.point.Point at 0x1ca1d6296c8>,
31: <shapely.geometry.point.Point at 0x1ca1d629648>,
32: <shapely.geometry.point.Point at 0x1ca1d629f88>,
33: <shapely.geometry.point.Point at 0x1ca1d629e08>,
34: <shapely.geometry.point.Point at 0x1ca1d629688>,
35: <shapely.geometry.point.Point at 0x1ca1d6297c8>,
36: <shapely.geometry.point.Point at 0x1ca1d629dc8>,
37: <shapely.geometry.point.Point at 0x1ca1d629948>,
38: <shapely.geometry.point.Point at 0x1ca1d629908>,
39: <shapely.geometry.point.Point at 0x1ca1d629808>,
40: <shapely.geometry.point.Point at 0x1ca1d629488>,
41: <shapely.geometry.point.Point at 0x1ca1d6299c8>,
42: <shapely.geometry.point.Point at 0x1ca1d629a48>,
43: <shapely.geometry.point.Point at 0x1ca1d6294c8>,
44: <shapely.geometry.point.Point at 0x1ca1d629888>,
45: <shapely.geometry.point.Point at 0x1ca1d629508>,
46: <shapely.geometry.point.Point at 0x1ca1d5e4ec8>,
47: <shapely.geometry.point.Point at 0x1ca1d5e4f88>,
48: <shapely.geometry.point.Point at 0x1ca1d5e4bc8>,
49: <shapely.geometry.point.Point at 0x1ca1d5e4c88>}}
【问题讨论】:
-
请在预期输出中包含您的数据帧样本。
-
您的点数据不可用,是 geopandas 数据框吗?
gdf.geometry.to_json()
将提供可用数据
标签:
python
pandas
dataframe
geometry
geopandas
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何在python中找到离线串最近的点? - Python技术站