Skip to content
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
18 changes: 17 additions & 1 deletion lib/iiif/v3/presentation/nav_place.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@ module Presentation
class NavPlace < IIIF::V3::AbstractResource
Rect = Struct.new(:coord1, :coord2)

COORD_REGEX = /(?:(?<hemisphere>[NSEW])\s*)?(?<degrees>\d+)[°⁰*](?:\s*(?<minutes>\d+)[\'ʹ′])?(?:\s*(?<seconds>\d+)["ʺ″])?(?:\s*(?<hemisphere>[NSEW]))?/
DECIMAL = /\d+(?:\.\d+)?/

# Matches one coordinate, in either of two forms:
# - degrees with a degree sign, optionally followed by minutes and seconds,
# any of which may be decimal, with the hemisphere before or after
# ("W 23°54'00\"", "23°54′00″W", "E 138.5°", "N 37°26ʹ30.5″");
# - decimal degrees with no degree sign, which need the hemisphere first
# ("E 138.0").
COORD_REGEX = /
(?:(?<hemisphere>[NSEW])\s*)?(?<![\d.])(?<degrees>#{DECIMAL})[°⁰*]
(?:\s*(?<minutes>#{DECIMAL})[\'ʹ′])?
(?:\s*(?<seconds>#{DECIMAL})["ʺ″])?
(?:\s*(?<hemisphere>[NSEW]))?
|
(?<hemisphere>[NSEW])\s*(?<degrees>\d+\.\d+)(?![\d.°⁰*\'ʹ′"ʺ″])
/x

# @param [Array<String>] coordinate_texts
# @param [String] base_uri prefix for the Feature Collection and Feature ids
def initialize(coordinate_texts:, base_uri:)
Expand Down
36 changes: 35 additions & 1 deletion spec/unit/iiif/v3/presentation/nav_place_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
["W 23°54'00\"--E 53°36'00\"/N 71°19'00\"--N 33°30'00\"",
'E 103°48ʹ/S 3°46ʹ).',
'X 103°48ʹ/Y 3°46ʹ).',
'In decimal degrees: (E 138.0--W 074.0/N 073.0--N 041.2).', # currently invalid therefore doesn't show up in nav_place
'In decimal degrees: (E 138.0--W 074.0/N 073.0--N 041.2).',
'23°54′00″W -- 53°36′00″E / 71°19′00″N -- 33°30′00″N'
]
end
Expand All @@ -33,6 +33,20 @@
properties: {},
geometry: { coordinates: [103.8, 3.766667], type: "Point" } },
{ id: 'https://purl.stanford.edu/iiif/feature/4',
type: 'Feature',
properties: {},
geometry: { type: 'MultiPolygon',
coordinates: [[[[138.0, 41.2],
[180.0, 41.2],
[180.0, 73.0],
[138.0, 73.0],
[138.0, 41.2]]],
[[[-180.0, 41.2],
[-74.0, 41.2],
[-74.0, 73.0],
[-180.0, 73.0],
[-180.0, 41.2]]]] } },
{ id: 'https://purl.stanford.edu/iiif/feature/5',
type: 'Feature',
properties: {},
geometry: { type: 'Polygon',
Expand Down Expand Up @@ -106,6 +120,26 @@
end
end

context 'with decimal degrees' do
let(:coordinate_texts) { ['E 138.5°/N 35.25°'] }

it 'reads the whole number' do
expect(JSON.parse(subject.build.to_json)['features'][0]['geometry']).to eq(
'type' => 'Point', 'coordinates' => [138.5, 35.25]
)
end
end

context 'with decimal degrees and minutes and seconds' do
let(:coordinate_texts) { ['W 122°15.5ʹ/N 37°26ʹ30.5″'] }

it 'keeps them' do
expect(JSON.parse(subject.build.to_json)['features'][0]['geometry']).to eq(
'type' => 'Point', 'coordinates' => [-122.258333, 37.441806]
)
end
end

context 'when coordinates are not present' do
let(:coordinate_texts) { [] }

Expand Down
Loading