-
Notifications
You must be signed in to change notification settings - Fork 53
GBIF EML Profile #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
GBIF EML Profile #268
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| # ================================================================= | ||
| # | ||
| # Terms and Conditions of Use | ||
| # | ||
| # Unless otherwise noted, computer program source code of this | ||
| # distribution # is covered under Crown Copyright, Government of | ||
| # Canada, and is distributed under the MIT License. | ||
| # | ||
| # The Canada wordmark and related graphics associated with this | ||
| # distribution are protected under trademark law and copyright law. | ||
| # No permission is granted to use them outside the parameters of | ||
| # the Government of Canada's corporate identity program. For | ||
| # more information, see | ||
| # http://www.tbs-sct.gc.ca/fip-pcim/index-eng.asp | ||
| # | ||
| # Copyright title to all 3rd party software distributed with this | ||
| # software is held by the respective copyright holders as noted in | ||
| # those files. Users are asked to read the 3rd Party Licenses | ||
| # referenced with those assets. | ||
| # | ||
| # Copyright (c) 2026 Niccolò Cantù | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person | ||
| # obtaining a copy of this software and associated documentation | ||
| # files (the "Software"), to deal in the Software without | ||
| # restriction, including without limitation the rights to use, | ||
| # copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the | ||
| # Software is furnished to do so, subject to the following | ||
| # conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be | ||
| # included in all copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
| # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
| # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
| # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| # OTHER DEALINGS IN THE SOFTWARE. | ||
| # | ||
| # ================================================================= | ||
|
|
||
| from pathlib import Path | ||
| import re | ||
|
|
||
| from bs4 import BeautifulSoup | ||
| import pycountry | ||
|
|
||
| from pygeometa.schemas.base import BaseOutputSchema | ||
|
|
||
| THISDIR = Path(__file__).parent | ||
|
|
||
|
|
||
| def text_or_null(node, strip=False): | ||
| if not node: | ||
| return None | ||
|
|
||
| if strip: | ||
| return node.text.strip() | ||
|
|
||
| return node.text | ||
|
|
||
|
|
||
| def text_or_empty(node, strip=False): | ||
| if not node: | ||
| return '' | ||
|
|
||
| if strip: | ||
| return node.text.strip() | ||
|
|
||
| return node.text | ||
|
|
||
|
|
||
| def scrub_dict(d): | ||
| if type(d) is dict: | ||
| return dict( | ||
| (k, scrub_dict(v)) | ||
| for k, v in d.items() | ||
| if v is not None and scrub_dict(v) is not None | ||
| ) | ||
| else: | ||
| return d | ||
|
|
||
|
|
||
| def to_contact_role(node, role, mapped_role=None): | ||
| if not mapped_role: | ||
| mapped_role = role | ||
|
|
||
| for idx, contact in enumerate(node.find_all(role)): | ||
| name = f'{text_or_empty(contact.find("surName"))}, ' | ||
| name += text_or_empty(contact.find('givenName')) | ||
| org = text_or_empty(contact.find('organizationName')) | ||
| yield ( | ||
| mapped_role + (f'_{idx}' if idx else ''), | ||
| { | ||
| 'organization': org, | ||
| 'individualname': name, | ||
| 'positionname': text_or_empty(contact.find('positionName')) | ||
| or text_or_empty(contact.find('role')), | ||
| 'phone': '', | ||
| 'url': '', | ||
| 'fax': '', | ||
| 'address': '', | ||
| 'city': '', | ||
| 'administrativearea': '', | ||
| 'postalcode': '', | ||
| 'country': text_or_empty(contact.find('country')), | ||
| 'email': text_or_empty(contact.find('electronicMailAddress')) | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| class GBIF_EMLOutputSchema(BaseOutputSchema): | ||
| def __init__(self): | ||
| super().__init__('gbif-eml', 'EML - GBIF profile', 'xml', THISDIR) | ||
|
|
||
| def import_(self, metadata): | ||
| soup = BeautifulSoup(metadata, features='lxml-xml') | ||
| dataset = soup.find('dataset') | ||
| mcf = { | ||
| 'mcf': { | ||
| 'version': 1, | ||
| }, | ||
| 'metadata': { | ||
| 'charset': 'utf8', | ||
| 'hierarchylevel': 'dataset', | ||
| 'datestamp': text_or_null(dataset.find('pubDate'), strip=True) | ||
| or '$date$', | ||
| }, | ||
| 'identification': {}, | ||
| 'contact': {}, | ||
| 'distribution': {} | ||
| } | ||
|
|
||
| for identifier in dataset.find_all('alternateIdentifier'): | ||
| mcf['metadata']['identifier'] = text_or_null(identifier) | ||
|
|
||
| if language := dataset.find('language'): | ||
| lang = text_or_null(language) | ||
| if lang and pycountry.languages.get(alpha_3=lang): | ||
| mcf['metadata']['language'] = pycountry.languages.get( | ||
| alpha_3=lang | ||
| ).alpha_2 | ||
|
|
||
| idf = mcf['identification'] | ||
|
|
||
| idf['title'] = text_or_null(dataset.find('title')) | ||
| idf['abstract'] = text_or_null(dataset.find('abstract')) | ||
|
|
||
| if intellectual_rights := dataset.find('intellectualRights'): | ||
| url = ( | ||
| intellectual_rights.find('ulink')['url'] | ||
| if intellectual_rights.find('ulink') | ||
| else None | ||
| ) | ||
| idf['rights'] = { | ||
| 'name': text_or_null(intellectual_rights.find('citetitle')), | ||
| 'url': url | ||
| } | ||
|
|
||
| idf['url'] = text_or_null(dataset.find('alternateIdentifier')) | ||
| idf['status'] = 'completed' | ||
|
|
||
| idf['maintenancefrequency'] = ( | ||
| text_or_null(dataset.find('maintenanceUpdateFrequency')) | ||
| or 'unknown' | ||
| ) | ||
|
|
||
| idf['dates'] = { | ||
| 'publication': text_or_null(dataset.find('pubDate'), strip=True) | ||
| } | ||
| idf['extents'] = {} | ||
|
|
||
| if coords := dataset.find('boundingCoordinates'): | ||
| idf['extents']['spatial'] = [{}] | ||
| spatial = idf['extents']['spatial'][0] | ||
|
|
||
| spatial['bbox'] = [ | ||
| float(coords.find('westBoundingCoordinate').text), | ||
| float(coords.find('southBoundingCoordinate').text), | ||
| float(coords.find('eastBoundingCoordinate').text), | ||
| float(coords.find('northBoundingCoordinate').text) | ||
| ] | ||
|
|
||
| spatial['crs'] = 4326 | ||
| spatial['description'] = text_or_null( | ||
| dataset.find('geographicDescription') | ||
| ) | ||
|
|
||
| idf['keywords'] = {} | ||
|
|
||
| ct = mcf['contact'] | ||
|
|
||
| for r, obj in to_contact_role(dataset, 'contact', 'pointOfContact'): | ||
| ct[r] = obj | ||
|
|
||
| for r, obj in to_contact_role( | ||
| dataset, 'metadataProvider', 'distributor' | ||
| ): | ||
| ct[r] = obj | ||
|
|
||
| for r, obj in to_contact_role(dataset, 'creator'): | ||
| ct[r] = obj | ||
|
|
||
| for r, obj in to_contact_role( | ||
| dataset, 'personnel', 'projectPersonnel' | ||
| ): | ||
| ct[r] = obj | ||
|
|
||
| for idx, keyword_set in enumerate(dataset.find_all('keywordSet')): | ||
| thesaurus = text_or_null(keyword_set.find('keywordThesaurus')) | ||
| match = re.search(r'(?P<url>https?://[^\s]+)', thesaurus) | ||
| definition = match.group('url') if match else None | ||
|
|
||
| idf['keywords'][f'default-{idx}'] = { | ||
| 'keywords': [ | ||
| text_or_null(kw) for kw in keyword_set.find_all('keyword') | ||
| ], | ||
| 'vocabulary': {'name': thesaurus, 'url': definition}, | ||
| } | ||
|
|
||
| mcf['spatial'] = {'datatype': 'vector', 'geomtype': 'composite'} | ||
|
|
||
| mcf['distribution'] = { | ||
| 'file': { | ||
| 'url': idf['url'], | ||
| 'type': 'WWW:LINK', | ||
| 'function': 'information', | ||
| 'description': '', | ||
| 'name': 'Darwin Core Archive' | ||
| } | ||
| } | ||
|
|
||
| return scrub_dict(mcf) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| <eml:eml xmlns:eml="eml://ecoinformatics.org/eml-2.1.1" xmlns:dc="http://purl.org/dc/terms/" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="eml://ecoinformatics.org/eml-2.1.1 http://rs.gbif.org/schema/eml-gbif-profile/1.1/eml.xsd" | ||
| packageId="{{ record['metadata']['dataseturi'] }}" system="http://gbif.org" | ||
| scope="system" xml:lang="{{ record['identification']['language'] }}"> | ||
|
|
||
| <dataset> | ||
| <alternateIdentifier>{{ record['identification'].get('doi') }}</alternateIdentifier> | ||
| <title xml:lang="{{ record['identification']['language'] }}">{{ record['identification']['title'] }}</title> | ||
| {# | ||
| <!--creator> | ||
| {% include 'person.j2' %} | ||
| </creator> | ||
| <creator> | ||
| {% include 'person.j2' %} | ||
| </creator> | ||
| <metadataProvider> | ||
| {% include 'person.j2' %} | ||
| </metadataProvider--> | ||
| #} | ||
| <pubDate> | ||
| {{ record['identification']['dates']['publication'] }} | ||
| </pubDate> | ||
| <language>{{ record['identification']['language'] }}</language> | ||
| <abstract> | ||
| <para>{{ record['identification']['abstract'] }}</para> | ||
| </abstract> | ||
| {% for group, keywords in record['identification']['keywords'].items() %} | ||
| <keywordSet> | ||
| {% for kw in keywords['keywords'] %} | ||
| <keyword>{{ kw }}</keyword> | ||
| {% endfor %} | ||
| <keywordThesaurus>{{ keywords['vocabulary']['name'] }}: {{ keywords['vocabulary']['url'] }}</keywordThesaurus> | ||
| </keywordSet> | ||
| {% endfor%} | ||
| <intellectualRights> | ||
| <para>This work is licensed under a <ulink url="{{ record['identification']['rights']['url'] }}"> | ||
| <citetitle>{{ record['identification']['rights']['name'] }}</citetitle> | ||
| </ulink>.</para> | ||
| </intellectualRights> | ||
| <distribution scope="document"> | ||
| <online> | ||
| {% for key, value in record['distribution'].items() %} | ||
| <url function="{{ value['function'] }}">{{ value['url' ]}}</url> | ||
| {% endfor %} | ||
| </online> | ||
| </distribution> | ||
| {% set extents = record['identification']['extents'] %} | ||
| {% set bbox = extents['spatial'][0]['bbox'] %} | ||
| <coverage> | ||
| <geographicCoverage> | ||
| <geographicDescription>{{ extents['spatial'][0]['description'] }}</geographicDescription> | ||
| <boundingCoordinates> | ||
| <westBoundingCoordinate>{{ bbox[0] }}</westBoundingCoordinate> | ||
| <eastBoundingCoordinate>{{ bbox[1] }}</eastBoundingCoordinate> | ||
| <northBoundingCoordinate>{{ bbox[2] }}</northBoundingCoordinate> | ||
| <southBoundingCoordinate>{{ bbox[3] }}</southBoundingCoordinate> | ||
| </boundingCoordinates> | ||
| </geographicCoverage> | ||
| {% if 'temporal' in extents %} | ||
| <temporalCoverage> | ||
| <rangeOfDates> | ||
| <beginDate> | ||
| <calendarDate>{{ extents['temporal'][0]['begin'] }}</calendarDate> | ||
| </beginDate> | ||
| {% if extents['temporal'][0]['end'] %} | ||
| <endDate> | ||
| <calendarDate>{{ extents['temporal'][0]['end'] }}</calendarDate> | ||
| </endDate> | ||
| {% endif %} | ||
| </rangeOfDates> | ||
| </temporalCoverage> | ||
| {% endif %} | ||
| <taxonomicCoverage> | ||
| <generalTaxonomicCoverage> | ||
| </generalTaxonomicCoverage> | ||
| <taxonomicClassification> | ||
| <taxonRankName></taxonRankName> | ||
| <taxonRankValue></taxonRankValue> | ||
| <commonName></commonName> | ||
| </taxonomicClassification> | ||
| </taxonomicCoverage> | ||
| </coverage> | ||
| <maintenance> | ||
| <description> | ||
| <para /> | ||
| </description> | ||
| <maintenanceUpdateFrequency></maintenanceUpdateFrequency> | ||
| </maintenance> | ||
|
|
||
| {# | ||
| <contact> | ||
| {% include 'person.j2' %} | ||
| </contact> | ||
| <methods> | ||
| <methodStep> | ||
| <description> | ||
| <para></para> | ||
| </description> | ||
| </methodStep> | ||
| <sampling> | ||
| <studyExtent> | ||
| <description> | ||
| <para> | ||
| </para> | ||
| </description> | ||
| </studyExtent> | ||
| <samplingDescription> | ||
| <para></para> | ||
| </samplingDescription> | ||
| </sampling> | ||
| <qualityControl> | ||
| <description> | ||
| <para></para> | ||
| </description> | ||
| </qualityControl> | ||
| </methods> | ||
| <project> | ||
| <title></title> | ||
| <personnel> | ||
| {% include 'person.j2' %} | ||
| <role /> | ||
| </personnel> | ||
| <abstract> | ||
| <para></para> | ||
| </abstract> | ||
| <funding> | ||
| <para>Artsdatabanken</para> | ||
| </funding> | ||
| <studyAreaDescription> | ||
| <descriptor name="generic" citableClassificationSystem="false"> | ||
| <descriptorValue></descriptorValue> | ||
| </descriptor> | ||
| </studyAreaDescription> | ||
| </project> | ||
| #} | ||
| </dataset> | ||
| {# | ||
| <additionalMetadata> | ||
| <metadata> | ||
| <gbif> | ||
| <dateStamp></dateStamp> | ||
| <hierarchyLevel>dataset</hierarchyLevel> | ||
| <citation></citation> | ||
| <resourceLogoUrl></resourceLogoUrl> | ||
| </gbif> | ||
| </metadata> | ||
| </additionalMetadata> | ||
| #} | ||
| </eml:eml> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.