Skip to content

Commit

Permalink
Add spatialCoverage to JSON-LD output
Browse files Browse the repository at this point in the history
  • Loading branch information
marksparkza committed Nov 8, 2023
1 parent 6f396e4 commit c55ed74
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 28 deletions.
85 changes: 60 additions & 25 deletions odp/catalog/mims.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def evaluate_record(
def create_published_record(self, record_model: RecordModel) -> PublishedRecordModel:
published_record: PublishedSAEONRecordModel = super().create_published_record(record_model)

# add related identifiers for published child records to
# the DataCite and ISO19115 metadata records
for child_doi, child_id in record_model.child_dois.items():
if child_snapshot := self.snapshot.get(child_id):
child_record_model, _ = child_snapshot
Expand All @@ -57,46 +59,79 @@ def create_published_record(self, record_model: RecordModel) -> PublishedRecordM
'relationType': 'HasPart',
}]

mims_catalog = Session.get(Catalog, self.catalog_id)
# add a JSON-LD metadata record
schemaorg_schema = Session.get(Schema, (ODPMetadataSchema.SCHEMAORG_DATASET, SchemaType.metadata))
published_record.metadata_records += [
PublishedMetadataModel(
schema_id=schemaorg_schema.id,
schema_uri=schemaorg_schema.uri,
metadata=self._create_jsonld_metadata(published_record)
)
]

return published_record

def _create_jsonld_metadata(
self, published_record: PublishedSAEONRecordModel
) -> dict[str, Any]:
mims_catalog = Session.get(Catalog, self.catalog_id)
datacite_metadata = self._get_metadata_dict(published_record, ODPMetadataSchema.SAEON_DATACITE4)

title = next(
(t.get('title') for t in datacite_metadata.get('titles', ())),
''
None
)
abstract = next(
(d.get('description') for d in datacite_metadata.get('descriptions', ())
if d.get('descriptionType') == 'Abstract'),
''
None
)
identifier = (
f'doi:{published_record.doi}' if published_record.doi else None
)
identifier = f'doi:{published_record.doi}' if published_record.doi else None
license = next(
(r.get('rightsURI') for r in datacite_metadata.get('rightsList', ())),
''
None
)
url = (
f'{mims_catalog.url}/'
f'{published_record.doi if published_record.doi else published_record.id}'
)
url = (f'{mims_catalog.url}/'
f'{published_record.doi if published_record.doi else published_record.id}')

published_record.metadata_records += [
PublishedMetadataModel(
schema_id=schemaorg_schema.id,
schema_uri=schemaorg_schema.uri,
metadata={
'@context': 'https://schema.org/',
'@type': 'Dataset',
'@id': url,
'name': title,
'description': abstract,
'identifier': identifier,
'keywords': self.create_keyword_index_data(published_record),
'license': license,
'url': url,
}
)
]
jsonld_metadata = {
"@context": "https://schema.org/",
"@type": "Dataset",
"@id": url,
"name": title,
"description": abstract,
"identifier": identifier,
"keywords": self.create_keyword_index_data(published_record),
"license": license,
"url": url,
}

return published_record
if box := next(
(g.get('geoLocationBox') for g in datacite_metadata.get('geoLocations', ())),
None
):
polygon = (
f'{box["southBoundLatitude"]},{box["westBoundLongitude"]} '
f'{box["southBoundLatitude"]},{box["eastBoundLongitude"]} '
f'{box["northBoundLatitude"]},{box["eastBoundLongitude"]} '
f'{box["northBoundLatitude"]},{box["westBoundLongitude"]} '
f'{box["southBoundLatitude"]},{box["westBoundLongitude"]}'
)
jsonld_metadata |= {
"spatialCoverage": {
"@type": "Place",
"geo": {
"@type": "GeoShape",
"polygon": polygon
},
},
}

return jsonld_metadata

def create_facet_index_data(
self, published_record: PublishedSAEONRecordModel
Expand Down
13 changes: 10 additions & 3 deletions test/api/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,19 @@ def test_redirect_to(
'@context': 'https://schema.org/',
'@type': 'Dataset',
# '@id': dynamic,
'name': metadata_examples['SAEON.ISO19115']['title'],
'description': metadata_examples['SAEON.ISO19115']['abstract'],
'license': metadata_examples['SAEON.ISO19115']['constraints'][0]['rightsURI'],
'name': 'Example Metadata Record: ISO19115 - SAEON Profile',
'description': "Concerning things that'd like to be under the sea, in an octopus's garden, in the shade.",
'license': 'https://creativecommons.org/licenses/by/4.0/',
# 'identifier': dynamic,
# 'keywords': dynamic,
# 'url': dynamic,
'spatialCoverage': {
'@type': 'Place',
'geo': {
'@type': 'GeoShape',
'polygon': '-34.4,17.9 -34.4,18.3 -34.0,18.3 -34.0,17.9 -34.4,17.9'
}
}
},
}

Expand Down

0 comments on commit c55ed74

Please sign in to comment.