-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagePreparation_utils.py
65 lines (49 loc) · 2.49 KB
/
imagePreparation_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
import geopandas as gpd
import earthpy.spatial as es
import shutil
from osgeo import gdal
from fileManagment_utils import *
def prepareImageGPD(shapeFilePath, source):
# description : feed with a shapeFile and an image, you will get the image cropped
# Open the shapeFile
shapeFile = gpd.read_file(shapeFilePath)
# Open the source image we want to crop
with rasterio.open(source) as src:
profile_src = src.profile
# reproject the shapefile in the correct projection
shapeFileReprojected = shapeFile.to_crs(profile_src['crs'])
# create a cropped image
imgCrop, imgCropMeta = es.crop_image(src, shapeFileReprojected)
return imgCrop, imgCropMeta
def imagePreparation(inputDirectory, outputDirectory, shapeFileDirectory):
# description : the function will browse all input images, prepare them and store them
# list the directories (different dates) in the input directory
listOfDirectories = fileList(inputDirectory)
# list the shapeFiles (different dates) in the shapeFile directory
listOfShapeFiles = fileList(shapeFileDirectory)
# create a dictionary {'date': ['image_name'],'date': ['image_name']}
listOfImages = {}
for i in listOfDirectories:
d = {i: fileList(inputDirectory + "/" + i)}
listOfImages.update(d)
for i in listOfDirectories:
for j in listOfImages[i]:
for k in listOfShapeFiles:
sourceFile = inputDirectory + "/" + i + "/" + j
destination = outputDirectory + "/" + i + "/" + k
destinationFile = outputDirectory + "/" + i + "/" + k + "/" + j
# if destination directory not exist create it, else overwrite
path = Path(destination)
path.mkdir(parents=True, exist_ok=True)
# copy the source file in the destination directory
shutil.copy2(sourceFile, destinationFile)
# update resolution
gdal.Warp(destinationFile, sourceFile, xRes=10, yRes=10)
# get the correct shapeFile
shapeFile = shapeFileDirectory + "/" + k + "/" + k + ".shp"
# Prepare the image (crop)
img, metha = prepareImageGPD(shapeFile, destinationFile)
print(i, j, " with ", k, " Prepared")
# Overwrite the destination file created before with the cropped image
with rasterio.open(destinationFile, "w", **metha) as dest:
dest.write(img)