-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The DataLoader object loads all files with a specific extension in the given directory. All the loaded files are stored in ascending order to keep track of the data's year. Issue #9.
- Loading branch information
Showing
8 changed files
with
241 additions
and
167 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import numpy as np | ||
import os | ||
import sys | ||
from osgeo import gdal | ||
|
||
class DataLoader(): | ||
|
||
def __init__(self, data_dir): | ||
self.data_dir = data_dir | ||
self.files = [] | ||
self.arrays = [] | ||
|
||
def load_directory(self, ext): | ||
for file in os.listdir(self.data_dir): | ||
if file.endswith(ext): | ||
# Stores the file without extension | ||
self.files.append(os.path.splitext(file)[0]) | ||
|
||
print(os.path.join(self.data_dir, file)) | ||
|
||
# Turning all the string-values into integers | ||
self.files = [int(file) for file in self.files] | ||
|
||
# Sorts the file in ascending order based on the year | ||
self.files = sorted(self.files, key=int) | ||
|
||
# Turns the integers back into string values with extensions | ||
self.files = [str(file) + ext for file in self.files] | ||
|
||
def create_np_arrays(self): | ||
for file in self.files: | ||
pop_data = gdal.Open(os.path.join(self.data_dir, file)) | ||
array = np.array(pop_data.GetRasterBand(1).ReadAsArray()) | ||
# Null-values (neg-values) are replaced with zeros | ||
array[array < 0] = 0 | ||
self.arrays.append(array) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import numpy as np | ||
import numpy as np | ||
import os | ||
import sys | ||
from osgeo import gdal | ||
import osr | ||
|
||
base_dir = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.append(base_dir) | ||
data_dir = os.path.relpath('..\\data', base_dir) | ||
config_dir = os.path.relpath('..\\configs', base_dir) | ||
|
||
|
||
# Loads the geotiff | ||
pop_data_10 = gdal.Open(os.path.join(data_dir, '2010.tif')) | ||
|
||
# Store the values of the geotif into a np.array | ||
pop_arr_10 = np.array(pop_data_10.GetRasterBand(1).ReadAsArray()) | ||
pop_arr_10 = np.delete(pop_arr_10, -1, axis=1) # Shape not the same as pop data from 14 and 15 | ||
|
||
print(pop_arr_10.shape) | ||
|
||
Projection = osr.SpatialReference() | ||
Projection.ImportFromWkt(pop_data_10.GetProjectionRef()) | ||
|
||
geoTransform = pop_data_10.GetGeoTransform() | ||
|
||
driver = gdal.GetDriverByName('GTiff') | ||
|
||
dst_ds = driver.Create('2010.tif', xsize=pop_arr_10.shape[1], ysize=pop_arr_10.shape[0], | ||
bands=1, eType=gdal.GDT_Float32) | ||
|
||
dst_ds.SetGeoTransform(( | ||
geoTransform[0], # x_min | ||
geoTransform[1], # pixel width | ||
geoTransform[2], # rotation | ||
geoTransform[3], # y_max | ||
geoTransform[4], # rotation | ||
geoTransform[5] # pixel height | ||
)) | ||
|
||
dst_ds.SetProjection(Projection.ExportToWkt()) | ||
dst_ds.GetRasterBand(1).WriteArray(pop_arr_10) | ||
dst_ds.FlushCache() # Write to disk. |