-
Notifications
You must be signed in to change notification settings - Fork 222
/
postgis_adapter.rb
195 lines (171 loc) · 5.67 KB
/
postgis_adapter.rb
1
2
3
4
5
6
7
8
9
10
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
60
61
62
63
64
65
66
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
93
94
95
96
97
98
99
100
101
102
103
104
105
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# frozen_string_literal: true
# The activerecord-postgis-adapter gem installs the *postgis*
# connection adapter into ActiveRecord.
# :stopdoc:
require "rgeo/active_record"
require "active_record/connection_adapters"
require "active_record/connection_adapters/postgresql_adapter"
require_relative "postgis/version"
require_relative "postgis/column_methods"
require_relative "postgis/schema_statements"
require_relative "postgis/database_statements"
require_relative "postgis/spatial_column_info"
require_relative "postgis/spatial_table_definition"
require_relative "postgis/spatial_column"
require_relative "postgis/arel_tosql"
require_relative "postgis/oid/spatial"
require_relative "postgis/oid/date_time"
require_relative "postgis/type" # has to be after oid/*
require_relative "postgis/create_connection"
# :startdoc:
module ActiveRecord
module ConnectionHandling # :nodoc:
def postgis_adapter_class
ConnectionAdapters::PostGISAdapter
end
def postgis_connection(config)
postgis_adapter_class.new(config)
end
end
module ConnectionAdapters
class PostGISAdapter < PostgreSQLAdapter
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: {},
}
# http://postgis.17.x6.nabble.com/Default-SRID-td5001115.html
DEFAULT_SRID = 0
include PostGIS::SchemaStatements
include PostGIS::DatabaseStatements
def arel_visitor # :nodoc:
Arel::Visitors::PostGIS.new(self)
end
def self.spatial_column_options(key)
SPATIAL_COLUMN_OPTIONS[key]
end
def postgis_lib_version
@postgis_lib_version ||= select_value("SELECT PostGIS_Lib_Version()")
end
def default_srid
DEFAULT_SRID
end
class << self
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
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
end
def srs_database_columns
{
auth_name_column: "auth_name",
auth_srid_column: "auth_srid",
proj4text_column: "proj4text",
srtext_column: "srtext",
}
end
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
def quote_default_expression(value, column)
if column.type == :geography || column.type == :geometry
quote(value)
else
super
end
end
# PostGIS specific types
[
:geography,
:geometry,
:geometry_collection,
:line_string,
:multi_line_string,
:multi_point,
:multi_polygon,
:st_point,
:st_polygon,
].each do |geo_type|
ActiveRecord::Type.register(geo_type, PostGIS::OID::Spatial, adapter: :postgis)
end
end
end
SchemaDumper.ignore_tables |= %w[
geography_columns
geometry_columns
layer
raster_columns
raster_overviews
spatial_ref_sys
topology
]
Tasks::DatabaseTasks.register_task(/postgis/, "ActiveRecord::Tasks::PostgreSQLDatabaseTasks")
end
# if using JRUBY, create ArJdbc::PostGIS module
# and prepend it to the PostgreSQL adapter since
# it is the default adapter_spec.
# see: https://github.com/jruby/activerecord-jdbc-adapter/blob/master/lib/arjdbc/postgresql/adapter.rb#27
if RUBY_ENGINE == "jruby"
module ArJdbc
module PostGIS
ADAPTER_NAME = 'PostGIS'
def adapter_name
ADAPTER_NAME
end
end
end
ArJdbc::PostgreSQL.prepend(ArJdbc::PostGIS)
end