-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneral Raster Operations.py
More file actions
42 lines (32 loc) · 1.93 KB
/
General Raster Operations.py
File metadata and controls
42 lines (32 loc) · 1.93 KB
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
from osgeo import gdal
import numpy as np
import matplotlib.pyplot as plt
#1 Open a dataset and collect the information
ds = gdal.Open("srtm_52_06.tif")
geot = ds.GetGeoTransform() ## get geotransform
proj = ds.GetProjection() ## get projection information
nob = ds.RasterCount() ## number of bands
band1 = ds.GetRaster(1) ## read the band which is needed - 1/2/3
array = band1.ReadAsArray() ## read the band as array
plt.figure()
plt.imshow(array) ## plotting the image
#2 create a condition for DEM> mean(height) or DEM>4000m
mask_mean = np.where((array >= np.mean(array)),1,0)
mask_4000 = np.where((array >= 4000),1,0) ## if DEM>(height) = 1 otherwise 0
height_4000 = (mask_4000 * array) ## height>4000 = mask* DEM_array
plt.figure()
plt.imshow(height_4000) ## plotting the image
#3 Set up GDAL output parameters and save the band informations
driver = gdal.GetDriverByName("GTiff") ## call the driver geotiff
driver.Register() ## register the driver
output = driver.Create("GTiff", ## type of the image
xsize = height_4000.shape[1], ## size of the x
ysize = height_4000.shape[0], ## size of the y
bands = 1, ## number of bands
eType = gdal.GDT_Int16) ## type of data
output.SetGeoTransform(geot) ## provide same geotransform information
output.SetProjection(proj) ## provide same projection information
outband = output.GetRasterBand(1) ## write the dataset bands
outband.WriteArray(height_4000) ## write the dataset as array
outband.SetNoDataValue(np.nan) ## Set no data value to NaN
outband.FlushCache()