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

- (Object) all_within(other, margin = 1)



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

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

- (Object) by_area(sort = 'asc' )



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

def by_area sort='asc'
  find(:all, :order => "ST_Area(geom) #{sort}" )
end

- (Object) by_boundaries(sort = 'asc' )



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

def by_boundaries sort='asc'
  find(:all, :order => "ST_Boundary(geom) #{sort}" )
end

- (Object) by_length(opts = {})



25
26
27
28
29
# File 'lib/postgis_adapter/functions/class.rb', line 25

def by_length opts = {}
  sort = opts.delete(:sort) || 'asc'
  opts.merge!(:order => "ST_length(geom) #{sort}")
  find(:all, opts)
end

- (Object) by_perimeter(sort = 'asc' )



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

def by_perimeter sort='asc'
  find(:all, :order => "ST_Perimeter(geom) #{sort}" )
end

- (Object) close_to(p, opts = {})

Order by distance



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

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

- (Object) closest_to(p, opts = {})

Returns the closest record



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

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

- (Object) contain(p, srid = 4326)



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

def contain(p, srid=4326)
  find(:first, :conditions => ["ST_Contains(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))"])
end

- (Object) contains(p, srid = 4326)



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

def contains(p, srid=4326)
  find(:all, :conditions => ["ST_Contains(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))"])
end

- (Object) get_geom_type(column)



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

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

- (Object) has_geom(*geom) 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
# File 'lib/postgis_adapter/acts_as_geom.rb', line 21

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
    when :multi_polygon then MultiPolygonFunctions
  end unless geom[0].kind_of? Symbol
end

- (Object) longest



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

def longest
  find(:first, :order => "ST_length(geom) DESC")
end