I have raster (with 5 bands) and shapfile in the same projection. I do this script for rastersize and do mask and clip raster or crop raster with mask but I need to have just clip raster without black area and without NONE VALUE or 0 value.

I use Python 3

import os
from osgeo import gdal
import numpy as np
import pandas as pd
import ogr
import rasterio as rio 
import fiona
import rasterio
import rasterio.mask

inDir = 'F:/python/Doy_2000/'
driver = ogr.GetDriverByName('ESRI Shapefile')

with fiona.open("F:/python/Doy_2000/poly2000.shp", "r") as shapefile:
    shapes = [feature["geometry"] for feature in shapefile]

with rasterio.open("F:/python/Doy_2000/raster1.tif") as src:
    out_image, out_transform = rasterio.mask.mask(src, shapes, 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("2000mask.tif", "w", **out_meta) as dest:

result after script :

enter image description here

I need to edit my script but just delete or mask NONE value or 0 value.

second question :

when i do the same thing for other shapefile ( polygones very small like points): enter image description here the resultas give me all area black :

enter image description here

how i can do to calcul mask and clip raster with shapfiles ( polygones verry small) ?!

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.