Esempio n. 1
0
    def _setconstraint(self,
                       parent,
                       qtype=None,
                       propertyname='csw30:AnyText',
                       keywords=[],
                       bbox=None,
                       cql=None,
                       identifier=None):
        if keywords or bbox is not None or qtype is not None or cql is not None or identifier is not None:
            node0 = etree.SubElement(
                parent, util.nspath_eval('csw30:Constraint', namespaces))
            node0.set('version', '1.1.0')

            if identifier is not None:  # set identifier filter, overrides all other parameters
                flt = fes2.FilterRequest()
                node0.append(flt.set(identifier=identifier))
            elif cql is not None:  # send raw CQL query
                # CQL passed, overrides all other parameters
                node1 = etree.SubElement(
                    node0, util.nspath_eval('csw30:CqlText', namespaces))
                node1.text = cql
            else:  # construct a Filter request
                flt = fes2.FilterRequest()
                node0.append(
                    flt.set(qtype=qtype,
                            keywords=keywords,
                            propertyname=propertyname,
                            bbox=bbox))
Esempio n. 2
0
    def getrecords(self,
                   constraints=[],
                   sortby=None,
                   typenames='csw30:Record',
                   esn='summary',
                   outputschema=namespaces['csw30'],
                   format=outputformat,
                   startposition=0,
                   maxrecords=10,
                   cql=None,
                   xml=None):
        """

        Construct and process a  GetRecords request

        Parameters
        ----------

        - constraints: the list of constraints (OgcExpression from owslib.fes2 module)
        - sortby: an OGC SortBy object (SortBy from owslib.fes2 module)
        - typenames: the typeNames to query against (default is csw30:Record)
        - esn: the ElementSetName 'full', 'brief' or 'summary' (default is 'summary')
        - outputschema: the outputSchema (default is 'http://www.opengis.net/cat/csw/3.0.0')
        - format: the outputFormat (default is 'application/xml')
        - startposition: requests a slice of the result set, starting at this position (default is 0)
        - maxrecords: the maximum number of records to return. No records are returned if 0 (default is 10)
        - cql: common query language text.  Note this overrides bbox, qtype, keywords
        - xml: raw XML request.  Note this overrides all other options

        """

        if xml is not None:
            if xml.startswith('<'):
                self.request = etree.fromstring(xml)
                val = self.request.find(
                    util.nspath_eval('csw30:Query/csw30:ElementSetName',
                                     namespaces))
                if val is not None:
                    esn = util.testXMLValue(val)
                val = self.request.attrib.get('outputSchema')
                if val is not None:
                    outputschema = util.testXMLValue(val, True)
            else:
                self.request = xml
        else:
            # construct request
            node0 = self._setrootelement('csw30:GetRecords')
            if etree.__name__ != 'lxml.etree':  # apply nsmap manually
                node0.set('xmlns:ows110', namespaces['ows110'])
                node0.set('xmlns:gmd', namespaces['gmd'])
                node0.set('xmlns:dif', namespaces['dif'])
                node0.set('xmlns:fgdc', namespaces['fgdc'])
            node0.set('outputSchema', outputschema)
            node0.set('outputFormat', format)
            node0.set('version', self.version)
            node0.set('service', self.service)
            if startposition > 0:
                node0.set('startPosition', str(startposition))
            node0.set('maxRecords', str(maxrecords))
            node0.set(util.nspath_eval('xsi:schemaLocation', namespaces),
                      schema_location)

            node1 = etree.SubElement(
                node0, util.nspath_eval('csw30:Query', namespaces))
            node1.set('typeNames', typenames)

            etree.SubElement(
                node1, util.nspath_eval('csw30:ElementSetName',
                                        namespaces)).text = esn

            if any([len(constraints) > 0, cql is not None]):
                node2 = etree.SubElement(
                    node1, util.nspath_eval('csw30:Constraint', namespaces))
                node2.set('version', '1.1.0')
                flt = fes2.FilterRequest()
                if len(constraints) > 0:
                    node2.append(flt.setConstraintList(constraints))
                # Now add a CQL filter if passed in
                elif cql is not None:
                    etree.SubElement(
                        node2, util.nspath_eval('csw30:CqlText',
                                                namespaces)).text = cql

            if sortby is not None and isinstance(sortby, fes2.SortBy):
                node1.append(sortby.toXML())

            self.request = node0

        self._invoke()

        if self.exceptionreport is None:
            self.results = {}

            # process search results attributes
            val = self._exml.find(
                util.nspath_eval(
                    'csw30:SearchResults',
                    namespaces)).attrib.get('numberOfRecordsMatched')
            self.results['matches'] = int(util.testXMLValue(val, True))
            val = self._exml.find(
                util.nspath_eval(
                    'csw30:SearchResults',
                    namespaces)).attrib.get('numberOfRecordsReturned')
            self.results['returned'] = int(util.testXMLValue(val, True))
            val = self._exml.find(
                util.nspath_eval('csw30:SearchResults',
                                 namespaces)).attrib.get('nextRecord')
            if val is not None:
                self.results['nextrecord'] = int(util.testXMLValue(val, True))
            else:
                warnings.warn(
                    """CSW Server did not supply a nextRecord value (it is optional), so the client
                should page through the results in another way.""")
                # For more info, see:
                # https://github.com/geopython/OWSLib/issues/100
                self.results['nextrecord'] = None

            # process list of matching records
            self.records = OrderedDict()

            self._parserecords(outputschema, esn)