[gdal-dev] count unique combination in 2 tifs

Giuseppe Amatulli giuseppe.amatulli at gmail.com
Wed Aug 8 15:42:48 PDT 2012


Hi,
having 2 integer Tif file (Rast00 , Rast10)

Rast00= 1,2,4,4,5,4,1,4,1,1,4,0
Rast10= 0,2,3,5,4,4,1,4,2,1,3,4

I need to exclude the no-data (0) and calculate unique combination in
pairs and count the observations
in order to obtain  something like this?
(2,2,1)
(4,3,2)
(4,5,1)
(5,4,1)
(4,4,2)
(1,1,2)
(1,2,1)
and save to a txt file:

I identify two ways:

First option (faster)

Rast00=dsRast00.GetRasterBand(1).ReadAsArray()
Rast10=dsRast10.GetRasterBand(1).ReadAsArray()

mask=( Rast00 != 0 ) & ( Rast10 != 0  )

Rast00_mask= Rast00[mask]
Rast10_mask= Rast10[mask]

array2D = np.array(zip( Rast00_mask,Rast10_mask))

unique=dict()
for row in  array2D :
    row = tuple(row)
    if row in unique:
        unique[row] += 1
    else:
        unique[row] = 1

print unique
(how to save unique to a txt file without dictionary format? )

Second option (slower)

Rast00=dsRast00.GetRasterBand(1).ReadAsArray()
Rast10=dsRast10.GetRasterBand(1).ReadAsArray()

array2D=np.array(zip(np.concatenate(Rast00),np.concatenate(Rast10)))
Rast00
unique=dict()
for row in  array2D :
    row = tuple(row)
    if row[0] != 0 :
        if row[1] != 0 :
            if row in unique:
                unique[row] += 1
            else:
                unique[row] = 1
print unique
(how to save unique to a txt file without dictionary format? )

In reality i was suppose the second option faster than the first one
because the Rast00 & Rast10 array were read only ones in the second
option, but this is not the case.
Is there a way to speed up the process?
I also try
1) to read the tif row by row
2) and create array index rather than unique=dict()
but the speeding is not improve.
Any suggestions are well come?
Thanks
-- 
Giuseppe Amatulli
Web: www.spatial-ecology.net


More information about the gdal-dev mailing list