Module: Utilities::Geo

Defined in:
lib/utilities/geo.rb

Overview

Special general routines for Geo-specific itams

Defined Under Namespace

Classes: ConvertToDecimalDegrees

Constant Summary

SPECIAL_LATLONG_SYMBOLS =

degree symbols, in addition to 'd', 'o', and '*' u00b0 “°” u00ba “º” u02da “˚” u030a “?” u221e “∞” u222b “∫”

tick symbols, in addition to “'” (“u0027”“), and '”' (“u0022”) u00a5 “¥” u00b4 “´” u02B9 “ʹ” u02BA “ʺ” u02BB “ʻ” u02BC “ʼ” u02CA “ˊ” u02EE “ˮ” u2032 “′” u2033 “″”

"do*\u00b0\u00ba\u02DA\u030a\u221e\u222b\u0027\u00b4\u02B9\u02BA\u02BB\u02BC\u02CA\u02EE\u2032\u2033\u0022".freeze
LAT_LON_REGEXP =
Regexp.new(/(?<lat>-?\d+\.?\d*),?\s*(?<long>-?\d+\.?\d*)/)
ONE_WEST =

EARTH_RADIUS = 6371000 # km, 3959 miles (mean Earth radius) # Not used? RADIANS_PER_DEGREE = ::Math::PI/180.0 DEGREES_PER_RADIAN = 180.0/::Math::PI

111_319.490779206
ONE_NORTH =

ONE_WEST = 111_319.444444444 # meters/degree (calculated mean)

110_574.38855796
REGEXP_COORD_1 =

' = u0027, converted so that the regex can be used for SQL

{
  # tt1: /\D?(?<lat>\d+\.\d+\s*(?<ca>[NS])*)\s(?<long>\d+\.\d+\s*(?<co>[EW])*)/i,
  dd1a: /(\d+\.\d+\s*([NS]))\s*(\d+\.\d+\s*([EW]))/i,

  dd1b: /(([NS])\s*\d+\.\d+)\s*(([EW])\s*\d+\.\d+\s*)/i,

  dd2:  /(\d+[\. ]\d+(\u0027?)\s*([NS]))[, ]?\s*(\d+[\. ]\d+(\u0027?)\s*([EW]))/i,

  dm1:  /\D(\d+) ?([\*°ººo\u02DA ]) ?(\d+[\.|,]\d+|\d+) ?([ ´\u0027\u02B9\u02BC\u02CA])? ?([NS])[\.,;]? ?(\d+) ?([\*°ººo\u02DA ]) ?(\d+[\.|,]\d+|\d+) ?([ ´\u0027\u02B9\u02BC\u02CA])? ?([WE])\W/i,

  dms2: /\W([NS])\.? ?(\d+) ?([\*°ººo\u02DA ]) ?(\d+) ?([ ´\u0027\u02B9\u02BC\u02CA]) ?(\d+[\.|,]\d+|\d+) ?([ ""´\u02BA\u02EE\u0027\u02B9\u02BC\u02CA])([´\u0027\u02B9\u02BC\u02CA])?[\.,;]? ?([WE])\.? ?(\d+) ?([\*°ººo\u02DA ]) ?(\d+) ?([ \u0027´\u02B9\u02BC\u02CA]) ?(\d+[\.|,]\d+|\d+) ?([ ""´\u02BA\u02EE\u0027\u02B9\u02BC\u02CA])?([´\u0027\u02B9\u02BC\u02CA])?/i,

  dm3:  /\W([NS])\.? ?(\d+) ?([\*°ººo\u02DA ]) ?(\d+[\.|,]\d+|\d+) ?([ ´\u0027\u02B9\u02BC\u02CA])[\.,;]? ?([WE])\.? ?(\d+) ?([\*°ººo\u02DA ]) ?(\d+[\.|,]\d+|\d+) ?([ ´\u0027\u02B9\u02BC\u02CA])?/i,

  dms4: /\D(\d+) ?([\*°ººo\u02DA ]) ?(\d+[\.,]\d+|\d+) ?([ ´\u0027\u02B9\u02BC\u02CA])? ?(\d+)(")? ?([NS])(\d+) ?([\*°ººo\u02DA ]) ?(\d+[\.,]\d+|\d+) ?([ ´\u0027\u02B9\u02BC\u02CA])? ?(\d+)(["\u0027])? ?([EW])/i,

  dd5:  /\W([NS])\.? ?(\d+[\.|,]\d+|\d+) ?([\*°ººo\u02DA ])[\.,;]?\s*([WE])\.? ?(\d+[\.|,]\d+|\d+) ?([\*°ººo\u02DA ])?/i,

  dd6:  /\D(\d+[\.|,]\d+|\d+) ?([\*°ººo\u02DA ]) ?([NS])[\.,;]?\s*(\d+[\.|,]\d+|\d+) ?([\*°ººo\u02DA ]) ?([WE])\W/i,

  dd7:  /\[(-?\d+[\.|,]\d+|\-?d+),.*?(-?\d+[\.|,]\d+|\-?d+)\]/i
}.freeze
REGEXP_COORD =

' = u0027, converted so that the regex can be used for SQL

{
  # tt1: /\D?(?<lat>\d+\.\d+\s*(?<ca>[NS])*)\s(?<long>\d+\.\d+\s*(?<co>[EW])*)/i,
  dd1a: {reg: /(?<lat>\d+\.\d+\s*[NS])\s*(?<long>\d+\.\d+\s*[EW])/i,
         hlp: 'decimal degrees, trailing ordinal, e.g. 23.23N  44.44W'},

  dd1b: {reg: /(?<lat>[NS]\s*\d+\.\d+)\s*(?<long>[EW]\s*\d+\.\d+)/i,
         hlp: 'decimal degrees, leading ordinal, e.g. N23.23  W44.44'},

  dd2:  {reg: /(?<lat>\d+[\. ]\d+\u0027?\s*[NS]),?\s*(?<long>\d+[\. ]\d+\u0027?\s*[EW])/i,
         hlp: "decimal degrees, trailing ordinal, e.g. 43.836' N, 89.258' W"},

  dm1:  {reg: /(?<lat>\d+\s*[\*°ººo\u02DA ](\d+[\.,]\d+|\d+)\s*[ ´\u0027\u02B9\u02BC\u02CA]?\s*[NS])[\.,;]?\s*(?<long>\d+\s*[\*°ººo\u02DA ](\d+[\.,]\d+|\d+)\s*[ ´\u0027\u02B9\u02BC\u02CA]?\s*[WE])/i,
         hlp: "degrees, decimal minutes, trailing ordinal, e.g. 45 54.2'N, 78 43.5'E"},

  dms2: {reg: /(?<lat>[NS]\.?\s*\d+\s*[\*°ººo\u02DA ]\s*\d+\s*[ ´\u0027\u02B9\u02BC\u02CA]\s*(\d+[\.,]\d+|\d+)\s*[ ""´\u02BA\u02EE\u0027\u02B9\u02BC\u02CA][´\u0027\u02B9\u02BC\u02CA]?)[\.,;]?\s*(?<long>[WE]\.?\s*\d+\s*[\*°ººo\u02DA ]\s*\d+\s*[ \u0027´\u02B9\u02BC\u02CA]\s*(\d+[\.,]\d+|\d+)\s*[ ""´\u02BA\u02EE\u0027\u02B9\u02BC\u02CA]?[´\u0027\u02B9\u02BC\u02CA]?)/i,
         hlp: "degrees, minutes, decimal seconds, leading ordinal, e.g. S42°5'18.1\" W88º11'43.3\""},

  dm3:  {reg: /(?<lat>[NS]\.?\s*\d+\s*[\*°ººo\u02DA ]\s*(\d+[\.,]\d+|\d+)\s*([ ´\u0027\u02B9\u02BC\u02CA]))[\.,;]?\s*(?<long>[WE]\.?\s*\d+\s*[\*°ººo\u02DA ]\s*(\d+[\.,]\d+|\d+)\s*[ ´\u0027\u02B9\u02BC\u02CA]?)/i,
         hlp: "degrees, decimal minutes, leading ordinal, e.g. S42º5.18' W88°11.43'"},

  dms4: {reg: /(?<lat>\d+\s*[\*°ººo\u02DA ]\s*(\d+[\.,]\d+|\d+)\s*[ ´\u0027\u02B9\u02BC\u02CA]?\s*\d+"?\s*[NS])\s*(?<long>\d+\s*[\*°ººo\u02DA ]\s*(\d+[\.,]\d+|\d+)\s*[ ´\u0027\u02B9\u02BC\u02CA]?\s*\d+["\u0027]?\s*[EW])/i,
         hlp: "degrees, minutes, decimal seconds, trailing ordinal, e.g. 24º7'2.0\"S65º24'13.1\"W"},

  dd5:  {reg: /(?<lat>[NS]\.?\s*(\d+[\.,]\d+|\d+)\s*[\*°ººo\u02DA ])[\.,;]?\s*(?<long>([WE])\.?\s*(\d+[\.,]\d+|\d+)\s*[\*°ººo\u02DA ]?)/i,
         hlp: 'decimal degrees, leading ordinal, e.g. S42.18° W88.34°'},

  dd6:  {reg: /(?<lat>(\d+[\.,]\d+|\d+)\s*[\*°ººo\u02DA ]\s*[NS])[\.,;]?\s*(?<long>(\d+[\.|,]\d+|\d+)\s*[\*°ººo\u02DA ]\s*[WE])/i,
         hlp: 'decimal degrees, trailing ordinal, e.g. 42.18°S 88.43°W'},

  dd7:  {reg: /\[(?<lat>-?\d+[\.,]\d+|\-?d+),.*?(?<long>-?\d+[\.,]\d+|\-?d+)\]/i,
         hlp: 'decimal degrees, no ordinal, specific format, e.g. [12.263, -49.398]'}
}.freeze

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) degrees_minutes_seconds_to_decimal_degrees(dms_in)

42∞5'18.1“S88∞11'43.3”W S42∞5'18.1“W88∞11'43.3” S42∞5.18'W88∞11.43' 42∞5.18'S88∞11.43'W S42.18∞W88.34∞ 42.18∞S88.43∞W -12.263, 49.398

42:5:18.1N 88:11:43.3W

no limit test, unless there is a ordinal letter included



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/utilities/geo.rb', line 240

def self.degrees_minutes_seconds_to_decimal_degrees(dms_in) # rubocop:disable Metrics/PerceivedComplexity !! But this is too complex :)
  match_string = nil
  # no_point     = false
  degrees      = 0.0
  minutes      = 0.0
  seconds      = 0.0

  # make SURE it is a string! Watch out for dms_in == -10
  dms_in       = dms_in.to_s
  dms          = dms_in.dup.upcase
  dms          = dms.gsub('DEG', 'º').gsub('DG', 'º')
  dms =~ /[NSEW]/i
  ordinal = $LAST_MATCH_INFO.to_s
  # return "#{dms}: Too many letters (#{ordinal})" if ordinal.length > 1
  # return nil if ordinal.length > 1
  dms     = dms.gsub!(ordinal, '').strip.downcase

  if dms.include? '.'
    no_point = false
    if dms.include? ':' # might be '42:5.1'
      /(?<degrees>-*\d+):(?<minutes>\d+\.*\d*)(:(?<seconds>\d+\.*\d*))*/ =~ dms
      match_string = $& # rubocop:disable Style/IdenticalConditionalBranches
    else
      # this will get over-ridden if the next regex matches
      /(?<degrees>-*\d+\.\d+)/ =~ dms
      match_string = $& # rubocop:disable Style/IdenticalConditionalBranches
    end
  else
    no_point = true
  end

  # >40°26′46″< >40°26′46″<
  dms.each_char {|c|
    next unless SPECIAL_LATLONG_SYMBOLS.include?(c)
    /^(?<degrees>-*\d{0,3}(\.\d+)*) # + or - three-digit number with optional '.' and additional decimal digits
        [do*\u00b0\u00ba\u02DA\u030a\u221e\u222b\uc2ba]*\s* # optional special degrees symbol, optional space
      (?<minutes>\d+\.*\d*)* # optional number, integer or floating-point
        ['\u00a5\u00b4\u02b9\u02bb\u02bc\u02ca\u2032\uc2ba]*\s* # optional special minutes symbol, optional space
      ((?<seconds>\d+\.*\d*) # optional number, integer or floating-point
        ['\u00a5\u00b4\u02b9\u02ba\u02bb\u02bc\u02ca\u02ee\u2032\u2033\uc2ba"]+)* # optional special seconds symbol, optional space
    /x =~ dms # '/(regexp)/x' modifier permits inline comments for regexp
    match_string = $&
    break # bail on the first character match
  }
  degrees = dms.to_f if match_string.nil? && no_point

  # @match_string = $&
  degrees = degrees.to_f
  case ordinal
    when 'W', 'S'
      sign = -1.0
    else
      sign = 1.0
  end
  if degrees < 0
    sign    *= -1
    degrees *= -1.0
  end
  frac = ((minutes.to_f * 60.0) + seconds.to_f) / 3600.0
  dd   = (degrees + frac) * sign
  case ordinal
    when 'N', 'S'
      limit = 90.0
    else
      limit = 180.0
  end
  # return "#{dms}: Out of range (#{dd})" if dd.abs > limit
  return nil if dd.abs > limit || dd == 0.0
  dd.round(6).to_s
end

+ (Object) distance_in_meters(elev_in)

12345 (presume meters) 123.45 123 ft > 123 ft. > 123 feet > 1 foot > 123 f > 123 f. 123 m > 123 meters > 123 m. 123 km > 123 km. > 123 kilometers



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/utilities/geo.rb', line 65

def self.distance_in_meters(elev_in)
  elev_in   = '0.0 meters' if elev_in.blank?
  elevation = elev_in.strip.downcase
  pieces    = elevation.split(' ')
  value     = elevation.to_f
  if pieces.count > 1 # two pieces, second is distance unit
    piece = 1
  else # one piece, may contain distance unit.
    if elevation.include?('.')
      value = elevation.to_f
    else
      value = elevation.to_i
    end
    piece = 0
  end
  scale = 1.0 # default is meters

  /(?<ft>f[oe]*[t]*\.*)|(?<m>[^k]m(eters)*[\.]*)|(?<km>kilometer(s)*|k[m]*[\.]*)/ =~ pieces[piece]
  # scale = $&

  scale = 1.0 unless m.blank?
  scale = 0.3048 unless ft.blank?
  scale = 1000.0 unless km.blank?

  elev = value * scale

  elev
end

+ (Object) error_box_for_point(geo_object, error_radius)

confirm that this says that the error radius is one degree or smaller



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/utilities/geo.rb', line 359

def self.error_box_for_point(geo_object, error_radius)
  p0      = geo_object
  delta_x = (error_radius / ONE_WEST) / Math.cos(p0.y * Math::PI / 180)
  delta_y = error_radius / ONE_NORTH

  Gis::FACTORY.polygon(
    Gis::FACTORY.line_string(
      [
        Gis::FACTORY.point(p0.x - delta_x, p0.y + delta_y), # northwest
        Gis::FACTORY.point(p0.x + delta_x, p0.y + delta_y), # northeast
        Gis::FACTORY.point(p0.x + delta_x, p0.y - delta_y), # southeast
        Gis::FACTORY.point(p0.x - delta_x, p0.y - delta_y) # southwest
      ]
    )
  )
end

+ (Object) hunt_lat_long(label, how = ' ')



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/utilities/geo.rb', line 169

def self.hunt_lat_long(label, how = ' ')
  if how.nil?
    pieces = [label]
  else
    pieces = label.split(how)
  end
  lat_long = {}
  pieces.each do |piece|
    # group of possible regex configurations
    # m = /(?<lat>\d+\.\d+\s*(?<ca>[NS])*)\s(?<long>\d+\.\d+\s*(?<co>[EW])*)/i =~ piece
    m = REGEXP_COORD[:dd1a][:reg].match(piece)
    if m.nil?
      piece.each_char do |c|
        next unless SPECIAL_LATLONG_SYMBOLS.include?(c)
        test = Utilities::Geo.degrees_minutes_seconds_to_decimal_degrees(piece)
        unless test.nil?
          if test.to_f.is_a? Numeric
            # might be a lat/long
            lat_long[:piece] = piece
            if lat_long[:lat].nil?
              lat_long[:lat] = piece
            else
              lat_long[:long]  = piece
              lat_long[:piece] = [lat_long[:lat], piece].join(how)
            end
          end
        end
        break
      end
    else
      lat_long[:piece] = m[0]
      lat_long[:lat]   = m[:lat]
      lat_long[:long]  = m[:long]
    end
  end
  lat_long
end

+ (Object) hunt_lat_long_full(label, filters = REGEXP_COORD.keys)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/utilities/geo.rb', line 152

def self.hunt_lat_long_full(label, filters = REGEXP_COORD.keys)
  trials = {}
  filters.each_with_index {|kee, dex|
    kee_string         = kee.to_s.upcase
    trials[kee_string] = {}
    named              = REGEXP_COORD[kee][:reg].match(label)
    unless named.nil?
      trials[kee_string][:piece] = named[0]
      trials[kee_string][:lat]   = named[:lat]
      trials[kee_string][:long]  = named[:long]
      named
    end
    trials[kee_string][:method] = "text, #{kee_string}"
  }
  trials
end

+ (Object) hunt_wrapper(label, filters = REGEXP_COORD.keys)



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/utilities/geo.rb', line 207

def self.hunt_wrapper(label, filters = REGEXP_COORD.keys)

  trials = self.hunt_lat_long_full(label, filters)

  ';, '.each_char {|sep|
    trial = self.hunt_lat_long(label, sep)
    found = "#{trial[:piece]}"
    unless trial[:lat].nil? and !trial[:long].nil?
      found = "(#{sep})" if found.blank?
    end
    trials["(#{sep})"] = trial.merge!(method: "(#{sep})")
  }
  trials
end

+ (Object) is_lat_long_special(c)



222
223
224
# File 'lib/utilities/geo.rb', line 222

def self.is_lat_long_special(c)
  SPECIAL_LATLONG_SYMBOLS.include?(c)
end

+ (Object) nearby_from_params(params)



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/utilities/geo.rb', line 317

def self.nearby_from_params(params)
  nearby_distance = params['nearby_distance'].to_i
  nearby_distance = CollectingEvent::NEARBY_DISTANCE if nearby_distance == 0

  decade = case nearby_distance.to_s.length
             when 1..2
               10
             when 3
               100
             when 4
               1_000
             when 5
               10_000
             when 6
               100_000
             when 7
               1_000_000
             when 8
               10_000_000
             else
               10
           end
  digit  = (nearby_distance.to_f / decade.to_f).round

  case digit
    when 0..1
      digit = 1
    when 2
      digit = 2
    when 3..5
      digit = 5
    when 6..10
      decade *= 10
      digit  = 1 # rubocop:disable Style/SpaceAroundOperators
  end

  params['digit1'] = digit.to_s
  params['digit2'] = decade.to_s
  digit * decade
end

Instance Method Details

- (Object) diamond_error_box

make a diamond 2 * radius tall and 2 * radius wide, with the reference point as center NOT TESTED/USED rubocop:disable Style/FirstParameterIndentation:



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/utilities/geo.rb', line 379

def diamond_error_box
  p0      = geo_object
  delta_x = (error_radius / ONE_WEST) / Math.cos(p0.y * Math::PI / 180)
  delta_y = error_radius / ONE_NORTH

  retval = Gis::FACTORY.polygon(Gis::FACTORY.line_string(
    [Gis::FACTORY.point(p0.x, p0.y + delta_y), # north
     Gis::FACTORY.point(p0.x + delta_x, p0.y), # east
     Gis::FACTORY.point(p0.x, p0.y - delta_y), # south
     Gis::FACTORY.point(p0.x - delta_x, p0.y) # west
    ]))
  box    = RGeo::Cartesian::BoundingBox.new(Gis::FACTORY)
  box.add(retval)
  box.to_geometry
end

- (Object) uni_string(char)



311
312
313
314
315
# File 'lib/utilities/geo.rb', line 311

def uni_string(char)
  format('\\u%04X', char.ord)
  # "\\#{sprintf('u%04X', char.ord)}"
  # '\\u%04X' % [char.ord]
end