Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_land slow for multiple coordinates #462

Open
jakobloeg opened this issue May 29, 2019 · 2 comments
Open

is_land slow for multiple coordinates #462

jakobloeg opened this issue May 29, 2019 · 2 comments

Comments

@jakobloeg
Copy link

Hello, I have an application where I would like to call is_land multiple times and for many coordinates, in the order of millions. Is there a way to speed up this process, instead of having to call is_land with one coordinate at the time. This is simply too slow for my application

@WeatherGod
Copy link
Member

WeatherGod commented May 29, 2019 via email

@molinav
Copy link
Member

molinav commented Jun 14, 2019

You can create a vectorized version of is_land with the help of maskoceans:

def is_land(lon, lat, resolution="l", grid=5):

    from mpl_toolkits.basemap import maskoceans

    lon, lat = np.broadcast_arrays(lon, lat)
    dummy = np.zeros(lon.shape, dtype=bool)
    mask = ~maskoceans(lon, lat, dummy, resolution=resolution, grid=grid).mask

    return mask

and use it e.g. as follows:

step = 0.25
lons = +0.5 * step + np.arange(-180, +180, +step)[None, :]
lats = -0.5 * step + np.arange(+90, -90, -step)[:, None]

land = is_land(lons, lats)

I use a similar function for my own purposes, and it is way faster than the normal is_land. However, the maskoceans function from my current version of basemap (1.0.8) cannot return correct values in the neighbourhood of latitudes equal to ±90° or longitudes equal to ±180°. This seems to happen when you request a coordinate that is outside the limits from the underlying GSHHG maps. However, Basemap.is_land is capable of handling such cases. I should report it at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants