Skip to content

Instantly share code, notes, and snippets.

@epifanio
Created November 21, 2023 14:15
Show Gist options
  • Save epifanio/1fd4bcb36139fcd1846a6a2e4efb52b4 to your computer and use it in GitHub Desktop.
Save epifanio/1fd4bcb36139fcd1846a6a2e4efb52b4 to your computer and use it in GitHub Desktop.
from geolinks import sniff_link
from owslib.csw import CatalogueServiceWeb
from owslib.fes import (
And,
BBox,
Or,
PropertyIsEqualTo,
PropertyIsGreaterThanOrEqualTo,
PropertyIsLessThanOrEqualTo,
PropertyIsLike,
)
csw_test_endpoint = "https://nbs.csw.met.no"
ElementSetName = "full"
start_time = "2023-10-01 00:00"
end_time = "2023-10-02 00:00"
csw = CatalogueServiceWeb(csw_test_endpoint, timeout=60)
# Create filters
filter1 = PropertyIsLessThanOrEqualTo(
    propertyname="apiso:TempExtent_begin", literal=f"{end_time}"
)
filter2 = PropertyIsGreaterThanOrEqualTo(
    propertyname="apiso:TempExtent_end", literal=f"{start_time}"
)
# Combine filters
filter = And([filter1, filter2])
# Use the filter in a CSW getrecords request
csw.getrecords2(
    constraints=[filter],
    esn=f"{ElementSetName}",
    startposition=1,
    maxrecords=5,
)
# the following returns ... 7153 (isn't it too much?
csw.results['matches']
#####
print("Found {} records.\n".format(len(csw.records.keys())))
for key, value in list(csw.records.items()):
print("Title: [{}]\nID: {}\n".format(value.title, key))
filtered_attributes = [attr for attr in dir(value) if not attr.startswith("_")]
if 'bbox' in filtered_attributes:
print('bbox: ', value.bbox.maxx, value.bbox.maxy, value.bbox.minx, value.bbox.miny)
msg = "geolink: {geolink}\nscheme: {scheme}\nURL: {url}\n".format
for ref in value.references:
print(msg(geolink=sniff_link(ref["url"]), **ref))
print("#########################################################", "\n")
@epifanio
Copy link
Author

Note:

csw.getrecords2(
    constraints=[filter],
    esn=f"{ElementSetName}",
    startposition=1,
    maxrecords=5,
)

returns 5 records, even if you set maxrecord>=10 , for how is set-up the server, - it will always return max 10 elements at the time.
means you have to loop through all found records using the csw.results['matches'] value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment