I have this code from here:
Getting pixel value of GDAL raster under OGR point without NumPy?
from math import floor
from osgeo import gdal,ogr
src_filename = '/tmp/test.tif'
shp_filename = '/tmp/test.shp'
src_ds=gdal.Open(src_filename)
gt_forward=src_ds.GetGeoTransform()
gt_reverse=gdal.InvGeoTransform(gt_forward)
rb=src_ds.GetRasterBand(1)
ds=ogr.Open(shp_filename)
lyr=ds.GetLayer()
for feat in lyr:
geom = feat.GetGeometryRef()
mx,my=geom.GetX(),
geom.GetY() #coord in map units
#Convert from map to pixel coordinates.
px,py=gdal.ApplyGeoTransform(gt_reverse, mx, my)
px = floor(px) #x pixel
py = floor(py) #y pixel
intval=rb.ReadAsArray(px,py,1,1)
print intval[0] #intval is a numpy array, length=1 as we only asked for 1 pixel value
I have a .vrt that has 7 bands. I want to iterate over each band and to extract the pixel values for each band and to save it to a nested list.
pixel_values=[['fid_1',value1,value2,value3,value4,value6,value7],['fid_2', value1,value2,value3,value4,value6,value7].. ..]
After that I want to export it to a csv.
Example:
pixel_values =[]
for band in range(1,8):
pixel_value.append(intval[0])
pixel_value.to_csv('data.csv')
My question is that: How can I extract the pixel value based on a point .shp file from a raster with multiple bands?