genthalili / searoute-py

A python package to calculate the shortest sea route between two points on Earth.
Apache License 2.0
63 stars 14 forks source link

Create M as and P as cached functions otherwise restrictions are persistent #33

Closed this-josh closed 3 weeks ago

this-josh commented 3 weeks ago

This PR fixes a bug that can occur when using restrictions currently if you

  1. Solve with no restriction restrictions=None
  2. Solve with a restriction restrictions = ['suez']

Then if you solve with no restriction restrictions=None it keeps the suez restriction.

Here is an MWE for the problem

import searoute as sr
import folium

src_lat = -15.46667
src_lon = 28.26667
dst_lat = 51.5
dst_lon = -0.1666667
b = sr.searoute(
    [src_lon, src_lat],
    [dst_lon, dst_lat],
    include_ports=True,
    port_params=dict(
        country_pol="ZM",
        country_pod="GB"
    ),
    restrictions = None
)
d = sr.searoute(
    [src_lon, src_lat],
    [dst_lon, dst_lat],
    include_ports=True,
    port_params=dict(
        country_pol="ZM",
        country_pod="GB"
    ),
    restrictions = ['suez']
)
nb = sr.searoute(
    [src_lon, src_lat],
    [dst_lon, dst_lat],
    include_ports=True,
    port_params=dict(
        country_pol="ZM",
        country_pod="GB"
    ),
    restrictions = None
)
# Swap coord order for folium
b['geometry']['coordinates'] = [list(reversed(coord)) for coord in b['geometry']['coordinates']]
d['geometry']['coordinates'] = [list(reversed(coord)) for coord in d['geometry']['coordinates']]
nb['geometry']['coordinates'] = [list(reversed(coord)) for coord in nb['geometry']['coordinates']]

m = folium.Map(location=[0,0],zoom_start=3)
folium.PolyLine(b['geometry']['coordinates'], color="blue", weight=2.5, opacity=1).add_to(m)
folium.PolyLine(d['geometry']['coordinates'], color="red", weight=2.5, opacity=1).add_to(m)
folium.PolyLine(nb['geometry']['coordinates'], color="black", weight=2.5, opacity=0.5).add_to(m)

m

You can see the disrupted and the second undisrupted go via the horn of Africa while the first undisrupted uses the Suez canal

SCR-20240605-lkia

genthalili commented 3 weeks ago

thanks @this-josh !

this-josh commented 3 weeks ago

No problem, thanks for a great package!