Module: PostgisAdapter::Functions::ClassMethods

Defined in:
lib/postgis_adapter/functions/class.rb,
lib/postgis_adapter/acts_as_geom.rb

Overview

Class Methods

Instance Method Summary collapse

Instance Method Details

#all_dwithin(other, margin = 1) ⇒ Object



48
49
50
# File 'lib/postgis_adapter/functions/class.rb', line 48

def all_dwithin(other, margin=1)
  where "ST_DWithin(#{default_geometry}, '#{other.as_ewkt}', #{margin})"
end

#all_within(other) ⇒ Object



52
53
54
# File 'lib/postgis_adapter/functions/class.rb', line 52

def all_within(other)
  where "ST_Within(#{default_geometry}, '#{other.as_ewkt}' )"
end

#by_area(sort = 'asc') ⇒ Object



40
41
42
# File 'lib/postgis_adapter/functions/class.rb', line 40

def by_area sort='asc'
  order "ST_Area(#{default_geometry}) #{sort}"
end

#by_boundaries(sort = 'asc') ⇒ Object



56
57
58
# File 'lib/postgis_adapter/functions/class.rb', line 56

def by_boundaries sort='asc'
  order "ST_Boundary(#{default_geometry}) #{sort}"
end

#by_length(opts = {}) ⇒ Object



23
24
25
26
# File 'lib/postgis_adapter/functions/class.rb', line 23

def by_length opts = {}
  sort = opts.delete(:sort) || 'asc'
  order "ST_length(#{default_geometry}) #{sort}"        
end

#by_perimeter(sort = 'asc') ⇒ Object



44
45
46
# File 'lib/postgis_adapter/functions/class.rb', line 44

def by_perimeter sort='asc'
  order "ST_Perimeter(#{default_geometry}) #{sort}" 
end

#close_to(p, opts = {}) ⇒ Object

Order by distance



18
19
20
21
# File 'lib/postgis_adapter/functions/class.rb', line 18

def close_to(p, opts = {})
  srid = opts.delete(:srid) || 4326
  order "ST_Distance(#{default_geometry}, '#{p.as_ewkt}' )"
end

#closest_to(p, opts = {}) ⇒ Object

Returns the closest record



11
12
13
14
# File 'lib/postgis_adapter/functions/class.rb', line 11

def closest_to(p, opts = {})
  srid = opts.delete(:srid) || 4326
  order("ST_Distance(#{default_geometry}, '#{p.as_ewkt}' )").first
end

#contain(p, srid = 4326) ⇒ Object



36
37
38
# File 'lib/postgis_adapter/functions/class.rb', line 36

def contain(p, srid=4326)
  where("ST_Contains(#{default_geometry}, '#{p.as_ewkt}' )").first
end

#contains(p, srid = 4326) ⇒ Object



32
33
34
# File 'lib/postgis_adapter/functions/class.rb', line 32

def contains(p, srid=4326)
  where "ST_Contains(#{default_geometry}, '#{p.as_ewkt}' )" 
end

#default_geometryObject



38
39
40
# File 'lib/postgis_adapter/acts_as_geom.rb', line 38

def default_geometry
  postgis_geoms
end

#get_geom_type(column) ⇒ Object



34
35
36
# File 'lib/postgis_adapter/acts_as_geom.rb', line 34

def get_geom_type(column)
  self.postgis_geoms.values[0] rescue 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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/postgis_adapter/acts_as_geom.rb', line 21

def has_geom(*geom)
  cattr_accessor :postgis_geoms
  self.postgis_geoms = geom[0].keys.first # {: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
                 when :multi_polygon then MultiPolygonFunctions
                 when :geometry    then GeometryFunctions
                 end unless geom[0].kind_of? Symbol
end

#longestObject



28
29
30
# File 'lib/postgis_adapter/functions/class.rb', line 28

def longest
  order("ST_length(geom) DESC").first
end