-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfunctions.py
More file actions
134 lines (100 loc) · 3.79 KB
/
Copy pathfunctions.py
File metadata and controls
134 lines (100 loc) · 3.79 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
#!/usr/bin/python
"""
Created on Mon Sep 10 14:37:54 2018
@author: gag
Necessary functions for opening and creating .HDF files. And to make the match between two .HDF files.
"""
import numpy as np
from osgeo import gdal, ogr, gdalconst
import sys
import matplotlib.pyplot as plt
def openFileHDF(file, nroBand):
"""
Function that opens an image with .HDF format and reads a specific band.
Parameters:
-----------
file : complete path of the raster image
nroBand : number of the band to be read
Returns:
--------
src_ds: source raster object
band:
GeoT: georeference
Project: projection
"""
try:
src_ds = gdal.Open(file)
except (RuntimeError, e):
print('Unable to open File')
print(e)
sys.exit(1)
cols = src_ds.RasterXSize
rows = src_ds.RasterYSize
#print cols
#print rows
bands = src_ds.RasterCount
#print bands
# se obtienen las caracteristicas de las imagen HDR
GeoT = src_ds.GetGeoTransform()
#print GeoT
Project = src_ds.GetProjection()
try:
srcband = src_ds.GetRasterBand(nroBand)
except(RuntimeError, e):
# for example, try GetRasterBand(10)
print('Band ( %i ) not found' % band_num)
print(e)
sys.exit(1)
band = srcband.ReadAsArray()
return src_ds, band, GeoT, Project
def createHDFfile(path, nameFileOut, driver, GeoT, Projection, img, xsize, ysize):
"""
Function that creates a new .HDF file.
Parameters:
-----------
path, nameFileOut: path and name of the file to be created,
driver: file type
GeoT, Projection: geotransform and projection data
img: data that represent the image
xsize, ysize: image size
Returns:
--------
"""
print("archivo creado:" + str(nameFileOut))
driver = gdal.GetDriverByName(driver)
ds = driver.Create(path + nameFileOut, xsize, ysize, 1, gdal.GDT_Float64)
ds.SetProjection(Projection)
geotransform = GeoT
ds.SetGeoTransform(geotransform)
ds.GetRasterBand(1).WriteArray(np.array(img))
return
def matchData(data_src, data_match, nRow, nCol, type):
"""
Function that performs the match to a raster data from a source raster modifying the projection,
the transformation and the size. Different interpolation methods are used.
Parameters:
-----------
data_src: raster source
data_match: raster to match
nRow, nCol: raster size
type: interpolation method ('Nearest', 'Bilinear', 'Cubic', 'Average')
Returns:
--------
data_result: a new raster created in memory
"""
#data_result = gdal.GetDriverByName('MEM').Create('', data_match.RasterXSize, data_match.RasterYSize, 1, gdalconst.GDT_Float64)
data_result = gdal.GetDriverByName('MEM').Create('', nCol, nRow, 1, gdalconst.GDT_Float64)
# Se establece el tipo de proyección y transfomcion en resultado que va ser coincidente con data_match
data_result.SetGeoTransform(data_match.GetGeoTransform())
data_result.SetProjection(data_match.GetProjection())
# se cambia la proyeccion de data_src, con los datos de data_match y se guarda en data_result
if (type == "Nearest"):
gdal.ReprojectImage(data_src,data_result,data_src.GetProjection(),data_match.GetProjection(), gdalconst.GRA_NearestNeighbour)
if (type == "Bilinear"):
gdal.ReprojectImage(data_src, data_result, data_src.GetProjection(), data_match.GetProjection(), gdalconst.GRA_Bilinear)
if (type == "Cubic"):
gdal.ReprojectImage(data_src, data_result, data_src.GetProjection(), data_match.GetProjection(), gdalconst.GRA_Cubic)
if (type == "Average"):
gdal.ReprojectImage(data_src, data_result, data_src.GetProjection(), data_match.GetProjection(), gdal.GRA_Average)
return data_result