Bala
Bala

Reputation: 121

Is there any python package available to calculate shortest distance between coordinates only via sea route?

There are online APIs available to calculate Port to Port distances, but I would like to do it at free of cost. I have tried things mentioned in other similar questions Find path by sea from coastal point A to coastal point B but it did not serve my purpose.

Reason :

  1. The distance calculated from left extreme of the map to right extreme of the map is not right as the earth is spherical. Example : Distance between -150,50 to 100,50 is less if it is calculated in spherical dimensions.
  2. The result gives only the route 'path', but unable to calculate the distance in km or miles from the path variable.

I have tried distance calculation like haversine, etc but those do not take only water routes as a consideration. The ultimate aim is to calculate port to port distance the vessel needs to travel via marine route. Not need to be very accurate like ocean beds, depth, etc need not be taken into consideration. Just the shortest distance via ocean from Port A to Port B in km or Nm is enough.

Inputs:

Start Destination :

[lat,long] End Destination :

[lat,long]

Output Expected:

Route coordinates:

[lat1,long1

lat2,long2

lat3,long3

lat4,long4

lat5,long5 ]

Distance:

xxx kms

Upvotes: 5

Views: 3182

Answers (2)

conmak
conmak

Reputation: 1470

Another new package we wrote to do exactly this is scgraph. Written by Connor Makowski (me - shameless self promotion) and the MIT Supply Chain CAVE lab team.

The benefit of this package is that it is faster than searoute (~0.05s scgraph vs ~0.5s for searoute) and requires no additional python dependencies.

It however, does not have any port information. Just a maritime network graph of common maritime routes (including nodes at ports - without port info).

pip install scgraph>=1.0.0

For your particular question:

# Use a maritime network geograph
from scgraph.geographs.marnet import marnet_geograph

# Get the shortest path between 
output = marnet_geograph.get_shortest_path(
    origin_node={"latitude": 31.23,"longitude": 121.47}, 
    destination_node={"latitude": 32.08,"longitude": -81.09}
)

# Show your output path
print(str([[i['latitude'],i['longitude']] for i in output['coordinate_path']]))

# Show the length
print('Length: ',output['length']) #=> Length:  19596.4653

On a map the output would look like: scgraph

Upvotes: 2

Gent
Gent

Reputation: 31

I suggest you to check this package: https://pypi.org/project/searoute/

and then you can use it as below :

import searoute as sr

#Define origin and destination points [long, lat]:
origin = [0.3515625, 50.064191736659104]
destination = [117.42187500000001, 39.36827914916014]

# > Returns a GeoJSON LineString Feature
route = sr.searoute(origin, destination)
print("{:.1f} {}".format(route.properties['length'], route.properties['units']))

Upvotes: 3

Related Questions