r/learnruby • u/przyjaciel 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.
View all comments
Show parent comment
2
u/przyjaciel Beginner Oct 14 '14
Thanks for the help.
If I output the result of this, I get the following returned polygon
It could be one step closer, since it shows us having retrieved the polygon containing the point but what I really need is the place mark name from the KML file.
No matter how I try to call the placemark_name_for_polygon method, I get a NoMethodError.
Here is a link to the spec for the method:
https://github.com/square/border_patrol/blob/72c50ea17c89f7fa89fbaed25ad3db3fa1d8eeb1/spec/lib/border_patrol_spec.rb
Thank you again for the help.