我试图找出某个点所在的区域(多边形)。
我正在尝试关注 Daniel Azuma 的 RubyConf2012 地理空间分析截屏视频。以下代码始终返回 nil:
Region.where{st_contains(poly, Point.last.coords)}.first
这是我的区域课程:
class Region < ActiveRecord::Base
attr_accessible :poly, :name, :multi
has_many :points
GEOFACTORY = RGeo::Geographic.simple_mercator_factory
set_rgeo_factory_for_column(:poly, GEOFACTORY.projection_factory)
def self.load
path = File.join(Rails.root, 'lib/tasks/uk/districts')
factory = GEOFACTORY
RGeo::Shapefile::Reader::open(path, :factory => factory) do |file|
file.each do |record|
name = record['NAME']
region = Region.where(:name => name).first ||
record.geometry.projection.each do |poly|
Region.create(:name => name, :poly => poly)
end
end
end
end
积分类:
class Point < ActiveRecord::Base
attr_accessible :coords
belongs_to :region
def self.find_region
region = Region.where{st_contains(poly, Point.last.coords)}.first
puts region
end
end