I am using rasterio to cut out the shape of a lake outlined in my shapefile from a geotiff. When using a shapefile of a singular lake with a geotiff, this works perfectly, but not when cutting out a geotiff of a single lake with a different shapefile that contains a lot of different lake outlines. I'm wondering if there is a problem with the way my code is written or the files I'm using?

Here's my code:

import rasterio
import rasterio.mask
import fiona

with fiona.open("liag_shapes/liag_shapes.shp", "r") as shapefile:
    features = [feature["geometry"]for feature in shapefile]

with rasterio.open("lakesimcoe11.tif") as src:
    out_image, out_transform = rasterio.mask.mask(src, features, 
filled=True, crop=True)
    out_meta = src.meta.copy()

out_meta.update({"driver": "GTiff",
             "height": out_image.shape[1],
             "width": out_image.shape[2],
             "transform": out_transform})

with rasterio.open("new_test.tif", "w", **out_meta) as dest:
  • In my experience an all black output is often indicative mismatched NoData values. Have you tried plotting out_image before you write it to see what it looks like? Also check the minimum value of out_image and see if it makes sense. I don't see any obvious issues with your code. – Charlie Parr Jul 18 '18 at 20:35
  • Thanks I'll try that, hopefully will at least give me a better idea of what's going on. – Kerry O'Neill Jul 20 '18 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.