activerecord-postgres-earthdistance 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. data/.gitignore +21 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +4 -10
  5. data/Gemfile.lock +92 -69
  6. data/README.md +124 -0
  7. data/Rakefile +12 -40
  8. data/activerecord-postgres-earthdistance.gemspec +24 -117
  9. data/lib/activerecord-postgres-earthdistance.rb +5 -46
  10. data/lib/activerecord-postgres-earthdistance/activerecord.rb +9 -10
  11. data/lib/activerecord-postgres-earthdistance/acts_as_geolocated.rb +24 -0
  12. data/lib/activerecord-postgres-earthdistance/railties.rb +40 -0
  13. data/lib/templates/setup_earthdistance.rb +4 -427
  14. data/spec/act_as_geolocated_spec.rb +29 -0
  15. data/{test/app/app/models → spec/fixtures}/place.rb +0 -0
  16. data/spec/spec_helper.rb +46 -0
  17. metadata +67 -109
  18. data/README.rdoc +0 -17
  19. data/VERSION +0 -1
  20. data/test/app/.gitignore +0 -4
  21. data/test/app/Gemfile +0 -5
  22. data/test/app/Gemfile.lock +0 -83
  23. data/test/app/README +0 -256
  24. data/test/app/Rakefile +0 -7
  25. data/test/app/app/controllers/application_controller.rb +0 -3
  26. data/test/app/app/helpers/application_helper.rb +0 -2
  27. data/test/app/app/views/layouts/application.html.erb +0 -14
  28. data/test/app/config.ru +0 -4
  29. data/test/app/config/application.rb +0 -42
  30. data/test/app/config/boot.rb +0 -13
  31. data/test/app/config/database.yml +0 -25
  32. data/test/app/config/environment.rb +0 -5
  33. data/test/app/config/environments/development.rb +0 -26
  34. data/test/app/config/environments/production.rb +0 -49
  35. data/test/app/config/environments/test.rb +0 -35
  36. data/test/app/config/initializers/backtrace_silencers.rb +0 -7
  37. data/test/app/config/initializers/inflections.rb +0 -10
  38. data/test/app/config/initializers/mime_types.rb +0 -5
  39. data/test/app/config/initializers/secret_token.rb +0 -7
  40. data/test/app/config/initializers/session_store.rb +0 -8
  41. data/test/app/config/initializers/use_sql_schema.rb +0 -3
  42. data/test/app/config/locales/en.yml +0 -5
  43. data/test/app/config/routes.rb +0 -58
  44. data/test/app/db/development_structure.sql +0 -807
  45. data/test/app/db/migrate/20110225205046_setup_earthdistance.rb +0 -434
  46. data/test/app/db/migrate/20110225205131_create_places.rb +0 -15
  47. data/test/app/db/schema.rb +0 -24
  48. data/test/app/db/seeds.rb +0 -7
  49. data/test/app/doc/README_FOR_APP +0 -2
  50. data/test/app/lib/tasks/.gitkeep +0 -0
  51. data/test/app/public/404.html +0 -26
  52. data/test/app/public/422.html +0 -26
  53. data/test/app/public/500.html +0 -26
  54. data/test/app/public/favicon.ico +0 -0
  55. data/test/app/public/images/rails.png +0 -0
  56. data/test/app/public/index.html +0 -239
  57. data/test/app/public/javascripts/application.js +0 -2
  58. data/test/app/public/javascripts/controls.js +0 -965
  59. data/test/app/public/javascripts/dragdrop.js +0 -974
  60. data/test/app/public/javascripts/effects.js +0 -1123
  61. data/test/app/public/javascripts/prototype.js +0 -6001
  62. data/test/app/public/javascripts/rails.js +0 -175
  63. data/test/app/public/robots.txt +0 -5
  64. data/test/app/public/stylesheets/.gitkeep +0 -0
  65. data/test/app/script/rails +0 -6
  66. data/test/app/test/performance/browsing_test.rb +0 -9
  67. data/test/app/test/test_helper.rb +0 -13
  68. data/test/app/test/unit/place_test.rb +0 -27
  69. data/test/app/vendor/plugins/.gitkeep +0 -0
@@ -1,48 +1,7 @@
1
- require 'rails'
2
- require 'rails/generators'
3
- require 'rails/generators/migration'
1
+ require 'active_support'
4
2
 
5
- # = Earth Distance Railtie
6
- #
7
- # Creates a new railtie for 2 reasons:
8
- #
9
- # * Initialize ActiveRecord properly
10
- # * Add earthdistance:setup generator
11
- class EarthDistance < Rails::Railtie
12
-
13
- initializer 'activerecord-postgres-earthdistance' do
14
- ActiveSupport.on_load :active_record do
15
- require "activerecord-postgres-earthdistance/activerecord"
16
- end
17
- end
18
-
19
- # Creates the earthdistance:setup generator. This generator creates a migration that
20
- # adds earthdistance support for your database. If fact, it's just the sql from the
21
- # contrib inside a migration. But it' s handy, isn't it?
22
- #
23
- # To use your generator, simply run it in your project:
24
- #
25
- # rails g earthdistance:setup
26
- class Setup < Rails::Generators::Base
27
- include Rails::Generators::Migration
28
-
29
- def self.source_root
30
- @source_root ||= File.join(File.dirname(__FILE__), 'templates')
31
- end
32
-
33
- def self.next_migration_number(dirname)
34
- if ActiveRecord::Base.timestamped_migrations
35
- Time.now.utc.strftime("%Y%m%d%H%M%S")
36
- else
37
- "%.3d" % (current_migration_number(dirname) + 1)
38
- end
39
- end
40
-
41
- def create_migration_file
42
- migration_template 'setup_earthdistance.rb', 'db/migrate/setup_earthdistance.rb'
43
- end
44
-
45
- end
3
+ ActiveSupport.on_load :active_record do
4
+ require "activerecord-postgres-earthdistance/activerecord"
46
5
  end
47
- # require "activerecord-postgres-earthdistance/string"
48
- # require "activerecord-postgres-earthdistance/hash"
6
+
7
+ require "activerecord-postgres-earthdistance/railties" if defined? Rails
@@ -1,16 +1,15 @@
1
1
  # Extends AR to add earthdistance functionality.
2
+ require "activerecord-postgres-earthdistance/acts_as_geolocated"
2
3
  module ActiveRecord
3
- class Base
4
- def self.acts_as_geolocated(options = {}, distances = {})
5
- @@latitude_column = options[:lat]
6
- @@longitude_column = options[:lng]
7
- @@latitude_column = (column_names.include?("lat") ? "lat" : "latitude") unless @@latitude_column
8
- @@longitude_column = (column_names.include?("lng") ? "lng" : "longitude") unless @@longitude_column
4
+ module ConnectionAdapters
5
+ module SchemaStatements
9
6
 
10
- def self.within_radius radius, lat, lng, unit = :meters
11
- where(["ll_to_earth(#{@@latitude_column}, #{@@longitude_column}) <@ earth_box(ll_to_earth(?, ?), ?)
12
- AND earth_distance(ll_to_earth(#{@@latitude_column}, #{@@longitude_column}), ll_to_earth(?, ?)) <= ?",
13
- lat, lng, radius, lat, lng, radius])
7
+ # Installs hstore by creating the Postgres extension
8
+ # if it does not exist
9
+ #
10
+ def add_earthdistance_index table_name, options = {}
11
+ execute "CREATE INDEX %s_earthdistance_ix ON %s USING gist (ll_to_earth(%s, %s));" %
12
+ [table_name, table_name, (options[:lat] || 'lat'), (options[:lng] || 'lng')]
14
13
  end
15
14
  end
16
15
  end
@@ -0,0 +1,24 @@
1
+ module ActiveRecordPostgresEarthdistance
2
+ module ActsAsGeolocated
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_geolocated(options = {})
10
+ cattr_accessor :latitude_column, :longitude_column
11
+ self.latitude_column = options[:lat] || (column_names.include?("lat") ? "lat" : "latitude")
12
+ self.longitude_column = options[:lng] || (column_names.include?("lng") ? "lng" : "longitude")
13
+ end
14
+
15
+ def within_radius radius, lat, lng
16
+ where(["ll_to_earth(#{self.latitude_column}, #{self.longitude_column}) <@ earth_box(ll_to_earth(?, ?), ?)" +
17
+ "AND earth_distance(ll_to_earth(#{self.latitude_column}, #{self.longitude_column}), ll_to_earth(?, ?)) <= ?",
18
+ lat, lng, radius, lat, lng, radius])
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ ActiveRecord::Base.send :include, ActiveRecordPostgresEarthdistance::ActsAsGeolocated
@@ -0,0 +1,40 @@
1
+ require 'rails'
2
+ require 'rails/generators'
3
+ require 'rails/generators/migration'
4
+
5
+ # = Earth Distance Railtie
6
+ #
7
+ # Creates a new railtie for 2 reasons:
8
+ #
9
+ # * Initialize ActiveRecord properly
10
+ # * Add earthdistance:setup generator
11
+ class EarthDistance < Rails::Railtie
12
+ # Creates the earthdistance:setup generator. This generator creates a migration that
13
+ # adds earthdistance support for your database. If fact, it's just the sql from the
14
+ # contrib inside a migration. But it' s handy, isn't it?
15
+ #
16
+ # To use your generator, simply run it in your project:
17
+ #
18
+ # rails g earthdistance:setup
19
+ class Setup < Rails::Generators::Base
20
+ include Rails::Generators::Migration
21
+
22
+ def self.source_root
23
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
24
+ end
25
+
26
+ def self.next_migration_number(dirname)
27
+ if ActiveRecord::Base.timestamped_migrations
28
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
29
+ else
30
+ "%.3d" % (current_migration_number(dirname) + 1)
31
+ end
32
+ end
33
+
34
+ def create_migration_file
35
+ migration_template 'setup_earthdistance.rb', 'db/migrate/setup_earthdistance.rb'
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -1,434 +1,11 @@
1
1
  class SetupEarthdistance < ActiveRecord::Migration
2
2
  def self.up
3
- sql = <<-EARTHDISTANCE_SQL
4
- /* $PostgreSQL: pgsql/contrib/cube/cube.sql.in,v 1.25 2009/06/11 18:30:03 tgl Exp $ */
5
-
6
- -- Adjust this setting to control where the objects get created.
7
- SET search_path = public;
8
-
9
- -- Create the user-defined type for N-dimensional boxes
10
- --
11
-
12
- CREATE OR REPLACE FUNCTION cube_in(cstring)
13
- RETURNS cube
14
- AS '$libdir/cube'
15
- LANGUAGE C IMMUTABLE STRICT;
16
-
17
- CREATE OR REPLACE FUNCTION cube(float8[], float8[]) RETURNS cube
18
- AS '$libdir/cube', 'cube_a_f8_f8'
19
- LANGUAGE C IMMUTABLE STRICT;
20
-
21
- CREATE OR REPLACE FUNCTION cube(float8[]) RETURNS cube
22
- AS '$libdir/cube', 'cube_a_f8'
23
- LANGUAGE C IMMUTABLE STRICT;
24
-
25
- CREATE OR REPLACE FUNCTION cube_out(cube)
26
- RETURNS cstring
27
- AS '$libdir/cube'
28
- LANGUAGE C IMMUTABLE STRICT;
29
-
30
- CREATE TYPE cube (
31
- INTERNALLENGTH = variable,
32
- INPUT = cube_in,
33
- OUTPUT = cube_out,
34
- ALIGNMENT = double
35
- );
36
-
37
- COMMENT ON TYPE cube IS 'multi-dimensional cube ''(FLOAT-1, FLOAT-2, ..., FLOAT-N), (FLOAT-1, FLOAT-2, ..., FLOAT-N)''';
38
-
39
- --
40
- -- External C-functions for R-tree methods
41
- --
42
-
43
- -- Comparison methods
44
-
45
- CREATE OR REPLACE FUNCTION cube_eq(cube, cube)
46
- RETURNS bool
47
- AS '$libdir/cube'
48
- LANGUAGE C IMMUTABLE STRICT;
49
-
50
- COMMENT ON FUNCTION cube_eq(cube, cube) IS 'same as';
51
-
52
- CREATE OR REPLACE FUNCTION cube_ne(cube, cube)
53
- RETURNS bool
54
- AS '$libdir/cube'
55
- LANGUAGE C IMMUTABLE STRICT;
56
-
57
- COMMENT ON FUNCTION cube_ne(cube, cube) IS 'different';
58
-
59
- CREATE OR REPLACE FUNCTION cube_lt(cube, cube)
60
- RETURNS bool
61
- AS '$libdir/cube'
62
- LANGUAGE C IMMUTABLE STRICT;
63
-
64
- COMMENT ON FUNCTION cube_lt(cube, cube) IS 'lower than';
65
-
66
- CREATE OR REPLACE FUNCTION cube_gt(cube, cube)
67
- RETURNS bool
68
- AS '$libdir/cube'
69
- LANGUAGE C IMMUTABLE STRICT;
70
-
71
- COMMENT ON FUNCTION cube_gt(cube, cube) IS 'greater than';
72
-
73
- CREATE OR REPLACE FUNCTION cube_le(cube, cube)
74
- RETURNS bool
75
- AS '$libdir/cube'
76
- LANGUAGE C IMMUTABLE STRICT;
77
-
78
- COMMENT ON FUNCTION cube_le(cube, cube) IS 'lower than or equal to';
79
-
80
- CREATE OR REPLACE FUNCTION cube_ge(cube, cube)
81
- RETURNS bool
82
- AS '$libdir/cube'
83
- LANGUAGE C IMMUTABLE STRICT;
84
-
85
- COMMENT ON FUNCTION cube_ge(cube, cube) IS 'greater than or equal to';
86
-
87
- CREATE OR REPLACE FUNCTION cube_cmp(cube, cube)
88
- RETURNS int4
89
- AS '$libdir/cube'
90
- LANGUAGE C IMMUTABLE STRICT;
91
-
92
- COMMENT ON FUNCTION cube_cmp(cube, cube) IS 'btree comparison function';
93
-
94
- CREATE OR REPLACE FUNCTION cube_contains(cube, cube)
95
- RETURNS bool
96
- AS '$libdir/cube'
97
- LANGUAGE C IMMUTABLE STRICT;
98
-
99
- COMMENT ON FUNCTION cube_contains(cube, cube) IS 'contains';
100
-
101
- CREATE OR REPLACE FUNCTION cube_contained(cube, cube)
102
- RETURNS bool
103
- AS '$libdir/cube'
104
- LANGUAGE C IMMUTABLE STRICT;
105
-
106
- COMMENT ON FUNCTION cube_contained(cube, cube) IS 'contained in';
107
-
108
- CREATE OR REPLACE FUNCTION cube_overlap(cube, cube)
109
- RETURNS bool
110
- AS '$libdir/cube'
111
- LANGUAGE C IMMUTABLE STRICT;
112
-
113
- COMMENT ON FUNCTION cube_overlap(cube, cube) IS 'overlaps';
114
-
115
- -- support routines for indexing
116
-
117
- CREATE OR REPLACE FUNCTION cube_union(cube, cube)
118
- RETURNS cube
119
- AS '$libdir/cube'
120
- LANGUAGE C IMMUTABLE STRICT;
121
-
122
- CREATE OR REPLACE FUNCTION cube_inter(cube, cube)
123
- RETURNS cube
124
- AS '$libdir/cube'
125
- LANGUAGE C IMMUTABLE STRICT;
126
-
127
- CREATE OR REPLACE FUNCTION cube_size(cube)
128
- RETURNS float8
129
- AS '$libdir/cube'
130
- LANGUAGE C IMMUTABLE STRICT;
131
-
132
-
133
- -- Misc N-dimensional functions
134
-
135
- CREATE OR REPLACE FUNCTION cube_subset(cube, int4[])
136
- RETURNS cube
137
- AS '$libdir/cube'
138
- LANGUAGE C IMMUTABLE STRICT;
139
-
140
- -- proximity routines
141
-
142
- CREATE OR REPLACE FUNCTION cube_distance(cube, cube)
143
- RETURNS float8
144
- AS '$libdir/cube'
145
- LANGUAGE C IMMUTABLE STRICT;
146
-
147
- -- Extracting elements functions
148
-
149
- CREATE OR REPLACE FUNCTION cube_dim(cube)
150
- RETURNS int4
151
- AS '$libdir/cube'
152
- LANGUAGE C IMMUTABLE STRICT;
153
-
154
- CREATE OR REPLACE FUNCTION cube_ll_coord(cube, int4)
155
- RETURNS float8
156
- AS '$libdir/cube'
157
- LANGUAGE C IMMUTABLE STRICT;
158
-
159
- CREATE OR REPLACE FUNCTION cube_ur_coord(cube, int4)
160
- RETURNS float8
161
- AS '$libdir/cube'
162
- LANGUAGE C IMMUTABLE STRICT;
163
-
164
- CREATE OR REPLACE FUNCTION cube(float8) RETURNS cube
165
- AS '$libdir/cube', 'cube_f8'
166
- LANGUAGE C IMMUTABLE STRICT;
167
-
168
- CREATE OR REPLACE FUNCTION cube(float8, float8) RETURNS cube
169
- AS '$libdir/cube', 'cube_f8_f8'
170
- LANGUAGE C IMMUTABLE STRICT;
171
-
172
- CREATE OR REPLACE FUNCTION cube(cube, float8) RETURNS cube
173
- AS '$libdir/cube', 'cube_c_f8'
174
- LANGUAGE C IMMUTABLE STRICT;
175
-
176
- CREATE OR REPLACE FUNCTION cube(cube, float8, float8) RETURNS cube
177
- AS '$libdir/cube', 'cube_c_f8_f8'
178
- LANGUAGE C IMMUTABLE STRICT;
179
-
180
- -- Test if cube is also a point
181
-
182
- CREATE OR REPLACE FUNCTION cube_is_point(cube)
183
- RETURNS bool
184
- AS '$libdir/cube'
185
- LANGUAGE C IMMUTABLE STRICT;
186
-
187
- -- Increasing the size of a cube by a radius in at least n dimensions
188
-
189
- CREATE OR REPLACE FUNCTION cube_enlarge(cube, float8, int4)
190
- RETURNS cube
191
- AS '$libdir/cube'
192
- LANGUAGE C IMMUTABLE STRICT;
193
-
194
- --
195
- -- OPERATORS
196
- --
197
-
198
- CREATE OPERATOR < (
199
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_lt,
200
- COMMUTATOR = '>', NEGATOR = '>=',
201
- RESTRICT = scalarltsel, JOIN = scalarltjoinsel
202
- );
203
-
204
- CREATE OPERATOR > (
205
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_gt,
206
- COMMUTATOR = '<', NEGATOR = '<=',
207
- RESTRICT = scalargtsel, JOIN = scalargtjoinsel
208
- );
209
-
210
- CREATE OPERATOR <= (
211
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_le,
212
- COMMUTATOR = '>=', NEGATOR = '>',
213
- RESTRICT = scalarltsel, JOIN = scalarltjoinsel
214
- );
215
-
216
- CREATE OPERATOR >= (
217
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_ge,
218
- COMMUTATOR = '<=', NEGATOR = '<',
219
- RESTRICT = scalargtsel, JOIN = scalargtjoinsel
220
- );
221
-
222
- CREATE OPERATOR && (
223
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_overlap,
224
- COMMUTATOR = '&&',
225
- RESTRICT = areasel, JOIN = areajoinsel
226
- );
227
-
228
- CREATE OPERATOR = (
229
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_eq,
230
- COMMUTATOR = '=', NEGATOR = '<>',
231
- RESTRICT = eqsel, JOIN = eqjoinsel,
232
- MERGES
233
- );
234
-
235
- CREATE OPERATOR <> (
236
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_ne,
237
- COMMUTATOR = '<>', NEGATOR = '=',
238
- RESTRICT = neqsel, JOIN = neqjoinsel
239
- );
240
-
241
- CREATE OPERATOR @> (
242
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_contains,
243
- COMMUTATOR = '<@',
244
- RESTRICT = contsel, JOIN = contjoinsel
245
- );
246
-
247
- CREATE OPERATOR <@ (
248
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_contained,
249
- COMMUTATOR = '@>',
250
- RESTRICT = contsel, JOIN = contjoinsel
251
- );
252
-
253
- -- these are obsolete/deprecated:
254
- CREATE OPERATOR @ (
255
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_contains,
256
- COMMUTATOR = '~',
257
- RESTRICT = contsel, JOIN = contjoinsel
258
- );
259
-
260
- CREATE OPERATOR ~ (
261
- LEFTARG = cube, RIGHTARG = cube, PROCEDURE = cube_contained,
262
- COMMUTATOR = '@',
263
- RESTRICT = contsel, JOIN = contjoinsel
264
- );
265
-
266
-
267
- -- define the GiST support methods
268
- CREATE OR REPLACE FUNCTION g_cube_consistent(internal,cube,int,oid,internal)
269
- RETURNS bool
270
- AS '$libdir/cube'
271
- LANGUAGE C IMMUTABLE STRICT;
272
-
273
- CREATE OR REPLACE FUNCTION g_cube_compress(internal)
274
- RETURNS internal
275
- AS '$libdir/cube'
276
- LANGUAGE C IMMUTABLE STRICT;
277
-
278
- CREATE OR REPLACE FUNCTION g_cube_decompress(internal)
279
- RETURNS internal
280
- AS '$libdir/cube'
281
- LANGUAGE C IMMUTABLE STRICT;
282
-
283
- CREATE OR REPLACE FUNCTION g_cube_penalty(internal,internal,internal)
284
- RETURNS internal
285
- AS '$libdir/cube'
286
- LANGUAGE C IMMUTABLE STRICT;
287
-
288
- CREATE OR REPLACE FUNCTION g_cube_picksplit(internal, internal)
289
- RETURNS internal
290
- AS '$libdir/cube'
291
- LANGUAGE C IMMUTABLE STRICT;
292
-
293
- CREATE OR REPLACE FUNCTION g_cube_union(internal, internal)
294
- RETURNS cube
295
- AS '$libdir/cube'
296
- LANGUAGE C IMMUTABLE STRICT;
297
-
298
- CREATE OR REPLACE FUNCTION g_cube_same(cube, cube, internal)
299
- RETURNS internal
300
- AS '$libdir/cube'
301
- LANGUAGE C IMMUTABLE STRICT;
302
-
303
-
304
- -- Create the operator classes for indexing
305
-
306
- CREATE OPERATOR CLASS cube_ops
307
- DEFAULT FOR TYPE cube USING btree AS
308
- OPERATOR 1 < ,
309
- OPERATOR 2 <= ,
310
- OPERATOR 3 = ,
311
- OPERATOR 4 >= ,
312
- OPERATOR 5 > ,
313
- FUNCTION 1 cube_cmp(cube, cube);
314
-
315
- CREATE OPERATOR CLASS gist_cube_ops
316
- DEFAULT FOR TYPE cube USING gist AS
317
- OPERATOR 3 && ,
318
- OPERATOR 6 = ,
319
- OPERATOR 7 @> ,
320
- OPERATOR 8 <@ ,
321
- OPERATOR 13 @ ,
322
- OPERATOR 14 ~ ,
323
- FUNCTION 1 g_cube_consistent (internal, cube, int, oid, internal),
324
- FUNCTION 2 g_cube_union (internal, internal),
325
- FUNCTION 3 g_cube_compress (internal),
326
- FUNCTION 4 g_cube_decompress (internal),
327
- FUNCTION 5 g_cube_penalty (internal, internal, internal),
328
- FUNCTION 6 g_cube_picksplit (internal, internal),
329
- FUNCTION 7 g_cube_same (cube, cube, internal);
330
-
331
-
332
-
333
- /* $PostgreSQL: pgsql/contrib/earthdistance/earthdistance.sql.in,v 1.11 2007/11/13 04:24:27 momjian Exp $ */
334
-
335
- -- Adjust this setting to control where the objects get created.
336
- SET search_path = public;
337
-
338
- -- The earth functions rely on contrib/cube having been installed and loaded.
339
-
340
- -- earth() returns the radius of the earth in meters. This is the only
341
- -- place you need to change things for the cube base distance functions
342
- -- in order to use different units (or a better value for the Earth's radius).
343
-
344
- CREATE OR REPLACE FUNCTION earth() RETURNS float8
345
- LANGUAGE SQL IMMUTABLE
346
- AS 'SELECT ''6378168''::float8';
347
-
348
- -- Astromers may want to change the earth function so that distances will be
349
- -- returned in degrees. To do this comment out the above definition and
350
- -- uncomment the one below. Note that doing this will break the regression
351
- -- tests.
352
- --
353
- -- CREATE OR REPLACE FUNCTION earth() RETURNS float8
354
- -- LANGUAGE SQL IMMUTABLE
355
- -- AS 'SELECT 180/pi()';
356
-
357
- -- Define domain for locations on the surface of the earth using a cube
358
- -- datatype with constraints. cube provides 3D indexing.
359
- -- The cube is restricted to be a point, no more than 3 dimensions
360
- -- (for less than 3 dimensions 0 is assumed for the missing coordinates)
361
- -- and that the point must be very near the surface of the sphere
362
- -- centered about the origin with the radius of the earth.
363
-
364
- CREATE DOMAIN earth AS cube
365
- CONSTRAINT not_point check(cube_is_point(value))
366
- CONSTRAINT not_3d check(cube_dim(value) <= 3)
367
- CONSTRAINT on_surface check(abs(cube_distance(value, '(0)'::cube) /
368
- earth() - 1) < '10e-7'::float8);
369
-
370
- CREATE OR REPLACE FUNCTION sec_to_gc(float8)
371
- RETURNS float8
372
- LANGUAGE SQL
373
- IMMUTABLE STRICT
374
- AS 'SELECT CASE WHEN $1 < 0 THEN 0::float8 WHEN $1/(2*earth()) > 1 THEN pi()*earth() ELSE 2*earth()*asin($1/(2*earth())) END';
375
-
376
- CREATE OR REPLACE FUNCTION gc_to_sec(float8)
377
- RETURNS float8
378
- LANGUAGE SQL
379
- IMMUTABLE STRICT
380
- AS 'SELECT CASE WHEN $1 < 0 THEN 0::float8 WHEN $1/earth() > pi() THEN 2*earth() ELSE 2*earth()*sin($1/(2*earth())) END';
381
-
382
- CREATE OR REPLACE FUNCTION ll_to_earth(float8, float8)
383
- RETURNS earth
384
- LANGUAGE SQL
385
- IMMUTABLE STRICT
386
- AS 'SELECT cube(cube(cube(earth()*cos(radians($1))*cos(radians($2))),earth()*cos(radians($1))*sin(radians($2))),earth()*sin(radians($1)))::earth';
387
-
388
- CREATE OR REPLACE FUNCTION latitude(earth)
389
- RETURNS float8
390
- LANGUAGE SQL
391
- IMMUTABLE STRICT
392
- AS 'SELECT CASE WHEN cube_ll_coord($1, 3)/earth() < -1 THEN -90::float8 WHEN cube_ll_coord($1, 3)/earth() > 1 THEN 90::float8 ELSE degrees(asin(cube_ll_coord($1, 3)/earth())) END';
393
-
394
- CREATE OR REPLACE FUNCTION longitude(earth)
395
- RETURNS float8
396
- LANGUAGE SQL
397
- IMMUTABLE STRICT
398
- AS 'SELECT degrees(atan2(cube_ll_coord($1, 2), cube_ll_coord($1, 1)))';
399
-
400
- CREATE OR REPLACE FUNCTION earth_distance(earth, earth)
401
- RETURNS float8
402
- LANGUAGE SQL
403
- IMMUTABLE STRICT
404
- AS 'SELECT sec_to_gc(cube_distance($1, $2))';
405
-
406
- CREATE OR REPLACE FUNCTION earth_box(earth, float8)
407
- RETURNS cube
408
- LANGUAGE SQL
409
- IMMUTABLE STRICT
410
- AS 'SELECT cube_enlarge($1, gc_to_sec($2), 3)';
411
-
412
- --------------- geo_distance
413
-
414
- CREATE OR REPLACE FUNCTION geo_distance (point, point)
415
- RETURNS float8
416
- LANGUAGE C IMMUTABLE STRICT AS '$libdir/earthdistance';
417
-
418
- --------------- geo_distance as operator <@>
419
-
420
- CREATE OPERATOR <@> (
421
- LEFTARG = point,
422
- RIGHTARG = point,
423
- PROCEDURE = geo_distance,
424
- COMMUTATOR = <@>
425
- );
426
-
427
- EARTHDISTANCE_SQL
428
- execute sql
3
+ execute "CREATE EXTENSION IF NOT EXISTS cube"
4
+ execute "CREATE EXTENSION IF NOT EXISTS earthdistance"
429
5
  end
430
6
 
431
7
  def self.down
432
- # Kinda... hard.
8
+ execute "DROP EXTENSION IF EXISTS earthdistance"
9
+ execute "DROP EXTENSION IF EXISTS cube"
433
10
  end
434
11
  end