-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgospatial.py
172 lines (150 loc) · 4.67 KB
/
gospatial.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# This file is intended to be a helper for running gospatial tools from a Python script.
# See gospatial_example.py for an example of how to use it.
import os
import sys
import subprocess
from sys import platform
exe_path = os.path.dirname(os.path.abspath(__file__))
wd = ""
if platform == 'win32':
ext = '.exe'
else:
ext = ''
exe_name = "go-spatial{}".format(ext)
def set_gospatial_dir(path):
global exe_path
exe_path = path
def set_working_dir(path):
global wd
wd = path
def help():
try:
os.chdir(exe_path)
cmd = []
cmd.append("." + os.path.sep + exe_name)
cmd.append("-help")
ps = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
ret = ""
while True:
line = ps.stdout.readline()
if line != '':
ret += line
else:
break
return ret
except Exception as e:
return e
def tool_args(tool_name):
try:
os.chdir(exe_path)
cmd = []
cmd.append("." + os.path.sep + exe_name)
cmd.append("-toolargs={}".format(tool_name))
ps = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
ret = ""
while True:
line = ps.stdout.readline()
if line != '':
ret += line
else:
break
return ret
except Exception as e:
return e
def tool_help(tool_name):
try:
os.chdir(exe_path)
cmd = []
cmd.append("." + os.path.sep + exe_name)
cmd.append("-toolhelp={}".format(tool_name))
ps = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
ret = ""
while True:
line = ps.stdout.readline()
if line != '':
ret += line
else:
break
return ret
except Exception as e:
return e
def list_tools():
try:
os.chdir(exe_path)
cmd = []
cmd.append("." + os.path.sep + exe_name)
cmd.append("-listtools")
ps = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
ret = ""
while True:
line = ps.stdout.readline()
if line != '':
ret += line
else:
break
return ret
except Exception as e:
return e
def default_callback(str):
print(str)
def run_tool(tool_name, args, callback = default_callback):
try:
os.chdir(exe_path)
cmd = []
cmd.append("." + os.path.sep + exe_name)
if len(wd) > 0:
cmd.append("-cwd=\"{}\"".format(wd))
cmd.append("-run={}".format(tool_name))
args_str = ""
for s in args:
args_str += s.replace("\"", "") + ";"
args_str = args_str[:-1]
cmd.append("-args=\"{}\"".format(args_str))
# print cmd
ps = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
# if platform == 'win32':
# ext = '.exe'
# else:
# ext = ''
#
# exe_name = "go-spatial"
# cmd = "." + os.path.sep + exe_name #"{0}{1}".format(exe_name, ext)
# if len(wd) > 0:
# cmd += " -cwd {}".format(wd)
#
# cmd += ' -run \"{}\"'.format(tool_name)
# args_str = ""
# for s in args:
# args_str += s.replace("\"", "") + ";"
# args_str = args_str[:-1]
# cmd += ' -args \"{}\"'.format(args_str)
# # print cmd
# ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
while True:
line = ps.stdout.readline()
if line != '':
callback(line.strip())
else:
break
return 0
except Exception as e:
print(e)
return 1
def version():
try:
os.chdir(exe_path)
cmd = []
cmd.append("." + os.path.sep + exe_name)
cmd.append("-version")
ps = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
ret = ""
while True:
line = ps.stdout.readline()
if line != '':
ret += line
else:
break
return ret
except Exception as e:
return e