-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusu04b.py
85 lines (67 loc) · 2.46 KB
/
usu04b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# USU OS Python Assignment 4b
#
# Write a script to calculate the average pixel value for the bands in aster.img.
# Read and process the data one block at a time. Do the calculation two ways:
# first including zeros in the calculation and then ignoring zeros.
import gdal
from gdalconst import *
import numpy as np
import time
gdal.UseExceptions()
rasterfile = 'data/usu04/aster.img'
# rasterfile = 'data/usu04/usgs-hoopa.tif'
# rasterfile = 'data/usu04/naip-stapp.tif'
# Register the raster driver and open the data source.
# rastDriver = gdal.GetDriverByName('HFA')
gdal.AllRegister()
ds = gdal.Open(rasterfile, GA_ReadOnly)
if ds is None:
print('Can''t open raster data file ' + rasterfile)
exit(1)
cols = ds.RasterXSize
rows = ds.RasterYSize
bands = ds.RasterCount
print('\nRaster file: ' + rasterfile)
print('\nRows x Columns x Bands: {0:d} x {1:d} x {2:d}'.format(rows, cols, bands))
print()
for n in range(1, bands+1):
band = ds.GetRasterBand(n)
xblock, yblock = band.GetBlockSize()
datatype = gdal.GetDataTypeName(band.DataType)
color_interp = gdal.GetColorInterpretationName(band.GetColorInterpretation())
print('Band {0}: Block={1}x{2} Type={3} ColorInterp={4}'.format(
n, xblock, yblock, datatype, color_interp))
startTime = time.time()
# Initialize arrays for the data and stats lists.
data = [None for n in range(bands+1)]
stats = [None for n in range(bands+1)]
for n in range(1, bands+1):
stats[n] = {'sum': 0.0, 'nonzero': 0}
# Read all bands a block at a time.
for yoff in range(0, rows, yblock):
for xoff in range(0, cols, xblock):
xsize = min(cols - xoff, xblock)
ysize = min(rows - yoff, yblock)
for n in range(1, bands+1):
band = ds.GetRasterBand(n)
data[n] = band.ReadAsArray(xoff, yoff, xsize, ysize)
# One block from each band is loaded.
for n in range(1, bands+1):
stats[n]['sum'] += data[n].astype(np.float).sum()
stats[n]['nonzero'] += np.greater(data[n], 0).sum()
band = data = None
pixel_count = rows * cols
fmt = 'Band {0}: Sum={1:,.0f} Pixels={2:,d} NonZeroPixels={3:,d} ' \
+ 'Mean={4:.2f} NonZeroMean={5:.2f}'
print()
for n in range(1, bands+1):
print(fmt.format(
n, stats[n]['sum'], pixel_count, stats[n]['nonzero'],
stats[n]['sum'] / pixel_count,
stats[n]['sum'] / stats[n]['nonzero']
))
endTime = time.time()
print('\ntime: {0:.3f} sec'.format(endTime - startTime))
# Done.
ds = None
exit(0)