-
Notifications
You must be signed in to change notification settings - Fork 1
/
PyWPSGrassModuleStarter.py
365 lines (321 loc) · 20.4 KB
/
PyWPSGrassModuleStarter.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*-
################################################################################
# Author: Soeren Gebbert
#
# Copyright (C) 2010 Soeren Gebbert
# mail to: soerengebbert <at> googlemail <dot> com
#
# License:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from gms.ErrorHandler import GMSError
import GlobalGrassSettings
import tempfile
from types import *
# Import the GrassModuleStarter
from gms.GrassModuleStarter import *
# Impot the PyWPS stuff.. Mask the name to avoid conflicts with the GrassModuleStarter
from pywps.Process.InAndOutputs import LiteralInput as PyWPSLiteralInput
from pywps.Process.InAndOutputs import ComplexInput as PyWPSComplexInput
from pywps.Process.InAndOutputs import ComplexOutput as PyWPSComplexOutput
class PyWPSGrassModuleStarter(GrassModuleStarter):
"""Start a grass module specified by PyWPS server"""
def __init__(self):
GrassModuleStarter.__init__(self)
############################################################################
def fromPyWPS(self, grassModule, inputs, outputs, pywps):
self._grassModule = grassModule
self._inputs = inputs
self._outputs = outputs
self._pywps = pywps
# Initiate the logging mechanism and the logfiles
ModuleLogging.__init__(self, GlobalGrassSettings.LOGFILE, GlobalGrassSettings.LOGFILE_MODULE_STDOUT, GlobalGrassSettings.LOGFILE_MODULE_STDERR)
# Parse the input parameter of the text file
self.inputParameter = InputParameter(self.logfile)
# Initiate the input parameter for the GrassModuleStarter
self._setInputParameterInit()
# Some debug stuff
# self.LogInfo(str(self._pywps.inputs))
# Parse the PyWPS input and output data
self._parsePyWPSInputsAndOutputs()
try:
self._createOutputFiles()
self._createInputOutputMaps()
try:
# Temporal directory must be created at the beginning
self._createTemporalDir(self.inputParameter.workDir)
self._setUpGrassLocation(self.inputParameter.grassGisBase, self.inputParameter.grassAddonPath)
# Import all data, run the module and export the data
# Before import check if zipped shape files are present in input and
# Extract them and update the input map
self._checkForZippedShapeFiles()
# Create the new location based on the first valid input and import all maps
self._importData()
# start the grass module
self._startGrassModule()
# now export the results
self._exportOutput()
# Pass outputs
self._passOutputs()
except:
raise
finally:
self._removeTempData()
except:
raise
finally:
self._closeLogfiles()
############################################################################
def _parsePyWPSInputsAndOutputs(self):
# Double for loop to identify requested inputs
for datainput in self._pywps.inputs["datainputs"]: # list
for input in self._inputs: # dict
# Identify requested inputs
if self._inputs[input].identifier != datainput["identifier"]:
continue
else:
message = "Add requested input with identifier \"" + datainput["identifier"] + "\""
self.LogInfo(message)
# Check for literal inputs
if isinstance(self._inputs[input], PyWPSLiteralInput):
# Attach only when not empty
if self._inputs[input].getValue() != None:
data = LiteralData()
data.identifier = self._inputs[input].identifier
# Store the value
data.value = str(self._inputs[input].getValue())
# In case an array is attached, store the values as comma
# separated list
if type(self._inputs[input].getValue()) == type([]):
data.value = ""
count = 0
size = len(self._inputs[input].getValue())
for i in self._inputs[input].getValue():
data.value += str(i)
if count > 0 and count < size - 1:
data.value += ","
count += 1
#check for double, integer, boolean, string
if self._inputs[input].dataType is IntType:
data.type = "integer"
elif self._inputs[input].dataType is FloatType:
data.type = "double"
elif self._inputs[input].dataType is StringType:
data.type = "string"
elif self._inputs[input].dataType is BooleanType:
# Override the stored value
data.type = "boolean"
if self._inputs[input].getValue() == False:
data.value = "False"
elif self._inputs[input].getValue() == True:
data.value = "True"
else:
data.value = "False"
else:
data.type = "string"
if self._inputs[input].identifier == 'multi_output':
if self._inputs[input].getValue().upper() == 'TRUE':
self.inputParameter.multiOutput = True
else:
self.inputParameter.literalDataList.append(data)
self.LogInfo("Added literal input " + data.identifier + " with value " + str(data.value) + " of type " + str(type(data.value)))
# Check for complex data
elif isinstance(self._inputs[input], PyWPSComplexInput):
if self._inputs[input].getValue() != None:
# Check for multiple complex inputs
if type(self._inputs[input].getValue()) == type([]):
for path in self._inputs[input].getValue():
data = ComplexData()
data.identifier = self._inputs[input].identifier
data.pathToFile = path
data.maxOccurs = self._inputs[input].maxOccurs
# Check for mime types
try:
data.mimeType = self._inputs[input].format["mimetype"]
except:
try:
data.mimeType = self._inputs[input].format["mimeType"]
except:
log = "Missing mimeType in input " + str(input)
self.LogError(log)
raise GMSError(log)
try:
# schema and encoding are not mandatory
if self._inputs[input].format.has_key("schema"):
data.schema = self._inputs[input].format["schema"]
else:
self.LogWarning("Missing schema")
if self._inputs[input].format.has_key("encoding"):
data.encoding = self._inputs[input].format["encoding"]
else:
self.LogWarning("Missing schema")
except:
pass
self.inputParameter.complexDataList.append(data)
self.LogInfo("Added complex input " + data.identifier + " with file path " + data.pathToFile)
# Single complex input
else:
data = ComplexData()
data.identifier = self._inputs[input].identifier
data.pathToFile = self._inputs[input].getValue()
data.maxOccurs = self._inputs[input].maxOccurs
# Check for mime types
try:
data.mimeType = self._inputs[input].format["mimetype"]
except:
try:
data.mimeType = self._inputs[input].format["mimeType"]
except:
log = "Missing mimetype in input " + str(input)
self.LogError(log)
raise GMSError(log)
try:
# schema and encoding are not mandatory
if self._inputs[input].format.has_key("schema"):
data.schema = self._inputs[input].format["schema"]
else:
self.LogWarning("Missing schema")
if self._inputs[input].format.has_key("encoding"):
data.encoding = self._inputs[input].format["encoding"]
else:
self.LogWarning("Missing schema")
except:
pass
self.inputParameter.complexDataList.append(data)
self.LogInfo("Added complex input " + data.identifier + " with file path " + data.pathToFile)
# Attach all requested outputs
# jmdj: A responsedocument with outputs is not mandatory in WPS requests.
# If no ouputs are present we will assume the default mimeTypes and structure from the process it self
try:
outputList = self._pywps.inputs["responseform"]["responsedocument"]["outputs"]
# may happen that responsedocument has some content inside
if outputList == []:
raise
# if no exception raise then continue to loop:
for output in outputList:
data = ComplexOutput()
data.identifier = output["identifier"]
try:
data.mimeType = output["mimetype"]
except:
log = "Missing mimeType in output " + str(output)
self.LogError(log)
raise GMSError(log)
try:
# schema and encoding are not mandatory
data.schema = output["schema"]
data.encoding = output["encoding"]
except:
self.LogWarning("Missing schema and encoding")
pass
self.inputParameter.complexOutputList.append(data)
self.LogInfo("Added complex output " + data.identifier + " of format " + data.mimeType)
except:
# Making outputs to have the same attributes as self._pywps.inputs
outputList=self._outputs.values()
# Making outputs to have the same attributes as self._pywps.inputs
# for output in outputList:
for output in outputList:
data = ComplexOutput()
data.identifier = output.identifier
try:
data.mimeType = output.format["mimetype"]
except:
log = "Missing mimeType in output from process" + str(output)
self.LogError(log)
raise GMSError(log)
try:
# schema and encoding are not mandatory
# self_outputs.values() contains a "schema" key with None, false positive.
if type(output.format["schema"]) is not NoneType:
data.schema = output.format["schema"]
data.encoding = output.format["encoding"]
except:
self.LogWarning("Missing schema and encoding from process")
pass
self.inputParameter.complexOutputList.append(data)
self.LogInfo("Added complex output " + data.identifier + " of format " + data.mimeType)
############################################################################
def _setInputParameterInit(self):
"""Initiate the input parameter"""
self.inputParameter = InputParameter(self.logfile)
self.inputParameter.grassGisBase = GlobalGrassSettings.GRASS_GIS_BASE
self.inputParameter.grassAddonPath = GlobalGrassSettings.GRASS_ADDON_PATH
self.inputParameter.grassVersion = GlobalGrassSettings.GRASS_VERSION
self.inputParameter.workDir = GlobalGrassSettings.WORKDIR
self.inputParameter.grassModule = self._grassModule
############################################################################
def _createOutputFiles(self):
"""Create the output file names which are used to identify the outputs"""
count = 0
for output in self.inputParameter.complexOutputList:
filename = tempfile.mkstemp(prefix=output.identifier + "_" + str(count),dir=GlobalGrassSettings.OUTPUTDIR)
output.pathToFile= filename[1]
# No need to let the file open
os.close(filename[0])
self.LogInfo("Created output filename " + filename[1])
count = count + 1
############################################################################
def _passOutputs(self):
for output in self.inputParameter.complexOutputList:
filename = str(output.pathToFile)
try:
self._outputs[output.identifier].setValue(filename)
except Exception, e:
self.LogError("Unable to attach output file, error is: %s"%(str(e)))
raise
self.LogInfo("Attached output file " + filename)
################################################################################
# Code for testing #############################################################
################################################################################
# We emulate the PyWPS inputs dict structure for testing
class pywps_inputs:
def __init__(self):
self.inputs = {'responseform': {'rawdataoutput': {}, 'responsedocument': {'lineage': False, 'status': False, 'storeexecuteresponse': True, 'outputs': [{'mimetype': u'text/xml', 'encoding': '', 'asreference': True, 'identifier': u'output', 'uom': '', 'schema': u'http://schemas.opengis.net/gml/2.1.2/feature.xsd'}]}}, 'service': 'wps', 'language': 'en-CA', 'request': 'execute', 'version': u'1.0.0', 'datainputs': [{'mimetype': u'text/xml', 'encoding': '', 'value': u'<ogr:FeatureCollection xmlns:gml="http://www.opengis.net/gml" xmlns:ogr="http://ogr.maptools.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.opengis.net/gml/3.1.1/base/ gml.xsd"><gml:boundedBy><gml:Box><gml:coord><gml:X>631431.2732342008</gml:X><gml:Y>217051.3475836431</gml:Y></gml:coord><gml:coord><gml:X>643603.8568773235</gml:X><gml:Y>225781.1802973978</gml:Y></gml:coord></gml:Box></gml:boundedBy><gml:featureMember><ogr:qt_temp fid="F0"><ogr:geometryProperty><gml:Point><gml:coordinates>631571.793680297443643,225728.485130111512262</gml:coordinates></gml:Point></ogr:geometryProperty><ogr:height>10</ogr:height></ogr:qt_temp></gml:featureMember><gml:featureMember><ogr:qt_temp fid="F1"><ogr:geometryProperty><gml:Point><gml:coordinates>635752.276951672858559,225113.708178438653704</gml:coordinates></gml:Point></ogr:geometryProperty><ogr:height>20</ogr:height></ogr:qt_temp></gml:featureMember><gml:featureMember><ogr:qt_temp fid="F2"><ogr:geometryProperty><gml:Point><gml:coordinates>640898.838289962848648,225781.180297397775576</gml:coordinates></gml:Point></ogr:geometryProperty><ogr:height>40</ogr:height></ogr:qt_temp></gml:featureMember><gml:featureMember><ogr:qt_temp fid="F3"><ogr:geometryProperty><gml:Point><gml:coordinates>643603.856877323472872,221319.65613382900483</gml:coordinates></gml:Point></ogr:geometryProperty><ogr:height>60</ogr:height></ogr:qt_temp></gml:featureMember><gml:featureMember><ogr:qt_temp fid="F4"><ogr:geometryProperty><gml:Point><gml:coordinates>640038.1505576208001,217999.860594795551151</gml:coordinates></gml:Point></ogr:geometryProperty><ogr:height>50</ogr:height></ogr:qt_temp></gml:featureMember><gml:featureMember><ogr:qt_temp fid="F5"><ogr:geometryProperty><gml:Point><gml:coordinates>633047.258364312234335,217051.347583643131657</gml:coordinates></gml:Point></ogr:geometryProperty><ogr:height>40</ogr:height></ogr:qt_temp></gml:featureMember><gml:featureMember><ogr:qt_temp fid="F6"><ogr:geometryProperty><gml:Point><gml:coordinates>635471.236059479531832,220599.488847583648749</gml:coordinates></gml:Point></ogr:geometryProperty><ogr:height>25</ogr:height></ogr:qt_temp></gml:featureMember><gml:featureMember><ogr:qt_temp fid="F7"><ogr:geometryProperty><gml:Point><gml:coordinates>631431.27323420078028,222443.819702602224424</gml:coordinates></gml:Point></ogr:geometryProperty><ogr:height>20</ogr:height></ogr:qt_temp></gml:featureMember></ogr:FeatureCollection>', 'identifier': u'input', 'type': 'ComplexValue', 'schema': u'http://schemas.opengis.net/gml/2.1.2/feature.xsd'}, {'dataType': '', 'identifier': u'-t', 'value': False, 'uom': ''}, {'dataType': '', 'identifier': u'-l', 'value': False, 'uom': ''}], 'identifier': [u'v.voronoi']}
################################################################################
# Main function to start the v.voronoi test
def main():
"""This is a simple test script to evaluate the PyWPS - GrassModuleStarter
wrapper. """
# Create a complex input for v.voronoi
identifier = "input"
title = "Name of input vector map"
minOccurs = 1
maxOccurs = 1
format = [{'mimeType': 'text/xml', 'schema': 'http://schemas.opengis.net/gml/2.1.2/feature.xsd', 'encoding': 'UTF-8'}]
input = PyWPSComplexInput(identifier=identifier, minOccurs=minOccurs, maxOccurs=maxOccurs, title=title,formats=format)
# The PyWPS request dict structure
requests = pywps_inputs()
# The first input
data = requests.inputs["datainputs"][0]["value"]
input.setMimeType(requests.inputs["datainputs"][0])
input.storeData(data)
inputs= {}
inputs[identifier] = input
# Create the complex output for v.voronoi
identifier = "output"
title = "Name of output vector map"
format = [{'mimeType': 'text/xml', 'schema': 'http://schemas.opengis.net/gml/2.1.2/feature.xsd', 'encoding': 'UTF-8'}]
output = PyWPSComplexOutput(identifier=identifier, title=title,formats=format)
outputs = {}
outputs[identifier] = output
# Start the GRASS process
starter = PyWPSGrassModuleStarter()
starter.fromPyWPS("v.voronoi", inputs, outputs, requests)
print "Generated GML output is located here: ", outputs[identifier].value
exit(0)
###############################################################################
if __name__ == "__main__":
main()