cloud_builder.py 1.97 KB
Newer Older
1
2
3
4
import sys
import os
import numpy
import random
5
6
import os.path as op

7
8
import gdal
import gdalconst
9
10
from subprocess import call

11

12
def show_help():
13
14
15
16
17
    print "This script is used to create clouds on data"
    print "Usage: cloud_builder.py mode plaincloudthreshold randomcloudthreshold inputpath outputplaincloudpath ouputrandomcloudpath"
    print "Mode : 0 %plain cloud image, 1 %random cloud image, 2 both"


18
def main(argv):
19
20
21
22
23
24
25
26
    mode = argv[1]
    mode = int(mode)
    plain_cloud_threshold = argv[2]
    plain_cloud_threshold = float(plain_cloud_threshold) / 100
    random_cloud_threshold = argv[3]
    input_path = argv[4]
    output_plain_cloud_path = argv[5]
    output_random_cloud_path = argv[6]
27

28
    dataset = gdal.Open(input_path, gdalconst.GA_ReadOnly)
29
30
    wide = dataset.RasterXSize
    high = dataset.RasterYSize
31

32
    if(mode == 0 or mode == 2):
33
34
35
36
37
        # build half cloud image
        str_exp = "idxX>" + \
            str(int(wide * plain_cloud_threshold)) + "?im1b1:205"
        call(["otbcli_BandMathX", "-il", input_path, "-out",
              output_plain_cloud_path, "-exp", str_exp])
38
    if(mode == 1 or mode == 2):
39
        # build random cloud image
40
41
42
43
44
        band = dataset.GetRasterBand(1)
        array = band.ReadAsArray(0, 0, wide, high)
        for i in range(0, wide):
            for j in range(0, high):
                if(random.randint(0, 100) < random_cloud_threshold):
45
46
47
48
49
                    array[i, j] = 205

        output_random_cloud_raster = gdal.GetDriverByName('GTiff').Create(
            output_random_cloud_path, wide, high, 1, gdal.GDT_Byte)
        output_random_cloud_raster.GetRasterBand(1).WriteArray(array)
50
        output_random_cloud_raster.FlushCache()
51

52
53
        # georeference the image and set the projection
        output_random_cloud_raster.SetGeoTransform(dataset.GetGeoTransform())
54
55
56
        output_random_cloud_raster.SetProjection(dataset.GetProjection())


57
if __name__ == "__main__":
58
59
60
61
    if len(sys.argv) != 7:
        show_help()
    else:
        main(sys.argv)