I am using the rgeo
and rgeo-shapefile
gems in my Rails 5 application. My goal is to load in a shapefile of neighborhoods and determine which neighborhood a latitude/longitude point is located within.
In their documentation they have this example.
require 'rgeo/shapefile'
name = "neighborhoods.shp"
RGeo::Shapefile::Reader.open(name) do |file|
puts "File contains #{file.num_records} records."
file.each do |record|
puts "Record number #{record.index}:"
puts " Geometry: #{record.geometry.as_text}"
puts " Attributes: #{record.attributes.inspect}"
end
file.rewind
record = file.next
puts "First record geometry was: #{record.geometry.as_text}"
end
I'm not quite sure how to move from this example towards what I would like to do.
Use the
#within?
method.The coordinate systems for the point and the neighborhood need to be the same. I believe its more efficient to use rectilinear projections for these types of operations but I've used them successfully in the past with WGS as well, though I remember I've gotten error messages for certain types of overlap-type queries in WGS/4326.
#within?
should work fine though.