-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadrsimage.py
63 lines (47 loc) · 1.71 KB
/
readrsimage.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
from osgeo import gdal
import numpy as np
def readrsimage(path):
"""
Read remote sensing image without geo information.
Return a numpy array.
"""
dataset = gdal.Open(path, gdal.GA_ReadOnly)
height = dataset.RasterYSize
width = dataset.RasterXSize
channels = dataset.RasterCount
dtype = np.float32
image = np.zeros((height, width, channels), dtype=dtype)
for band in range(channels):
band_data = dataset.GetRasterBand(band + 1)
image[:, :, band] = band_data.ReadAsArray()
return image
def readrsimage_with_geoinfo(path):
"""
introduction
-----------------------------
read remote sensing image with geoinfomation.
"""
dataset = gdal.Open(path, gdal.GA_ReadOnly)
height = dataset.RasterYSize
width = dataset.RasterXSize
channels = dataset.RasterCount
projection = dataset.GetProjection()
geotransform = dataset.GetGeoTransform()
dtype = np.float32
image = np.zeros((height, width, channels), dtype=dtype)
for band in range(channels):
band_data = dataset.GetRasterBand(band + 1)
image[:, :, band] = band_data.ReadAsArray()
return image, projection, geotransform
def writersimage(save_path, image, format = 'GTiff'):
datatype = gdal.GDT_Float32
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
driver = gdal.GetDriverByName(format)
ds_to_save = driver.Create(save_path, width, height, channels, datatype)
for band in range(channels):
ds_to_save.GetRasterBand(band + 1).WriteArray(image[:, :, band])
ds_to_save.FlushCache()
del image
del ds_to_save