Class: ActiveRecord::ConnectionAdapters::PostGISAdapter

Inherits:
PostgreSQLAdapter
  • Object
show all
Includes:
ActiveRecord::ConnectionAdapters::PostGIS::DatabaseStatements, ActiveRecord::ConnectionAdapters::PostGIS::Quoting, ActiveRecord::ConnectionAdapters::PostGIS::SchemaStatements
Defined in:
lib/active_record/connection_adapters/postgis_adapter.rb

Constant Summary collapse

ADAPTER_NAME =
'PostGIS'
SPATIAL_COLUMN_OPTIONS =
{
  geography:           { geographic: true },
  geometry:            {},
  geometry_collection: {},
  line_string:         {},
  multi_line_string:   {},
  multi_point:         {},
  multi_polygon:       {},
  spatial:             {},
  st_point:            {},
  st_polygon:          {},
}
DEFAULT_SRID =
0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecord::ConnectionAdapters::PostGIS::DatabaseStatements

#truncate_tables

Methods included from ActiveRecord::ConnectionAdapters::PostGIS::SchemaStatements

#create_table_definition, #new_column_from_field, #spatial_column_info

Methods included from ActiveRecord::ConnectionAdapters::PostGIS::Quoting

#type_cast

Class Method Details

.initialize_type_map(map = type_map) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_record/connection_adapters/postgis_adapter.rb', line 67

def initialize_type_map(map = type_map)
  %w[
    geography
    geometry
    geometry_collection
    line_string
    multi_line_string
    multi_point
    multi_polygon
    st_point
    st_polygon
  ].each do |geo_type|
    map.register_type(geo_type) do |_, _, sql_type|
      # sql_type is a string that comes from the database definition
      # examples:
      #   "geometry(Point,4326)"
      #   "geography(Point,4326)"
      #   "geometry(Polygon,4326) NOT NULL"
      #   "geometry(Geography,4326)"
      geo_type, srid, has_z, has_m, geographic = PostGIS::OID::Spatial.parse_sql_type(sql_type)
      PostGIS::OID::Spatial.new(geo_type: geo_type, srid: srid, has_z: has_z, has_m: has_m, geographic: geographic)
    end
  end

  super
end

.native_database_typesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_record/connection_adapters/postgis_adapter.rb', line 94

def native_database_types
  @native_database_types ||= begin
    default_types = PostgreSQLAdapter.native_database_types
    default_types.merge({
      geography:           { name: "geography" },
      geometry:            { name: "geometry" },
      geometry_collection: { name: "geometry_collection" },
      line_string:         { name: "line_string" },
      multi_line_string:   { name: "multi_line_string" },
      multi_point:         { name: "multi_point" },
      multi_polygon:       { name: "multi_polygon" },
      spatial:             { name: "geometry" },
      st_point:            { name: "st_point" },
      st_polygon:          { name: "st_polygon" }
    })
  end
end

.spatial_column_options(key) ⇒ Object



54
55
56
# File 'lib/active_record/connection_adapters/postgis_adapter.rb', line 54

def self.spatial_column_options(key)
  SPATIAL_COLUMN_OPTIONS[key]
end

Instance Method Details

#arel_visitorObject

:nodoc:



50
51
52
# File 'lib/active_record/connection_adapters/postgis_adapter.rb', line 50

def arel_visitor # :nodoc:
  Arel::Visitors::PostGIS.new(self)
end

#default_sridObject



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

def default_srid
  DEFAULT_SRID
end

#postgis_lib_versionObject



58
59
60
# File 'lib/active_record/connection_adapters/postgis_adapter.rb', line 58

def postgis_lib_version
  @postgis_lib_version ||= select_value("SELECT PostGIS_Lib_Version()")
end

#quote(value) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/active_record/connection_adapters/postgis_adapter.rb', line 122

def quote(value)
  if RGeo::Feature::Geometry.check_type(value)
    "'#{RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value)}'"
  elsif value.is_a?(RGeo::Cartesian::BoundingBox)
    "'#{value.min_x},#{value.min_y},#{value.max_x},#{value.max_y}'::box"
  else
    super
  end
end

#quote_default_expression(value, column) ⇒ Object



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

def quote_default_expression(value, column)
  if column.type == :geography || column.type == :geometry
    quote(value)
  else
    super
  end
end

#srs_database_columnsObject



113
114
115
116
117
118
119
120
# File 'lib/active_record/connection_adapters/postgis_adapter.rb', line 113

def srs_database_columns
  {
    auth_name_column: "auth_name",
    auth_srid_column: "auth_srid",
    proj4text_column: "proj4text",
    srtext_column:    "srtext",
  }
end