Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse dcat:spatialResolutionInMeters as float #285

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading