Skip to content

Commit

Permalink
Parse dcat:spatialResolutionInMeters as float
Browse files Browse the repository at this point in the history
According to the spec this should be typed as an xsd:Decimal, but we are
parsing it as int:

https://www.w3.org/TR/vocab-dcat-3/#Property:dataset_spatial_resolution
  • Loading branch information
amercader committed Jun 4, 2024
1 parent 373cb77 commit 25e9544
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
20 changes: 19 additions & 1 deletion ckanext/dcat/profiles/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def _object_value_int_list(self, subject, predicate):
Both subject and predicate must be rdflib URIRef or BNode objects
If the value can not be parsed as intger, returns an empty list
If the value can not be parsed as integer, returns an empty list
"""
object_values = []
for object in self.g.objects(subject, predicate):
Expand All @@ -241,6 +241,24 @@ def _object_value_int_list(self, subject, predicate):
pass
return object_values

def _object_value_float_list(self, subject, predicate):
"""
Given a subject and a predicate, returns the value of the object as a
list of floats
Both subject and predicate must be rdflib URIRef or BNode objects
If the value can not be parsed as a float, returns an empty list
"""
object_values = []
for object in self.g.objects(subject, predicate):
if object:
try:
object_values.append(float(object))
except ValueError:
pass
return object_values

def _object_value_list(self, subject, predicate):
"""
Given a subject and a predicate, returns a list with all the values of
Expand Down
2 changes: 1 addition & 1 deletion ckanext/dcat/profiles/euro_dcat_ap_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ 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_int_list(
spatial_resolution_in_meters = self._object_value_float_list(
dataset_ref, DCAT.spatialResolutionInMeters
)
if spatial_resolution_in_meters:
Expand Down
6 changes: 2 additions & 4 deletions ckanext/dcat/tests/test_euro_dcatap_2_profile_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_spatial_resolution_in_meters_multiple(self):
dataset = URIRef('http://example.org/datasets/1')
g.add((dataset, RDF.type, DCAT.Dataset))

spatial_resolution_in_meters = 30
spatial_resolution_in_meters = 30.5
g.add((dataset, DCAT.spatialResolutionInMeters, Literal(spatial_resolution_in_meters, datatype=XSD.decimal)))

spatial_resolution_in_meters_2 = 20
Expand All @@ -368,9 +368,7 @@ def test_spatial_resolution_in_meters_multiple(self):
extras = self._extras(datasets[0])

spatial_resolution_list = json.loads(extras['spatial_resolution_in_meters'])
assert len(spatial_resolution_list) == 2
assert spatial_resolution_in_meters in spatial_resolution_list
assert spatial_resolution_in_meters_2 in spatial_resolution_list
assert sorted(spatial_resolution_list) == [20.0, 30.5]

def test_isreferencedby_multiple(self):
g = Graph()
Expand Down

0 comments on commit 25e9544

Please sign in to comment.