Class: ActiveRecord::ConnectionAdapters::PostGISAdapter::SpatialColumn

Inherits:
ConnectionAdapters::PostgreSQLColumn
  • Object
show all
Defined in:
lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory_settings_, table_name_, name_, default_, oid_type_, sql_type_ = nil, null_ = true, opts_ = nil) ⇒ SpatialColumn

Returns a new instance of SpatialColumn.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 11

def initialize(factory_settings_, table_name_, name_, default_, oid_type_, sql_type_=nil, null_=true, opts_=nil)
  @factory_settings = factory_settings_
  @table_name = table_name_
  @geographic = sql_type_ =~ /geography/i ? true : false
  if opts_
    # This case comes from an entry in the geometry_columns table
    @geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name(opts_[:type]) ||
      ::RGeo::Feature::Geometry
    @srid = opts_[:srid].to_i
    @has_z = opts_[:has_z] ? true : false
    @has_m = opts_[:has_m] ? true : false
  elsif @geographic
    # Geographic type information is embedded in the SQL type
    @geometric_type = ::RGeo::Feature::Geometry
    @srid = 4326
    @has_z = @has_m = false
    if sql_type_ =~ /geography\((.*)\)$/i
      params_ = $1.split(',')
      if params_.size >= 2
        if params_.first =~ /([a-z]+[^zm])(z?)(m?)/i
          @has_z = $2.length > 0
          @has_m = $3.length > 0
          @geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name($1)
        end
        if params_.last =~ /(\d+)/
          @srid = $1.to_i
        end
      end
    end
  elsif sql_type_ =~ /geography|geometry|point|linestring|polygon/i
    # Just in case there is a geometry column with no geometry_columns entry.
    @geometric_type = ::RGeo::Feature::Geometry
    @srid = @has_z = @has_m = nil
  else
    # Non-spatial column
    @geometric_type = @has_z = @has_m = @srid = nil
  end
  super(name_, default_, oid_type_, sql_type_, null_)
  if spatial?
    if @srid
      @limit = {:srid => @srid, :type => @geometric_type.type_name.underscore}
      @limit[:has_z] = true if @has_z
      @limit[:has_m] = true if @has_m
      @limit[:geographic] = true if @geographic
    else
      @limit = {:no_constraints => true}
    end
  end
end

Instance Attribute Details

#geographicObject (readonly) Also known as: geographic?

Returns the value of attribute geographic.



62
63
64
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 62

def geographic
  @geographic
end

#geometric_typeObject (readonly)

Returns the value of attribute geometric_type.



64
65
66
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 64

def geometric_type
  @geometric_type
end

#has_mObject (readonly) Also known as: has_m?

Returns the value of attribute has_m.



66
67
68
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 66

def has_m
  @has_m
end

#has_zObject (readonly) Also known as: has_z?

Returns the value of attribute has_z.



65
66
67
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 65

def has_z
  @has_z
end

#sridObject (readonly)

Returns the value of attribute srid.



63
64
65
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 63

def srid
  @srid
end

Class Method Details

.convert_to_geometry(input_, factory_settings_, table_name_, column_, geographic_, srid_, has_z_, has_m_) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 106

def self.convert_to_geometry(input_, factory_settings_, table_name_, column_, geographic_, srid_, has_z_, has_m_)
  if srid_
    constraints_ = {:geographic => geographic_, :has_z_coordinate => has_z_,
      :has_m_coordinate => has_m_, :srid => srid_}
  else
    constraints_ = nil
  end
  if ::RGeo::Feature::Geometry === input_
    factory_ = factory_settings_.get_column_factory(table_name_, column_, constraints_)
    ::RGeo::Feature.cast(input_, factory_) rescue nil
  elsif input_.respond_to?(:to_str)
    input_ = input_.to_str
    if input_.length == 0
      nil
    else
      factory_ = factory_settings_.get_column_factory(table_name_, column_, constraints_)
      marker_ = input_[0,1]
      if marker_ == "\x00" || marker_ == "\x01" || input_[0,4] =~ /[0-9a-fA-F]{4}/
        ::RGeo::WKRep::WKBParser.new(factory_, :support_ewkb => true).parse(input_) rescue nil
      else
        ::RGeo::WKRep::WKTParser.new(factory_, :support_ewkt => true).parse(input_) rescue nil
      end
    end
  else
    nil
  end
end

Instance Method Details

#has_spatial_constraints?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 78

def has_spatial_constraints?
  !@srid.nil?
end

#klassObject



83
84
85
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 83

def klass
  spatial? ? ::RGeo::Feature::Geometry : super
end

#spatial?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 73

def spatial?
  type == :spatial || type == :geography
end

#type_cast(value_) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb', line 88

def type_cast(value_)
  if spatial?
    SpatialColumn.convert_to_geometry(value_, @factory_settings, @table_name, name,
      @geographic, @srid, @has_z, @has_m)
  else
    super
  end
end