from owslib.csw import CatalogueServiceWeb
from owslib.fes import PropertyIsEqualTo, PropertyIsLike, BBox
csw = CatalogueServiceWeb('https://catalog.data.gov/csw')
query = PropertyIsEqualTo('csw:AnyText', 'Africa')
csw.getrecords2(constraints=[query], maxrecords=20)
print('number of records={}\n'.format(len(csw.records.keys())))
print(csw.records)
number of records=10 OrderedDict([('cb90573b-9694-44f2-844b-225aa18d269f', <owslib.csw.CswRecord object at 0x7f3dc00b9d30>), ('DOC-5431', <owslib.csw.CswRecord object at 0x7f3dc0077278>), ('8dd433c4-c0c9-4b01-be2c-ba2d0179ef68', <owslib.csw.CswRecord object at 0x7f3dc00772e8>), ('26d4429f-1b83-4903-98f8-58321bbebbd6', <owslib.csw.CswRecord object at 0x7f3dc0077e48>), ('69b647c7-360b-433e-a81e-a4648a68f868', <owslib.csw.CswRecord object at 0x7f3dc007d048>), ('2ea8cfed-9253-4893-a917-3887dc091292', <owslib.csw.CswRecord object at 0x7f3dc007d668>), ('C179003262-ORNL_DAAC', <owslib.csw.CswRecord object at 0x7f3dc007da90>), ('e5cfb3c3-482d-4557-81c4-d2702c4a0478', <owslib.csw.CswRecord object at 0x7f3dc007da20>), ('C301253-GHRC', <owslib.csw.CswRecord object at 0x7f3dc007df98>), ('36561437-6f46-46f9-9094-4d6051a672cf', <owslib.csw.CswRecord object at 0x7f3dc00860f0>)])
We always use the get_csw_records
function below when accessing CSW, which iterates over the pagesize
until the maxrecords
specified by the user is reached.
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)
get_csw_records(csw, [query], maxrecords=20)
print('number of records={}\n'.format(len(csw.records.keys())))
print(csw.records)
number of records=20 OrderedDict([('gov.noaa.ngdc.mgg.geology:archived-data_LacCore-sample-repository', <owslib.csw.CswRecord object at 0x7f3dc0077860>), ('gov.noaa.ngdc.mgg.geology:archived-data_BPCRC-polar-rock-repository', <owslib.csw.CswRecord object at 0x7f3dc00770f0>), ('b7242ec9-df80-4107-9104-df2b9fa02a6b', <owslib.csw.CswRecord object at 0x7f3dc00867b8>), ('DDI-MCC-BEN-WB-PFR-2015-v01', <owslib.csw.CswRecord object at 0x7f3dc00864e0>), ('gov.noaa.ngdc.mgg.geology:G02885', <owslib.csw.CswRecord object at 0x7f3dc00862b0>), ('17355dc0-4149-439c-82b6-ac77596d6157', <owslib.csw.CswRecord object at 0x7f3dc0086048>), ('ddc3adf8-f95a-48c9-bdb2-beab9c0b69cc', <owslib.csw.CswRecord object at 0x7f3dc0019a58>), ('C179002886-ORNL_DAAC', <owslib.csw.CswRecord object at 0x7f3dc0019ac8>), ('noaa.ncei.nsidc.G01175', <owslib.csw.CswRecord object at 0x7f3dc00190b8>), ('gov.noaa.cpc:CPC-FEWS-NET-DLY-ARC2-AFRICA-V2013', <owslib.csw.CswRecord object at 0x7f3dc0022048>), ('gov.noaa.ngdc.mgg.photos:51', <owslib.csw.CswRecord object at 0x7f3dc007da20>), ('gov.noaa.ncdc:C00628', <owslib.csw.CswRecord object at 0x7f3dc007dcf8>), ('NASA-0000006', <owslib.csw.CswRecord object at 0x7f3dc007d710>), ('d9ec4b32-015b-11e5-b9a9-22000b8e85d8', <owslib.csw.CswRecord object at 0x7f3dc007d860>), ('100681', <owslib.csw.CswRecord object at 0x7f3dc007d2e8>), ('be6872b2-0158-11e5-b9a9-22000b8e85d8', <owslib.csw.CswRecord object at 0x7f3dc007d0b8>), ('gov.noaa.ncdc:C00784', <owslib.csw.CswRecord object at 0x7f3dd019fc18>), ('gov.noaa.nodc:7601422', <owslib.csw.CswRecord object at 0x7f3dd013d198>), ('gov.noaa.nodc:0002013', <owslib.csw.CswRecord object at 0x7f3dc0077ef0>), ('gov.noaa.nmfs.inport:18742', <owslib.csw.CswRecord object at 0x7f3dc0077358>)])