forked from tankwin08/qgis-earthengine-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_js_to_python.py
75 lines (59 loc) · 2.66 KB
/
convert_js_to_python.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
66
67
68
69
70
71
72
73
74
75
''' Convert GEE JavaScript to Python script to be used in QGIS.
example:
$ python convert_js_to_python.py --input Image/band_math.py
'''
import os
import argparse
def dict_key_str(line):
keys = ['bands', 'min', 'max', 'gain', 'bias', 'gamma', 'palette', 'opacity',
'format', 'radius', 'units', 'normalize', 'kernel', 'iterations', 'threshold',
'sigma', 'magnitude', 'size', 'connectedness', 'maxSize', 'eightConnected',
'reducer', 'labelBand', 'color', 'source', 'maxDistance', 'referenceImage',
'maxOffset', 'patchWidth', 'strokeWidth', 'width', 'geometry', 'scale', 'maxPixels',
'collection', 'selectors', 'leftField', 'rightField']
for key in keys:
if ":" in line and key in line:
line = line.replace(key + ":", "'" + key + "':")
return line
def js_to_python(in_file):
root_dir = os.path.dirname(os.path.abspath(__file__))
in_file_path = os.path.join(root_dir, in_file)
github_url = "# GitHub URL: " + "https://github.com/giswqs/qgis-earthengine-examples/tree/master/" + in_file + "\n\n"
header = github_url + "import ee \n" + "from ee_plugin import Map \n"
# print(header)
output = header + "\n"
with open(in_file_path) as f:
lines = f.readlines()
for line in lines:
line = line.replace("//", "#")
line = line.replace(";", "")
line = line.replace("var ", "")
line = line.replace("true", "True")
line = line.replace("false", "False")
line = line.replace("null", "{}")
# line = line.replace("or", "Or")
# line = line.replace("and", 'And')
line = dict_key_str(line)
line = line.rstrip()
if "= function" in line:
line = line.replace(" = function", "")
line = line.replace("{", "")
line = "def " + line.rstrip() + ":"
if line.strip() == "}":
line = line.replace("}", "")
# print(line)
if line.lstrip().startswith("."):
output = output.rstrip() + " " + "\\" + "\n" + line + "\n"
else:
output = output + line + "\n"
print(output)
with open(in_file_path, 'w') as f:
f.write(output)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str,
help="Path to the input python file")
# parser.add_argument('--image_path', type=str, help="Path to the input GeoTIFF image")
# parser.add_argument('--save_path', type=str, help="Path where the output map will be saved")
args = parser.parse_args()
js_to_python(args.input)