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.



120
121
122
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 120

def geometry_type
  @geometry_type
end

#sridObject (readonly)

Returns the value of attribute srid.



120
121
122
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 120

def srid
  @srid
end

#with_mObject (readonly)

Returns the value of attribute with_m.



120
121
122
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 120

def with_m
  @with_m
end

#with_zObject (readonly)

Returns the value of attribute with_z.



120
121
122
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 120

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



122
123
124
125
126
127
128
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 122

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



152
153
154
155
156
157
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 152

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



132
133
134
135
136
137
138
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 132

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.



143
144
145
146
147
148
# File 'lib/postgis_adapter/common_spatial_adapter.rb', line 143

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