1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
# -*- coding: utf-8 -*-
require 'geocoder/sql'
require 'geocoder/stores/base'
##
# Add geocoding functionality to any ActiveRecord object.
#
module Geocoder::Store
module ActiveRecord
include Base
##
# Implementation of 'included' hook method.
#
def self.included(base)
base.extend ClassMethods
base.class_eval do
# scope: geocoded objects
scope :geocoded, lambda {
where("#{table_name}.#{geocoder_options[:latitude]} IS NOT NULL " +
"AND #{table_name}.#{geocoder_options[:longitude]} IS NOT NULL")
}
# scope: not-geocoded objects
scope :not_geocoded, lambda {
where("#{table_name}.#{geocoder_options[:latitude]} IS NULL " +
"OR #{table_name}.#{geocoder_options[:longitude]} IS NULL")
}
# scope: not-reverse geocoded objects
scope :not_reverse_geocoded, lambda {
where("#{table_name}.#{geocoder_options[:fetched_address]} IS NULL")
}
##
# Find all objects within a radius of the given location.
# Location may be either a string to geocode or an array of
# coordinates (<tt>[lat,lon]</tt>). Also takes an options hash
# (see Geocoder::Store::ActiveRecord::ClassMethods.near_scope_options
# for details).
#
scope :near, lambda{ |location, *args|
latitude, longitude = Geocoder::Calculations.extract_coordinates(location)
if Geocoder::Calculations.coordinates_present?(latitude, longitude)
options = near_scope_options(latitude, longitude, *args)
select(options[:select]).where(options[:conditions]).
order(options[:order])
else
# If no lat/lon given we don't want any results, but we still
# need distance and bearing columns so you can add, for example:
# .order("distance")
select(select_clause(nil, null_value, null_value)).where(false_condition)
end
}
##
# Find all objects within the area of a given bounding box.
# Bounds must be an array of locations specifying the southwest
# corner followed by the northeast corner of the box
# (<tt>[[sw_lat, sw_lon], [ne_lat, ne_lon]]</tt>).
#
scope :within_bounding_box, lambda{ |*bounds|
sw_lat, sw_lng, ne_lat, ne_lng = bounds.flatten if bounds
if sw_lat && sw_lng && ne_lat && ne_lng
where(Geocoder::Sql.within_bounding_box(
sw_lat, sw_lng, ne_lat, ne_lng,
full_column_name(geocoder_options[:latitude]),
full_column_name(geocoder_options[:longitude])
))
else
select(select_clause(nil, null_value, null_value)).where(false_condition)
end
}
end
end
##
# Methods which will be class methods of the including class.
#
module ClassMethods
def distance_from_sql(location, *args)
latitude, longitude = Geocoder::Calculations.extract_coordinates(location)
if Geocoder::Calculations.coordinates_present?(latitude, longitude)
distance_sql(latitude, longitude, *args)
end
end
##
# Get options hash suitable for passing to ActiveRecord.find to get
# records within a radius (in kilometers) of the given point.
# Options hash may include:
#
# * +:units+ - <tt>:mi</tt> or <tt>:km</tt>; to be used.
# for interpreting radius as well as the +distance+ attribute which
# is added to each found nearby object.
# Use Geocoder.configure[:units] to configure default units.
# * +:bearing+ - <tt>:linear</tt> or <tt>:spherical</tt>.
# the method to be used for calculating the bearing (direction)
# between the given point and each found nearby point;
# set to false for no bearing calculation. Use
# Geocoder.configure[:distances] to configure default calculation method.
# * +:select+ - string with the SELECT SQL fragment (e.g. “id, name”)
# * +:select_distance+ - whether to include the distance alias in the
# SELECT SQL fragment (e.g. <formula> AS distance)
# * +:select_bearing+ - like +:select_distance+ but for bearing.
# * +:order+ - column(s) for ORDER BY SQL clause; default is distance;
# set to false or nil to omit the ORDER BY clause
# * +:exclude+ - an object to exclude (used by the +nearbys+ method)
# * +:distance_column+ - used to set the column name of the calculated distance.
# * +:bearing_column+ - used to set the column name of the calculated bearing.
# * +:min_radius+ - the value to use as the minimum radius.
# ignored if database is sqlite.
# default is 0.0
#
def near_scope_options(latitude, longitude, radius = 20, options = {})
if options[:units]
options[:units] = options[:units].to_sym
end
latitude_attribute = options[:latitude] || geocoder_options[:latitude]
longitude_attribute = options[:longitude] || geocoder_options[:longitude]
options[:units] ||= (geocoder_options[:units] || Geocoder.config.units)
select_distance = options.fetch(:select_distance) { true }
options[:order] = "" if !select_distance && !options.include?(:order)
select_bearing = options.fetch(:select_bearing) { true }
bearing = bearing_sql(latitude, longitude, options)
distance = distance_sql(latitude, longitude, options)
distance_column = options.fetch(:distance_column) { 'distance' }
bearing_column = options.fetch(:bearing_column) { 'bearing' }
# If radius is a DB column name, bounding box should include
# all rows within the maximum radius appearing in that column.
# Note: performance is dependent on variability of radii.
bb_radius = radius.is_a?(Symbol) ? maximum(radius) : radius
b = Geocoder::Calculations.bounding_box([latitude, longitude], bb_radius, options)
args = b + [
full_column_name(latitude_attribute),
full_column_name(longitude_attribute)
]
bounding_box_conditions = Geocoder::Sql.within_bounding_box(*args)
if using_unextended_sqlite?
conditions = bounding_box_conditions
else
min_radius = options.fetch(:min_radius, 0).to_f
# if radius is a DB column name,
# find rows between min_radius and value in column
if radius.is_a?(Symbol)
c = "BETWEEN ? AND #{radius}"
a = [min_radius]
else
c = "BETWEEN ? AND ?"
a = [min_radius, radius]
end
conditions = [bounding_box_conditions + " AND (#{distance}) " + c] + a
end
{
:select => select_clause(options[:select],
select_distance ? distance : nil,
select_bearing ? bearing : nil,
distance_column,
bearing_column),
:conditions => add_exclude_condition(conditions, options[:exclude]),
:order => options.include?(:order) ? options[:order] : "#{distance_column} ASC"
}
end
##
# SQL for calculating distance based on the current database's
# capabilities (trig functions?).
#
def distance_sql(latitude, longitude, options = {})
method_prefix = using_unextended_sqlite? ? "approx" : "full"
Geocoder::Sql.send(
method_prefix + "_distance",
latitude, longitude,
full_column_name(options[:latitude] || geocoder_options[:latitude]),
full_column_name(options[:longitude]|| geocoder_options[:longitude]),
options
)
end
##
# SQL for calculating bearing based on the current database's
# capabilities (trig functions?).
#
def bearing_sql(latitude, longitude, options = {})
if !options.include?(:bearing)
options[:bearing] = Geocoder.config.distances
end
if options[:bearing]
method_prefix = using_unextended_sqlite? ? "approx" : "full"
Geocoder::Sql.send(
method_prefix + "_bearing",
latitude, longitude,
full_column_name(options[:latitude] || geocoder_options[:latitude]),
full_column_name(options[:longitude]|| geocoder_options[:longitude]),
options
)
end
end
##
# Generate the SELECT clause.
#
def select_clause(columns, distance = nil, bearing = nil, distance_column = 'distance', bearing_column = 'bearing')
if columns == :id_only
return full_column_name(primary_key)
elsif columns == :geo_only
clause = ""
else
clause = (columns || full_column_name("*"))
end
if distance
clause += ", " unless clause.empty?
clause += "#{distance} AS #{distance_column}"
end
if bearing
clause += ", " unless clause.empty?
clause += "#{bearing} AS #{bearing_column}"
end
clause
end
##
# Adds a condition to exclude a given object by ID.
# Expects conditions as an array or string. Returns array.
#
def add_exclude_condition(conditions, exclude)
conditions = [conditions] if conditions.is_a?(String)
if exclude
conditions[0] << " AND #{full_column_name(primary_key)} != ?"
conditions << exclude.id
end
conditions
end
def using_unextended_sqlite?
using_sqlite? && !using_sqlite_with_extensions?
end
def using_sqlite?
!!connection.adapter_name.match(/sqlite/i)
end
def using_sqlite_with_extensions?
connection.adapter_name.match(/sqlite/i) &&
defined?(::SqliteExt) &&
%W(MOD POWER SQRT PI SIN COS ASIN ATAN2).all?{ |fn_name|
connection.raw_connection.function_created?(fn_name)
}
end
def using_postgres?
connection.adapter_name.match(/postgres/i)
end
##
# Use OID type when running in PosgreSQL
#
def null_value
using_postgres? ? 'NULL::text' : 'NULL'
end
##
# Value which can be passed to where() to produce no results.
#
def false_condition
using_unextended_sqlite? ? 0 : "false"
end
##
# Prepend table name if column name doesn't already contain one.
#
def full_column_name(column)
column = column.to_s
column.include?(".") ? column : [table_name, column].join(".")
end
end
##
# Get nearby geocoded objects.
# Takes the same options hash as the near class method (scope).
# Returns nil if the object is not geocoded.
#
def nearbys(radius = 20, options = {})
return nil unless geocoded?
options.merge!(:exclude => self) unless send(self.class.primary_key).nil?
self.class.near(self, radius, options)
end
##
# Look up coordinates and assign to +latitude+ and +longitude+ attributes
# (or other as specified in +geocoded_by+). Returns coordinates (array).
#
def geocode
do_lookup(false) do |o,rs|
if r = rs.first
unless r.latitude.nil? or r.longitude.nil?
o.__send__ "#{self.class.geocoder_options[:latitude]}=", r.latitude
o.__send__ "#{self.class.geocoder_options[:longitude]}=", r.longitude
end
r.coordinates
end
end
end
alias_method :fetch_coordinates, :geocode
##
# Look up address and assign to +address+ attribute (or other as specified
# in +reverse_geocoded_by+). Returns address (string).
#
def reverse_geocode
do_lookup(true) do |o,rs|
if r = rs.first
unless r.address.nil?
o.__send__ "#{self.class.geocoder_options[:fetched_address]}=", r.address
end
r.address
end
end
end
alias_method :fetch_address, :reverse_geocode
end
end
|