The best way to get the coordinates from drawcoastlines()is to use its class attribute get_segments(). There is an example of how you can get the distance from the coast to one point with longitude and latitude in decimal degrees. You can adapt this function to use a unique map to calculate all the points in the list. Hope this helps you.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
def distance_from_coast(lon,lat,resolution='l',degree_in_km=111.12):
plt.ioff()
m = Basemap(projection='robin',lon_0=0,resolution=resolution)
coast = m.drawcoastlines()
coordinates = np.vstack(coast.get_segments())
lons,lats = m(coordinates[:,0],coordinates[:,1],inverse=True)
dists = np.sqrt((lons-lon)**2+(lats-lat)**2)
if np.min(dists)*degree_in_km<1:
return True
else:
return False
Another way to get it:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import os
def save_coastal_data(path,resolution='f'):
m = Basemap(projection='robin',lon_0=0,resolution=resolution)
coast = m.drawcoastlines()
coordinates = np.vstack(coast.get_segments())
lons,lats = m(coordinates[:,0],coordinates[:,1],inverse=True)
D = {'lons':lons,'lats':lats}
np.save(os.path.join(path,'coastal_basemap_data.npy'),D)
def distance_from_coast(lon,lat,fpath,degree_in_km=111.12):
D = np.load(fpath).tolist()
lons,lats = D['lons'],D['lats']
dists = np.sqrt((lons-lon)**2+(lats-lat)**2)
print np.min(dists)*degree_in_km
path = 'path/to/directory'
save_coastal_data(path,resolution='h')
distance_from_coast(-117.2547,32.8049,
os.path.join(path,'coastal_basemap_data.npy'))
I have 0.7 km.
source
share