r/learnruby • Beginner • Oct 14 '14

Point in Polygon Gem

I'm new to Ruby and learning as I go along, while working on a small personal project.

I want to input a geocoded point (lat & long), and return just the name of the map area it is in. I found a great gem that fits my needs almost perfectly over at GitHub, but have only been able to get it to return true and false.

https://github.com/square/border_patrol

In the documentation, I found some code that I believe should extract the 'placemark' in my KML file but I keep getting undefined methods.

Here is my code. I have multiple polygons, inside placemark parents in my KML file.

require 'open-uri'
require 'border_patrol'

kml_file = open("regionlist.kml")
region = BorderPatrol.parse_kml(kml_file)

userinput = BorderPatrol::Point.new(-122.5, 37.75)

region_area = region.contains_point?(userinput)
puts "Your point is in the region:", region_area

kml_data = File.read("regionlist.kml")
doc = Nokogiri::XML(kml_data)
polygon_node = doc.search('Placemark').first

placemark_name = BorderPatrol.placemark_name_for_polygon(polygon_node)

Much thanks for any help! I can make what I need work without this, but it would be nice to have a single KML where all the polygons are checked and the name of the 'point in polygon = true' being returned.

2 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/przyjaciel Beginner Oct 15 '14

Big thanks for your help!

I finally diagnosed the problem, which was as expected something silly.

I installed the gem through

gem install border_patrol

but the version hosted there doesn't include any of the commits on github that include the reader accessor or methods for placemark_names even though the version number is the same.

I had to update the gem manually from the source on Github, and build it then install but now everything works.

Here is my now working code:

require 'open-uri'
require 'border_patrol'

kml_file = open("regionlist.kml")
regions = BorderPatrol.parse_kml(kml_file)

funtown = BorderPatrol::Point.new(-122.5, 37.75)
sadcastle = BorderPatrol::Point.new(-122.4, 37.75)

region_area = regions.find { |polygon| polygon.contains_point?(funtown) }
puts "Your point is in the region area for #{region_area.placemark_name}"

region_area = regions.find { |polygon| polygon.contains_point?(sadcastle) }
puts "Your point is in the region area for #{region_area.placemark_name}"