|
| 1 | +require 'geo/coord' |
| 2 | + |
| 3 | +module IIIF |
| 4 | + module V3 |
| 5 | + module Presentation |
| 6 | + class NavPlace < IIIF::V3::AbstractResource |
| 7 | + Rect = Struct.new(:coord1, :coord2) |
| 8 | + |
| 9 | + COORD_REGEX = /(?<hemisphere>[NSEW]) (?<degrees>\d+)[°⁰*] ?(?<minutes>\d+)?[ʹ']? ?(?<seconds>\d+)?[ʺ"]?/ |
| 10 | + |
| 11 | + def initialize(coordinate_texts:, base_uri:) |
| 12 | + @coordinate_texts = coordinate_texts |
| 13 | + @base_uri = base_uri |
| 14 | + end |
| 15 | + |
| 16 | + # @return [Boolean] indicates if coordinate_texts passed in are valid |
| 17 | + def valid? |
| 18 | + !(coordinates.nil? || coordinates.empty?) |
| 19 | + end |
| 20 | + |
| 21 | + def build |
| 22 | + raise ArgumentError.new('invalid coordinates') unless valid? |
| 23 | + |
| 24 | + { |
| 25 | + id: "#{base_uri}/feature-collection/1", |
| 26 | + type: 'FeatureCollection', |
| 27 | + features: features |
| 28 | + } |
| 29 | + end |
| 30 | + |
| 31 | + private |
| 32 | + |
| 33 | + attr_reader :coordinate_texts, :base_uri |
| 34 | + |
| 35 | + def coordinates |
| 36 | + @coordinates ||= coordinate_texts.map do |coordinate_text| |
| 37 | + coordinate_parts = coordinate_text.split(%r{ ?--|/}) |
| 38 | + case coordinate_parts.length |
| 39 | + when 2 |
| 40 | + coord_for(coordinate_parts[0], coordinate_parts[1]) |
| 41 | + when 4 |
| 42 | + rect_for(coordinate_parts) |
| 43 | + end |
| 44 | + end.compact |
| 45 | + end |
| 46 | + |
| 47 | + def coord_for(long_str, lat_str) |
| 48 | + long_matcher = long_str.match(COORD_REGEX) |
| 49 | + lat_matcher = lat_str.match(COORD_REGEX) |
| 50 | + return unless long_matcher && lat_matcher |
| 51 | + |
| 52 | + Geo::Coord.new(latd: lat_matcher[:degrees], latm: lat_matcher[:minutes], lats: lat_matcher[:seconds], lath: lat_matcher[:hemisphere], |
| 53 | + lngd: long_matcher[:degrees], lngm: long_matcher[:minutes], lngs: long_matcher[:seconds], lngh: long_matcher[:hemisphere]) |
| 54 | + end |
| 55 | + |
| 56 | + def rect_for(coordinate_parts) |
| 57 | + coord1 = coord_for(coordinate_parts[0], coordinate_parts[2]) |
| 58 | + coord2 = coord_for(coordinate_parts[1], coordinate_parts[3]) |
| 59 | + return if coord1.nil? || coord2.nil? |
| 60 | + |
| 61 | + Rect.new(coord1, coord2) |
| 62 | + end |
| 63 | + |
| 64 | + def features |
| 65 | + coordinates.map.with_index(1) do |coordinate, index| |
| 66 | + { |
| 67 | + id: "#{base_uri}/iiif/feature/#{index}", |
| 68 | + type: 'Feature', |
| 69 | + properties: {}, |
| 70 | + geometry: coordinate.is_a?(Rect) ? polygon_geometry(coordinate) : point_geometry(coordinate) |
| 71 | + } |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def point_geometry(coord) |
| 76 | + { |
| 77 | + type: 'Point', |
| 78 | + coordinates: [format(coord.lng), format(coord.lat)] |
| 79 | + } |
| 80 | + end |
| 81 | + |
| 82 | + def polygon_geometry(rect) |
| 83 | + { |
| 84 | + type: 'Polygon', |
| 85 | + coordinates: [ |
| 86 | + [ |
| 87 | + [format(rect.coord1.lng), format(rect.coord1.lat)], |
| 88 | + [format(rect.coord2.lng), format(rect.coord1.lat)], |
| 89 | + [format(rect.coord2.lng), format(rect.coord2.lat)], |
| 90 | + [format(rect.coord1.lng), format(rect.coord2.lat)], |
| 91 | + [format(rect.coord1.lng), format(rect.coord1.lat)] |
| 92 | + ] |
| 93 | + ] |
| 94 | + } |
| 95 | + end |
| 96 | + |
| 97 | + # @param [BigDecimal] coordinate value from geocoord gem |
| 98 | + # @return [String] string formatted with max 6 digits after the decimal point |
| 99 | + # The to_f ensures removal of scientific notation of BigDecimal before converting to a string. |
| 100 | + # examples: |
| 101 | + # input value is BigDecimal("-23.9") or "0.239e2", output value is "-23.9" as string |
| 102 | + # input value is BigDecimal("23.9424213434") or "0.239424213434e2", output value is "23.942421" as string |
| 103 | + def format(decimal) |
| 104 | + decimal.truncate(6).to_f.to_s |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | +end |
0 commit comments