I was trying to create river cross-section profiles based on the point terrestical measurements. When trying to create a Shapely LineString from a Series of points with the common id, I realized that the order of given points really matters as the LineString would just connect given points 'indexwise' (connect points in the list-given order). The below code illustrates the default behaviour:
from shapely.geometry import Point, LineString
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
# Generate random points
x=np.random.randint(0,100,10)
y=np.random.randint(0,50,10)
data = zip(x,y)
# Create Point and default LineString GeoSeries
gdf_point = gpd.GeoSeries([Point(j,k) for j,k in data])
gdf_line = gpd.GeoSeries(LineString(zip(x,y)))
# plot the points and "default" LineString
ax = gdf_line.plot(color='red')
gdf_point.plot(marker='*', color='green', markersize=5,ax=ax)
That would produce the image:
Question: Is there any built-in method within Shapely that would automatically create the most logical (a.k.a.: the shortest, the least complicated, the least criss-cross,...) line through the given list of random 2D points?
Below can you find the desired line (green) compared to the default (red).
Here is what solved my cross-section LineString simplification problem. However, my solution doesn't correctly address computationally more complex task of finding the ultimately shortest path through the given points. As the commenters suggested, there are many libraries and scripts available to solve that particulal problem, but in case anyone want to keep it simple, you can use what did the trick for me. Feel free to use and comment!
def simplify_LineString(linestring):
'''
Function reorders LineString vertices in a way that they each vertix is followed by the nearest remaining vertix.
Caution: This doesn't calculate the shortest possible path (travelling postman problem!) This function performs badly
on very random points since it doesn't see the bigger picture.
It is tested only with the positive cartesic coordinates. Feel free to upgrade and share a better function!
Input must be Shapely LineString and function returns Shapely Linestring.
'''
from shapely.geometry import Point, LineString
import math
if not isinstance(linestring,LineString):
raise IOError("Argument must be a LineString object!")
#create a point lit
points_list = list(linestring.coords)
####
# DECIDE WHICH POINT TO START WITH - THE WESTMOST OR SOUTHMOST? (IT DEPENDS ON GENERAL DIRECTION OF ALL POINTS)
####
points_we = sorted(points_list, key=lambda x: x[0])
points_sn = sorted(points_list, key=lambda x: x[1])
# calculate the the azimuth of general diretction
westmost_point = points_we[0]
eastmost_point = points_we[-1]
deltay = eastmost_point[1] - westmost_point[1]
deltax = eastmost_point[0] - westmost_point[0]
alfa = math.degrees(math.atan2(deltay, deltax))
azimut = (90 - alfa) % 360
if (azimut > 45 and azimut < 135):
#General direction is west-east
points_list = points_we
else:
#general direction is south-north
points_list = points_sn
####
# ITERATIVELY FIND THE NEAREST VERTIX FOR THE EACH REMAINING VERTEX
####
# Create a new, ordered points list, starting with the east or southmost point.
ordered_points_list = points_list[:1]
for iteration in range(0, len(points_list[1:])):
current_point = ordered_points_list[-1] # current point that we are looking the nearest neighour to
possible_candidates = [i for i in points_list if i not in ordered_points_list] # remaining (not yet sortet) points
distance = 10000000000000000000000
best_candidate = None
for candidate in possible_candidates:
current_distance = Point(current_point).distance(Point(candidate))
if current_distance < distance:
best_candidate = candidate
distance = current_distance
ordered_points_list.append(best_candidate)
return LineString(ordered_points_list)
There is no built in function, but shapely has a distance function.
You could easily iterate over the points and calculate the shortest distance between them and construct the 'shortest' path.
There are some examples in the offical github repo.
Google's OR-Tools offer a nice and efficient way for solving the Travelling Salesman Problem: https://developers.google.com/optimization/routing/tsp.
Following the tutorial on their website would give you a solution from this (based on your example code):
to this:
Related
from a set of points I built the Voronoi tessellation using scipy:
from scipy.spatial import Voronoi
vor = Voronoi(points)
Now I would like to build a Polygon in Shapely from the regions the Voronoi algorithm created. The problem is that the Polygon class requires a list of counter-clockwise vertices. Although I know how to order these vertices, I can't solve the problem because often this is my result:
(overlapping polygon). This is the code (ONE RANDOM EXAMPLE):
def order_vertices(l):
mlat = sum(x[0] for x in l) / len(l)
mlng = sum(x[1] for x in l) / len(l)
# https://stackoverflow.com/questions/1709283/how-can-i-sort-a-coordinate-list-for-a-rectangle-counterclockwise
def algo(x):
return (math.atan2(x[0] - mlat, x[1] - mlng) + 2 * math.pi) % 2*math.pi
l.sort(key=algo)
return l
a = np.asarray(order_vertices([(9.258054711746084, 45.486245994138976),
(9.239284166975443, 45.46805963143515),
(9.271640747003861, 45.48987234571072),
(9.25828782103321, 45.44377372506324),
(9.253993275176263, 45.44484395950612),
(9.250114174032936, 45.48417979682819)]))
plt.plot(a[:,0], a[:,1])
How can I solve this problem?
If you're just after a collection of polygons you don't need to pre-order the point to build them.
The scipy.spatial.Voronoi object has a ridge_vertices attribute containing indices of vertices forming the lines of the Voronoi ridge. If the index is -1 then the ridge goes to infinity.
First start with some random points to build the Voronoi object.
import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d
import shapely.geometry
import shapely.ops
points = np.random.random((10, 2))
vor = Voronoi(points)
voronoi_plot_2d(vor)
You can use this to build a collection of Shapely LineString objects.
lines = [
shapely.geometry.LineString(vor.vertices[line])
for line in vor.ridge_vertices
if -1 not in line
]
The shapely.ops module has a polygonize that returns a generator for Shapely Polygon objects.
for poly in shapely.ops.polygonize(lines):
#do something with each polygon
Or if you wanted a single polygon formed from the region enclosed by the Voronoi tesselation you can use the Shapely unary_union method:
shapely.ops.unary_union(list(shapely.ops.polygonize(lines)))
As others have said, it is because you have to rebuild the polygons from the resulting points correctly based on indexes. Although you have the solution, I thought I should mention there is also another pypi supported tesselation package called Pytess (Disclaimer: I am the package maintainer) where the voronoi function returns the voronoi polygons fully built for you.
The library is able to generate ordered list of coordinates, you just need to make use of the index lists provided:
import numpy as np
from scipy.spatial import Voronoi
...
ids = np.array(my_points_list)
vor = Voronoi(points)
polygons = {}
for id, region_index in enumerate(vor.point_region):
points = []
for vertex_index in vor.regions[region_index]:
if vertex_index != -1: # the library uses this for infinity
points.append(list(vor.vertices[vertex_index]))
points.append(points[0])
polygons[id]=points
each polygon in the polygons dictionary can be exported to geojson or brought into shapely and I was able to render them properly in QGIS
The function you implemented (order_vertices() ), cannot work in your case, because it simply takes a already ordered sequence of coordinates, which builds a rectangle, and inverts the direction of the polygon (and maybe works only with rectangles...).
But you have a not ordered sequence of coordinates
Generally speaking, you can not build a polygon from a arbitrary sequence of not ordered vertices because there is no a unique solution for concave polygons, as showed in this example: https://stackoverflow.com/a/7408711/4313133
However, if you are sure that your polygons are always convex, you can build a convex hull with this code: https://stackoverflow.com/a/15945375/4313133 (tested right now, it worked for me)
Probably you can build the convex hull with scipy as well, but I dint test it: scipy.spatial.ConvexHull
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.
I have two pairs of lat/lon (expressed in decimal degrees) along with their radius (expressed in meters). What I am trying to achieve is to find if an intersect between these two points exits (of course, it is obvious that this doesn't hold here but the plan is to try this algorithm in many other data points). In order to check this I am using Shapely's intersects() function. My question however is how should I deal with the different units? Should I make some sort of transformation \ projection first (same units for both lat\lon and radius)?
48.180759,11.518950,19.0
47.180759,10.518950,10.0
EDIT:
I found this library here (https://pypi.python.org/pypi/utm) which seems helpfull. However, I am not 100% sure if I apply it correctly. Any ideas?
X = utm.from_latlon(38.636782, 21.414384)
A = geometry.Point(X[0], X[1]).buffer(30.777)
Y = utm.from_latlon(38.636800, 21.414488)
B = geometry.Point(Y[0], Y[1]).buffer(23.417)
A.intersects(B)
SOLUTION:
So, I finally managed to solve my problem. Here are two different implementations that both solve the same problem:
X = from_latlon(48.180759, 11.518950)
Y = from_latlon(47.180759, 10.518950)
print(latlonbuffer(48.180759, 11.518950, 19.0).intersects(latlonbuffer(47.180759, 10.518950, 19.0)))
print(latlonbuffer(48.180759, 11.518950, 100000.0).intersects(latlonbuffer(47.180759, 10.518950, 100000.0)))
X = from_latlon(48.180759, 11.518950)
Y = from_latlon(47.180759, 10.518950)
print(geometry.Point(X[0], X[1]).buffer(19.0).intersects(geometry.Point(Y[0], Y[1]).buffer(19.0)))
print(geometry.Point(X[0], X[1]).buffer(100000.0).intersects(geometry.Point(Y[0], Y[1]).buffer(100000.0)))
Shapely only uses the Cartesian coordinate system, so in order to make sense of metric distances, you would need to either:
project the coordinates into a local projection system that uses distance units in metres, such as a UTM zone.
buffer a point from (0,0), and use a dynamic azimuthal equidistant projection centered on the lat/lon point to project to geographic coords.
Here's how to do #2, using shapely.ops.transform and pyproj
import pyproj
from shapely.geometry import Point
from shapely.ops import transform
from functools import partial
WGS84 = pyproj.Proj(init='epsg:4326')
def latlonbuffer(lat, lon, radius_m):
proj4str = '+proj=aeqd +lat_0=%s +lon_0=%s +x_0=0 +y_0=0' % (lat, lon)
AEQD = pyproj.Proj(proj4str)
project = partial(pyproj.transform, AEQD, WGS84)
return transform(project, Point(0, 0).buffer(radius_m))
A = latlonbuffer(48.180759, 11.518950, 19.0)
B = latlonbuffer(47.180759, 10.518950, 10.0)
print(A.intersects(B)) # False
Your two buffered points don't intersect. But these do:
A = latlonbuffer(48.180759, 11.518950, 100000.0)
B = latlonbuffer(47.180759, 10.518950, 100000.0)
print(A.intersects(B)) # True
As shown by plotting the lon/lat coords (which distorts the circles):
from a set of points I built the Voronoi tessellation using scipy:
from scipy.spatial import Voronoi
vor = Voronoi(points)
Now I would like to build a Polygon in Shapely from the regions the Voronoi algorithm created. The problem is that the Polygon class requires a list of counter-clockwise vertices. Although I know how to order these vertices, I can't solve the problem because often this is my result:
(overlapping polygon). This is the code (ONE RANDOM EXAMPLE):
def order_vertices(l):
mlat = sum(x[0] for x in l) / len(l)
mlng = sum(x[1] for x in l) / len(l)
# https://stackoverflow.com/questions/1709283/how-can-i-sort-a-coordinate-list-for-a-rectangle-counterclockwise
def algo(x):
return (math.atan2(x[0] - mlat, x[1] - mlng) + 2 * math.pi) % 2*math.pi
l.sort(key=algo)
return l
a = np.asarray(order_vertices([(9.258054711746084, 45.486245994138976),
(9.239284166975443, 45.46805963143515),
(9.271640747003861, 45.48987234571072),
(9.25828782103321, 45.44377372506324),
(9.253993275176263, 45.44484395950612),
(9.250114174032936, 45.48417979682819)]))
plt.plot(a[:,0], a[:,1])
How can I solve this problem?
If you're just after a collection of polygons you don't need to pre-order the point to build them.
The scipy.spatial.Voronoi object has a ridge_vertices attribute containing indices of vertices forming the lines of the Voronoi ridge. If the index is -1 then the ridge goes to infinity.
First start with some random points to build the Voronoi object.
import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d
import shapely.geometry
import shapely.ops
points = np.random.random((10, 2))
vor = Voronoi(points)
voronoi_plot_2d(vor)
You can use this to build a collection of Shapely LineString objects.
lines = [
shapely.geometry.LineString(vor.vertices[line])
for line in vor.ridge_vertices
if -1 not in line
]
The shapely.ops module has a polygonize that returns a generator for Shapely Polygon objects.
for poly in shapely.ops.polygonize(lines):
#do something with each polygon
Or if you wanted a single polygon formed from the region enclosed by the Voronoi tesselation you can use the Shapely unary_union method:
shapely.ops.unary_union(list(shapely.ops.polygonize(lines)))
As others have said, it is because you have to rebuild the polygons from the resulting points correctly based on indexes. Although you have the solution, I thought I should mention there is also another pypi supported tesselation package called Pytess (Disclaimer: I am the package maintainer) where the voronoi function returns the voronoi polygons fully built for you.
The library is able to generate ordered list of coordinates, you just need to make use of the index lists provided:
import numpy as np
from scipy.spatial import Voronoi
...
ids = np.array(my_points_list)
vor = Voronoi(points)
polygons = {}
for id, region_index in enumerate(vor.point_region):
points = []
for vertex_index in vor.regions[region_index]:
if vertex_index != -1: # the library uses this for infinity
points.append(list(vor.vertices[vertex_index]))
points.append(points[0])
polygons[id]=points
each polygon in the polygons dictionary can be exported to geojson or brought into shapely and I was able to render them properly in QGIS
The function you implemented (order_vertices() ), cannot work in your case, because it simply takes a already ordered sequence of coordinates, which builds a rectangle, and inverts the direction of the polygon (and maybe works only with rectangles...).
But you have a not ordered sequence of coordinates
Generally speaking, you can not build a polygon from a arbitrary sequence of not ordered vertices because there is no a unique solution for concave polygons, as showed in this example: https://stackoverflow.com/a/7408711/4313133
However, if you are sure that your polygons are always convex, you can build a convex hull with this code: https://stackoverflow.com/a/15945375/4313133 (tested right now, it worked for me)
Probably you can build the convex hull with scipy as well, but I dint test it: scipy.spatial.ConvexHull
If I have a line defined by a start and end coordinates, how do I get n equally spaced points on that line, taking the curvature of the earth into account?
I'm looking for an algorithm, and/or a python library that implements this.
Using geographiclib, a python implementation of GeographicLib, I was able to do this:
from geographiclib.geodesic import Geodesic
number_points = 10
gd = Geodesic.WGS84.Inverse(35, 0, 35, 90)
line = Geodesic.WGS84.Line(gd['lat1'], gd['lon1'], gd['azi1'])
for i in range(number_points + 1):
point = line.Position(gd['s12'] / number_points * i)
print((point['lat2'], point['lon2']))
output:
(35.0, -7.40353472481637e-21)
(38.29044006500327, 7.8252809205988445)
(41.01134777655358, 16.322054184499173)
(43.056180665524245, 25.451710440063902)
(44.328942450747135, 35.08494460239694)
(44.76147256654079, 45.00000000000001)
(44.328942450747135, 54.91505539760305)
(43.05618066552424, 64.54828955993611)
(41.01134777655356, 73.67794581550085)
(38.290440065003274, 82.17471907940114)
(34.99999999999999, 90.0
You can use the npts method from pyproj's Geod class.
See
https://jswhit.github.io/pyproj/pyproj.Geod-class.html
Given a single initial point and terminus point (specified by python
floats lon1,lat1 and lon2,lat2), returns a list of longitude/latitude
pairs describing npts equally spaced intermediate points along the
geodesic between the initial and terminus points.
Emphasis mine because I missed that at first.
First you create an Geod instance, specifying the ellipsoid you want it to use. Then you can call the method.
from pyproj import Geod
geod = Geod("+ellps=WGS84")
points = geod.npts(lon1=-89.6627,
lat1=39.7658,
lon2=147.2800,
lat2=-42.8500,
npts=100
)
points is now a list of tuples on the geodesic line between your start and end point:
[(-91.27649937899028, 39.21278457275204),
(-92.86468478264302, 38.6377120347621),
(-94.42723159402209, 38.04136774269571),
(-95.96421169120758, 37.42453136174509),
(-97.47578514283185, 36.78797425216882),
...