コード例 #1
0
def WebCoverageService(url, version=None, xml=None, cookies=None, timeout=30, auth=None, headers=None):
    ''' wcs factory function, returns a version specific WebCoverageService object '''

    if not auth:
        auth = Authentication()

    if version is None:
        if xml is None:
            reader = wcsBase.WCSCapabilitiesReader(auth=auth, headers=headers)
            request = reader.capabilities_url(url)
            xml = openURL(
                request, cookies=cookies, timeout=timeout, auth=auth, headers=headers).read()

        capabilities = etree.etree.fromstring(xml)
        version = capabilities.get('version')
        del capabilities

    clean_url = clean_ows_url(url)

    if version == '1.0.0':
        return wcs100.WebCoverageService_1_0_0.__new__(
            wcs100.WebCoverageService_1_0_0, clean_url, xml, cookies, auth=auth, timeout=timeout, headers=headers)
    elif version == '1.1.0':
        return wcs110.WebCoverageService_1_1_0.__new__(
            wcs110.WebCoverageService_1_1_0, url, xml, cookies, auth=auth, timeout=timeout, headers=headers)
    elif version == '1.1.1':
        return wcs111.WebCoverageService_1_1_1.__new__(
            wcs111.WebCoverageService_1_1_1, url, xml, cookies, auth=auth, timeout=timeout, headers=headers)
    elif version == '2.0.0':
        return wcs200.WebCoverageService_2_0_0.__new__(
            wcs200.WebCoverageService_2_0_0, url, xml, cookies, auth=auth, timeout=timeout, headers=headers)
    elif version == '2.0.1':
        return wcs201.WebCoverageService_2_0_1.__new__(
            wcs201.WebCoverageService_2_0_1, url, xml, cookies, auth=auth, timeout=timeout, headers=headers)
コード例 #2
0
 def __init__(
     self,
     url,
     version,
     xml=None,
     parse_remote_metadata=False,
     timeout=30,
     username=None,
     password=None,
     auth=None,
 ):
     """Initialize."""
     if auth:
         if username:
             auth.username = username
         if password:
             auth.password = password
     self.url = url
     self.version = version
     self.timeout = timeout
     self.auth = auth or Authentication(username, password)
     self._capabilities = None
     reader = WFSCapabilitiesReader(self.version, auth=self.auth)
     if xml:
         self._capabilities = reader.readString(xml)
     else:
         self._capabilities = reader.read(self.url)
     self._buildMetadata(parse_remote_metadata)
コード例 #3
0
 def __init__(
     self,
     url,
     version,
     xml=None,
     parse_remote_metadata=False,
     timeout=30,
     headers=None,
     username=None,
     password=None,
     auth=None,
 ):
     """Initialize."""
     if auth:
         if username:
             auth.username = username
         if password:
             auth.password = password
     else:
         auth = Authentication()
     super(WebFeatureService_2_0_0, self).__init__(auth)
     log.debug("building WFS %s" % url)
     self.url = url
     self.version = version
     self.timeout = timeout
     self.headers = headers
     self._capabilities = None
     reader = WFSCapabilitiesReader(self.version,
                                    headers=self.headers,
                                    auth=self.auth)
     if xml:
         self._capabilities = reader.readString(xml)
     else:
         self._capabilities = reader.read(self.url)
     self._buildMetadata(parse_remote_metadata)
コード例 #4
0
 def __init__(
     self,
     url,
     version,
     xml=None,
     parse_remote_metadata=False,
     timeout=30,
     username=None,
     password=None,
     auth=None,
 ):
     """Initialize."""
     if auth:
         if username:
             auth.username = username
         if password:
             auth.password = password
     else:
         auth = Authentication(username, password)
     super(WebFeatureService_1_1_0, self).__init__(auth)
     self.url = url
     self.version = version
     self.timeout = timeout
     self._capabilities = None
     self.owscommon = OwsCommon("1.0.0")
     reader = WFSCapabilitiesReader(self.version, auth=self.auth)
     if xml:
         self._capabilities = reader.readString(xml)
     else:
         self._capabilities = reader.read(self.url)
     self._buildMetadata(parse_remote_metadata)
コード例 #5
0
ファイル: wms111.py プロジェクト: yalaudah/OWSLib
    def __init__(self, url, version='1.1.1', xml=None, username=None, password=None,
                 parse_remote_metadata=False, headers=None, timeout=30, auth=None):
        """Initialize."""
        if auth:
            if username:
                auth.username = username
            if password:
                auth.password = password
        self.url = url
        self.version = version
        self.timeout = timeout
        self.headers = headers
        self._capabilities = None
        self.auth = auth or Authentication(username, password)

        # Authentication handled by Reader
        reader = WMSCapabilitiesReader(
            self.version, url=self.url, headers=headers, auth=self.auth)
        if xml:  # read from stored xml
            self._capabilities = reader.readString(xml)
        else:  # read from server
            self._capabilities = reader.read(self.url, timeout=self.timeout)

        self.request = reader.request

        # avoid building capabilities metadata if the
        # response is a ServiceExceptionReport
        se = self._capabilities.find('ServiceException')
        if se is not None:
            err_message = str(se.text).strip()
            raise ServiceException(err_message)

        # build metadata objects
        self._buildMetadata(parse_remote_metadata)
コード例 #6
0
ファイル: schema.py プロジェクト: mlefort/OWSLib
def get_schema(
    url, typename, version="1.0.0", timeout=30, headers=None, username=None, password=None, auth=None
):
    """Parses DescribeFeatureType response and creates schema compatible
    with :class:`fiona`

    :param str url: url of the service
    :param str version: version of the service
    :param str typename: name of the layer
    :param int timeout: request timeout
    :param str username: service authentication username
    :param str password: service authentication password
    :param Authentication auth: instance of owslib.util.Authentication
    """
    if auth:
        if username:
            auth.username = username
        if password:
            auth.password = password
    else:
        auth = Authentication(username, password)
    url = _get_describefeaturetype_url(url, version, typename)
    root = _get_remote_describefeaturetype(url, timeout=timeout,
                                           headers=headers, auth=auth)

    if ":" in typename:
        typename = typename.split(":")[1]
    type_element = root.find("./{%s}element" % XS_NAMESPACE)
    complex_type = type_element.attrib["type"].split(":")[1]
    elements = _get_elements(complex_type, root)
    nsmap = None
    if hasattr(root, "nsmap"):
        nsmap = root.nsmap
    return _construct_schema(elements, nsmap)
コード例 #7
0
ファイル: maindialog.py プロジェクト: oscar1780/QGIS
    def _get_csw(self):
        """convenience function to init owslib.csw.CatalogueServiceWeb"""  # spellok

        self.disable_ssl_verification = self.disableSSLVerification.isChecked()
        auth = None

        if self.disable_ssl_verification:
            try:
                auth = Authentication(verify=False)
            except NameError:
                pass

        # connect to the server
        with OverrideCursor(Qt.WaitCursor):
            try:
                self.catalog = CatalogueServiceWeb(
                    self.catalog_url,  # spellok
                    timeout=self.timeout,
                    username=self.catalog_username,
                    password=self.catalog_password,
                    auth=auth)
                return True
            except ExceptionReport as err:
                msg = self.tr('Error connecting to service: {0}').format(err)
            except ValueError as err:
                msg = self.tr('Value Error: {0}').format(err)
            except Exception as err:
                msg = self.tr('Unknown Error: {0}').format(err)

        QMessageBox.warning(self, self.tr('CSW Connection error'), msg)
        return False
コード例 #8
0
def get_layers_for_wms(wms_url):
    """
    Retrieve metadata from a WMS service including layers, available styles, and the bounding box.

    Args:
        wms_url(str): URL to the WMS service endpoint.

    Returns:
        dict<layer_name:dict<styles,bbox>>: A dictionary with a key for each WMS layer available and a dictionary value containing metadata about the layer.
    """
    auth = Authentication(verify=False)
    wms = WebMapService(wms_url, auth=auth)
    #wms = WebMapService(wms_url)
    layers = wms.contents

    log.debug('WMS Contents:')
    log.debug(layers)

    layers_dict = dict()
    for layer_name, layer in layers.items():
        layer_styles = layer.styles
        layer_bbox = layer.boundingBoxWGS84
        leaflet_bbox = [[layer_bbox[1], layer_bbox[0]],
                        [layer_bbox[3], layer_bbox[2]]]
        layers_dict.update(
            {layer_name: {
                'styles': layer_styles,
                'bbox': leaflet_bbox
            }})

    log.debug('Layers Dict:')
    log.debug(layers_dict)
    return layers_dict
コード例 #9
0
ファイル: ogcwms.py プロジェクト: nakul-shahdadpuri/MSS
    def __init__(self,
                 url,
                 version=None,
                 xml=None,
                 username=None,
                 password=None,
                 parse_remote_metadata=False,
                 headers=None,
                 timeout=config_loader(dataset="WMS_request_timeout"),
                 auth=None):
        """Initialize."""

        if auth:
            if username:
                auth.username = username
            if password:
                auth.password = password
        self.url = url
        self.version = version
        self.timeout = timeout
        self.headers = headers
        self._capabilities = None
        self.auth = auth or Authentication(username, password)

        # Authentication handled by Reader
        reader = WMSCapabilitiesReader(self.version,
                                       url=self.url,
                                       headers=headers,
                                       auth=self.auth)
        if xml:
            # read from stored xml
            self._capabilities = reader.readString(xml)
        else:
            # read from server
            self._capabilities = reader.read(self.url, timeout=self.timeout)

        self.request = reader.request
        if not self.version:
            self.version = self._capabilities.attrib["version"]
            if self.version not in ["1.1.1", "1.3.0"]:
                self.version = "1.1.1"
            reader.version = self.version

        self.WMS_NAMESPACE = "{http://www.opengis.net/wms}" if self.version == "1.3.0" else ""
        self.OGC_NAMESPACE = "{http://www.opengis.net/ogc}" if self.version == "1.3.0" else ""

        # avoid building capabilities metadata if the
        # response is a ServiceExceptionReport
        se = self._capabilities.find('ServiceException')
        if se is not None:
            err_message = str(se.text).strip()
            raise ServiceException(err_message)

        # (mss) Store capabilities document.
        self.capabilities_document = reader.capabilities_document
        # (mss)

        # build metadata objects
        self._buildMetadata(parse_remote_metadata)
コード例 #10
0
ファイル: wcsBase.py プロジェクト: yalaudah/OWSLib
 def __init__(self, version=None, cookies=None, auth=None):
     """Initialize
     @type version: string
     @param version: WCS Version parameter e.g '1.0.0'
     """
     self.version = version
     self._infoset = None
     self.cookies = cookies
     self.auth = auth or Authentication()
コード例 #11
0
ファイル: common.py プロジェクト: yalaudah/OWSLib
 def __init__(self, version='1.0', username=None, password=None, auth=None):
     """Initialize"""
     if auth:
         if username:
             auth.username = username
         if password:
             auth.password = password
     self.auth = auth or Authentication(username, password)
     self.version = version
     self._infoset = None
コード例 #12
0
ファイル: csw.py プロジェクト: napattack/md-ingestion
 def __init__(self, community, url, schema, fromdate, clean, limit, outdir,
              verify):
     super().__init__(community, url, fromdate, clean, limit, outdir,
                      verify)
     logging.captureWarnings(True)
     self.csw = CatalogueServiceWeb(self.url,
                                    auth=Authentication(verify=self.verify))
     self._schema_type = schema
     self._schema = None
     self._constraints = None
コード例 #13
0
ファイル: maindialog.py プロジェクト: oscar1780/QGIS
    def show_metadata(self):
        """show record metadata"""

        if not self.treeRecords.selectedItems():
            return

        item = self.treeRecords.currentItem()
        if not item:
            return

        identifier = get_item_data(item, 'identifier')

        self.disable_ssl_verification = self.disableSSLVerification.isChecked()
        auth = None

        if self.disable_ssl_verification:
            try:
                auth = Authentication(verify=False)
            except NameError:
                pass

        try:
            with OverrideCursor(Qt.WaitCursor):
                cat = CatalogueServiceWeb(
                    self.catalog_url,
                    timeout=self.timeout,  # spellok
                    username=self.catalog_username,
                    password=self.catalog_password,
                    auth=auth)
                cat.getrecordbyid(
                    [self.catalog.records[identifier].identifier])
        except ExceptionReport as err:
            QMessageBox.warning(
                self, self.tr('GetRecords error'),
                self.tr('Error getting response: {0}').format(err))
            return
        except KeyError as err:
            QMessageBox.warning(self, self.tr('Record parsing error'),
                                self.tr('Unable to locate record identifier'))
            return

        record = cat.records[identifier]
        record.xml_url = cat.request

        crd = RecordDialog()
        metadata = render_template('en', self.context, record,
                                   'record_metadata_dc.html')

        style = QgsApplication.reportStyleSheet()
        crd.textMetadata.document().setDefaultStyleSheet(style)
        crd.textMetadata.setHtml(metadata)
        crd.exec_()
コード例 #14
0
ファイル: common.py プロジェクト: bgisb/gis-support-plugin
 def __init__(self, version='1.1.1', url=None, un=None, pw=None, headers=None, auth=None):
     """Initialize"""
     self.version = version
     self._infoset = None
     self.url = url
     if auth:
         if un:
             auth.username = un
         if pw:
             auth.password = pw
     self.headers = headers
     self.request = None
     self.auth = auth or Authentication(un, pw)
コード例 #15
0
def load_data(bbox, max_features=1000):
    wfs = WebFeatureService(NOVO_FCAMPO_WFS,
                            version='1.1.0',
                            auth=Authentication(verify=False))
    response = wfs.getfeature(typename='geoquimica:novo-fcampo',
                              bbox=bbox,
                              srsname='urn:x-ogc:def:crs:EPSG:4326',
                              maxfeatures=max_features)

    data = gpd.read_file(response)
    data.rename(lambda x: str(x).lower(), axis='columns', inplace=True)

    return data
コード例 #16
0
ファイル: wfs300.py プロジェクト: ludona7/OWSLib
    def __init__(self,
                 url,
                 version,
                 json_,
                 timeout=30,
                 username=None,
                 password=None,
                 auth=None):
        """
        initializer; implements Requirement 1 (/req/core/root-op)

        @type url: string
        @param url: url of WFS root document
        @type json_: string
        @param json_: json object
        @param timeout: time (in seconds) after which requests should timeout
        @param username: service authentication username
        @param password: service authentication password
        @param auth: instance of owslib.util.Authentication

        @return: initialized WebFeatureService_3_0_0 object
        """

        if "?" in url:
            self.url, self.url_query_string = url.split("?")
        else:
            self.url = url.rstrip("/") + "/"
            self.url_query_string = None

        self.version = version
        self.json_ = json_
        self.timeout = timeout
        if auth:
            if username:
                auth.username = username
            if password:
                auth.password = password
        self.auth = auth or Authentication(username, password)

        if json_ is not None:  # static JSON string
            self.links = json.loads(json_)["links"]
        else:
            response = http_get(url, headers=REQUEST_HEADERS,
                                auth=self.auth).json()
            self.links = response["links"]
コード例 #17
0
 def __init__(self,
              version,
              identifier,
              cookies,
              auth=None,
              timeout=30,
              headers=None):
     """Initialize
     @type version: string
     @param version: WCS Version parameter e.g '1.0.0'
     """
     self.version = version
     self._infoset = None
     self.identifier = identifier
     self.cookies = cookies
     self.headers = headers
     self.timeout = timeout
     self.auth = auth or Authentication()
コード例 #18
0
    def __init__(self, host, **kwargs):
        """Create a WCS client attached to the given host address (an URL).

        Args:
            host (str): the server URL.
            **kwargs: The keyword arguments with credentials to access OGC WCS.
        """
        invalid_parameters = set(kwargs) - {"username", "password"}

        if invalid_parameters:
            raise AttributeError(
                'invalid parameter(s): {}'.format(invalid_parameters))

        if 'username' in kwargs:
            auth = Authentication(username=kwargs['username'],
                                  password=kwargs['password'])
            self.wcs_owslib = WebCoverageService(host,
                                                 version='1.0.0',
                                                 auth=auth)
        else:
            self.wcs_owslib = WebCoverageService(host, version='1.0.0')
コード例 #19
0
ファイル: ogcwms.py プロジェクト: nakul-shahdadpuri/MSS
def openURL(url_base,
            data=None,
            method='Get',
            cookies=None,
            username=None,
            password=None,
            timeout=config_loader(dataset="WMS_request_timeout"),
            headers=None,
            verify=None,
            cert=None,
            auth=None,
            proxies=None):
    # (mss) added proxies
    # (mss) timeout default of 30secs set by the config_loader
    """
    Function to open URLs.

    Uses requests library but with additional checks for OGC service exceptions and url formatting.
    Also handles cookies and simple user password authentication.
    """
    headers = headers if headers is not None else {}
    rkwargs = {}

    rkwargs['timeout'] = timeout

    if auth:
        if username:
            auth.username = username
        if password:
            auth.password = password
        if cert:
            auth.cert = cert
        verify = verify and auth.verify
    else:
        auth = Authentication(username, password, cert, verify)
    if auth.username and auth.password:
        rkwargs['auth'] = (auth.username, auth.password)
    rkwargs['cert'] = auth.cert
    rkwargs['verify'] = verify

    # FIXUP for WFS in particular, remove xml style namespace
    # @TODO does this belong here?
    method = method.split("}")[-1]

    if method.lower() == 'post':
        try:
            etree.fromstring(data)
            headers['Content-Type'] = 'text/xml'
        except (ParseError, UnicodeEncodeError) as error:
            # (mss)
            logging.debug("ParseError, UnicodeEncodeError %s", error)

        rkwargs['data'] = data

    elif method.lower() == 'get':
        rkwargs['params'] = data

    else:
        raise ValueError(
            f"Unknown method ('{method}'), expected 'get' or 'post'")

    if cookies is not None:
        rkwargs['cookies'] = cookies

    req = requests.request(
        method.upper(),
        url_base,
        headers=headers,
        # MSS
        proxies=proxies,
        **rkwargs)

    if req.status_code in [400, 401]:
        raise ServiceException(req.text)

    if req.status_code in [404, 500, 502, 503, 504]:  # add more if needed
        req.raise_for_status()

    # check for service exceptions without the http header set
    if 'Content-Type' in req.headers and req.headers['Content-Type'] in [
            'text/xml', 'application/xml', 'application/vnd.ogc.se_xml',
            'application/vnd.ogc.wms_xml'
    ]:
        # just in case 400 headers were not set, going to have to read the xml to see if it's an exception report.
        se_tree = etree.fromstring(req.content)

        # to handle the variety of namespaces and terms across services
        # and versions, especially for "legacy" responses like WMS 1.3.0
        possible_errors = [
            '{http://www.opengis.net/ows}Exception',
            '{http://www.opengis.net/ows/1.1}Exception',
            '{http://www.opengis.net/ogc}ServiceException', 'ServiceException'
        ]

        for possible_error in possible_errors:
            serviceException = se_tree.find(possible_error)
            if serviceException is not None:
                # and we need to deal with some message nesting
                raise ServiceException('\n'.join([
                    str(t).strip() for t in serviceException.itertext()
                    if t.strip()
                ]))

    return ResponseWrapper(req)
コード例 #20
0
 def __init__(self, auth=None, timeout=30, headers=None):
     self.auth = auth or Authentication()
     self.headers = headers
     self.timeout = timeout
コード例 #21
0
    def __init__(self,
                 url,
                 lang='en-US',
                 version='3.0.0',
                 timeout=10,
                 skip_caps=False,
                 username=None,
                 password=None,
                 auth=None):
        """

        Construct and process a GetCapabilities request

        Parameters
        ----------

        - url: the URL of the CSW
        - lang: the language (default is 'en-US')
        - version: version (default is '3.0.0')
        - timeout: timeout in seconds
        - skip_caps: whether to skip GetCapabilities processing on init (default is False)
        - username: username for HTTP basic authentication
        - password: password for HTTP basic authentication
        - auth: instance of owslib.util.Authentication

        """
        if auth:
            if username:
                auth.username = username
            if password:
                auth.password = password
        self.url = util.clean_ows_url(url)
        self.lang = lang
        self.version = version
        self.timeout = timeout
        self.auth = auth or Authentication(username, password)
        self.service = 'CSW'
        self.exceptionreport = None
        self.owscommon = ows.OwsCommon('2.0.0')

        if not skip_caps:  # process GetCapabilities
            # construct request

            data = {
                'service': self.service,
                'version': self.version,
                'request': 'GetCapabilities'
            }

            self.request = urlencode(data)

            self._invoke()

            if self.exceptionreport is None:
                self.updateSequence = self._exml.getroot().attrib.get(
                    'updateSequence')

                # ServiceIdentification
                val = self._exml.find(
                    util.nspath_eval('ows200:ServiceIdentification',
                                     namespaces))
                if val is not None:
                    self.identification = ows.ServiceIdentification(
                        val, self.owscommon.namespace)
                else:
                    self.identification = None
                # ServiceProvider
                val = self._exml.find(
                    util.nspath_eval('ows200:ServiceProvider', namespaces))
                if val is not None:
                    self.provider = ows.ServiceProvider(
                        val, self.owscommon.namespace)
                else:
                    self.provider = None
                # ServiceOperations metadata
                self.operations = []
                for elem in self._exml.findall(
                        util.nspath_eval(
                            'ows200:OperationsMetadata/ows200:Operation',
                            namespaces)):  # noqa
                    self.operations.append(
                        ows.OperationsMetadata(elem, self.owscommon.namespace))
                self.constraints = {}
                for elem in self._exml.findall(
                        util.nspath_eval(
                            'ows200:OperationsMetadata/ows200:Constraint',
                            namespaces)):  # noqa
                    self.constraints[elem.attrib['name']] = ows.Constraint(
                        elem, self.owscommon.namespace)
                self.parameters = {}
                for elem in self._exml.findall(
                        util.nspath_eval(
                            'ows200:OperationsMetadata/ows200:Parameter',
                            namespaces)):  # noqa
                    self.parameters[elem.attrib['name']] = ows.Parameter(
                        elem, self.owscommon.namespace)

                # FilterCapabilities
                val = self._exml.find(
                    util.nspath_eval('fes:Filter_Capabilities', namespaces))
                self.filters = fes2.FilterCapabilities(val)
コード例 #22
0
from owslib.wfs import WebFeatureService
from owslib.util import Authentication
from pprint import pprint
import geopandas as gpd

wfs = WebFeatureService(
    'https://geoservicos.cprm.gov.br/geoserver/geoquimica/wfs',
    version='1.1.0',
    auth=Authentication(verify=False))

pprint([operation.name for operation in wfs.operations])

pprint(list(wfs.contents))

pprint(wfs.get_schema('geoquimica:novo-fcampo'))

response = wfs.getfeature(typename='geoquimica:novo-fcampo',
                          bbox=(-41.26, -12.82, -40.88, -12.52),
                          srsname='urn:x-ogc:def:crs:EPSG:4326')

#with open('/tmp/geoquimica.gml', 'wb') as output:
#    output.write(response.read())

df = gpd.read_file(response)

print(df.head())
コード例 #23
0
 def __init__(self, headers=None, auth=None):
     self.auth = auth or Authentication()
     self.headers = headers
コード例 #24
0
ファイル: common.py プロジェクト: vuilleumierc/OWSLib
 def __init__(self, auth=None):
     self.auth = auth or Authentication()