Skip to content

Commit

Permalink
v1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
genthalili committed Oct 30, 2022
1 parent 219dfc8 commit 29a8aae
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 3,617 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ Change Log
1.0.7 (13/10/2022)
------------------
- Imporved network connectivity
- Connection points were not adjusted
- Connection points were not adjusted

1.0.8 (25/10/2022)
------------------
- Imporved network connectivity
- Added append_orig_dest parameter as input to append origin and destination points to the LineString
- Updated README.md
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ routeMiles = sr.searoute(origin, destination, units="mi")

~~~

## Parameters

`origin`
Mandatory. An array of 2 floats representing longitude and latitude i.e : `[{lon}, {lat}]`

`destination`
Mandatory. An array of 2 floats representing longitude and latitude i.e : `[{lon}, {lat}]`

`units`
Optional. Default to `km` = kilometers, can be `m` = meters `mi` = miles `ft` = feets `in` = inches `deg` = degrees `cen` = centimeters `rad` = radians `naut` = nauticals `yd` = yards

`speed_knot`
Optional. Speed of the boat, default 24 knots

`append_orig_dest`
Optional. If the origin and destination should be appended to the LineString, default is `False`

## Credits

Based on Eurostat's [Searoute Java library](https://github.com/eurostat/searoute) and Dijkstra's algorithm implemented by [perliedman](https://www.liedman.net/geojson-path-finder/).
Binary file modified searoute/__pycache__/searoute.cpython-310.pyc
Binary file not shown.
3,606 changes: 0 additions & 3,606 deletions searoute/data/marnet_densified_old.json

This file was deleted.

1 change: 0 additions & 1 deletion searoute/data/marnet_densified_old2.json

This file was deleted.

2 changes: 1 addition & 1 deletion searoute/data/marnet_densified_v2.geojson

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions searoute/data/marnet_densified_v2_old.geojson

Large diffs are not rendered by default.

45 changes: 40 additions & 5 deletions searoute/searoute.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

here = path.abspath(path.dirname(__file__))
path2Json = 'data/marnet_densified_v2.geojson'
#path2Json = 'data/maritime-trade-routes.geojson'
pathGeojson = path.join(here, path2Json)

def createFeatureCollection(path):
Expand Down Expand Up @@ -58,8 +59,11 @@ def createGraph():

G = nx.MultiDiGraph()

G.graph['crs'] = fc.crs['properties']['name']

try:
G.graph['crs'] = fc.crs['properties']['name']
except:
G.graph['crs'] = 'EPSG:3857'

for i in fc.features:
coords = i['geometry']['coordinates']
coord_l = len(coords)
Expand All @@ -70,17 +74,21 @@ def createGraph():
c_node = get_unique_number(c_X, c_Y)
G.add_node(c_node, x=c_X, y=c_Y)



if(k<coord_l-1):
n_coord = coords[k+1]
n_X = n_coord[0]
n_Y = n_coord[1]
nx_node = get_unique_number(n_X, n_Y)


ll = LineString([(c_X, c_Y), (n_X, n_Y)])
lenght = measurement.length(ll)
G.add_edge(c_node, nx_node, weight=lenght)
G.add_edge(nx_node, c_node, weight=lenght)


k= k+1

return G
Expand All @@ -89,14 +97,15 @@ def createGraph():

G = createGraph()

def searoute(origin, destination, units='km', speed_knot=24):
def searoute(origin, destination, units='km', speed_knot=24, append_orig_dest = False):
"""
searoute Returnes the shortest sea route between two points on Earth.
:param origin: a point as array lon, lat format ex. [0.3515625, 50.064191736659104]
:param destination: a point as array lon, lat format ex. [117.42187500000001, 39.36827914916014]
:param units: units default to 'km' = kilometers, can be 'm' = meters 'mi = miles 'ft' = feets 'in' = inches 'deg' = degrees 'cen' = centimeters 'rad' = radians 'naut' = nauticals 'yd' = yards
:param speed_knot: spped of the boat, default 24 knots
:param speed_knot: speed of the boat, default 24 knots
:param append_orig_dest: add origin and dest geopoints in the LineString of the route, default is False
:return: a Feature (geojson) of a LineSring of sea route with parameters : unit and legth
"""

Expand All @@ -108,9 +117,35 @@ def searoute(origin, destination, units='km', speed_knot=24):
# Get the shortest route by distance
shortest_route_by_distance = ox.shortest_path(G, origin_node, destination_node, weight='weight')
ls = []
previousX = None
for i in shortest_route_by_distance:
node = G.nodes[i]
ls.append((node['x'], node['y']))
nowX = node['x']

if previousX:
if previousX-nowX<-180:
nowX = -180-(180-nowX)
elif previousX-nowX>180:
nowX = nowX+360
ls.append((nowX, node['y']))
previousX = nowX

if append_orig_dest:

if len(ls) == 1:
#to avoid strage connections remove, eventually non-ocean connections with one coord, remove it to add origin and dest
ls = []
# add origin at fist position of the linestring
ls.insert(0, origin )
# add destination at the last position of the linestring
nowX = destination[0]
if previousX:
if previousX-nowX<-180:
nowX = -180-(180-nowX)
elif previousX-nowX>180:
nowX = nowX+360
ls.append((nowX, destination[1]))


lineString = LineString(ls)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='searoute',
version='1.0.7',
version='1.0.8',
description='A python package for generating the shortest sea route between two points on Earth.',
long_description=open('README.md').read() + '\n\n' +
open('CHANGELOG.txt').read(),
Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
destination = [-118.2629, 33.7276]


route = sr.searoute(origin, destination)
route = sr.searoute(origin, destination, append_orig_dest=True)
# > Returns a GeoJSON LineString Feature
# show route distance with unit
print("{:.1f} {}".format(route.properties['length'], route.properties['units']))
Expand All @@ -15,4 +15,4 @@
# Optionally, define the units for the length calculation included in the properties object.
# Defaults to km, can be can be 'm' = meters 'mi = miles 'ft' = feets 'in' = inches 'deg' = degrees
# 'cen' = centimeters 'rad' = radians 'naut' = nauticals 'yd' = yards
routeMiles = sr.searoute(origin, destination, units="mi")
routeMiles = sr.searoute(origin, destination, units="mi")

0 comments on commit 29a8aae

Please sign in to comment.