Skip to content

Commit

Permalink
Only one value of DCAT.spatialResolutionInMeters allowed in DCAT-AP 2…
Browse files Browse the repository at this point in the history
….1.1
  • Loading branch information
amercader committed Jun 19, 2024
1 parent 715daa7 commit c9dea8d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 39 deletions.
16 changes: 11 additions & 5 deletions ckanext/dcat/profiles/euro_dcat_ap_2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from decimal import Decimal
from decimal import Decimal, DecimalException

from rdflib import URIRef, BNode, Literal
from ckanext.dcat.utils import resource_uri
Expand Down Expand Up @@ -59,14 +59,20 @@ def parse_dataset(self, dataset_dict, dataset_ref):
self._add_spatial_to_dict(dataset_dict, key, spatial)

# Spatial resolution in meters
spatial_resolution_in_meters = self._object_value_float_list(
spatial_resolution = self._object_value_float_list(
dataset_ref, DCAT.spatialResolutionInMeters
)
if spatial_resolution_in_meters:
if spatial_resolution:
# For some reason we incorrectly allowed lists in this property at some point
# keep support for it but default to single value
value = (
spatial_resolution[0] if len(spatial_resolution) == 1
else json.dumps(spatial_resolution)
)
dataset_dict["extras"].append(
{
"key": "spatial_resolution_in_meters",
"value": json.dumps(spatial_resolution_in_meters),
"value": value,
}
)

Expand Down Expand Up @@ -226,7 +232,7 @@ def graph_from_dataset(self, dataset_dict, dataset_ref):
Literal(Decimal(value), datatype=XSD.decimal),
)
)
except (ValueError, TypeError):
except (ValueError, TypeError, DecimalException):
self.g.add(
(dataset_ref, DCAT.spatialResolutionInMeters, Literal(value))
)
Expand Down
2 changes: 0 additions & 2 deletions ckanext/dcat/schemas/dcat_ap_2.1_full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ dataset_fields:

- field_name: spatial_resolution_in_meters
label: Spatial resolution in meters
preset: multiple_text
validators: ignore_missing scheming_multiple_number
help_text: Minimum spatial separation resolvable in a dataset, measured in meters.

- field_name: access_rights
Expand Down
4 changes: 1 addition & 3 deletions ckanext/dcat/tests/test_euro_dcatap_2_profile_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def test_dataset_all_fields(self):

assert extras['temporal_resolution'] == temporal_resolution

spatial_resolution_list = json.loads(extras['spatial_resolution_in_meters'])
assert len(spatial_resolution_list) == 1
assert spatial_resolution_in_meters in spatial_resolution_list
assert extras['spatial_resolution_in_meters'] == spatial_resolution_in_meters

isreferencedby_list = json.loads(extras['is_referenced_by'])
assert len(isreferencedby_list) == 1
Expand Down
21 changes: 8 additions & 13 deletions ckanext/dcat/tests/test_euro_dcatap_2_profile_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from builtins import str
from builtins import object
import json
from decimal import Decimal
import six

import pytest
Expand Down Expand Up @@ -43,7 +44,7 @@ def test_graph_from_dataset(self):
'metadata_modified': '2021-06-21T15:21:09.075774',
'extras': [
{'key': 'temporal_resolution', 'value': 'PT15M'},
{'key': 'spatial_resolution_in_meters', 'value': '[30,20]'},
{'key': 'spatial_resolution_in_meters', 'value': '30'},
{'key': 'is_referenced_by', 'value': '[\"https://doi.org/10.1038/sdata.2018.22\", \"test_isreferencedby\"]'},
]
}
Expand Down Expand Up @@ -77,11 +78,8 @@ def test_graph_from_dataset(self):
assert self._triple(g, dataset_ref, item[1], _type(value), _datatype)

# Spatial Resolution in Meters
values = json.loads(extras['spatial_resolution_in_meters'])
assert len([t for t in g.triples((dataset_ref, DCAT.spatialResolutionInMeters, None))]) == len(values)

for value in values:
assert self._triple(g, dataset_ref, DCAT.spatialResolutionInMeters, Literal(float(value),
value = extras['spatial_resolution_in_meters']
assert self._triple(g, dataset_ref, DCAT.spatialResolutionInMeters, Literal(Decimal(value),
datatype=XSD.decimal))

def test_spatial_resolution_in_meters_single_value(self):
Expand Down Expand Up @@ -109,7 +107,7 @@ def test_spatial_resolution_in_meters_single_value(self):

assert len([t for t in g.triples((dataset_ref, DCAT.spatialResolutionInMeters, None))]) == 1
assert self._triple(g, dataset_ref, DCAT.spatialResolutionInMeters,
Literal(float(extras['spatial_resolution_in_meters']), datatype=XSD.decimal))
Literal(Decimal(extras['spatial_resolution_in_meters']), datatype=XSD.decimal))

def test_spatial_resolution_in_meters_a_value_is_not_a_number(self):

Expand All @@ -123,7 +121,7 @@ def test_spatial_resolution_in_meters_a_value_is_not_a_number(self):
'metadata_created': '2021-06-21T15:21:09.034694',
'metadata_modified': '2021-06-21T15:21:09.075774',
'extras': [
{'key': 'spatial_resolution_in_meters', 'value': '[\"foo\",20]'}
{'key': 'spatial_resolution_in_meters', 'value': 'foo'}
]
}

Expand All @@ -134,11 +132,8 @@ def test_spatial_resolution_in_meters_a_value_is_not_a_number(self):

dataset_ref = s.graph_from_dataset(dataset)

values = json.loads(extras['spatial_resolution_in_meters'])
assert len([t for t in g.triples((dataset_ref, DCAT.spatialResolutionInMeters, None))]) == len(values)
assert self._triple(g, dataset_ref, DCAT.spatialResolutionInMeters, Literal(values[0]))
assert self._triple(g, dataset_ref, DCAT.spatialResolutionInMeters,
Literal(float(values[1]), datatype=XSD.decimal))
value = extras['spatial_resolution_in_meters']
assert self._triple(g, dataset_ref, DCAT.spatialResolutionInMeters, Literal(value))

def test_spatial_resolution_value_is_invalid_json(self):

Expand Down
3 changes: 2 additions & 1 deletion ckanext/dcat/tests/test_euro_dcatap_profile_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from builtins import object
import json
import uuid
from decimal import Decimal

import pytest

Expand Down Expand Up @@ -702,7 +703,7 @@ def test_distribution_fields(self):
assert self._triple(g, distribution, DCT.modified, resource['modified'], XSD.dateTime)

# Numbers
assert self._triple(g, distribution, DCAT.byteSize, float(resource['size']), XSD.decimal)
assert self._triple(g, distribution, DCAT.byteSize, Decimal(resource['size']), XSD.decimal)

# Checksum
checksum = self._triple(g, distribution, SPDX.checksum, None)[2]
Expand Down
26 changes: 13 additions & 13 deletions ckanext/dcat/tests/test_scheming_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_e2e_ckan_to_dcat(self):
"centroid": {"type": "Point", "coordinates": [1.26639, 41.12386]},
}
],
"spatial_resolution_in_meters": [1.5, 2.0],
"spatial_resolution_in_meters": 1.5,
"resources": [
{
"name": "Resource 1",
Expand Down Expand Up @@ -196,6 +196,13 @@ def test_e2e_ckan_to_dcat(self):
assert self._triple(g, dataset_ref, DCT.type, dataset["dcat_type"])
assert self._triple(g, dataset_ref, ADMS.versionNotes, dataset["version_notes"])
assert self._triple(g, dataset_ref, DCT.accessRights, dataset["access_rights"])
assert self._triple(
g,
dataset_ref,
DCAT.spatialResolutionInMeters,
dataset["spatial_resolution_in_meters"],
data_type=XSD.decimal,
)

# Dates
assert self._triple(
Expand Down Expand Up @@ -248,13 +255,6 @@ def test_e2e_ckan_to_dcat(self):
== dataset["applicable_legislation"]
)

assert (
self._triples_list_python_values(
g, dataset_ref, DCAT.spatialResolutionInMeters
)
== dataset["spatial_resolution_in_meters"]
)

# Repeating subfields

contact_details = [t for t in g.triples((dataset_ref, DCAT.contactPoint, None))]
Expand Down Expand Up @@ -373,7 +373,9 @@ def test_e2e_ckan_to_dcat(self):
# Resources: standard fields

assert self._triple(g, distribution_ref, DCT.rights, resource["rights"])
assert self._triple(g, distribution_ref, ADMS.status, URIRef(resource["status"]))
assert self._triple(
g, distribution_ref, ADMS.status, URIRef(resource["status"])
)
assert self._triple(
g,
distribution_ref,
Expand Down Expand Up @@ -641,6 +643,7 @@ def test_e2e_dcat_to_ckan(self):
assert dataset["issued"] == u"2012-05-10"
assert dataset["modified"] == u"2012-05-10T21:04:00"
assert dataset["temporal_resolution"] == "PT15M"
assert dataset["spatial_resolution_in_meters"] == "1.5"

# List fields
assert sorted(dataset["conforms_to"]) == ["Standard 1", "Standard 2"]
Expand All @@ -658,10 +661,7 @@ def test_e2e_dcat_to_ckan(self):
"http://dataset.info.org/doc1",
"http://dataset.info.org/doc2",
]
assert sorted(dataset["spatial_resolution_in_meters"]) == [
1.5,
2.0,
]

assert sorted(dataset["is_referenced_by"]) == [
"https://doi.org/10.1038/sdata.2018.22",
"test_isreferencedby",
Expand Down
2 changes: 1 addition & 1 deletion ckanext/dcat/tests/test_shacl.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_validate_dcat_ap_2():
"centroid": {"type": "Point", "coordinates": [1.26639, 41.12386]},
}
],
"spatial_resolution_in_meters": [1.5, 2.0],
"spatial_resolution_in_meters": 1.5,
"resources": [
{
"name": "Resource 1",
Expand Down
1 change: 0 additions & 1 deletion examples/dataset.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
<dct:license rdf:resource="https://data.some.org/link/to/license"/>
<dct:spatial rdf:resource="http://publications.europa.eu/mdr/authority/country/ZWE"/>
<dcat:spatialResolutionInMeters rdf:datatype="http://www.w3.org/2001/XMLSchema#decimal">1.5</dcat:spatialResolutionInMeters>
<dcat:spatialResolutionInMeters rdf:datatype="http://www.w3.org/2001/XMLSchema#decimal">2.0</dcat:spatialResolutionInMeters>
<dct:accrualPeriodicity rdf:resource="http://purl.org/cld/freq/daily"/>
<dct:accessRights>public</dct:accessRights>
<foaf:page rdf:resource="http://dataset.info.org/doc1"/>
Expand Down

0 comments on commit c9dea8d

Please sign in to comment.