source: trunk/zoo-project/zoo-services/wcps-proxy/cgi-env/WCPS.py @ 779

Last change on this file since 779 was 743, checked in by djay, 8 years ago

Add WCPS-Proxy, data-api proxy and update Makefile for dealing with specific location of fastcgi. Add status update for contour lines computation.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Author : Vlad Merticariu <merticariu@rasdaman.com>
4#
5# Copyright 2015 rasdaman GmbH. All rights reserved.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the
9# "Software"), to deal in the Software without restriction, including with
10# out limitation the rights to use, copy, modify, merge, publish,
11# distribute, sublicense, and/or sell copies of the Software, and to
12# permit persons to whom the Software is furnished to do so, subject to
13# the following conditions:
14#
15# The above copyright notice and this permission notice shall be included
16# in all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25#
26import zoo
27import requests
28
29WCPS_REQUEST_TEMPLATE = "%endPoint%?service=WCS&version=2.0.1&request=ProcessCoverages&query=%query%"
30
31def WCPS(conf,inputs,outputs):
32        end_point = validate_end_point(inputs["endPoint"]["value"])
33        query = get_correct_query(inputs["query"]["value"],outputs["Response"]["mimeType"])
34        response_data_type = get_data_type(query)
35        status, response = execute_wcps_request(end_point, query)
36        import sys   
37        print >> sys.stderr,query
38        #handle errors
39        if status:
40                conf["lenv"]["message"] = zoo._("The end point at " + end_point + " could not be reached.")
41                return zoo.SERVICE_FAILED
42        if "ows:ExceptionReport" in response:
43                conf["lenv"]["message"] = parse_error_message(response)
44                return zoo.SERVICE_FAILED
45               
46        #no errors, everything ok       
47        outputs["Response"]["value"] = remove_trailing_characters(response, response_data_type)
48        #outputs["Response"]["mimeType"] = response_data_type
49        #outputs["Response"]["encoding"] = get_encoding(outputs["Response"]["mimeType"])
50        return zoo.SERVICE_SUCCEEDED
51
52def parse_error_message(response):
53        return response.split("<ows:ExceptionText>")[1].split("</ows:ExceptionText>")[0]
54
55def validate_end_point(end_point):
56        ret = end_point
57        if not end_point.startswith("http"):
58                ret = "http://" + end_point
59        return ret
60
61def get_data_type(query):
62        ret = "text/plain"
63        if "\"png\"" in query:
64                ret = "image/png"
65        if "\"csv\"" in query:
66                ret = "text/csv"
67        if "\"tiff\"" in query:
68                ret = "image/tiff"
69        if "\"jpeg\"" in query or "\"jpg\"" in query:
70                ret = "image/jpeg"
71        return ret
72
73def get_correct_query(query,mimeType):
74    return query.replace("[format]",get_data_ext(mimeType))
75
76def get_data_ext(mimeType):
77        ret = "csv"
78        for i in ["png","csv","tiff","jpeg"]:
79                if i in mimeType:
80                        ret = i
81                        break
82        return ret
83
84def get_encoding(data_type):
85        ret = "utf-8"
86        if data_type.startswith("image"):
87                ret = "base64"
88        return ret
89
90def remove_trailing_characters(response, data_type):
91        response_start = 27
92        if data_type.startswith("image"):
93                response_start += len(data_type)
94        else:
95                response_start += 10
96        ret = response[response_start:-17]
97        return ret
98
99def execute_wcps_request(end_point, query):
100        composed_url = WCPS_REQUEST_TEMPLATE.replace("%endPoint%", end_point).replace("%query%", query) 
101        try:
102                response = requests.get(composed_url)
103        except:
104                #can not open url
105                return 1, ""
106        #all ok
107        return 0, response.content
108
109   
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png