[GIS] Masking land with matplotlib basemap in Python

geolocationmatplotlib-basemappython

I've been trying to mask land with .is_land from mpl_toolkits.basemap.

When running following code:

map = Basemap(llcrnrlon = 7.298914095230308, llcrnrlat = 58.98235690451632, 
           urcrnrlon = 12.27072348324015, urcrnrlat = 
           57.92306182768044,projection='cyl', resolution='f')
value = map.is_land(11.61168822665539, 57.86868795294363)

print(value) returns False

but when not specifying region:

map = Basemap(projection='cyl', resolution='f')
value = map.is_land(11.61168822665539, 57.86868795294363)

print(value) returns True

I can't figure out why.

I really do need to specify region or else the code will be 5-6 times slower to run.

Best Answer

Basemap.is_land() uses point-in-polygon algorithm to find a solution, it requires one of the GSHHS coastline polygons in the data files supplied with Basemap package. With resolution='f', you ask for the biggest file (80 MB) with most detailed coastlines and lakes. When you don't specify lat-long extents, the whole world coastline data is loaded and run. But, when lat-long limits are specified, the world coastline polygons are sliced/cut for the required parts. This simplifies the coastline data before use, some vertex points might be deleted/added, causing deviation on parts of the coastlines/lakes. It also filters out polygons outside specified extents. This process could be the reasons to your findings. Also note that points over lakes inside land regions are not counted as land points.