point([lat, lon]).within(us_geo_thing)
# Mask off the seas.
import cartopy.io.shapereader as shpreader
from cartopy.mpl.patch import geos_to_path
from shapely.geometry import Point
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
reader = shpreader.Reader(shpfilename)
countries = reader.records()
us_multipoly, = [country.geometry for country in countries
if country.attributes['name'] == 'United States']
main_us_geom = sorted(us_multipoly.geoms, key=lambda geom: geom.area)[-1]
def get_mask(cube, geom):
mask = np.ones(cube.data.shape)
p = -1
for i in np.ndindex(cube.data.shape):
if i[0] != p:
print i[0],
p = i[0]
this_cube = cube[i]
this_lat = this_cube.coord('latitude').points[0]
this_lon = this_cube.coord('longitude').points[0] - 360
this_point = Point(this_lon, this_lat)
mask[i] = this_point.within(geom)
return mask
--
You received this message because you are subscribed to the Google Groups "Iris" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scitools-iri...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I also have a branch of iris which adds such a capability to iris - though I didn't finish making it merge-worthy at this point (I can dig that out if you're interested in taking it on).
Also, Filipe wrote a blog post about the manual way of doing this: https://ocefpaf.github.io/python4oceanographers/blog/2015/08/17/shapely_in_polygon/.
--
If you do end up rolling your own, you will quickly find it is painfully slow unless you apply some optimisations. The first you will find is to "prepare" your geometry. Doing so makes a huge difference to the run time. If you're doing masking type operations (which we are all describing) there is also some built-in functionality within Shapely (I added it a few years ago now) which vectorises the masking operation to squeeze out every last drop of performance.
My original notebook is available at http://nbviewer.ipython.org/gist/pelson/9785576.
Fillipe, feel free to write a super-awesome blog post about shapely.vectorized ;)
rasterio (via GDAL) has very high performance for rasterizing shapes. In my experience, it's much faster than shapely.
Here's an example using xray -- I imagine it would be pretty easy to adapt to Iris:
Cheers,Stephan