Skip to content

Instantly share code, notes, and snippets.

@maxcollombin
Last active October 13, 2023 13:39
Show Gist options
  • Select an option

  • Save maxcollombin/88cfaefa4db558bb3947fb43ef1a8dff to your computer and use it in GitHub Desktop.

Select an option

Save maxcollombin/88cfaefa4db558bb3947fb43ef1a8dff to your computer and use it in GitHub Desktop.
This script allows to harvest all the records from the GeoCat CSW service
"""
CSW harvesting script for the GeoCat CSW service.
This script allows to harvest all the records from the GeoCat CSW service
Author: [Maxime Collombin]
Date: [14/08/2023]
"""
# -*- coding: utf-8 -*-
from owslib.csw import CatalogueServiceWeb
from owslib.fes import PropertyIsLike
import pandas as pd
import requests
# Define the base URL of the CSW service
url = 'https://www.geocat.ch/geonetwork/srv/fre/csw'
# Define the query parameters
params = {
'service': 'CSW',
'version': '2.0.2',
'request': 'GetRecords',
'constraintLanguage': 'CQL_TEXT',
'constraint': "dc:subject = 'modèle de géodonnées minimal MGDM'",
'constraint_language_version': '1.1.0',
'typeNames': 'csw:Record',
'resultType': 'hits'
}
# Send the request to the CSW service and store the response
response = requests.get(url, params=params)
# Extract the value of numberOfRecordsMatched from the response content
response_content = response.content.decode('utf-8')
start_index = response_content.index('numberOfRecordsMatched') + len('numberOfRecordsMatched') + 2
end_index = response_content.index('"', start_index)
num_matched = int(response_content[start_index:end_index])
# Define the CSW service URL with the params defined above
csw_url = url + '?' + '&'.join(['='.join(i) for i in list(params.items())[:-1]]) + '&resultType=results&maxRecords=' + str(num_matched) + '&lang=fr'
# Define the CSW service URL
csw_url = url + '?service=CSW&version=2.0.2&request=GetRecords&constraintLanguage=CQL_TEXT&constraint=dc:type%20=%20\'model\'&constraint_language_version=1.1.0&typeNames=csw:Record&resultType=results&maxRecords=' + str(num_matched) + '&lang=fr'
# get the records attributes
csw = CatalogueServiceWeb(csw_url)
csw.getrecords2(maxrecords=num_matched)
records = csw.records
df = pd.DataFrame(columns=['identifier', 'title', 'abstract', 'url'])
for key, value in records.items():
df = df.append({'identifier': value.identifier, 'title': value.title, 'abstract': value.abstract, 'url': 'https://www.geocat.ch/geonetwork/srv/fre/catalog.search#/metadata/' + str(value.identifier)}, ignore_index=True)
# save the dataframe to an xlsx file with utf-8 encoding
df.to_excel('csw_records.xlsx', encoding='utf-8')
@maxcollombin

maxcollombin commented Oct 9, 2023 •

Copy link
Copy Markdown
Author

There seem to be several records on geocat.ch for the same MGDM. One for the geoservices and/or bulk data and one for the models without linking the 2 sources.

Example for the alignment of national roads:

What could be the explanations/reasons?

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