GeoKit – have You slept on trigonometry?

I was just about to write that GeoKit is useful plugin but somewhat dormant (previous release was in Jan 08 if I recollect correctly), but I’ve checked project homepage and voila! It has new release! GeoKit was dived into gem (framework independent) and Rails plugin. That way You can use geokit-gem to make some distance calculations even if You didn’t like trigonometry at school :)

How often You had wished time wouldn't move so slow? Image (c) <a href='http://flickr.com/photos/fake_eyes/'>Orange42</a>” title=”Bored at lessons” width=”500″ height=”375″ class=”size-full wp-image-415″ /><figcaption id=How often You had wished time wouldn't move so slow? Image (c) Orange42

GeoKit does provide some finders for ActiveRecord like find_closest (hard to guess what it does?). Remember that GeoKit expects latitude and longitude fields in AR object to be not nil. If You have some objects with latitude or longitude equal to nil they will be always closest to any given point. Probably not what do You want.

It may be dependent on what are You trying to do, but for me setting default latitude and longitude to values I won’t spot in the wild in my dataset was good enough. But this was special case – I have set default to North Pole, and I was looking not only closest but also close – no further than single miles and I was sure this would not trigger with North Pole.

Why not set defaults to something like -200? You didn’t like trigonometry, do You? Functions used to calculate distance on the sphere are periodic, so You have chance You will hit false positive – latitude and longitude -200/-200 will me mapped into -90..90 and -180..180 ranges and could match anyway.

So when doing any searches and You can not assure Your data have always latitude and longitude You have to remember to add conditions lat is not null && lng is not null to every query (like Place.find_closest(:origin => [some_lat, some_lng], :conditions => 'lat is not null && lng is not null').

Anyway – GeoKit is nice gem/plugin and if You need to operate on latitude and longitude – use it, do not write own code. On the other hand, You could learn a bit from trigonometry :))

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.