Convert Geolocation Data From String to Degrees

Problem this snippet solves:

The whereis command returns latitude and longitude as strings instead of floating points to save room in the geolation database. If you need degrees, you need to divide by 10000 to get them. This simple proc does the work for you..

How to use this snippet:

Add the proc to your specific rule or to your library rule of procs, then "call" it as appropriate.

Code :

### proc to convert integers to lat/lon degrees ###
proc geoloc_mod {latitude longitude} {
  return [list [expr {$latitude / 10000.}] [expr {$longitude / 10000.}]]
}

### example iRule taking advantage of the proc ###
when HTTP_REQUEST {
  set lat [whereis [IP::client_addr] latitude]
  set lon [whereis [IP::client_addr] longitude]
  if { [string is integer $lat] && [string is integer $lon] } {
    #use proc to convert geolocation from strings to degrees, store in a list
    set geoloc_deg [call geoloc_mod $lat $lon]
  }
  log local0. "Latitude: [lindex $geoloc_deg 0], Longititude: [lindex $geoloc_deg 1]"
}

Tested this on version:

11.6
Published May 21, 2015
Version 1.0

Was this article helpful?

2 Comments

  • @jason Why do you check this?

    string is integer $lat

    Because if it’s false, geoloc_deg won’t be assigned value... which will lead to tcl error.

  • I migrated a bunch of codeshare stuff from wiki to this module a while back, so it has my name attached to it. But I will punt on responsibility here! If it were me, I'd move all that logic to the proc instead of splitting it between the event and the proc.