python - Matplotlib Basemap Coastal Coordinates -
is there way query basemap extract coastal coordinates? user provides lat/lng
, function returns true/false
if coordinates within 1km coast?
the best way coordinates drawcoastlines()
using class attribute get_segments()
. there example how can distance coast single point longitude ans latitude in decimal degrees. can adapt function use unique map calculate points in list. hope it's you.
from mpl_toolkits.basemap import basemap import matplotlib.pyplot plt import numpy 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 it:
from mpl_toolkits.basemap import basemap import matplotlib.pyplot plt import numpy 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 #define path path = 'path/to/directory' #run 1 time save data. cost less time save_coastal_data(path,resolution='h') distance_from_coast(-117.2547,32.8049, os.path.join(path,'coastal_basemap_data.npy'))
i've got 0.7 km.
Comments
Post a Comment