Filter Rails records to within geo-polygonal area using latitude/longitude
<p>In my Rails app I have search areas, which are geographic polygons (created using the Google Maps API). Once drawn on the map, the search area's coordinates are saved to a record as an array of hashes. It looks something like this:</p>

<pre><code>SearchArea.last.geo
=&gt; [{"lat"=&gt;"48.425555463221066", "lng"=&gt;"-123.3793830871582"}, {"lat"=&gt;"48.41336528699442", "lng"=&gt;"-123.37800979614258"}, {"lat"=&gt;"48.41006090395106", "lng"=&gt;"-123.31672668457031"}, {"lat"=&gt;"48.44696663691928", "lng"=&gt;"-123.32067489624023"}]
</code></pre>

<p>I also have a handful of location records whose coordinates are saved as a hash, like so:</p>

<pre><code>Location.last.geo
=&gt; {"lat"=&gt;"28.8138124", "lng"=&gt;"-96.9977682"}
</code></pre>

<p>I need to be able to search for/filter records that have geo coordinates that fall within a given search area. I have no experience with geospatial programming, so I'm hoping someone can lend a hand here. It would be really appreciated!</p>

<p>Things I've looked in to:</p>

<ul>
<li>I know the Google Maps API has a <a href="https://developers.google.com/maps/documentation/javascript/geometry?hl=en" rel="nofollow noreferrer">Geometry Library</a>. Unfortunately this is client side.</li>
<li>I found <a href="https://stackoverflow.com/questions/15303568/detect-if-point-in-latitude-longitude-based-polygon">this</a> answer, which led to <a href="http://alienryderflex.com/polygon/" rel="nofollow noreferrer">this</a> article, but it is written in C.</li>
<li><a href="http://jakescruggs.blogspot.ca/2009/07/point-inside-polygon-in-ruby.html" rel="nofollow noreferrer">This article</a> from 2009 has some code that looks like it should work, but I'm not sure how I would implement it using latitude/longitude coordinates.</li>
<li><a href="http://rosettacode.org/wiki/Ray-casting_algorithm" rel="nofollow noreferrer">This wiki page</a> has a whole bunch of implementations of Ray-casting in various languages, but again, I'm not sure how to implement it in Ruby/Rails.</li>
</ul>