|
| 1 | +import json |
| 2 | + |
| 3 | +import pyarrow as pa |
| 4 | +from lxml import etree |
| 5 | +from pygeometa.schemas.gbif_eml import GBIF_EMLOutputSchema |
| 6 | +from pygeometa.schemas.iso19139 import ISO19139OutputSchema |
| 7 | +from shapely.geometry import box |
| 8 | + |
| 9 | +from ..settings import env as logger |
| 10 | +from .settings import ( |
| 11 | + AWS_ENDPOINT_URL, |
| 12 | + CSW_PATH, |
| 13 | + GEOAPI_PUBLISH_URL, |
| 14 | + IPT_URL, |
| 15 | + RESOURCES_PREFIX, |
| 16 | + S3_BUCKET, |
| 17 | + conn, |
| 18 | +) |
| 19 | + |
| 20 | +PARSER = etree.XMLParser(resolve_entities=False) |
| 21 | +eml = GBIF_EMLOutputSchema() |
| 22 | +iso = ISO19139OutputSchema() |
| 23 | + |
| 24 | + |
| 25 | +def get_anytext(bag): |
| 26 | + """ |
| 27 | + generate bag of text for free text searches |
| 28 | + accepts list of words, string of XML, or etree.Element |
| 29 | + """ |
| 30 | + |
| 31 | + if isinstance(bag, list): # list of words |
| 32 | + return " ".join([_f for _f in bag if _f]).strip() |
| 33 | + else: # xml |
| 34 | + if isinstance(bag, bytes) or isinstance(bag, str): |
| 35 | + # serialize to lxml |
| 36 | + bag = etree.fromstring(bag, PARSER) # noqa: S320 |
| 37 | + # get all XML element content |
| 38 | + return " ".join([value.strip() for value in bag.xpath("//text()")]) |
| 39 | + |
| 40 | + |
| 41 | +def eml_to_record(ds, text): |
| 42 | + metadata = eml.import_(text) |
| 43 | + |
| 44 | + metadata["metadata"]["identifier"] = f"ipt__{ds['id']}" |
| 45 | + |
| 46 | + xml = iso.write(metadata) |
| 47 | + fts = get_anytext(xml) |
| 48 | + idf = metadata["identification"] |
| 49 | + bbox = idf["extents"]["spatial"][0]["bbox"] |
| 50 | + |
| 51 | + contribs = [] |
| 52 | + for role, contact in metadata["contact"].items(): |
| 53 | + role = role.split("_")[0] |
| 54 | + contribs.append(contact["individualname"]) |
| 55 | + |
| 56 | + keywords = [] |
| 57 | + for _k, v in idf["keywords"].items(): |
| 58 | + keywords += v["keywords"] |
| 59 | + |
| 60 | + if ds.get("ipt_dwca"): |
| 61 | + links = [ |
| 62 | + { |
| 63 | + "name": "Parquet", |
| 64 | + "description": "The resource as (geo)parquet file", |
| 65 | + "protocol": "FILE:GEO", |
| 66 | + "url": f"{AWS_ENDPOINT_URL}/{S3_BUCKET}{RESOURCES_PREFIX}{ds['id']}.parquet", # noqa: E501 |
| 67 | + }, |
| 68 | + { |
| 69 | + "name": "DWCA", |
| 70 | + "description": "The resource as Darwin Core Archive", |
| 71 | + "protocol": "file", |
| 72 | + "url": f"{IPT_URL}/archive.do?r={ds['id']}", # noqa: E501 |
| 73 | + }, |
| 74 | + ] |
| 75 | + |
| 76 | + if GEOAPI_PUBLISH_URL: |
| 77 | + links.append( |
| 78 | + { |
| 79 | + "name": "OGC API Feature", |
| 80 | + "description": "OGC REST API to the resource", |
| 81 | + "protocol": "OGCFeat", |
| 82 | + "url": f"{GEOAPI_PUBLISH_URL}/collections/ipt__{ds['id']}/items?f=json", # noqa: E501 |
| 83 | + }, |
| 84 | + ) |
| 85 | + else: |
| 86 | + links = [] |
| 87 | + |
| 88 | + return { |
| 89 | + "identifier": metadata["metadata"]["identifier"], |
| 90 | + "typename": "gmd:MD_Metadata", |
| 91 | + "schema": "http://www.isotc211.org/2005/gmd", |
| 92 | + "mdsource": "local", |
| 93 | + "insert_date": idf["dates"]["publication"], |
| 94 | + "title": ds["title"], |
| 95 | + "date_modified": idf["dates"]["publication"], |
| 96 | + "type": "dataset", |
| 97 | + "format": None, |
| 98 | + "wkt_geometry": box(*bbox).wkt, |
| 99 | + "metadata": xml, |
| 100 | + "xml": xml, |
| 101 | + "keywords": ", ".join(set(keywords)), |
| 102 | + "metadata_type": "application/xml", |
| 103 | + "anytext": fts, |
| 104 | + "abstract": metadata["identification"]["abstract"], |
| 105 | + "date": idf["dates"]["publication"], |
| 106 | + "creator": "Norsk institutt for naturforskning (NINA)", |
| 107 | + "publisher": "Norsk institutt for naturforskning (NINA)", |
| 108 | + "contributor": "; ".join(set(contribs)), |
| 109 | + "links": json.dumps(links), |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +def write_eml_record(rows): |
| 114 | + logger.info("converting to arrow") |
| 115 | + records = pa.Table.from_pylist(rows) # noqa: F841 |
| 116 | + logger.info("write to S3") |
| 117 | + conn.sql("from records").write_parquet( |
| 118 | + f"s3://{S3_BUCKET}{CSW_PATH}", |
| 119 | + compression="zstd", |
| 120 | + overwrite=True, |
| 121 | + ) |
0 commit comments