def validate(self): """ Implementation of the base recipe validate method """ self.validate_base(True) if 'coverage_id' not in self.options: self.options['coverage_id'] = self.session.get_coverage_id() if 'partitioning_scheme' not in self.options: self.options['partitioning_scheme'] = self.__DEFAULT_PARTITIONING if 'backup' not in self.options: self.options['backup'] = False else: self.options['backup'] = bool(self.options['backup']) if self.options['backup']: if 'backup_dir' not in self.options: raise RuntimeException( "You need to provide a directory where to store the backup. Use the backup_dir parameter for this" ) if not os.access(self.options['backup_dir'], os.W_OK): raise RuntimeException( "Your backup directory is not writable, please remedy this." ) # Validate wcs_endpoint validate_and_read_url(self.options['wcs_endpoint'])
def parse_source_coverage_ids(self, input_source_coverage_ids): """ Source coverage ids is a list which contains full coverage id or coverage ids defined by regex :param list[string] input_source_coverage_ids: source of coverage ids in the ingredients file :return: list[string]: a list of concrete coverage ids without regex """ gml = validate_and_read_url( ConfigManager.wcs_service + "?service=WCS&version=2.0.1&request=GetCapabilities") tree = ET.ElementTree(ET.fromstring(gml)) namespaces = {'wcs': 'http://www.opengis.net/wcs/2.0'} elements = tree.findall(".//wcs:CoverageId", namespaces) # all available coverage ids which server provides current_coverage_ids = [] for element in elements: current_coverage_ids.append(element.text) result_coverage_ids = [] for coverage_id_pattern in input_source_coverage_ids: if is_regex(coverage_id_pattern): # coverage id by regex (e.g: test_*) for coverage_id in current_coverage_ids: if re.match(coverage_id_pattern, coverage_id): result_coverage_ids.append(coverage_id) else: # concrete coverage id (e.g: test_cov1) result_coverage_ids.append(coverage_id_pattern) return result_coverage_ids
def execute(self, request, output=False, mock=False, input_base_url=None): """ Executes a WCST request and returns the response to it :param WCSTRequest request: the request to be executed :rtype str """ request = self.prepare_request(request) base_url_tmp = self.base_url if input_base_url is not None: base_url_tmp = input_base_url service_call = base_url_tmp + "?" + request.get_query_string() if output: log.info(service_call) if mock: log.info( make_bold( "This is just a mocked request, no data will be changed.")) log.info(service_call) return try: response = decode_res( validate_and_read_url(base_url_tmp, request.get_query_string())) except Exception as ex: raise WCSTException( 404, "Failed reading response from WCS service. " "Detailed error: {}.".format(str(ex)), service_call) namespaces = "" # check if a WMS error occurred and parse accordingly if response.find("ServiceExceptionReport") != -1: namespaces = None # if WCST error then using ows:ExceptionReport elif response.find("ExceptionReport") != -1: namespaces = {"ows": "http://www.opengis.net/ows/2.0"} # Check error from response self.__check_request_for_errors(response, namespaces, service_call) try: if str(response) != "" and str(response) != "None": return encode_res(response) return "" except Exception as ex: raise WCSTException( 0, "General exception while executing the request: " + str(ex), service_call)
def __describe_coverage(self): """ Send a DescribeCoverage request to petascope """ try: service_call = self.wcs_service + "?service=WCS&request=DescribeCoverage&version=" + \ Session.get_WCS_VERSION_SUPPORTED() + "&coverageId=" + self.coverage_id response = validate_and_read_url(service_call) return response except Exception as ex: raise RuntimeException("Could not retrieve the axis labels. " "Detail error: {}".format(str(ex)))
def exists(self): """ Returns true if the coverage exist, false otherwise :rtype bool """ try: # Check if coverage exists via the Non-standard REST endpoint service_call = self.wcs_service + "/objectExists?coverageId=" + self.coverage_id response = decode_res(validate_and_read_url(service_call)) return response == "true" except Exception as ex: # Something is wrong, try with the standard WCS DescribeCoverage request pass try: # Check if coverage exists in WCS DescribeCoverage result service_call = self.wcs_service + "?service=WCS&request=DescribeCoverage&version=" + \ Session.get_WCS_VERSION_SUPPORTED() \ + "&coverageId=" + self.coverage_id response = validate_and_read_url(service_call) if decode_res(response).strip() != "": return True return False except Exception as ex: exception_text = str(ex) if "Missing basic authentication header" in exception_text: raise RuntimeException("Endpoint '{}' requires valid rasdaman credentials with format username:password in a text file. \n" "Hint: Create this identify file first with read permission for user running wcst_import, \n" "then rerun wcst_import.sh ingredients.json -i path_to_the_identity_file.".format(self.wcs_service)) if not "NoSuchCoverage" in exception_text: raise RuntimeException("Could not check if the coverage exists. " "Reason: {}".format(exception_text)) else: # coverage doesn't exist return False
def description(self): """ Gets the description from coverage_id in wcs_url :rtype: String """ xmlstr = validate_and_read_url(self._get_description_url()) # Check if coverage id does not exist in wcs_endpoint by returning an Exception if xmlstr.find("ExceptionReport") != -1: raise RuntimeException( "Could not read the coverage description for coverage id: {} with url: {} " .format(self.coverage_id, self.wcs_url)) # If coverage id does exist then return its description return xmlstr
def _get_data_type(self, slice): """ Returns the data type of the slice by downloading the slice and trying to guess it with GDAL :param Slice slice: slice :rtype: str """ if isinstance(slice.data_provider, UrlDataProvider): # Do this only for coverages that have more than one axis if len(slice.axis_subsets) > 1: fu = FileUtil() contents = validate_and_read_url(slice.data_provider.get_url()) file_path = fu.write_to_tmp_file(contents, "tif") return GDALGmlUtil(file_path).get_band_gdal_type() return None
def get_axis_labels(self): """ Returns the axis labels as a list :rtype list[str] """ try: service_call = self.wcs_service + "?service=WCS&request=DescribeCoverage&version=" + \ Session.get_WCS_VERSION_SUPPORTED() + "&coverageId=" + self.coverage_id response = validate_and_read_url(service_call) return response.split("axisLabels=\"")[1].split('"')[0].split(" ") except Exception as ex: raise RuntimeException( "Could not retrieve the axis labels. " "Check that the WCS service is up and running on url: {}. " "Detail error: {}".format(self.wcs_service, str(ex)))
def exists(self): """ Returns true if the coverage exist, false otherwise :rtype bool """ try: # Check if coverage exists in WCS GetCapabilities result service_call = self.wcs_service + "?service=WCS&request=GetCapabilities&acceptVersions=" + \ Session.get_WCS_VERSION_SUPPORTED() response = validate_and_read_url(service_call) root = etree.fromstring(response) coverage_id_elements = root.xpath( "//*[local-name() = 'CoverageId']") # Iterate all <CoverageId> elements to check coverageId exists already for element in coverage_id_elements: if self.coverage_id == element.text: return True return False except Exception as ex: raise RuntimeException("Could not check if the coverage exists. " "Detail error: {}".format(str(ex)))
def exists(self): """ Returns true if the coverage exist, false otherwise :rtype bool """ try: # Check if coverage exists in WCS DescribeCoverage result service_call = self.wcs_service + "?service=WCS&request=DescribeCoverage&version=" + \ Session.get_WCS_VERSION_SUPPORTED() \ + "&coverageId=" + self.coverage_id response = validate_and_read_url(service_call) if decode_res(response).strip() != "": return True return False except Exception as ex: if not "NoSuchCoverage" in str(ex): raise RuntimeException( "Could not check if the coverage exists. " "Detail error: {}".format(str(ex))) else: # coverage doesn't exist return False
def _get_coverage_data_as_array(self, data_url): xmlstr = validate_and_read_url(data_url) root = etree.fromstring(xmlstr) tupleList = root.xpath("//gml:tupleList", namespaces=self._get_ns()) return tupleList[0].text.split(",")