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)
Related
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]
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.
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]
I have a large 4-dimensional dataset of Temperatures [time,pressure,lat,lon].
I need to find all grid points within a region defined by lat/lon indices and calculate an average over the region to leave me with a 2-dimensional array.
I know how to do this if my region is a rectangle (or square) but how can this be done with an irregular polygon?
Below is an image showing the regions I need to average together and the lat/lon grid the data is gridded to in the array
I believe this should solve your problem.
The code below generates all cells in a polygon defined by a list of vertices.
It "scans" the polygon row by row keeping track of the transition columns where you (re)-enter or exit the polygon.
def row(x, transitions):
""" generator spitting all cells in a row given a list of transition (in/out) columns."""
i = 1
in_poly = True
y = transitions[0]
while i < len(transitions):
if in_poly:
while y < transitions[i]:
yield (x,y)
y += 1
in_poly = False
else:
in_poly = True
y = transitions[i]
i += 1
def get_same_row_vert(i, vertices):
""" find all vertex columns in the same row as vertices[i], and return next vertex index as well."""
vert = []
x = vertices[i][0]
while i < len(vertices) and vertices[i][0] == x:
vert.append(vertices[i][1])
i += 1
return vert, i
def update_transitions(old, new):
""" update old transition columns for a row given new vertices.
That is: merge both lists and remove duplicate values (2 transitions at the same column cancel each other)"""
if old == []:
return new
if new == []:
return old
o0 = old[0]
n0 = new[0]
if o0 == n0:
return update_transitions(old[1:], new[1:])
if o0 < n0:
return [o0] + update_transitions(old[1:], new)
return [n0] + update_transitions(old, new[1:])
def polygon(vertices):
""" generator spitting all cells in the polygon defined by given vertices."""
vertices.sort()
x = vertices[0][0]
transitions, i = get_same_row_vert(0, vertices)
while i < len(vertices):
while x < vertices[i][0]:
for cell in row(x, transitions):
yield cell
x += 1
vert, i = get_same_row_vert(i, vertices)
transitions = update_transitions(transitions, vert)
# define a "strange" polygon (hook shaped)
vertices = [(0,0),(0,3),(4,3),(4,0),(3,0),(3,2),(1,2),(1,1),(2,1),(2,0)]
for cell in polygon(vertices):
print cell
# or do whatever you need to do
The general class of problems is called "Point in Polygon", where the (fairly) standard algorithm is based on drawing a test line through the point under consideration and counting the number of times it crosses polygon boundaries (its really cool/weird that it works so simply, I think). This is a really good overview which includes implementation information.
For your problem in particular, since each of your regions are defined based on a small number of square cells - I think a more brute-force approach might be better. Perhaps something like:
For each region, form a list of all of the (lat/lon) squares which define it. Depending on how your regions are defined, this may be trivial, or annoying...
For each point you are examining, figure out which square it lives in. Since the squares are so well behaves, you can do this manually using opposite corners of each square, or using a method like numpy.digitize.
Test whether the square the point lives in, is in one of the regions.
If you're still having trouble, please provide some more details about your problem (specifically, how your regions are defined) --- that will make it easier to offer advice.
Having list of rectangles parallel to axis in form (minx, miny, maxx, maxy):
rectangles = [
Rectangle(90,40,110,70),
Rectangle(10,40,40,70),
Rectangle(75,60,95,80),
Rectangle(30,20,60,50),
Rectangle(100,20,130,50),
Rectangle(70,10,85,40)
]
I need to get list of groups of rectangles, where each rectangle intersects with at least one other:
[
(Rectangle(10,40,40,70), Rectangle(30,20,60,50)),
(Rectangle(70,10,85,40)),
(Rectangle(75,60,95,80), Rectangle(90,40,110,70), Rectangle(100,20,130,50))
]
The algorithm can't be naive, it needs to be fast.
What I tried:
Find python interval tree implementation - I couldn't find anything good...
I tried this repo: https://github.com/booo/rectangleintersection/blob/master/rectangleIntersection.py, it works with the example above but fails with real world data.
I read through scikit image and Shapely documentation but didn't find algorithms for rectangle intersection.
Intersecting rectangles can be viewed as connected nodes in a graph, and sets of "transitively" intersecting rectangles as Connected Components. To find out which rectangles intersect, we first do a Plane Sweep. To make this reasonably fast we need an Interval Tree. Banyan provides one:
from collections import defaultdict
from itertools import chain
from banyan import SortedDict, OverlappingIntervalsUpdator
def closed_regions(rects):
# Sweep Line Algorithm to set up adjacency sets:
neighbors = defaultdict(set)
status = SortedDict(updator=OverlappingIntervalsUpdator)
events = sorted(chain.from_iterable(
((r.left, False, r), (r.right, True, r)) for r in set(rects)))
for _, is_right, rect in events:
for interval in status.overlap(rect.vertical):
neighbors[rect].update(status[interval])
if is_right:
status.get(rect.vertical, set()).discard(rect)
else:
status.setdefault(rect.vertical, set()).add(rect)
# Connected Components Algorithm for graphs:
seen = set()
def component(node, neighbors=neighbors, seen=seen, see=seen.add):
todo = set([node])
next_todo = todo.pop
while todo:
node = next_todo()
see(node)
todo |= neighbors[node] - seen
yield node
for node in neighbors:
if node not in seen:
yield component(node)
rect.vertical BTW is the tuple (rect.top, rect.bottom).
Time complexity is O(n log n + k), where n is the number of rectangles and k the number of actual intersections. So it's pretty close to optimal.
edit: Because there was some confusion, I need to add that the rectangles are expected to have left <= right and top <= bottom. IOW, the origin of the coordinate system they are in is in the upper left corner, not in the lower left corner as is usual in geometry.