Esempio n. 1
0
    def test_wfs_build_getfeature_request_sortby(self):
        """Test the owsutil.wfs_build_getfeature_request method with a sortby.

        Test whether the XML of the WFS GetFeature call is generated correctly.

        """
        sort_by = SortBy([SortProperty('diepte_tot_m', 'DESC')])

        try:
            sort_by = etree.tostring(sort_by.toXML(), encoding='unicode')
        except LookupError:
            # Python2.7 without lxml uses 'utf-8' instead.
            sort_by = etree.tostring(sort_by.toXML(), encoding='utf-8')

        xml = owsutil.wfs_build_getfeature_request(
            'dov-pub:Boringen',
            propertyname=['fiche', 'diepte_tot_m'],
            sort_by=sort_by)
        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" '
            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
            'service="WFS" version="1.1.0" '
            'xsi:schemaLocation="http://www.opengis.net/wfs '
            'http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"><wfs:Query '
            'typeName="dov-pub:Boringen"><wfs:PropertyName>fiche</wfs'
            ':PropertyName><wfs:PropertyName>diepte_tot_m</wfs:PropertyName'
            '><ogc:Filter/><ogc:SortBy><ogc:SortProperty><ogc:PropertyName'
            '>diepte_tot_m</ogc:PropertyName><ogc:SortOrder>DESC</ogc'
            ':SortOrder></ogc:SortProperty></ogc:SortBy></wfs:Query></wfs'
            ':GetFeature>')
def get_csw_records(csw, filter_list, pagesize=10, maxrecords=1000):
    """Iterate `maxrecords`/`pagesize` times until the requested value in
    `maxrecords` is reached.
    """
    from owslib.fes import SortBy, SortProperty

    # Iterate over sorted results.
    sortby = SortBy([SortProperty("dc:title", "ASC")])
    csw_records = {}
    startposition = 0
    nextrecord = getattr(csw, "results", 1)
    while nextrecord != 0:
        csw.getrecords2(
            constraints=filter_list,
            startposition=startposition,
            maxrecords=pagesize,
            sortby=sortby,
        )
        csw_records.update(csw.records)
        if csw.results["nextrecord"] == 0:
            break
        startposition += pagesize + 1  # Last one is included.
        if startposition >= maxrecords:
            break
    csw.records.update(csw_records)
Esempio n. 3
0
def get_csw_records(csw, filter_list, pagesize=10, maxrecords=1000, **kwargs):
    """
    Iterate `maxrecords`/`pagesize` times until the requested value in
    `maxrecords` is reached.

    `kwargs` can by any `getrecords2` key word argument, like esn='full'.

    """
    from owslib.fes import SortBy, SortProperty

    # Iterate over sorted results.
    sortby = SortBy([SortProperty('dc:title', 'ASC')])
    csw_records = {}
    startposition = 0

    nextrecord = getattr(csw, 'results', 1)
    while nextrecord != 0:
        csw.getrecords2(constraints=filter_list,
                        startposition=startposition,
                        maxrecords=pagesize,
                        sortby=sortby,
                        **kwargs)
        csw_records.update(csw.records)
        if csw.results['nextrecord'] == 0:
            break
        startposition += pagesize + 1  # Last one is included.
        if startposition >= maxrecords:
            break
    csw.records.update(csw_records)
    return csw
Esempio n. 4
0
    def test_search_sortby_valid(self, mp_get_schema,
                                 mp_remote_describefeaturetype,
                                 mp_remote_wfs_feature, mp_dov_xml):
        """Test the search method with the query parameter and the sort_by
        parameter with a valid sort field.

        Test whether a dataframe is returned.

        Parameters
        ----------
        mp_get_schema : pytest.fixture
            Monkeypatch the call to a remote OWSLib schema.
        mp_remote_describefeaturetype : pytest.fixture
            Monkeypatch the call to a remote DescribeFeatureType.
        mp_remote_wfs_feature : pytest.fixture
            Monkeypatch the call to get WFS features.
        mp_dov_xml : pytest.fixture
            Monkeypatch the call to get the remote XML data.

        """
        df = self.search_instance.search(
            query=self.valid_query_single,
            sort_by=SortBy([SortProperty(self.valid_returnfields_extra[0])]))

        assert isinstance(df, DataFrame)
Esempio n. 5
0
    def test_search_sortby_invalid(self, mp_get_schema,
                                   mp_remote_describefeaturetype,
                                   mp_remote_wfs_feature, mp_dov_xml):
        """Test the search method with the query parameter and the sort_by
        parameter with an invalid sort field.

        Test whether an InvalidFieldError is raised.

        Parameters
        ----------
        mp_get_schema : pytest.fixture
            Monkeypatch the call to a remote OWSLib schema.
        mp_remote_describefeaturetype : pytest.fixture
            Monkeypatch the call to a remote DescribeFeatureType.
        mp_remote_wfs_feature : pytest.fixture
            Monkeypatch the call to get WFS features.
        mp_dov_xml : pytest.fixture
            Monkeypatch the call to get the remote XML data.

        """
        if self.xml_field is None:
            return

        with pytest.raises(InvalidFieldError):
            self.search_instance.search(
                query=self.valid_query_single,
                sort_by=SortBy([SortProperty(
                    self.xml_field)]))
Esempio n. 6
0
    def test_search_sortby_invalid(self, mp_remote_describefeaturetype,
                                   mp_remote_wfs_feature, mp_dov_xml):
        """Test the search method with the query parameter and the sort_by
        parameter with an invalid sort field.

        Test whether an InvalidFieldError is raised.

        Parameters
        ----------
        mp_remote_describefeaturetype : pytest.fixture
            Monkeypatch the call to a remote DescribeFeatureType.
        mp_remote_wfs_feature : pytest.fixture
            Monkeypatch the call to get WFS features.
        mp_dov_xml : pytest.fixture
            Monkeypatch the call to get the remote XML data.

        """
        with pytest.raises(InvalidFieldError):
            df = self.get_search_object().search(
                query=self.get_valid_query_single(),
                sort_by=SortBy([SortProperty(self.get_xml_field())]))
Esempio n. 7
0
 def __init__(self, endpoint=None):
     super(CswService, self).__init__(endpoint)
     self.sortby = SortBy([SortProperty('dc:identifier')])
Esempio n. 8
0
    def getidentifiers(self,
                       qtype=None,
                       typenames="csw:Record",
                       esn="brief",
                       keywords=[],
                       limit=None,
                       page=10,
                       outputschema="gmd",
                       startposition=0,
                       cql=None,
                       constraints=[],
                       **kw):
        from owslib.csw import namespaces
        # from owslib.namespaces import namespaces
        # constraints = []
        csw = self._ows(**kw)
        # self.sortby = SortBy([SortProperty('dc:identifier')])
        # self.sortby = SortBy([SortProperty('Identifier'),'ASC'])
        self.sortby = SortBy([SortProperty('Identifier', 'ASC')])

        if qtype is not None:
            constraints.append(PropertyIsEqualTo("dc:type", qtype))

        kwa = {
            "constraints": constraints,
            "typenames": typenames,
            "esn": esn,
            "startposition": startposition,
            "maxrecords": page,
            "outputschema": namespaces[outputschema],
            "cql": cql,
            "sortby": self.sortby
        }
        i = 0

        matches = 0

        while True:
            log.info('Making CSW request: getrecords2 %r', kwa)

            csw.getrecords2(**kwa)
            if csw.exceptionreport:
                err = 'Error getting identifiers: %r' % \
                      csw.exceptionreport.exceptions
                log.error(err)
                raise CswError(err)

            if matches == 0:
                matches = csw.results['matches']

            identifiers = csw.records.keys()
            if limit is not None:
                identifiers = identifiers[:(limit - startposition)]
            for ident in identifiers:
                yield ident

            if len(identifiers) == 0:
                break

            i += len(identifiers)
            if limit is not None and i > limit:
                break

            startposition += page
            if startposition >= (matches + 1):
                break

            kwa["startposition"] = startposition
# <codecell>

csw.get_operation_by_name('GetRecords').constraints

# <codecell>

# adjust to match MaxRecordDefault of CSW, if would be cleaner if we pick this up Capabilities XML
# this issue will allow for this: https://github.com/geopython/OWSLib/issues/211
pagesize = 10
sort_property = 'dc:title'  # a supported queryable of the CSW
sort_order = 'ASC'  # should be 'ASC' or 'DESC'

# <codecell>

sortby = SortBy([SortProperty(sort_property, sort_order)])
foo=sortby.properties

# <codecell>

# hopefully something like this will be implemented in fes soon
def dateRange(start_date='1900-01-01',stop_date='2100-01-01',constraint='overlaps'):
    if constraint == 'overlaps':
        start = fes.PropertyIsLessThanOrEqualTo(propertyname='apiso:TempExtent_begin', literal=stop_date)
        stop = fes.PropertyIsGreaterThanOrEqualTo(propertyname='apiso:TempExtent_end', literal=start_date)
    elif constraint == 'within':
        start = fes.PropertyIsGreaterThanOrEqualTo(propertyname='apiso:TempExtent_begin', literal=start_date)
        stop = fes.PropertyIsLessThanOrEqualTo(propertyname='apiso:TempExtent_end', literal=stop_date)
    return start,stop

# <codecell>