Module: PostgisFunctions::ClassMethods
- Defined in:
- lib/postgis_functions/class.rb,
lib/postgis_adapter/acts_as_geom.rb
Overview
Class Methods
Instance Method Summary collapse
- #all_within(other, margin = 1) ⇒ Object
- #by_area(sort = 'asc') ⇒ Object
- #by_boundaries(sort = 'asc') ⇒ Object
- #by_length(opts = {}) ⇒ Object
- #by_perimeter(sort = 'asc') ⇒ Object
-
#close_to(p, opts = {}) ⇒ Object
Order by distance.
-
#closest_to(p, opts = {}) ⇒ Object
Returns the closest record.
- #contain(p, srid = 4326) ⇒ Object
- #contains(p, srid = 4326) ⇒ Object
- #get_geom_type(column) ⇒ Object
-
#has_geom(*geom) ⇒ Object
(also: #acts_as_geom)
has_geom :db_field => :geom_type Examples:.
- #longest ⇒ Object
Instance Method Details
#all_within(other, margin = 1) ⇒ Object
50 51 52 53 |
# File 'lib/postgis_functions/class.rb', line 50 def all_within(other, margin=1) # find(:all, :conditions => "ST_DWithin(geom, ST_GeomFromEWKB(E'#{other.as_ewkt}'), #{margin})") find(:all, :conditions => "ST_DWithin(geom, ST_GeomFromEWKT(E'#{other.as_hex_ewkb}'), #{margin})") end |
#by_area(sort = 'asc') ⇒ Object
42 43 44 |
# File 'lib/postgis_functions/class.rb', line 42 def by_area sort='asc' find(:all, :order => "ST_Area(geom) #{sort}" ) end |
#by_boundaries(sort = 'asc') ⇒ Object
55 56 57 |
# File 'lib/postgis_functions/class.rb', line 55 def by_boundaries sort='asc' find(:all, :order => "ST_Boundary(geom) #{sort}" ) end |
#by_length(opts = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/postgis_functions/class.rb', line 24 def by_length opts = {} sort = opts.delete(:sort) || 'asc' opts.merge!(:order => "ST_length(geom) #{sort}") find(:all, opts) end |
#by_perimeter(sort = 'asc') ⇒ Object
46 47 48 |
# File 'lib/postgis_functions/class.rb', line 46 def by_perimeter sort='asc' find(:all, :order => "ST_Perimeter(geom) #{sort}" ) end |
#close_to(p, opts = {}) ⇒ Object
Order by distance
18 19 20 21 22 |
# File 'lib/postgis_functions/class.rb', line 18 def close_to(p, opts = {}) srid = opts.delete(:srid) || 4326 opts.merge!(:order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))") find(:all, opts) end |
#closest_to(p, opts = {}) ⇒ Object
Returns the closest record
10 11 12 13 14 |
# File 'lib/postgis_functions/class.rb', line 10 def closest_to(p, opts = {}) srid = opts.delete(:srid) || 4326 opts.merge!(:order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))") find(:first, opts) end |
#contain(p, srid = 4326) ⇒ Object
38 39 40 |
# File 'lib/postgis_functions/class.rb', line 38 def contain(p, srid=4326) find(:first, :conditions => ["ST_Contains(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))"]) end |
#contains(p, srid = 4326) ⇒ Object
34 35 36 |
# File 'lib/postgis_functions/class.rb', line 34 def contains(p, srid=4326) find(:all, :conditions => ["ST_Contains(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))"]) end |
#get_geom_type(column) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/postgis_adapter/acts_as_geom.rb', line 31 def get_geom_type(column) self.postgis_geoms.values[0] rescue nil # self.columns.select { |c| c.name == column.to_s }[0].geometry_type # rescue ActiveRecord::StatementInvalid => e # nil end |
#has_geom(*geom) ⇒ Object Also known as: acts_as_geom
has_geom :db_field => :geom_type Examples:
has_geom :data => :point has_geom :geom => :line_string has_geom :geom => :polygon
20 21 22 23 24 25 26 27 28 |
# File 'lib/postgis_adapter/acts_as_geom.rb', line 20 def has_geom(*geom) cattr_accessor :postgis_geoms self.postgis_geoms = geom[0] # {:columns => column send :include, case geom[0].values[0] when :point then PointFunctions when :polygon then PolygonFunctions when :line_string, :multi_line_string then LineStringFunctions end unless geom[0].kind_of? Symbol end |
#longest ⇒ Object
30 31 32 |
# File 'lib/postgis_functions/class.rb', line 30 def longest find(:first, :order => "ST_length(geom) DESC") end |