我想计算由值1和2组成的两个栅格图层(ramdom,模拟)的成本,第一个是随机创建的,第二个是使用两个相关的成本图层进行优化。代码看起来有点像下面。但是,我无法使该功能读取我的第二个光栅。
driver1 = gdal.GetDriverByName ('RST')
driver1.Register ()
ready = gdal.Open ('ramdom.rst', gdal.GDT_Byte)
cols = ready.RasterXSize
rows = ready.RasterYSize
bands = ready.RasterCount
geotransform = ready.GetGeoTransform()
read_ramdom = ready.ReadAsArray ()
global sum1, sum2, value1, value2
sum1, sum2, value1, value2 = 0, 0, 0, 0
classes = 2
global ssum
def cost(something):
global ssum
global sum1, sum2, value1, value2
global classes
while classes >=0:
if something == something:
something = ready.ReadAsArray()
elif something == something2:
something = simul_read.ReadAsArray()
for i in range (0, rows):
for j in range (0, cols):
numb = something [i, j]
if numb == 1:
one = names [numb -1]
ds1 = gdal.Open (one, gdal.GDT_Float32)
data1 = ds1.ReadAsArray (j, i, 1, 1)
value1 = data1 [0, 0]
sum1 = sum1+value1
if numb == 2:
two = names [numb -1]
ds2 = gdal.Open (two, gdal.GDT_Float32)
data2 = ds2.ReadAsArray (j, i, 1, 1)
value2 = data2 [0, 0]
sum2 = sum2+value2
classes = classes - 1
ssum = sum1 + sum2
return ssum
simul_read = gdal.Open ('simulated.rst', gdal.GDT_Byte)
read_simulated = simul_read.ReadAsArray ()
A = cost(something)
B = cost(something2)
C = cost(something) - cost(something2)
print ' The initial COST was :',' ', A, '\n'
print ' The final COST is :',' ', B, '\n'
print ' The gain is :', C , '\n'