Determine which intersection result further along a line in Shapely - python

Context
(minimum working example code at the end)
I am trying to create a line smoothing algorithm: From the first line vertex, a circle of a specified radius is created and this circle is intersected with the line (LineString). This results into 1 to many intersection results (Points). The farthest intersection point along the line is then taken, a new circle is created, intersected with the line and so on until the line endpoint is approached.
Visualized result with an oddly-passing parameters is in the image below. The blue line is the input, the green line is the smoothed result. The line is in the UTM Projection, but it doesn't matter.
The issue
Now I roughly know how to design most of the algorithm (I can pass it on full here if successful).
What I don't know is how to always choose the farthest intersection point along the line from the current intersection results. Shapely utilizes Point or MultiPoint as an interserciton result in this case. If I reach MultiPoint parts through geoms, they're probably ordered by ascending x coordinate (not sure though).
Attempted solution
I first thought I'd always take the last member from the circle_intersections.geoms. As you probably guess, if I apply this to the algorithm, this wouldn't work once the line changes its direction against the ascending x coordinate. Then, the intersection point I need is usually the first (or other) from the list. And thus, it gets into infinite loop, passing the intersection points between two circles.
In the image above, it worked just because the radius was set such that the searched intersection point was always further in x coordinate.
Secondly, I tried to iterate through the line and filter out the intersection point which splits the line farthest along. Here I must be making some kind of mistake I don't see.
You find both attempts in the find_farthest_intersection function.
Code
Imports:
import shapely
from shapely.geometry import Point, LineString
from shapely.ops import split
import matplotlib.pyplot as plt
The algorithm:
def smooth(point, geometry, r, first_pass=True):
circle = point.buffer(r)
circle_intersections = geometry.intersection(circle.exterior)
# It's always a single point at the first iteration
if isinstance(circle_intersections, Point):
next_point = circle_intersections
else:
next_point = find_farthest_intersection(
geometry,
circle_intersections
)
# If you want to visualize in Jupyter
# plt.plot(
# *point.buffer(6).exterior.xy,
# *circle.exterior.xy,
# *next_point.buffer(5).exterior.xy,
# )
if not first_pass and Point(geometry.coords[-1]).within(circle):
return [geometry.coords[-1]]
return [point.coords[0]] + smooth(
next_point,
geometry,
r,
first_pass=False
)
def find_farthest_intersection(geometry, intersections):
# How to return the right intersection furthest along the line?
# Naive and wrong solution
# When the line heads against X this becomes the opposite point than I want
# return circle_intersections.geoms[-1]
# Other attempted solution
# Not working, results in the same problem as with the solution above.
# Find each intersection point's distance from the
# line beginning
intersection_pnt_dsts_from_line_start = [
split(geometry, point).geoms[0].length
for point in intersections.geoms
]
# Return the intersection point with the maximum
# distance from the line beginning
return max(
zip(
intersection_pnt_dsts_from_line_start,
intersections.geoms
),
key=lambda item: item[0]
)[1]
If you want to visualize in eg. Jupyter (also uncomment pyplots in the smooth function).
# it's in UTM btw
line = shapely.wkt.loads('LINESTRING (478098.5964211893 5442495.543688663, 478229.0423690955 5442525.981076508, 478242.0869638861 5442558.592563484, 478198.6049812507 5442595.552248725, 478129.033809034 5442649.904727018, 478168.1675934059 5442691.212610522, 478209.4754769095 5442665.123420941, 478209.4754769095 5442628.163735701, 478213.8236751731 5442615.11914091, 478231.2164682273 5442599.900446988, 478252.957459545 5442593.378149592, 478305.1358387075 5442602.07454612, 478322.5286317617 5442652.078826151, 478329.0509291569 5442732.520494026, 478383.4034074512 5442762.957881871, 478353.5095443893 5442726.541721413, 478421.4501422573 5442696.37609596, 478471.7261846794 5442718.117087278, 478434.7664994393 5442728.987582936, 478399.9809133308 5442732.248731634, 478458.6815898888 5442757.250871649, 478526.0786629738 5442780.078912533, 478532.6009603692 5442848.563035184, 478579.3440917023 5442883.348621292)')
r = 100 # radius parameter
plt.plot(*line.xy)
result = smooth(
Point(line.coords[0]), # first time it's the first line node
line,
r # smaller number or more complicated lines cause fail
)
# Would then do
# plt.plot(*LineString(result).xy)

So I played around more with geometric operations and with help of #50194077 found the solution is rather simple. I can split the line by each intersection point and measure the distance from the line beginning of the first split result. The longest distance points to the farthest intersection point, i.e. the intersection point I want.
The key to facilitate the split is to create a buffer of the intersection point. Its size should addresses shapely precision issues and be as low as possible (eg 1Oe-9).
Revised function:
def find_farthest_intersection(geometry, intersection_points):
intersection_points_from_geom_distance = []
for intersection_point in intersection_points:
# Create a little buffer
precision_buffer = intersection_point.buffer(10e-9)
# Split line on buffer and take the first line fragment
intersection_points_from_geom_distance.append([
intersection_point,
split(line, precision_buffer)[0].length]
)
# Return the intersection point with the maximum
# distance from the line beginning
return max(
intersection_points_from_geom_distance,
key=lambda item: item[1]
)[0]

Related

Find intersection between line and shape file with python

I'm trying to clip lines based on a shape-file in python. I have a script that works, but it is very slow. Are there a faster way to do this? A prerequisite are that I have to do it with python.
from shapely.geometry import LineString, shape, Point
from shapely.affinity import rotate
from itertools import chain
from shapely.ops import cascaded_union
from fiona import open
import numpy as np
# open the shp-file with land geometry
shoreline = open('land_boundary.shp')
shapes = [shape(f['geometry']) for f in shoreline]
mergedshorelines = cascaded_union(shapes)
# create an arbitrary line
x,y = 696346,6601295
x_end, y_end = 746345,6601295
line1 = LineString([(x, y), (x_end, y_end)])
# Creates lines from arbitrary line with 1 degree step
radii = [rotate(line1, deg, (x,y)) for deg in chain(np.mod(np.arange(0,360,1),360))]
mergedradii = cascaded_union(radii)
# the intersection between the two multilines is computed and the intersection point with the smallest distance is choosen
point = Point(x,y)
points_ok = []
#----THIS IS THE SLOW PART-------
# clip lines with land boundary
for line in mergedradii:
if line.intersects(mergedshorelines):
if line.intersection(mergedshorelines).type == "MultiPoint":
# multiple points: select the point with the minimum distance
a = {}
for pt in line.intersection(mergedshorelines):
a[point.distance(pt)] = pt
points_ok.append(a[min(a.keys())])
if line.intersection(mergedshorelines).type == "Point":
# ok, only one intersection
points_ok.append(line.intersection(mergedshorelines))
Shoreline_points = cascaded_union(points_ok) # coordinates of all intersection.
Appreciate any input!
Cheers!
/Björn
You can save a lot of time by calling intersection just once and saving the result, instead of calling it multiple times. I call the intersection method once at the top of the loop and save it as a variable, then refer to the variable in the logic, instead of running it again.
You can also save time by skipping the intersects check entirely, because if a line doesn't intersect with mergedshorelines, it won't be a MultiPoint or Point object, it will be a LineString.
You can also optimize the part of the script that looks for the closest point in a MultiPoint. When trying to find the closest point to the origin of the line, you only need to check the first and last point in a MultiPoint, because the points are sorted. So either the first or last point will be closest to the origin.
for line in mergedradii:
intersection = line.intersection(mergedshorelines)
if intersection.type == "MultiPoint":
# multiple points: select the point with the minimum distance
first_pt = intersection[0]
last_pt = intersection[-1]
if point.distance(first_pt) < point.distance(last_pt):
points_ok.append(first_pt)
else:
points_ok.append(last_pt)
elif intersection.type == "Point":
# ok, only one intersection
points_ok.append(intersection)
Shoreline_points = cascaded_union(points_ok) # coordinates of all intersection.
On my computer, this script runs in about 40 seconds, while the original script took around 190 seconds.

Finding Intersections Region Based Trajectories vs. Line Trajectories

I have two trajectories (i.e. two lists of points) and I am trying to find the intersection points for both these trajectories. However, if I represent these trajectories as lines, I might miss real world intersections (just misses).
What I would like to do is to represent the line as a polygon with certain width around the points and then find where the two polygons intersect with each other.
I am using the python spatial library but I was wondering if anyone has done this before. Here is a picture of the line segments which don't intersect because they just miss each other. Below is the sample data code that represents the trajectory of two objects.
object_trajectory=np.array([[-3370.00427248, 3701.46800775],
[-3363.69164715, 3702.21408203],
[-3356.31277271, 3703.06477984],
[-3347.25951787, 3704.10740164],
[-3336.739511 , 3705.3958357 ],
[-3326.29355823, 3706.78035903],
[-3313.4987339 , 3708.2076586 ],
[-3299.53433345, 3709.72507366],
[-3283.15486406, 3711.47077376],
[-3269.23487255, 3713.05635557]])
target_trajectory=np.array([[-3384.99966703, 3696.41922372],
[-3382.43687562, 3696.6739521 ],
[-3378.22995178, 3697.08802862],
[-3371.98983789, 3697.71490469],
[-3363.5900481 , 3698.62666805],
[-3354.28520354, 3699.67613798],
[-3342.18581931, 3701.04853915],
[-3328.51519511, 3702.57528111],
[-3312.09691577, 3704.41961271],
[-3297.85543763, 3706.00878621]])
plt.plot(object_trajectory[:,0],object_trajectory[:,1],'b',color='b')
plt.plot(vehicle_trajectory[:,0],vehicle_trajectory[:,1],'b',color='r')
Let's say you have two lines defined by numpy arrays x1, y1, x2, and y2.
import numpy as np
You can create an array distances[i, j] containing the distances between the ith point in the first line and the jth point in the second line.
distances = ((x1[:, None] - x2[None, :])**2 + (y1[:, None] - y2[None, :])**2)**0.5
Then you can find indices where distances is less than some threshold you want to define for intersection. If you're thinking of the lines as having some thickness, the threshold would be half of that thickness.
threshold = 0.1
intersections = np.argwhere(distances < threshold)
intersections is now a N by 2 array containing all point pairs that are considered to be "intersecting" (the [i, 0] is the index from the first line, and [i, 1] is the index from the second line). If you want to get the set of all the indices from each line that are intersecting, you can use something like
first_intersection_indices = np.asarray(sorted(set(intersections[:, 0])))
second_intersection_indices = np.asarray(sorted(set(intersections[:, 1])))
From here, you can also determine how many intersections there are by taking only the center value for any consecutive values in each list.
L1 = []
current_intersection = []
for i in range(first_intersection_indices.shape[0]):
if len(current_intersection) == 0:
current_intersection.append(first_intersection_indices[i])
elif first_intersection_indices[i] == current_intersection[-1]:
current_intersection.append(first_intersection_indices[i])
else:
L1.append(int(np.median(current_intersection)))
current_intersection = [first_intersection_indices[i]]
print(len(L1))
You can use these to print the coordinates of each intersection.
for i in L1:
print(x1[i], y1[i])
Turns out that the shapely package already has a ton of convinience functions that get me very far with this.
from shapely.geometry import Point, LineString, MultiPoint
# I assume that self.line is of type LineString (i.e. a line trajectory)
region_polygon = self.line.buffer(self.lane_width)
# line.buffer essentially generates a nice interpolated bounding polygon around the trajectory.
# Now we can identify all the other points in the other trajectory that intersects with the region_polygon that we just generated. You can also use .intersection if you want to simply generate two polygon trajectories and find the intersecting polygon as well.
is_in_region = [region_polygon.intersects(point) for point in points]

Algorithm to close a polygon

I have a part of perimeter of a polygon and need to close it.Please refer this image
As I can see there is only one unique way to close the polygon without dividing the polygon and without the edges intersecting.
And the closing edges would be b->c,d->e,f->g,h->a
Is there any algo to achieve this?
I can think of only one brute force method, try every possible combination and check if it forms a closed polygon(Any good algos to check if it is closed polygon?)
Is there any better way or a known algorithm?
Note: The vertices should be connected by single straight lines only and polygon is not necessarily convex
Also, You can safely assume that these segments always form a polygon because I get these line segments from a polygon and Im trying to recreate the polygon
I think that in "well-behaved" (small gaps, not too irregular shape, etc.) cases, one might get away with following approach. The idea is to assume that the solution (particular permutation of the input line segments which are then assumed to be connected with straight lines) minimizes the length of the resulting MultiLineString defining the boundary of the polygon of interest.
To tackle this problem, the implementation below uses the 2-opt heuristic to the traveling salesman problem. It proceeds in following steps:
the set of vertices is defined as the union of the endpoints of all input line segments
it tries to connect these points in order to minimize the total length of the resulting MultiLineString under the constraint that the points belonging to the same input line segment are always connected (i.e., the 2-opt algorithm is allowed to split only edges connecting different line segments - this is handled by the extra if condition in the main double for-loop).
The result is then:
import logging
import random
import sys
from shapely.geometry import LineString, Polygon
from shapely.ops import polygonize, linemerge
#prevent shapely from showing an error message on is_valid tests
logger = logging.getLogger()
logger.setLevel(logging.ERROR)
#input lines (LineStrings)
lines = [
[(3.15,3.94), (4.06,3.91), (4.27,3.49)],
[(0.84,2.99), (0.97,3.67), (1.68,3.91), (2.68,3.92)],
[(4.46,3.23), (5.12,2.97), (4.60,2.00)],
[(4.13,1.44), (4.41,0.68), (1.14,1.99)]
]
random.shuffle(lines)
N, pnts = 0, []
pnt2line = {}
for line_id, line in enumerate(lines):
#for each line, save its endpoints and remember
#to which line each point belongs
for pnt in [line[0], line[-1]]:
pnt2line[N] = line_id
pnts.append(pnt)
N += 1
#as initial guess, try to connect these points sequentially
route = [i for i in range(0, N)]
def nrm_idx(N, idx):
return (N + idx) % N
def get_polygon(route):
#for given route, attempt to construct the resulting polygon
segments = []
m = len(route)
for idx in range(0, m):
i, j = route[idx], route[nrm_idx(m, idx+1)]
if pnt2line[i] == pnt2line[j]:
#if two consecutive points belong to the same line, consider this line
segments.append(lines[pnt2line[i]])
else:
#otherwise, connect these points with a straight line
segments.append([pnts[i], pnts[j]])
return Polygon(linemerge(segments))
def get_weight(route):
P = get_polygon(route)
return P.length if P.is_valid else sys.maxsize
def edge_is_fixed(pnt_i, pnt_j):
#check if an edge specified by point pnt_i/pnt_j can be dissected or not
#in the latter case, the points belong to the same line/line segment
return (pnt2line[pnt_i] == pnt2line[pnt_j])
def opt_swap(route, i, k):
#perform 2-opt swap
return route[0:i] + route[i:k+1][::-1] + route[k+1:]
flag = True
while flag:
flag = False
best_weight = get_weight(route)
for i in range(0, N-1):
for k in range(i+1, N):
if edge_is_fixed(route[nrm_idx(N, i-1)], route[i]) or edge_is_fixed(route[k], route[nrm_idx(N, k+1)]):
continue
new_route = opt_swap(route, i, k)
weight = get_weight(new_route)
if weight < best_weight:
route = new_route[:]
best_weight = weight
flag = True
P = get_polygon(route)
for x, y in P.exterior.coords:
print(x, y)
For your input (approximated), the result is then indeed:
Here's something that might work:
- Make a set containing only the open points (points that are only on one edge, i.e. the labelled ones in your diagram)
- Run a convex hull algorithm on that set
- Use the edges of the convex hull to complete the polygon with the existing edges. (I.e if the convex hull contains A->B, but A and B are already indirectly connected via adjacent edges in your pre-existing set of edges, discard the edge A->B in the convex hull)
EDIT
I previously suggested co-opting convex hull algorithms but that approach has shortcomings, including the case where the points would not make a convex shape.
Note that, according to your stipulations, there are sets that would not have solutions, such as:
(it's not possible to complete this into a polygon with no crossing lines, using only single straight lines between open points)

Finding the distance of coordinates from the beginning of a route in Shapely

I have a list of coordinates (lat/lon) representing a route.
Given a certain radius and another coordinate I need to check if the coord is in the route (within the given radius from any point) and its distance from the beginning of the route.
I looked at Shapely and it looks like a good solution.
I started off by creating a StringLine
from shapely.geometry import LineString
route = LineString[(x, y), (x1, y1), ...]
Then to check if the point is near the route I've added a buffer and checked
for intersection
from shapely.geometry import Point
p = Point(x, y)
r = 0.5
intersection = p.buffer(r).intersection(route)
if intersection.is_empty:
print "Point not on route"
else:
# Calculate P distance from the begning of route
I'm stuck calculating the distance. I thought of splitting the route at p and measuring the length of the first half but the intersection result I get is a HeterogeneousGeometrySequence which I'm not sure what I can do with.
I believe I found a solution:
if p.buffer(r).intersects(route):
return route.project(p)
Rather than buffering a geometry, which is expensive and imperfect (since buffering requires a number of segments and many other options), just see if the point is within a distance threshold:
if route.distance(p) <= r:
return route.project(p)
Also, you probably realised by now that your distance units are in degrees. If you want linear distances, like meters, you would need to make it much more complicated using different libraries.

Creating a spatial index for QGIS 2 spatial join (PyQGIS)

I've written a bit of code to do a simple spatial join in QGIS 2 and 2.2 (points that lie within a buffer to take attribute of the buffer). However, I'd like to employ a QgsSpatialIndex in order to speed things up a bit. Where can I go from here:
pointProvider = self.pointLayer.dataProvider()
rotateProvider = self.rotateBUFF.dataProvider()
all_point = pointProvider.getFeatures()
point_spIndex = QgsSpatialIndex()
for feat in all_point:
point_spIndex.insertFeature(feat)
all_line = rotateProvider.getFeatures()
line_spIndex = QgsSpatialIndex()
for feat in all_line:
line_spIndex.insertFeature(feat)
rotate_IDX = self.rotateBUFF.fieldNameIndex('bearing')
point_IDX = self.pointLayer.fieldNameIndex('bearing')
self.pointLayer.startEditing()
for rotatefeat in self.rotateBUFF.getFeatures():
for pointfeat in self.pointLayer.getFeatures():
if pointfeat.geometry().intersects(rotatefeat.geometry()) == True:
pointID = pointfeat.id()
bearing = rotatefeat.attributes()[rotate_IDX]
self.pointLayer.changeAttributeValue(pointID, point_IDX, bearing)
self.pointLayer.commitChanges()
To do this kind of spatial join, you can use the QgsSpatialIndex (http://www.qgis.org/api/classQgsSpatialIndex.html) intersects(QgsRectangle) function to get a list of candidate featureIDs or the nearestNeighbor (QgsPoint,n) function to get the list of the n nearest neighbours as featureIDs.
Since you only want the points that lie within the buffer, the intersects function seems most suitable. I have not tested if a degenerate bbox (point) can be used. If not, just make a very small bounding box around your point.
The intersects function returns all features that have a bounding box that intersects the given rectangle, so you will have to test these candidate features for a true intersection.
Your outer loop should be on the points (you want to to add attribute values to each point from their containing buffer).
# If degenerate rectangles are allowed, delta could be 0,
# if not, choose a suitable, small value
delta = 0.1
# Loop through the points
for point in all_point:
# Create a search rectangle
# Assuming that all_point consist of QgsPoint
searchRectangle = QgsRectangle(point.x() - delta, point.y() - delta, point.x() + delta, point.y() + delta)
# Use the search rectangle to get candidate buffers from the buffer index
candidateIDs = line_index.intesects(searchRectangle)
# Loop through the candidate buffers to find the first one that contains the point
for candidateID in candidateIDs:
candFeature == rotateProvider.getFeatures(QgsFeatureRequest(candidateID)).next()
if candFeature.geometry().contains(point):
# Do something useful with the point - buffer pair
# No need to look further, so break
break

Categories

Resources