Class: Krikri::Enrichments::SplitCoordinates
- Inherits:
-
Object
- Object
- Krikri::Enrichments::SplitCoordinates
- Includes:
- Audumbla::FieldEnrichment
- Defined in:
- lib/krikri/enrichments/split_coordinates.rb
Overview
Splits a string given in ‘lat’ or ‘long’ in an edm:Place object and assigns ‘lat’ and ‘long’ with the split values.
Instance Method Summary collapse
- #assign_latlong!(place, lat, long) ⇒ Object
-
#coord_values(s) ⇒ Array
Given a String ‘s’, return an array of two elements split on a comma and any whitespace around the comma.
-
#enrich_value(place) ⇒ DPLA::MAP::Place
Enrich a ‘DPLA::MAP::Place’ object by splitting the string given in its ‘lat’ or ‘long’.
Instance Method Details
#assign_latlong!(place, lat, long) ⇒ Object
44 45 46 47 |
# File 'lib/krikri/enrichments/split_coordinates.rb', line 44 def assign_latlong!(place, lat, long) place.lat = lat if lat place.long = long if long end |
#coord_values(s) ⇒ Array
Given a String ‘s’, return an array of two elements split on a comma and any whitespace around the comma.
If the string does not split into two strings representing decimal values, then return [nil, nil] because the string does not make sense as coordinates.
60 61 62 63 64 65 66 |
# File 'lib/krikri/enrichments/split_coordinates.rb', line 60 def coord_values(s) coords = s.split(/ *, */) return [nil, nil] if coords.size != 2 coords.map! { |c| c.to_f.to_s == c ? c : nil } # must be decimal ... return [nil, nil] unless coords[0] && coords[1] # ... i.e. not nil [coords[0], coords[1]] end |
#enrich_value(place) ⇒ DPLA::MAP::Place
Enrich a ‘DPLA::MAP::Place’ object by splitting the string given in its ‘lat’ or ‘long’.
place.lat and place.long are ActiveTriples::Terms, we only care about the first value. If multiple values are given, this enrichment will remove them.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/krikri/enrichments/split_coordinates.rb', line 29 def enrich_value(place) return place if !place.is_a? DPLA::MAP::Place return place unless splittable?(place.lat) || splittable?(place.long) if place.lat.any? latlong = coord_values(place.lat.first) assign_latlong!(place, latlong.first, latlong.last) elsif place.long.any? latlong = coord_values(place.long.first) assign_latlong!(place, latlong.last, latlong.first) end place end |