[743] | 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 | # |
---|
| 26 | import zoo |
---|
| 27 | import requests |
---|
| 28 | |
---|
| 29 | WCPS_REQUEST_TEMPLATE = "%endPoint%?service=WCS&version=2.0.1&request=ProcessCoverages&query=%query%" |
---|
| 30 | |
---|
| 31 | def 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 | |
---|
| 52 | def parse_error_message(response): |
---|
| 53 | return response.split("<ows:ExceptionText>")[1].split("</ows:ExceptionText>")[0] |
---|
| 54 | |
---|
| 55 | def 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 | |
---|
| 61 | def 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 | |
---|
| 73 | def get_correct_query(query,mimeType): |
---|
| 74 | return query.replace("[format]",get_data_ext(mimeType)) |
---|
| 75 | |
---|
| 76 | def 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 | |
---|
| 84 | def get_encoding(data_type): |
---|
| 85 | ret = "utf-8" |
---|
| 86 | if data_type.startswith("image"): |
---|
| 87 | ret = "base64" |
---|
| 88 | return ret |
---|
| 89 | |
---|
| 90 | def 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 | |
---|
| 99 | def 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 | |
---|