简体   繁体   English

如果到下一个点的距离大于给定阈值,则将 LineString 拆分为多个 LineString

[英]Split LineString into several LineStrings if distance to next Point is > than a given threshold

I have a shapely Point list:我有一个匀称的点列表:

0     POINT (527644.217 5340266.216)
11    POINT (527644.921 5340266.268)
22    POINT (527645.889 5340266.246)
34    POINT (527646.423 5340266.200)
45    POINT (527646.979 5340266.127)
...

and created a LineString.并创建了一个 LineString。

用箭头显示的太长距离示例

The arrow in the picture shows the example of a too long distance between two Points.图中的箭头显示了两点之间距离过长的示例。

I tried to go through a loop and create a new LineString if the distance to the next point is too long.如果到下一个点的距离太长,我试图通过一个循环并创建一个新的 LineString But it does not do the right thing.但它没有做正确的事情。

liness=list()
start=0

for i in range(0,len(gdf.geometry)-1):

  dist=gdf.geometry.iloc[i].distance(gdf.geometry.iloc[i+1])

  if dist > line_tresh:
   
   #List of Points which are too far away
   points_too_far_away.append(LineString([gdf.geometry.iloc[i],gdf.geometry.iloc[i+1]]))
   
   #list of new separated LineStrings 
   liness.append(LineString(gdf.geometry[start:i-1].tolist()))
   

   start=i

Is there any better way to get the solution?有没有更好的方法来获得解决方案?

You can try to use the apply method with shapely 's distance function.您可以尝试将apply方法与shapelydistance函数一起使用。 Here's an example:下面是一个例子:

# Setting up example 
import pandas as pd
import shapely

df = pd.DataFrame({'id':range(5),
                   'wkt':['POINT (527644.217 5340266.216)',
                          'POINT (527644.921 5340266.268)',
                          'POINT (527645.889 5340266.246)',
                          'POINT (527646.423 5340266.200)',
                          'POINT (527646.979 5340266.127)']})

df['geom'] = df['wkt'].apply(shapely.wkt.loads)

# Adding columns for distance calculation
df['geom_2'] = df['geom'].shift(-1)

def my_dist(in_row):
    return in_row['geom'].distance(in_row['geom_2'])

df['seq_dist'] = df.loc[:df.shape[0]-2].apply(my_dist, axis=1)

dist_threshold = 0.6

df['break'] = df['seq_dist'] > dist_threshold

In this example, the df['break'] column will contain an indicator telling you where the distances are bigger than your dist_threshold .在此示例中, df['break']列将包含一个指示符,告诉您距离大于dist_threshold

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM