the_geom_geojson 0.0.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b923b316a16ae77ca598e803b10757e992adba8
4
- data.tar.gz: e70e6840959521ef9375a61754d4290105cf4faa
3
+ metadata.gz: b0cb34abb081e5fdf3f64760adc9b8ed2a4b2690
4
+ data.tar.gz: c370e1e32937b9bdf8b0f668dfdea446f224df75
5
5
  SHA512:
6
- metadata.gz: 017a2f05ac92ad2d8722896f613f39066c77b95c445ad844c987a85c2705aa136e0510698112aa606bc29480de947357f2eb4a718c94c86a922fafefe29d1485
7
- data.tar.gz: 247a8189a55c71b300e2b8f568c026302dda564da710e9bf2d6d839495bd51231a91a4ebf6f5058f5c2934415177350ab69bde16579e91aa3794e60566eed6f1
6
+ metadata.gz: 7286bb6c4c26277a6e18a5c2598e8068e4e1057ad569e820cfa625e890263a6801866912ecf1d423b170908b1bbeb30514681c7c890de5135f004375ccf58f70
7
+ data.tar.gz: 43525ed84188d6306b38803a783912397b5260caf39781d3bebf6659e8d75b324b76dd218d0459dd631f1ceec34d7c21ef39202263aedff4b5e8ae2e6f29e6f3
data/CHANGELOG CHANGED
@@ -1,3 +1,40 @@
1
+ 0.1.0 / 2014-10-19
2
+
3
+ * Breaking changes
4
+
5
+ * Provide .with_geojson scope for faster #the_geom_geojson accesses (RGeo is WAY slower than ST_AsGeoJSON)
6
+
7
+ * Enhancements
8
+
9
+ * Much faster than 0.0.5 if you use .with_geojson scope; unlike 0.0.4, works even if you don't
10
+ * Opportunistically use read_attribute(:the_geom_geojson) if the user has included it in their SELECT
11
+ * Provide TheGeomGeoJSON.ewkb_to_geojson(ewkb)
12
+ * Warn when EWKB->GeoJSON parsing is taking too long
13
+
14
+ 0.0.5 / 2014-10-17
15
+
16
+ * Enhancements
17
+
18
+ * Don't go to PostGIS database to convert WKB->GeoJSON, use rubyland RGeo instead.
19
+
20
+ 0.0.4 / 2014-10-09
21
+
22
+ * Enhancements
23
+
24
+ * Define example polygons in a clockwise direction to ensure compatibility with D3 3.0
25
+
26
+ 0.0.3 / 2014-10-08
27
+
28
+ * Enhancements
29
+
30
+ * Simplify geojson examples - thanks @ihough https://github.com/seamusabshere/the_geom_geojson/pull/1 !
31
+
32
+ 0.0.2 / 2014-07-22
33
+
34
+ * Enhancements
35
+
36
+ * Support alternate primary keys set on ActiveRecord with `self.primary_key = X`
37
+
1
38
  0.0.1 / 2014-07-17
2
39
 
3
40
  initial release!
data/README.md CHANGED
@@ -6,6 +6,8 @@ For PostGIS/PostgreSQL and ActiveRecord, provides `the_geom_geojson` getter and
6
6
 
7
7
  Web mapping libraries like [Leaflet](http://leafletjs.com/) often don't support PostGIS's native [Well-Known Binary (WKB) and Well-Known Text (WKT)](http://postgis.net/docs/using_postgis_dbmanagement.html#OpenGISWKBWKT) representation, but they do support [GeoJSON](http://geojson.org/), so this library helps translate between the two.
8
8
 
9
+ <img src="https://www.nyu.edu/greyart/exhibits/cisneros/images/garcianew.jpg" alt="Composición constructiva 16" />
10
+
9
11
  ## Requirements
10
12
 
11
13
  * [PostgreSQL](postgresql.org) >=9
@@ -3,6 +3,8 @@ module TheGeomGeoJSON
3
3
  class << self
4
4
  def included(model)
5
5
  model.class_eval do
6
+ scope :with_geojson, -> { select('*', 'ST_AsGeoJSON(the_geom) AS the_geom_geojson') }
7
+
6
8
  after_save do
7
9
  if @the_geom_geojson_dirty
8
10
  raise "can't update the_geom without an id" if id.nil?
@@ -27,7 +29,7 @@ module TheGeomGeoJSON
27
29
  memo << 'the_geom = ST_SetSRID(ST_GeomFromGeoJSON(?), 4326)' if has_the_geom
28
30
  memo << ',' if has_the_geom && has_the_geom_webmercator
29
31
  memo << 'the_geom_webmercator = ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON(?), 4326), 3857)' if has_the_geom_webmercator
30
- memo << ' WHERE id = ?'
32
+ memo << " WHERE #{model.quoted_primary_key} = ?"
31
33
  memo.join.freeze
32
34
  end)
33
35
  model.send :sanitize_sql_array, [sql, the_geom_geojson, the_geom_geojson, id]
@@ -42,14 +44,19 @@ module TheGeomGeoJSON
42
44
  @the_geom_geojson_change = v
43
45
  end
44
46
 
45
- # warning inefficient!
47
+
46
48
  def the_geom_geojson
47
49
  if @the_geom_geojson_dirty
48
50
  @the_geom_geojson_change
49
- elsif id
50
- self.class.connection_pool.with_connection do |c|
51
- c.select_value "SELECT ST_AsGeoJSON(the_geom) FROM #{self.class.quoted_table_name} WHERE id = #{c.quote(id)} LIMIT 1"
51
+ elsif preselected = read_attribute(:the_geom_geojson)
52
+ preselected
53
+ elsif the_geom
54
+ started_at = Time.now
55
+ memo = TheGeomGeoJSON.ewkb_to_geojson the_geom
56
+ if (elapsed = Time.now - started_at) > 0.1
57
+ $stderr.puts "[the_geom_geojson] EWKB->GeoJSON parsing took #{elapsed}s, recommend using #{self.class.name}.with_geojson scope"
52
58
  end
59
+ memo
53
60
  end
54
61
  end
55
62
 
@@ -1,16 +1,15 @@
1
1
  module TheGeomGeoJSON
2
2
  EXAMPLES = {
3
- barre_montp_metro_polygon: '{"type":"Polygon","coordinates":[[[-72.2289400021792,44.4269414217094],[-72.3678968361699,44.2041095212377],[-72.4425771306298,44.1287229154304],[-72.5888838521079,44.1603344756557],[-72.683137316234,44.013279134225],[-72.9163355643347,44.0719380666887],[-72.9109606402212,44.2818163689305],[-72.8045593317476,44.4517204276558],[-72.6539563860782,44.3982706050046],[-72.5963646467109,44.4793368998783],[-72.4901804489683,44.4257596612796],[-72.4338671543455,44.5061428140398],[-72.2289400021792,44.4269414217094]]]}',
4
- barre_point: '{"type":"Point","coordinates":[-72.4861,44.1853]}',
5
- burlington: '{"type":"Polygon","coordinates":[[[-73.2544326782227,44.4952808693111],[-73.2623291015625,44.4685812127015],[-73.1936645507812,44.4494675360069],[-73.1428527832031,44.4636808664498],[-73.143196105957,44.4869538473269],[-73.2544326782227,44.4952808693111]]]}',
6
- burlington_metro: '{"type":"Polygon","coordinates":[[[-73.2230186462402,44.4918522395362],[-73.2298851013184,44.4622106823564],[-73.2110023498535,44.4399083513046],[-73.1461143493652,44.4478744471836],[-73.1716918945312,44.4902603071915],[-73.2230186462402,44.4918522395362]]]}',
7
- burlington_point: '{"type":"Point","coordinates":[-73.1936,44.4775]}',
8
- downtown_burlington: '{"type":"Polygon","coordinates":[[[-73.2180404663086,44.4876886323838],[-73.2206153869629,44.4734811475783],[-73.2110023498535,44.4668661383073],[-73.2032775878906,44.4759309607507],[-73.2032775878906,44.4827898905016],[-73.2180404663086,44.4876886323838]]]}',
9
- downtown_burlington_point: '{"type":"Point","coordinates":[-73.2101440429688,44.4762984238562]}',
10
- montreal: '{"type":"Polygon","coordinates":[[[-74.0643310546875,45.6946698435472],[-74.06982421875,45.3405631388986],[-73.67431640625,45.2284805958436],[-73.23486328125,45.3251166433327],[-73.2568359375,45.6140374113509],[-73.641357421875,45.775186183521],[-74.0643310546875,45.6946698435472]]]}',
11
- northampton_ma_point: '{"type":"Point","coordinates":[-72.6381,42.318]}',
12
- south_burlington: '{"type":"Polygon","coordinates":[[[-73.1759834289551,44.4743385938871],[-73.1826782226562,44.4607404612394],[-73.1629371643066,44.4563295757472],[-73.1550407409668,44.4667436310651],[-73.1698036193848,44.4738486261104],[-73.1759834289551,44.4743385938871]]]}',
13
- south_burlington_point: '{"type":"Point","coordinates":[-73.1708335876465,44.4677236818036]}',
14
- vermont: '{"type":"MultiPolygon","coordinates":[[[[-71.4652810350704,45.0136740921081],[-71.5386411060663,44.9876482991498],[-71.4949119839671,44.904935677564],[-71.5559082311558,44.8492522250988],[-71.5932506534844,44.7821926292262],[-71.6267620560918,44.7461089549863],[-71.5999650349199,44.7054878828813],[-71.5854265437744,44.6569382972147],[-71.5529291054811,44.6296118479332],[-71.5477715366207,44.5715907748108],[-71.590912834446,44.5654790326065],[-71.5721467665558,44.5383875158682],[-71.5773924416804,44.504795628544],[-71.6556174756354,44.4575842635223],[-71.6653785347815,44.437497307203],[-71.7426030604065,44.401593649355],[-71.7900571774886,44.4003277721065],[-71.8124840748932,44.3568172825136],[-71.8725731294504,44.3366032393682],[-71.9159562732437,44.3470295764368],[-72.0328714035569,44.3205183335643],[-72.068267173563,44.2725758737951],[-72.0476497892934,44.2394907913363],[-72.0656232451116,44.1889985746845],[-72.0390245886718,44.1555266718593],[-72.0531506510485,44.1186649606436],[-72.0419965506196,44.0793215610918],[-72.083470475863,44.021391036795],[-72.1168821381898,43.9938693880775],[-72.0989880476446,43.9573015829302],[-72.1180268061557,43.9232395021891],[-72.1850390675781,43.8635292290268],[-72.1833237715031,43.8096345502268],[-72.2318451262613,43.7478797412623],[-72.2710062037477,43.7340378639244],[-72.3000814868528,43.7057585766342],[-72.3286014830337,43.6012223664117],[-72.374963015393,43.5787671391696],[-72.398567536734,43.5113585632972],[-72.3802145312888,43.4925891723995],[-72.4145989561172,43.3642557678067],[-72.3899518331573,43.3560406089049],[-72.4103476303482,43.3304832941341],[-72.3954226049632,43.3132689557969],[-72.4370005653915,43.2560485116378],[-72.4386548616246,43.2014661570238],[-72.457067782571,43.1465235064938],[-72.4328401191079,43.1194093843771],[-72.4335666904147,43.0855700936913],[-72.4670369657832,43.0541563651196],[-72.4437081638785,43.0098992105563],[-72.4757890076724,42.9714813427244],[-72.5322718195464,42.9547865595992],[-72.5250663219872,42.9145238867145],[-72.5572279085895,42.8536740697468],[-72.5417204846968,42.8074556433103],[-72.4789240060734,42.762844222053],[-72.4584471135625,42.7269600083283],[-72.8463581795254,42.7373692412871],[-73.2761920979235,42.745980717451],[-73.2908881592095,42.8019554141581],[-73.2786777953224,42.8334723973611],[-73.270102777218,43.0298023092314],[-73.256564420585,43.2577019995215],[-73.246878246029,43.5257594146829],[-73.2495213860356,43.554195454945],[-73.2971234303866,43.5876006914255],[-73.3018282638826,43.6243579630326],[-73.3661314692282,43.6233617442716],[-73.3993105885789,43.5681870726839],[-73.4313322540602,43.5879173137677],[-73.4285070276083,43.6365346263424],[-73.3704507107918,43.7250565919606],[-73.3509986681513,43.7724341746593],[-73.3927963012338,43.8227736353836],[-73.3741504033419,43.8762343153286],[-73.4088622107153,43.934506898594],[-73.4103904443496,44.0265056738413],[-73.4379015767698,44.0451251846575],[-73.3906264489292,44.191066922614],[-73.319046344295,44.2502847730753],[-73.3117644008574,44.2791900694105],[-73.3354266881899,44.3599373816722],[-73.2959980390626,44.4287345257907],[-73.2996335004757,44.4774481456921],[-73.3197769055187,44.5235698292573],[-73.3745706674855,44.5756808411071],[-73.3901885448712,44.6189330442253],[-73.3703297781352,44.6627714750981],[-73.365618517551,44.7418072879085],[-73.3351731870471,44.804256760167],[-73.3797359695302,44.8386464823755],[-73.3607532750112,44.8966194647899],[-73.3396999897322,44.9163803468889],[-73.3429328823848,45.0107159350691],[-73.0653616036896,45.0159616529509],[-72.6346759983699,45.014687873586],[-72.5553981209717,45.0080184883601],[-72.3142543161308,45.0038470393866],[-71.9139806338036,45.0076285637164],[-71.4652810350704,45.0136740921081]]]]}',
3
+ burlington: '{"type":"Polygon","coordinates":[[[-73.223,44.491],[-73.171,44.49],[-73.146,44.447],[-73.211,44.439],[-73.229,44.462],[-73.223,44.491]]]}',
4
+ burlington_point: '{"type":"Point","coordinates":[-73.193,44.477]}',
5
+ burlington_downtown: '{"type":"Polygon","coordinates":[[[-73.218,44.487],[-73.203,44.482],[-73.203,44.475],[-73.211,44.466],[-73.22,44.473],[-73.218,44.487]]]}',
6
+ burlington_downtown_point: '{"type":"Point","coordinates":[-73.21,44.476]}',
7
+ south_burlington: '{"type":"Polygon","coordinates":[[[-73.175,44.474],[-73.169,44.473],[-73.158,44.466],[-73.162,44.456],[-73.182,44.46],[-73.175,44.474]]]}',
8
+ south_burlington_point: '{"type":"Point","coordinates":[-73.17,44.467]}',
9
+ montpelier: '{"type":"Polygon","coordinates":[[[-72.228,44.426],[-72.367,44.204],[-72.442,44.128],[-72.588,44.16],[-72.683,44.013],[-72.916,44.071],[-72.91,44.281],[-72.804,44.451],[-72.653,44.398],[-72.596,44.479],[-72.49,44.425],[-72.433,44.506],[-72.228,44.426]]]}',
10
+ montpelier_point: '{"type":"Point","coordinates":[-72.486,44.185]}',
11
+ vermont: '{"type":"Polygon","coordinates":[[[-71.494,45.02],[-71.551,44.58],[-72.03,44.306],[-72.047,44.093],[-72.397,43.542],[-72.466,42.729],[-73.253,42.751],[-73.241,43.542],[-73.423,44.079],[-73.291,44.449],[-73.353,45.009],[-71.494,45.02]]]}',
12
+ montreal_canada: '{"type":"Polygon","coordinates":[[[-74.064,45.694],[-73.641,45.775],[-73.256,45.614],[-73.234,45.325],[-73.674,45.228],[-74.069,45.34],[-74.064,45.694]]]}',
13
+ northampton_ma_point: '{"type":"Point","coordinates":[-72.638,42.318]}',
15
14
  }
16
15
  end
@@ -1,3 +1,3 @@
1
1
  module TheGeomGeoJSON
2
- VERSION = "0.0.1"
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,5 +1,15 @@
1
1
  require_relative 'the_geom_geojson/version'
2
2
 
3
+ require 'rgeo'
4
+ require 'rgeo/geo_json'
5
+
3
6
  module TheGeomGeoJSON
4
7
  Dirty = Class.new(RuntimeError)
8
+
9
+ class << self
10
+ def ewkb_to_geojson(ewkb)
11
+ parser = RGeo::WKRep::WKBParser.new nil, support_ewkb: true
12
+ JSON.dump RGeo::GeoJSON.encode(parser.parse(ewkb))
13
+ end
14
+ end
5
15
  end
@@ -5,10 +5,16 @@ unless ENV['FAST'] == 'true'
5
5
  system 'dropdb', '--if-exists', dbname
6
6
  system 'createdb', dbname
7
7
  system 'psql', dbname, '--command', 'CREATE EXTENSION postgis'
8
+ system 'psql', dbname, '--command', 'CREATE EXTENSION "uuid-ossp"'
8
9
  system 'psql', dbname, '--command', 'CREATE TABLE pets (id serial primary key, the_geom geometry(Geometry,4326), the_geom_webmercator geometry(Geometry,3857))'
10
+ system 'psql', dbname, '--command', 'CREATE TABLE pet_alt_ids (alt_id serial primary key, the_geom geometry(Geometry,4326), the_geom_webmercator geometry(Geometry,3857))'
11
+ system 'psql', dbname, '--command', 'CREATE TABLE pet_text_ids (id text unique not null, the_geom geometry(Geometry,4326), the_geom_webmercator geometry(Geometry,3857))'
12
+ system 'psql', dbname, '--command', 'CREATE TABLE pet_uuids (id uuid unique not null, the_geom geometry(Geometry,4326), the_geom_webmercator geometry(Geometry,3857))'
13
+ system 'psql', dbname, '--command', 'CREATE TABLE pet_auto_uuids (id uuid unique not null default uuid_generate_v4(), the_geom geometry(Geometry,4326), the_geom_webmercator geometry(Geometry,3857))'
9
14
  end
10
15
 
11
16
  require 'active_record'
17
+ require 'securerandom'
12
18
 
13
19
  ActiveRecord::Base.establish_connection "postgresql://127.0.0.1/#{dbname}"
14
20
 
@@ -22,6 +28,30 @@ class Pet < ActiveRecord::Base
22
28
  include TheGeomGeoJSON::ActiveRecord
23
29
  end
24
30
 
31
+ class PetAltId < ActiveRecord::Base
32
+ include TheGeomGeoJSON::ActiveRecord
33
+ self.primary_key = 'alt_id'
34
+ end
35
+
36
+ class PetTextId < ActiveRecord::Base
37
+ include TheGeomGeoJSON::ActiveRecord
38
+ self.primary_key = 'id'
39
+ end
40
+
41
+ class PetUuid < ActiveRecord::Base
42
+ include TheGeomGeoJSON::ActiveRecord
43
+ self.primary_key = 'id'
44
+ end
45
+
46
+ class PetAutoUuid < ActiveRecord::Base
47
+ include TheGeomGeoJSON::ActiveRecord
48
+ self.primary_key = 'id'
49
+
50
+ before_save do
51
+ self.id ||= SecureRandom.uuid
52
+ end
53
+ end
54
+
25
55
  if ENV['PRY'] == 'true'
26
56
  require 'pry'
27
57
  require 'logger'
@@ -31,19 +61,19 @@ end
31
61
 
32
62
  describe TheGeomGeoJSON do
33
63
  describe 'ActiveRecord' do
34
- # # select st_setsrid(st_geomfromgeojson('{"type":"Point","coordinates":[-73.1936,44.4775]}'),4326);
64
+ # # SELECT ST_SetSRID(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-73.193,44.477]}'), 4326);
35
65
  # st_setsrid
36
66
  # ----------------------------------------------------
37
- # 0101000020E61000005C2041F1634C52C085EB51B81E3D4640
67
+ # 0101000020E61000003108AC1C5A4C52C0931804560E3D4640
38
68
  # (1 row)
39
- let(:the_geom_expected) { '0101000020E61000005C2041F1634C52C085EB51B81E3D4640' }
69
+ let(:the_geom_expected) { '0101000020E61000003108AC1C5A4C52C0931804560E3D4640' }
40
70
 
41
- # # select st_transform(st_setsrid(st_geomfromgeojson('{"type":"Point","coordinates":[-73.1936,44.4775]}'),4326), 3857);
71
+ # # SELECT ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-73.193,44.477]}'), 4326), 3857);
42
72
  # st_transform
43
73
  # ----------------------------------------------------
44
- # 0101000020110F000011410192E8145FC137D58F0ECD215541
74
+ # 0101000020110F0000C22156DFD7145FC10858288EB9215541
45
75
  # (1 row)
46
- let(:the_geom_webmercator_expected) { '0101000020110F000011410192E8145FC137D58F0ECD215541' }
76
+ let(:the_geom_webmercator_expected) { '0101000020110F0000C22156DFD7145FC10858288EB9215541' }
47
77
 
48
78
  before do
49
79
  Pet.delete_all
@@ -59,46 +89,125 @@ describe TheGeomGeoJSON do
59
89
  it "lets you access the_geom_geojson" do
60
90
  expect(@pet.the_geom_geojson).to eq(TheGeomGeoJSON::EXAMPLES[:burlington_point])
61
91
  end
92
+ it "can be loaded from db" do
93
+ if @pet.persisted?
94
+ reloaded = pet_model.find @pet.id
95
+ expect(reloaded.the_geom).to eq(the_geom_expected)
96
+ expect(reloaded.the_geom_webmercator).to eq(the_geom_webmercator_expected)
97
+ expect(reloaded.the_geom_geojson).to eq(TheGeomGeoJSON::EXAMPLES[:burlington_point])
98
+ end
99
+ end
100
+ it "can be loaded with .with_geojson scope" do
101
+ if @pet.persisted?
102
+ reloaded = pet_model.with_geojson.find @pet.id
103
+ expect(reloaded.the_geom_geojson).to eq(TheGeomGeoJSON::EXAMPLES[:burlington_point])
104
+ end
105
+ end
62
106
  end
63
107
 
64
- describe "creating" do
65
- before do
66
- @pet = Pet.create! the_geom_geojson: TheGeomGeoJSON::EXAMPLES[:burlington_point]
108
+ shared_examples 'different states of persistence' do
109
+ describe "creating" do
110
+ before do
111
+ @pet = create_pet the_geom_geojson: TheGeomGeoJSON::EXAMPLES[:burlington_point]
112
+ end
113
+ it_behaves_like 'the_geom_geojson=(geojson)'
114
+ end
115
+
116
+ describe "building and saving" do
117
+ before do
118
+ @pet = build_pet the_geom_geojson: TheGeomGeoJSON::EXAMPLES[:burlington_point]
119
+ @pet.save!
120
+ end
121
+ it_behaves_like 'the_geom_geojson=(geojson)'
122
+ end
123
+
124
+ describe "modifying" do
125
+ before do
126
+ @pet = create_pet
127
+ @pet.the_geom_geojson = TheGeomGeoJSON::EXAMPLES[:burlington_point]
128
+ @pet.save!
129
+ end
130
+ it_behaves_like 'the_geom_geojson=(geojson)'
131
+ end
132
+
133
+ describe "building (without saving)" do
134
+ before do
135
+ @pet = build_pet the_geom_geojson: TheGeomGeoJSON::EXAMPLES[:burlington_point]
136
+ end
137
+ it "raises exception if you try to access the_geom" do
138
+ expect{@pet.the_geom}.to raise_error(TheGeomGeoJSON::Dirty)
139
+ end
140
+ it "raises exception if you try to access the_geom_webmercator" do
141
+ expect{@pet.the_geom_webmercator}.to raise_error(TheGeomGeoJSON::Dirty)
142
+ end
143
+ it "lets you access the_geom_geojson" do
144
+ expect(@pet.the_geom_geojson).to eq(TheGeomGeoJSON::EXAMPLES[:burlington_point])
145
+ end
67
146
  end
68
- it_behaves_like 'the_geom_geojson=(geojson)'
69
147
  end
70
148
 
71
- describe "building and saving" do
72
- before do
73
- @pet = Pet.new the_geom_geojson: TheGeomGeoJSON::EXAMPLES[:burlington_point]
74
- @pet.save!
149
+ def create_pet(attrs = {})
150
+ pet_model.create! attrs
151
+ end
152
+ def build_pet(attrs = {})
153
+ pet_model.new attrs
154
+ end
155
+
156
+ describe "with autoincrement id" do
157
+ def pet_model
158
+ Pet
75
159
  end
76
- it_behaves_like 'the_geom_geojson=(geojson)'
160
+ it_behaves_like 'different states of persistence'
77
161
  end
78
162
 
79
- describe "modifying" do
80
- before do
81
- @pet = Pet.create!
82
- @pet.the_geom_geojson = TheGeomGeoJSON::EXAMPLES[:burlington_point]
83
- @pet.save!
163
+ describe "with alt id" do
164
+ def pet_model
165
+ PetAltId
84
166
  end
85
- it_behaves_like 'the_geom_geojson=(geojson)'
167
+ it_behaves_like 'different states of persistence'
86
168
  end
87
169
 
88
- describe "building (without saving)" do
89
- before do
90
- @pet = Pet.new the_geom_geojson: TheGeomGeoJSON::EXAMPLES[:burlington_point]
170
+ describe "with auto-generating uuid" do
171
+ def pet_model
172
+ PetAutoUuid
91
173
  end
92
- it "raises exception if you try to access the_geom" do
93
- expect{@pet.the_geom}.to raise_error(TheGeomGeoJSON::Dirty)
174
+ it_behaves_like 'different states of persistence'
175
+ end
176
+
177
+ describe "with text id" do
178
+ def pet_model
179
+ PetTextId
94
180
  end
95
- it "raises exception if you try to access the_geom_webmercator" do
96
- expect{@pet.the_geom_webmercator}.to raise_error(TheGeomGeoJSON::Dirty)
181
+ def create_pet(attrs = {})
182
+ PetTextId.create! attrs.reverse_merge(id: rand.to_s)
97
183
  end
98
- it "lets you access the_geom_geojson" do
99
- expect(@pet.the_geom_geojson).to eq(TheGeomGeoJSON::EXAMPLES[:burlington_point])
184
+ def build_pet(attrs = {})
185
+ PetTextId.new attrs.reverse_merge(id: rand.to_s)
100
186
  end
187
+ it_behaves_like 'different states of persistence'
101
188
  end
102
189
 
190
+ describe "with uuid id" do
191
+ def pet_model
192
+ PetUuid
193
+ end
194
+ def create_pet(attrs = {})
195
+ PetUuid.create! attrs.reverse_merge(id: SecureRandom.uuid)
196
+ end
197
+ def build_pet(attrs = {})
198
+ PetUuid.new attrs.reverse_merge(id: SecureRandom.uuid)
199
+ end
200
+ it_behaves_like 'different states of persistence'
201
+ end
202
+ end
203
+
204
+ describe '::EXAMPLES' do
205
+ TheGeomGeoJSON::EXAMPLES.each do |name, geojson|
206
+ it "[:#{name}] respects the right-hand-rule" do
207
+ pet = Pet.create the_geom_geojson: geojson
208
+ rhr_geom = Pet.connection.execute("SELECT ST_ForceRHR(the_geom) FROM pets WHERE id = '#{pet.id}'").getvalue(0,0)
209
+ expect(pet.the_geom).to eq(rhr_geom)
210
+ end
211
+ end
103
212
  end
104
213
  end
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency 'activerecord'
22
22
  spec.add_runtime_dependency 'pg'
23
+ spec.add_runtime_dependency 'rgeo'
24
+ spec.add_runtime_dependency 'rgeo-geojson'
23
25
 
24
26
  spec.add_development_dependency "bundler", "~> 1.6"
25
27
  spec.add_development_dependency "rake"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the_geom_geojson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seamus Abshere
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-18 00:00:00.000000000 Z
11
+ date: 2014-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rgeo
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rgeo-geojson
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: bundler
43
71
  requirement: !ruby/object:Gem::Requirement