简体   繁体   中英

Calculating parameter on raster using gdal and python

I'm trying to create a script which reads an array of rasters and calculate a parameter, but I get this error:

operands could not be broadcast together with shapes (2718,4310) (70,86).

# -*- coding: cp1252 -*-
from osgeo import gdal
import numpy as np
import sys ,os
from osgeo.gdalconst import *
output_dir='C:\\wamp\\www\\Donnees_CRTS\\irrisat_dessimination-smiej'
etp = os.path.join(output_dir,"L8_L8_ETpot_24_30m_2016_223.tif")
rain = os.path.join(output_dir,"GLDAS_NOAH025SUBP_3H.A2016223.sumday.rain.tif")
eff = os.path.join(output_dir,"eff_v11.tif")
driver=gdal.GetDriverByName('GTIff')
driver.Register()
paths = []
paths.append(etp)
paths.append(rain)
paths.append(eff)
raster_px = []
rasters_px = []
bands = []
def open_raster(raster):       
    for i in range(len(paths)) :
       raster = gdal.Open(paths[i])
       columns = raster.RasterXSize
       lines = raster.RasterYSize
       band = raster.GetRasterBand(1).ReadAsArray(0,0,columns,lines)
       bands.append(band)
       gt=raster.GetGeoTransform()
       raster_px.append(gt)
    
       band=None  
   if raster is None :
    print ("Erreur : Impossible d'ouvrir le raster: ")
   try :
    smallest = min(raster_px)
    raster.SetGeoTransform(smallest)

    
   except:
    print ("Erreur : Impossible d'extraire la bande")

  return smallest,  columns, lines



def parameters(parameter) :
    diff = bands[0]-bands[1]
   if parameter == "IWR" :
     iwr=diff/bands[2]
return iwr



def createImage(new_image,columns,lines,smallest):

    new_image=driver.Create("iwr9.tif", columns,lines, 1, gdal.GDT_Byte)
    new_image.SetProjection("EPSG:4326")
    new_image.SetGeoTransform(smallest)
    new_image.GetRasterBand(1).WriteRaster( 0, 0, columns, lines, output_dir 
  )
  new_image=None
  return columns, lines

def main() :

smallest, columns, lines = open_raster(paths)
p = "IWR"

parameters(p)
createImage("iwr9.tif",columns, lines,smallest)
main()

From what I understand, you do the following:

You have three different.tif-files, each containing one band. You read the content of these images into (numpy)-arrays and store them in a list of arrays, called bands . So band[0] is an array of etp, band[1] is an array of rain, band[2] is an array of eff. These three arrays have a x&y-dimension (ie the dimensions of the image/"picture").

bands[1] - bands[0] is an array division which means that you subtract the value of each array item from the item of the other array.

For instance

import numpy as np
x = np.array([10, 20, 30, 40, 50])
y = np.array([1, 2, 3, 4, 5])
z = x - y # print(z): 9 18 27 36 45

The example above deals with 1D-array, but the same works for 2D-arrays. Now if python tells you that it cannot "broadcast" one array into the other, then it means that the item-wise calculation failed as the shape of the arrays are different . In the above example if x has length 5 but y has lenght 4, then numpy/python cannot know what to do with the missing items and throws an error.

You can check that by looking at the shape of your bands item (make sure you import numpy as np )

np.shape(bands[0])
np.shape(bands[1])
np.shape(bands[2])

Probably they will differ which causes your error. So at first you would have to make sure that all your images have the same size, before you perform pixel-wise algebra.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM