Rasterio: .mask() not reading geometry

Created on 17 May 2018  路  4Comments  路  Source: mapbox/rasterio

Thanks for the great work! I have an error that is making no sense to me.
cropping a raster with a geojson:

geoms = {
             'geometry': {
                 'type': 'Polygon',
                 'coordinates':
                     [[[15.303386083270999, -4.373279567953402], [15.303475916729, -4.373279567953402],
                       [15.303475916729, -4.373186432046598], [15.303386083270999, -4.373186432046598]]]
             }
    }

    with rasterio.open(base_raster) as src:
        out_image, out_transform = mask(src, geoms, crop=True)

however features.py returns an error:

    geom = geometry.get('geometry') or geometry
AttributeError: 'str' object has no attribute 'get'

but attribute `geometry' is there!

Operating system

Windows 10

Rasterio version and provenance

'1.0a12'

Most helpful comment

Sorry about the confusion @lorenzori. I've updated docstrings to help out.

All 4 comments

@lorenzori geoms should be a list instead of a dict: https://github.com/mapbox/rasterio/blob/master/rasterio/mask.py#L124

I see now that our description in the docstring is a little confusing, as we say this on the next line:

GeoJSON-like dict representation of polygons that will be used to
        create the mask.

However, what we mean is that each item in the list is a GeoJSON-like dict.

This should work:

geoms = ['geometry': {
                 'type': 'Polygon',
                 'coordinates':
                     [[[15.303386083270999, -4.373279567953402], [15.303475916729, -4.373279567953402],
                       [15.303475916729, -4.373186432046598], [15.303386083270999, -4.373186432046598]]]
             }
]

Sorry about the confusion @lorenzori. I've updated docstrings to help out.

Got it, thanks for the support! just for future reference, this example works:

geoms = [{'type': 'Polygon', 'coordinates': [
        [(15.173283, -4.293467), (15.372248, -4.283885),
         (15.168365, -4.479364), (15.448367, -4.506647)]]}]

@lorenzori thanks for that note. This also worked for me when other solutions have not.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ungarj picture ungarj  路  4Comments

sgillies picture sgillies  路  3Comments

valpesendorfer picture valpesendorfer  路  3Comments

lwasser picture lwasser  路  3Comments

snowman2 picture snowman2  路  4Comments