-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgdal_utils.py
101 lines (75 loc) · 3.05 KB
/
gdal_utils.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import tempfile
import os.path as pth
import numpy as np
from pyDMS.pyDMSUtils import saveImg, openRaster, getRasterInfo
#Since the conda environment is not active
#make sure to set the env_variables needed for gdal
import os
cur_path = os.path.dirname(os.path.abspath(__file__))
if os.name == 'nt':
os.environ["PROJ_LIB"] = os.path.join(cur_path, "../Library/share/proj")
os.environ["GDAL_DATA"] = os.path.join(cur_path, "../Library/share/gdal")
else:
os.environ["PROJ_LIB"] = os.path.join(cur_path, "../share/proj")
os.environ["GDAL_DATA"] = os.path.join(cur_path, "../share/gdal")
from osgeo import gdal
def slope_from_dem(dem_file_path, output=None):
if not output:
output = dem_file_path.replace('.tif', '_slope.tif')
gdal.DEMProcessing(output, dem_file_path, "slope", computeEdges=True)
return output
def aspect_from_dem(dem_file_path, output=None):
if not output:
output = pth.splitext(dem_file_path)[0]+'_aspect.tif'
gdal.DEMProcessing(output, dem_file_path, "aspect", computeEdges=True)
return output
def save_image(data, geotransform, projection, filename):
return saveImg(data, geotransform, projection, filename)
def resample_with_gdalwarp(src, template, resample_alg="cubicspline"):
# Get template projection, extent and resolution
proj, gt, sizeX, sizeY, extent, _ = raster_info(template)
# Resample with GDAL warp
out_ds = gdal.Warp("",
src,
format="MEM",
dstSRS=proj,
xRes=gt[1],
yRes=gt[5],
outputBounds=extent,
resampleAlg=resample_alg)
return out_ds
def raster_info(raster):
return getRasterInfo(raster)
def raster_data(raster, bands=1, rect=None):
def _read_band(fid, band, rect):
if rect:
return fid.GetRasterBand(band).ReadAsArray(rect.x, rect.y, rect.width, rect.height)
else:
return fid.GetRasterBand(band).ReadAsArray()
fid, closeOnExit = openRaster(raster)
if type(bands) == int:
bands = [bands]
data = None
for band in bands:
if data is None:
data = _read_band(fid, band, rect)
else:
data = np.dstack((data, _read_band(fid, band, rect)))
if closeOnExit:
fid = None
return data
def merge_raster_layers(input_list, output_filename, separate=False):
merge_list = []
for input_file in input_list:
bands = raster_info(input_file)[5]
# GDAL Build VRT cannot stack multiple multi-band images, so they have to be split into
# multiple singe-band images first.
if bands > 1:
for band in range(1, bands+1):
temp_filename = tempfile.mkstemp(suffix="_"+str(band)+".vrt")[1]
gdal.BuildVRT(temp_filename, [input_file], bandList=[band])
merge_list.append(temp_filename)
else:
merge_list.append(input_file)
fp = gdal.BuildVRT(output_filename, merge_list, separate=separate)
return fp