-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpsclient.py
153 lines (122 loc) · 5.99 KB
/
wpsclient.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
#!/usr/bin/env python
#-*-coding:utf-8-*-
"""
/***************************************************************************
wpsclient.py Wps客户端库
-------------------------------------------------------------------
Date : 2012.5.13 9:50
Copyright : (C) 2012 by 张祖鹏
email : [email protected]
Authors : 张祖鹏
***************************************************************************
* *
* This program is free library; 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. *
* *
***************************************************************************/
"""
from xml.dom.xmlbuilder import DOMBuilder
import urllib2
import logging
from httplib import HTTPConnection
from OGCCommon import *
from description import processDescribe
from description import strProcessDescribe
class WpsClient(object):
"""
访问wps服务的富客户端接口
"""
theOGCservice = "WPS"
theVersion = "1.0.0"
def __init__(self, serverurl, proxy=None, logfile=None):
"""
设置要访问的wps服务的url及其代理
@param servername: wps服务url路径
"""
self.theProcessesSupported = {}
self.theServeUrl = serverurl
# TODO: 添加代理功能
# TODO: 判断服务是否可用,待修改
urllib2.urlopen(serverurl+"?version=1.0.0&service=WPS&request=GetCapabilities", timeout=5)
# 设置日志文件
if logfile:
logging.basicConfig(filename = logfile)
def _getWPSServiceXML(self, request, identifier=''):
requesturl = '{0}?version={1}&service={2}&request={3}'.format(self.theServeUrl,
self.theVersion, self.theOGCservice, request)
if identifier != '':
requesturl = '{0}&identifier={1}'.format(requesturl, identifier)
try:
return DOMBuilder().parseURI(requesturl)
except:
logging.exception(exc_info())
raise Exception(exc_info()[2])
def GetCapabilities(self, isVerbose=False):
"""
得到wps服务站点提供的处理服务
@param isVerbose: True则返回概述、元数据等详细信息,否则只得到处理标识符
"""
procList = []
processElems = self._getWPSServiceXML('GetCapabilities').getElementsByTagNameNS(
OGCNamespaceURI['wps1.0.0'], 'Process')
if not isVerbose:
for elem in processElems:
if elem:
identifier = elem.getElementsByTagNameNS(OGCNamespaceURI['ows1.1'],
'Identifier')[0].firstChild.nodeValue
version = elem.attributes.getNamedItemNS(OGCNamespaceURI['wps1.0.0'],
'processVersion').nodeValue
procList.append([identifier,version])
else:
for elem in processElems:
if elem:
procList.append(ProcessBrief().getProcessBrief(elem))
return procList
def ProcessDescribe(self, identifier):
"""
得到此标识符处理服务详细信息,格式为[identifier, abstract,{inputs},{outputs}]
如果wps服务中没有对应的标识符,则返回异常
"""
if not self.theProcessesSupported.has_key(identifier):
self.theProcessesSupported[identifier] = processDescribe(self._getWPSServiceXML(
'DescribeProcess',identifier))
return self.theProcessesSupported[identifier]
def ReadableProcessDescribe(self,identifier):
return strProcessDescribe(self.ProcessDescribe(identifier))
def Execute(self, identifier, *argv, **kwargs):
#获得描诉信息
desc = self.theProcessesSupported[identifier]
if not desc:
try:
desc = self.ProcessDescribe(identifier)
except:
raise
# Wrap input parameter
inputxml = self._wrapInputParam(desc[1], *argv, **kwargs)
outputxml = self._wrapOutputParam(desc[1], *argv, **kwargs)
# 创建XML格式文档
postString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
postString += "<wps:Execute service=\""+ self.theOGCservice +"\" version=\""+ self.theVersion + "\"" + \
" xmlns:wps=\"http://www.opengis.net/wps/1.0.0\"" + \
" xmlns:ows=\"http://www.opengis.net/ows/1.1\"" +\
" xmlns:xlink=\"http://www.w3.org/1999/xlink\"" +\
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""\
" xsi:schemaLocation=\"http://www.opengis.net/wps/1.0.0" +\
" http://schemas.opengis.net/wps/1.0.0/wpsExecute_request.xsd\">"
postString += "<ows:Identifier>"+identifier+"</ows:Identifier>\n"
postString += "<wps:DataInputs>"
# 发送请求
if __name__ == "__main__":
wps = WpsClient("http://localhost/cgi-bin/pywps.cgi")
# for brief in wps.GetCapabilities(False):
# print brief[0] + " v." + brief[1]+"\n"
#
# for brief in wps.GetCapabilities(True):
# print str(brief)+"\n"
# for brief in wps.GetCapabilities():
# print wps.strProcessDescribe(wps.ProcessDescribe(brief[0]))
print wps.ReadableProcessDescribe('v.buffer')
print wps.ReadableProcessDescribe('v.to.points')
print wps.ReadableProcessDescribe('r.clump')