-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2Raster.py
More file actions
64 lines (42 loc) · 1.61 KB
/
Copy pathArray2Raster.py
File metadata and controls
64 lines (42 loc) · 1.61 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 25 16:50:29 2022
@author: pierreaudisio
"""
from osgeo import gdal
import numpy as np
import osr
path = ""
ds = gdal.Open(path)
array = ds.ReadAsArray()
def array_to_raster(raster, array):
dst_filename = '/home/pierreaudisio/Bureau/Mangrove/test.tif'
# Top left corner coordinate and resolution of the array
GT0, GT1, GT2, GT3, GT4, GT5 = ds.GetGeoTransform()
nrows,ncols = np.shape(array)
# Transformation from the image coordinate space, to the georeferenced coordinate space
geotransform=(GT0, GT1, GT2, GT3, GT4, GT5)
# Open the file
driver = gdal.GetDriverByName('GTiff')
output_raster = driver.Create(dst_filename, ncols, nrows, 1 ,gdal.GDT_Float32)
# Specify its coordinates
output_raster.SetGeoTransform(geotransform)
# Establish its coordinate encoding
src = osr.SpatialReference()
src.ImportFromEPSG(3163)
# Exports the coordinate system to the file
output_raster.SetProjection( src.ExportToWkt() )
# Writes my array to the raster
output_raster.GetRasterBand(1).WriteArray(array)
# Write to disk.
output_raster.FlushCache()
return output_raster.GetRasterBand(1)
#%%
def raster2array(raster, array_bis, outpath, OutPutName):
# Write to TIFF
kwargs = raster.meta
kwargs.update(dtype=rasterio.float32, count=1, compress='lzw')
with rasterio.open(os.path.join(outpath, str(OutPutName) + '.tif'), 'w', **kwargs) as dst:
dst.write_band(1, array_bis.astype(rasterio.float32))
return ()