Module: GeoFoo::Core
- Defined in:
- lib/geo_foo/core.rb
Class Method Summary collapse
-
.create_table ⇒ Object
migration helper: create the database table.
-
.drop_table ⇒ Object
migration helper: drop the database table.
-
.find_neighbours_by_coords(lat, lon, radius = 100.0) ⇒ Object
find all locations within a radius for a given location.
Class Method Details
.create_table ⇒ Object
migration helper: create the database table
29 30 31 32 33 |
# File 'lib/geo_foo/core.rb', line 29 def self.create_table execute "CREATE TABLE #{TableName} (id serial PRIMARY KEY)" execute "SELECT AddGeometryColumn('#{TableName}', 'point', #{SRID}, 'POINT', 2)" execute "CREATE INDEX #{TableName}_point_index ON #{TableName} USING GIST (point)" end |
.drop_table ⇒ Object
migration helper: drop the database table
36 37 38 |
# File 'lib/geo_foo/core.rb', line 36 def self.drop_table execute "DROP TABLE #{TableName}" end |
.find_neighbours_by_coords(lat, lon, radius = 100.0) ⇒ Object
find all locations within a radius for a given location
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/geo_foo/core.rb', line 17 def self.find_neighbours_by_coords lat, lon, radius=100.0 # compute an appropriate bounding box size for this latitude # XXX handle case where lat is (close to) +-90deg (poles) bbox_size= (radius.to_f / (EarthRadius * Math.cos(lat.to_rad))).to_deg distance = "ST_Distance_Sphere(point, #{as_point(lat,lon)})" (execute "SELECT (id) FROM #{TableName} "\ "WHERE ST_DWithin(#{as_point(lat,lon)}, point, #{bbox_size}) "\ "AND #{distance} < #{radius} "\ "ORDER BY #{distance}").map { |row| row["id"].to_i } end |