1

I have MODIS EVI file stacking with 10bands. I use Python 3 in Spyder.

I'm trying to rastersize shapefile and do mask to my MODIS EVI.

This is my script :

import os
import glob
from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage
import pandas as pd
import datetime as dt
import ogr
import rasterio as rio 
import fiona
import rasterio
import rasterio.mask

inDir = 'D:/projetp/Doy_2000/'
driver = ogr.GetDriverByName('ESRI Shapefile')
os.chdir(inDir)  

with fiona.open("C:/Doy2000/1996n.shp", "r") as shapefile:
    shapes = [feature["geometry"] for feature in shapefile]

with rasterio.open("D:/projetp/Doy_2000/MODIS_EVI_2000.tif") as src:
    out_image, out_transform = rasterio.mask.mask(src, shapes, crop=True)
    out_meta = src.meta

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

with rasterio.open("MODIS_MASK_2000.tif", "w", **out_meta) as dest:
    dest.write(out_image)

when i whrite out_image in spyder i hade this resultas

print(out_image)
[[[   0    0    0 ...    0    0    0]
  [   0    0 1682 ...    0    0    0]
  [   0 1841 1888 ...    0    0    0]
  ...
  [   0    0    0 ... 2180    0    0]
  [   0    0    0 ... 4033    0    0]
  [   0    0    0 ...    0    0    0]]

 [[   0    0    0 ...    0    0    0]
  [   0    0 1659 ...    0    0    0]
  [   0 1829 1830 ...    0    0    0]
  ...
  [   0    0    0 ... 3953    0    0]
  [   0    0    0 ... 4627    0    0]
  [   0    0    0 ...    0    0    0]]

 [[   0    0    0 ...    0    0    0]
  [   0    0 1706 ...    0    0    0]
  [   0 1547 1740 ...    0    0    0]
  ...
  [   0    0    0 ... 4419    0    0]
  [   0    0    0 ... 4564    0    0]
  [   0    0    0 ...    0    0    0]]

 ...

 [[   0    0    0 ...    0    0    0]
  [   0    0 1998 ...    0    0    0]
  [   0 2255 1841 ...    0    0    0]
  ...
  [   0    0    0 ... 3339    0    0]
  [   0    0    0 ... 3501    0    0]
  [   0    0    0 ...    0    0    0]]

 [[   0    0    0 ...    0    0    0]
  [   0    0 1992 ...    0    0    0]
  [   0 3337 3337 ...    0    0    0]
  ...
  [   0    0    0 ... 2058    0    0]
  [   0    0    0 ... 2080    0    0]
  [   0    0    0 ...    0    0    0]]

 [[   0    0    0 ...    0    0    0]
  [   0    0 7913 ...    0    0    0]
  [   0 7659 7294 ...    0    0    0]
  ...
  [   0    0    0 ... 5014    0    0]
  [   0    0    0 ... 4626    0    0]
  [   0    0    0 ...    0    0    0]]]

And when I visualize my out_image in QGIS I have this result

enter image description here

Do I need to delete the value 0 or not? And how can I show my out_images in Python?

The values are so big. How can I apply a scale factor *1/10000 to have values (-1,1)? - How can I calculate statistics ( mean of pixel of my out_image just inside the area not in black area )! Because I need just mean of pixel for all bands to export after in dataframe!

closed as unclear what you're asking by user2856, Mark Ireland, Jochen Schwarze, Andre Silva, whyzar Jan 17 at 21:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    These questions relate more to numpy indexing and broadcasting than anything else, so read up on those topics. You can do things like np.mean(out_image[out_image != 0]) to mask out zeros when calculating stats. – mikewatt Jan 15 at 19:50
  • @ need i to delete the value 0 or not ? thank u – loula melyacou Jan 15 at 19:54
  • @Luke thanks but in my mask.tif i don't have 0 in all values is just for black pixel i need just remove it – loula melyacou Jan 15 at 21:51
  • @Luke how can i do that change it to another value cause i need just the area in cercle not in the black area !? – loula melyacou Jan 15 at 22:35