inaka

Latest blog entries

/
The Art of Writing a Blogpost

The Art of Writing a Blogpost

Mar 09 2017 : Matias Vera

/
SpellingCI: No more spelling mistakes in your markdown flies!

Feb 14 2017 : Felipe Ripoll

/
Fast reverse geocoding with offline-geocoder

Do you need a blazing fast reverse geocoder? Enter offline-geocoder!

Jan 18 2017 : Roberto Romero

/
Using Jayme to connect to the new MongooseIM REST services

MongooseIM has RESTful services!! Here I show how you can use them in an iOS application.

Dec 13 2016 : Sergio Abraham

/
20 Questions, or Maybe a Few More

20 Questions, or Maybe a Few More

Nov 16 2016 : Stephanie Goldner

/
The Power of Meeting People

Because conferences and meetups are not just about the technical stuff.

Nov 01 2016 : Pablo Villar

/
Finding the right partner for your app build

Sharing some light on how it is to partner with us.

Oct 27 2016 : Inaka

/
Just Play my Sound

How to easily play a sound in Android

Oct 25 2016 : Giaquinta Emiliano

/
Opening our Guidelines to the World

We're publishing our work guidelines for the world to see.

Oct 13 2016 : Brujo Benavides

/
Using NIFs: the easy way

Using niffy to simplify working with NIFs on Erlang

Oct 05 2016 : Hernan Rivas Acosta

/
Function Naming In Swift 3

How to write clear function signatures, yet expressive, while following Swift 3 API design guidelines.

Sep 16 2016 : Pablo Villar

/
Jenkins automated tests for Rails

How to automatically trigger rails tests with a Jenkins job

Sep 14 2016 : Demian Sciessere

/
Erlang REST Server Stack

A description of our usual stack for building REST servers in Erlang

Sep 06 2016 : Brujo Benavides

/
Replacing JSON when talking to Erlang

Using Erlang's External Term Format

Aug 17 2016 : Hernan Rivas Acosta

/
Gadget + Lewis = Android Lint CI

Integrating our Android linter with Github's pull requests

Aug 04 2016 : Fernando Ramirez and Euen Lopez

/
Passwordless login with phoenix

Introducing how to implement passwordless login with phoenix framework

Jul 27 2016 : Thiago Borges

/
Beam Olympics

Our newest game to test your Beam Skills

Jul 14 2016 : Brujo Benavides

/
Otec

Three Open Source Projects, one App

Jun 28 2016 : Andrés Gerace

/
CredoCI

Running credo checks for elixir code on your github pull requests

Jun 16 2016 : Alejandro Mataloni

/
Thoughts on rebar3

Thoughts on rebar3

Jun 08 2016 : Hernán Rivas Acosta

/
See all Inaka's blog posts >>

/
Fast reverse geocoding with offline-geocoder

Roberto Romero wrote this on January 18, 2017 under gem, geocoding, ruby .

Sometimes, you can find yourself having to reverse geocode a large bunch of points. That is, finding an address from a coordinate pair (latitude and longitude). Some time ago, I found myself in that situation: we were developing an app containing a pretty big dataset of locations, having a latitude and a longitude, but lacking an address.

What you usually do is buy a service to reverse geocode your data. However, this is slow and expensive, and sometimes all you need is an approximate address, a city, to put the place in the map. We found ourselves in that scenario. We did not need high precision, we only wanted to know where, in a general sense, the locations belonged.

So, with such requirements in mind, we built the offline-geocoder gem. This gem contains a pretty large dataset of cities (over 138000 entries). Each entry contains city coordinates, name, administrative unit, and country. Offline-geocoder uses the awesome gem kdtree to create a kd tree with all this data in order to find the nearest neighbor for a specific pair of coordinates. A kd tree is a data structure that recursively partitions the world in order to rapidly answer nearest neighbor queries. A generic kd tree can support any number of dimensions, and can return either the nearest neighbor or a set of N nearest neighbors.

Searching through this kd tree is blazingly fast. Once you enter a coordinate pair, the gem can return the nearest city instantly. For instance, in my machine (a 4 core i7) offline-geocoder can find more than 181.000 locations per second.

Using this gem is pretty easy too. You can install it in the usual fashion:

  • Add this line to your application's Gemfile:
gem 'offline_geocoder'

Then execute:

$ bundle
  • Or install it yourself as:

    $ gem install offline_geocoder

Once that is done, you can use it like this:

require "offline_geocoder"

geocoder = OfflineGeocoder.new

results = geocoder.search(51.5214588, -0.1729636)

p results

The above code will output this:

{
  :lat=>51.51116,
  :lon=>-0.18426,
  :name=>"Bayswater",
  :admin1=>"England",
  :admin2=>"Greater London",
  :cc=>"GB",
  :country=>"United Kingdom"
}

And... That's it! I do hope this gem can find many uses and solve many headaches and problems. As always, if you have any suggestions or further questions related to this, please leave a comment or contact me by twitter or facebook.