Skip to content

Commit

Permalink
Merge pull request #111 from datacite/handle_geolocationpolygon_backw…
Browse files Browse the repository at this point in the history
…ards_compat

Support returning single geoLocationPolygon
  • Loading branch information
richardhallett authored Apr 13, 2021
2 parents 6d9aa30 + 3b899e4 commit ed72be6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
17 changes: 14 additions & 3 deletions lib/bolognese/readers/datacite_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ def read_datacite(string: nil, **options)
if !gl.is_a?(Hash) || gl["geoLocationPoint"].is_a?(String) || gl["geoLocationBox"].is_a?(String) || gl["geoLocationPolygon"].is_a?(String)
nil
else

# Handle scenario where multiple geoLocationPolygons are allowed within a single geoLocation
# we want to return an array if it's already an array (i.e. multiple geoLocationPolygons)
# vs if it's singular just return the object
# This is for backwards compatability to allow both scenarios.
if gl.dig("geoLocationPolygon").kind_of?(Array)
geoLocationPolygon = gl.dig("geoLocationPolygon").map do |glp|
Array.wrap(glp.dig("polygonPoint")).map { |glpp| { "polygonPoint" => glpp } }.compact.presence
end.compact.presence
else
geoLocationPolygon = Array.wrap(gl.dig("geoLocationPolygon", "polygonPoint")).map { |glp| { "polygonPoint" => glp } }.compact.presence
end

{
"geoLocationPoint" => {
"pointLatitude" => gl.dig("geoLocationPoint", "pointLatitude"),
Expand All @@ -237,9 +250,7 @@ def read_datacite(string: nil, **options)
"southBoundLatitude" => gl.dig("geoLocationBox", "southBoundLatitude"),
"northBoundLatitude" => gl.dig("geoLocationBox", "northBoundLatitude")
}.compact.presence,
"geoLocationPolygon" => Array.wrap(gl.dig("geoLocationPolygon")).map do |glp|
Array.wrap(glp.dig("polygonPoint")).map { |glpp| { "polygonPoint" => glpp } }.compact.presence
end.compact.presence,
"geoLocationPolygon" => geoLocationPolygon,
"geoLocationPlace" => parse_attributes(gl["geoLocationPlace"], first: true).to_s.strip.presence
}.compact
end
Expand Down
20 changes: 20 additions & 0 deletions spec/fixtures/datacite-geolocationpolygons-multiple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,25 @@
</polygonPoint>
</geoLocationPolygon>
</geoLocation>
<geoLocation>
<geoLocationPolygon>
<polygonPoint>
<pointLongitude>65</pointLongitude>
<pointLatitude>80</pointLatitude>
</polygonPoint>
<polygonPoint>
<pointLongitude>55</pointLongitude>
<pointLatitude>75</pointLatitude>
</polygonPoint>
<polygonPoint>
<pointLongitude>45</pointLongitude>
<pointLatitude>73</pointLatitude>
</polygonPoint>
<polygonPoint>
<pointLongitude>65</pointLongitude>
<pointLatitude>80</pointLatitude>
</polygonPoint>
</geoLocationPolygon>
</geoLocation>
</geoLocations>
</resource>
10 changes: 9 additions & 1 deletion spec/readers/datacite_reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,15 @@
{"polygonPoint"=>{"pointLatitude"=>"73", "pointLongitude"=>"45"}},
{"polygonPoint"=>{"pointLatitude"=>"80", "pointLongitude"=>"65"}}
]
] }
] },
{ "geoLocationPolygon"=>
[
{"polygonPoint"=>{"pointLatitude"=>"80", "pointLongitude"=>"65"}},
{"polygonPoint"=>{"pointLatitude"=>"75", "pointLongitude"=>"55"}},
{"polygonPoint"=>{"pointLatitude"=>"73", "pointLongitude"=>"45"}},
{"polygonPoint"=>{"pointLatitude"=>"80", "pointLongitude"=>"65"}}
]
}
]
)
end
Expand Down

0 comments on commit ed72be6

Please sign in to comment.