Skip to content

Instantly share code, notes, and snippets.

@monga
Created October 11, 2021 11:46
Show Gist options
  • Save monga/c0db0d92e2e48ada5c50b4d2fcd9be5b to your computer and use it in GitHub Desktop.
Save monga/c0db0d92e2e48ada5c50b4d2fcd9be5b to your computer and use it in GitHub Desktop.
from IPython.display import display, HTML, IFrame
import geopandas
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
from shapely.geometry import Point
def is_land(longitude, latitude, w=world):
"""Check if the point (longitude, latitude) is within a country in the world shape GeoDataFrame
OK for one, but for more a spatial join is much faster
"""
for _, country in w.iterrows():
if Point(longitude, latitude).within(country['geometry']):
return (True, country)
return (False, None)
points = (90+90)*np.random.random((9,2)) - 90
for p in points:
lat, long = p
print('{}'.format('L' if is_land(long,lat)[0] else 'W'))
display(IFrame("https://www.openstreetmap.org/export/embed.html?bbox={}%2C{}%2C{}%2C{}&layer=mapnik".format(long,lat,long+1e-5*300,lat+1e-5*300),
width=300, height=300))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment