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.
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. 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. 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.