|
| 1 | +import re |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from bs4 import BeautifulSoup |
| 5 | +from pygeometa.schemas.base import BaseOutputSchema |
| 6 | + |
| 7 | +THISDIR = Path(__file__).parent |
| 8 | + |
| 9 | + |
| 10 | +def text_or_null(node, strip=False): |
| 11 | + if not node: |
| 12 | + return None |
| 13 | + |
| 14 | + if strip: |
| 15 | + return node.text.strip() |
| 16 | + |
| 17 | + return node.text |
| 18 | + |
| 19 | + |
| 20 | +def text_or_empty(node, strip=False): |
| 21 | + if not node: |
| 22 | + return "" |
| 23 | + |
| 24 | + if strip: |
| 25 | + return node.text.strip() |
| 26 | + |
| 27 | + return node.text |
| 28 | + |
| 29 | + |
| 30 | +def scrub_dict(d): |
| 31 | + if type(d) is dict: |
| 32 | + return dict( |
| 33 | + (k, scrub_dict(v)) |
| 34 | + for k, v in d.items() |
| 35 | + if v is not None and scrub_dict(v) is not None |
| 36 | + ) |
| 37 | + else: |
| 38 | + return d |
| 39 | + |
| 40 | + |
| 41 | +def to_contact_role(node, role, mapped_role=None): |
| 42 | + if not mapped_role: |
| 43 | + mapped_role = role |
| 44 | + |
| 45 | + for idx, contact in enumerate(node.find_all(role)): |
| 46 | + name = f'{text_or_empty(contact.find("surName"))}, ' |
| 47 | + name += text_or_empty(contact.find("givenName")) |
| 48 | + org = text_or_empty(contact.find("organizationName")) |
| 49 | + yield ( |
| 50 | + mapped_role + (f"_{idx}" if idx else ""), |
| 51 | + { |
| 52 | + "organization": org, |
| 53 | + "individualname": name, |
| 54 | + "positionname": text_or_empty(contact.find("positionName")) |
| 55 | + or text_or_empty(contact.find("role")), |
| 56 | + "phone": "", |
| 57 | + "url": "", |
| 58 | + "fax": "", |
| 59 | + "address": "", |
| 60 | + "city": "", |
| 61 | + "administrativearea": "", |
| 62 | + "postalcode": "", |
| 63 | + "country": text_or_empty(contact.find("country")), |
| 64 | + "email": text_or_empty(contact.find("electronicMailAddress")), |
| 65 | + }, |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +class GBIF_EMLOutputSchema(BaseOutputSchema): |
| 70 | + def __init__(self): |
| 71 | + super().__init__("gbif-eml", "EML - GBIF profile", "xml", THISDIR) |
| 72 | + |
| 73 | + def import_(self, metadata): |
| 74 | + soup = BeautifulSoup(metadata, features="lxml-xml") |
| 75 | + dataset = soup.find("dataset") |
| 76 | + mcf = { |
| 77 | + "mcf": { |
| 78 | + "version": 1, |
| 79 | + }, |
| 80 | + "metadata": { |
| 81 | + "charset": "utf8", |
| 82 | + "hierarchylevel": "dataset", |
| 83 | + "datestamp": "$datetime$", |
| 84 | + }, |
| 85 | + "identification": {}, |
| 86 | + "contact": {}, |
| 87 | + "distribution": {}, |
| 88 | + } |
| 89 | + |
| 90 | + for identifier in dataset.find_all("alternateIdentifier"): |
| 91 | + mcf["metadata"]["identifier"] = text_or_null(identifier) |
| 92 | + |
| 93 | + if language := dataset.find("language"): |
| 94 | + mcf["metadata"]["language"] = text_or_null(language) |
| 95 | + |
| 96 | + idf = mcf["identification"] |
| 97 | + |
| 98 | + idf["title"] = text_or_null(dataset.find("title")) |
| 99 | + idf["abstract"] = text_or_null(dataset.find("abstract")) |
| 100 | + |
| 101 | + if intellectual_rights := dataset.find("intellectualRights"): |
| 102 | + url = ( |
| 103 | + intellectual_rights.find("ulink")["url"] |
| 104 | + if intellectual_rights.find("ulink") |
| 105 | + else None |
| 106 | + ) |
| 107 | + idf["rights"] = { |
| 108 | + "name": text_or_null(intellectual_rights.find("citetitle")), |
| 109 | + "url": url, |
| 110 | + } |
| 111 | + |
| 112 | + idf["url"] = text_or_null(dataset.find("alternateIdentifier")) |
| 113 | + idf["status"] = "completed" |
| 114 | + |
| 115 | + # if maintenance := dataset.find("maintenance"): |
| 116 | + # metadata.maintenance_update_description = text_or_null( |
| 117 | + # maintenance.find("description") |
| 118 | + # ) |
| 119 | + |
| 120 | + idf["maintenancefrequency"] = ( |
| 121 | + text_or_null(dataset.find("maintenanceUpdateFrequency")) or |
| 122 | + "unknown" |
| 123 | + ) |
| 124 | + |
| 125 | + idf["dates"] = {"publication": text_or_null(dataset.find("pubDate"))} |
| 126 | + idf["extents"] = {} |
| 127 | + |
| 128 | + if coords := dataset.find("boundingCoordinates"): |
| 129 | + idf["extents"]["spatial"] = [{}] |
| 130 | + spatial = idf["extents"]["spatial"][0] |
| 131 | + |
| 132 | + spatial["bbox"] = [ |
| 133 | + float(coords.find("westBoundingCoordinate").text), |
| 134 | + float(coords.find("southBoundingCoordinate").text), |
| 135 | + float(coords.find("eastBoundingCoordinate").text), |
| 136 | + float(coords.find("northBoundingCoordinate").text), |
| 137 | + ] |
| 138 | + |
| 139 | + spatial["crs"] = "4326" |
| 140 | + spatial["description"] = \ |
| 141 | + text_or_null(dataset.find("geographicDescription")) |
| 142 | + |
| 143 | + # temporal = idf["extents"]["temporal"] |
| 144 | + # temporal["begin"] |
| 145 | + # temporal["end"] |
| 146 | + # temporal["resolution"] |
| 147 | + |
| 148 | + idf["keywords"] = {} |
| 149 | + |
| 150 | + ct = mcf["contact"] |
| 151 | + |
| 152 | + for r, obj in to_contact_role(dataset, "contact", "pointOfContact"): |
| 153 | + ct[r] = obj |
| 154 | + |
| 155 | + for r, obj in to_contact_role(dataset, |
| 156 | + "metadataProvider", |
| 157 | + "distributor"): |
| 158 | + ct[r] = obj |
| 159 | + |
| 160 | + for r, obj in to_contact_role(dataset, "creator"): |
| 161 | + ct[r] = obj |
| 162 | + |
| 163 | + for r, obj in to_contact_role(dataset, |
| 164 | + "personnel", |
| 165 | + "projectPersonnel"): |
| 166 | + ct[r] = obj |
| 167 | + |
| 168 | + for idx, keyword_set in enumerate(dataset.find_all("keywordSet")): |
| 169 | + thesaurus = text_or_null(keyword_set.find("keywordThesaurus")) |
| 170 | + match = re.search(r"(?P<url>https?://[^\s]+)", thesaurus) |
| 171 | + definition = match.group("url") if match else None |
| 172 | + |
| 173 | + idf["keywords"][f"default-{idx}"] = { |
| 174 | + "keywords": [ |
| 175 | + text_or_null(kw) for kw in keyword_set.find_all("keyword") |
| 176 | + ], |
| 177 | + "vocabulary": {"name": thesaurus, "url": definition}, |
| 178 | + } |
| 179 | + |
| 180 | + mcf["spatial"] = {"datatype": "vector", "geomtype": "composite"} |
| 181 | + |
| 182 | + mcf["distribution"] = { |
| 183 | + "file": { |
| 184 | + "url": idf["url"], |
| 185 | + "type": "WWW:LINK", |
| 186 | + "function": "information", |
| 187 | + "description": "", |
| 188 | + "name": "Darwin Core Archive", |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return scrub_dict(mcf) |
0 commit comments