Module: SpatialColumn

Included in:
ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn
Defined in:
lib/postgis_adapter/common_spatial_adapter.rb

Overview

using a mixin instead of subclassing Column since each adapter can have a specific subclass of Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#geometry_typeObject (readonly)

Returns the value of attribute geometry_type.



30
31
32
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 30

def geometry_type
  @geometry_type
end

#sridObject (readonly)

Returns the value of attribute srid.



30
31
32
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 30

def srid
  @srid
end

#with_mObject (readonly)

Returns the value of attribute with_m.



30
31
32
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 30

def with_m
  @with_m
end

#with_zObject (readonly)

Returns the value of attribute with_z.



30
31
32
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 30

def with_z
  @with_z
end

Instance Method Details

#initialize(name, default, sql_type = nil, null = true, srid = -1,, with_z = false, with_m = false) ⇒ Object



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

def initialize(name, default, sql_type = nil, null = true,srid=-1,with_z=false,with_m=false)
  super(name,default,sql_type,null)
  @geometry_type = geometry_simplified_type(@sql_type)
  @srid = srid
  @with_z = with_z
  @with_m = with_m
end

#klassObject

Redefines klass to add support for geometries



62
63
64
65
66
67
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 62

def klass
  case type
  when :geometry then GeoRuby::SimpleFeatures::Geometry
  else super
  end
end

#type_cast(value) ⇒ Object

Redefines type_cast to add support for geometries



42
43
44
45
46
47
48
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 42

def type_cast(value)
  return nil if value.nil?
  case type
  when :geometry then self.class.string_to_geometry(value)
  else super
  end
end

#type_cast_code(var_name) ⇒ Object

Redefines type_cast_code to add support for geometries.

WARNING : Since ActiveRecord keeps only the string values directly returned from the database, it translates from these to the correct types everytime an attribute is read (using the code returned by this method), which is probably ok for simple types, but might be less than efficient for geometries. Also you cannot modify the geometry object returned directly or your change will not be saved.



53
54
55
56
57
58
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 53

def type_cast_code(var_name)
  case type
  when :geometry then "#{self.class.name}.string_to_geometry(#{var_name})"
  else super
  end
end