terraformer 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94f290a9625b7c0820cf1489bc822ec82f24243d
4
- data.tar.gz: 8ccdb74b6538360ca45f7af6bc2e6c5ba84cdcd3
3
+ metadata.gz: 4bdd47e750c746d35d7e55ac4ecbca82548ca9b0
4
+ data.tar.gz: b5612978bf4ba8145a4cbb4103574680e3e1e8a9
5
5
  SHA512:
6
- metadata.gz: 21a14a49544eb19c39f96f6d7a8061f060600985b0e15820dcb06aa5f99ad9ea9036140400906139855acc2050120d32186774c249235b062a267cce252d302e
7
- data.tar.gz: a98fe28abc34063e50b8b269c3cf4c0a83ceb7a9613f11d7b05761cdeb902cc937b6b123c4c8b2bacfa1d45e719ce0cb2bb04b7251616dcfa8128bc0dae1c559
6
+ metadata.gz: de22c0fe889529a57e7cbd8538344b1e2aa73ce6a218c1f0908d239c177b56eb77f9fa47f436f81dd90302ab93f5c3aaec05f28fda7f38a98d8091b084619891
7
+ data.tar.gz: a7b86a474675c9c63023820afa8131d6288a6158780ed2e03b0f9fcd43c7a13a82d83df287c34cfe04d2057d155859059caf343f8a239d5047903d249d476048
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
1
  Gemfile.lock
2
2
  tmp
3
+
4
+ coverage/
data/Gemfile CHANGED
@@ -7,4 +7,5 @@ group :test do
7
7
  gem 'pry', '~>0.10'
8
8
  gem 'pry-nav', '~>0.2'
9
9
  gem 'ruby-prof', '~>0.15'
10
+ gem 'simplecov', :require => false
10
11
  end
data/README.md CHANGED
@@ -1,19 +1,27 @@
1
1
  terraformer-ruby
2
2
  ================
3
3
 
4
- mostly faithful port of [terraformer](https://github.com/Esri/Terraformer)
4
+ [terraformer-ruby](https://github.com/kenichi/terraformer-ruby) is a mostly faithful port of [terraformer](https://github.com/Esri/Terraformer).
5
5
 
6
- ### Installation
6
+ ## Installation
7
7
 
8
- `gem install terraformer`
8
+ In your application's Gemfile:
9
9
 
10
- or include in an application's `Gemfile`
10
+ ```ruby
11
+ gem 'terraformer'
12
+ ```
13
+
14
+ Or install it manually:
11
15
 
12
- `gem terraformer`
16
+ ```sh
17
+ $ gem install terraformer
18
+ ```
13
19
 
14
- ### Basic usage
20
+ ## Usage
15
21
 
16
- [terraformer-ruby](https://github.com/esripdx/terraformer-ruby) is a port of [terraformer](https://github.com/Esri/Terraformer).
22
+ ```ruby
23
+ require 'terraformer'
24
+ ```
17
25
 
18
26
  ##### Create a Terraformer primitive from GeoJSON
19
27
 
@@ -51,4 +59,21 @@ You can also have Terraformer perform many geometric operations like convex hull
51
59
  > convex_hull = polygon.convex_hull
52
60
  > point.within? convex_hull #returns true
53
61
  > bounding_box = polygon.bbox #returns the bounding box for this object.
54
- ```
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ After checking out the source, run the tests:
67
+
68
+ ```
69
+ $ git clone git@github.com:kenichi/terraformer-ruby.git
70
+ $ cd terraformer-ruby
71
+ $ bundle install
72
+ $ bundle exec rake test
73
+ ```
74
+
75
+ You can also generate RDoc:
76
+
77
+ ```
78
+ $ bundle exec rdoc --main README.md
79
+ ```
data/lib/terraformer.rb CHANGED
@@ -6,7 +6,6 @@ require 'ext/array'
6
6
  require 'ext/big_decimal'
7
7
  require 'ext/big_math'
8
8
  require 'ext/enumerable'
9
- require 'ext/hash'
10
9
  require 'forwardable'
11
10
 
12
11
  # terraformer.rb - a toolkit for working with geojson in ruby
@@ -22,12 +22,12 @@ module Terraformer
22
22
  @properties ||= {}
23
23
  end
24
24
 
25
- def to_hash
25
+ def to_hash *args
26
26
  h = {
27
27
  type: type,
28
28
  properties: properties,
29
29
  }
30
- h[:geometry] = geometry.to_hash if geometry
30
+ h[:geometry] = geometry.to_hash(*args) if geometry
31
31
  h[:id] = id if id
32
32
  h
33
33
  end
@@ -77,10 +77,10 @@ module Terraformer
77
77
  features << feature
78
78
  end
79
79
 
80
- def to_hash
80
+ def to_hash *args
81
81
  h = {
82
82
  type: type,
83
- features: features.map(&:to_hash)
83
+ features: features.map {|f| f.to_hash *args}
84
84
  }
85
85
  h[:crs] = crs if crs
86
86
  h
@@ -120,10 +120,10 @@ module Terraformer
120
120
  @geometries ||= []
121
121
  end
122
122
 
123
- def to_hash
123
+ def to_hash *args
124
124
  {
125
125
  type: type,
126
- geometries: geometries.map(&:to_hash)
126
+ geometries: geometries.map {|g| g.to_hash *args}
127
127
  }
128
128
  end
129
129
 
@@ -48,20 +48,20 @@ module Terraformer
48
48
 
49
49
  def add_point p
50
50
  p = p.coordinates if Point === p
51
- raise ArugmentError unless Coordinate === p
51
+ raise ArgumentError unless Coordinate === p
52
52
  coordinates << p
53
53
  end
54
54
  alias_method :<<, :add_point
55
55
 
56
56
  def insert_point idx, p
57
57
  p = p.coordinates if Point === p
58
- raise ArugmentError unless Coordinate === p
58
+ raise ArgumentError unless Coordinate === p
59
59
  coordinates.insert idx, p
60
60
  end
61
61
 
62
62
  def remove_point p
63
63
  p = p.coordinates if Point === p
64
- raise ArugmentError unless Coordinate === p
64
+ raise ArgumentError unless Coordinate === p
65
65
  coordinates.delete p
66
66
  end
67
67
 
@@ -110,7 +110,7 @@ module Terraformer
110
110
  end
111
111
 
112
112
  def add_vertex p
113
- insert_vertex coordinates[0].length - 2, p
113
+ insert_vertex -2, p
114
114
  end
115
115
  alias_method :<<, :add_vertex
116
116
 
@@ -1,3 +1,3 @@
1
1
  module Terraformer
2
- VERSION = '0.0.9'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,64 @@
1
+ require_relative './helper'
2
+
3
+ describe Terraformer::Circle do
4
+
5
+ describe 'construction' do
6
+ it 'constrcuts from integers' do
7
+ c = Terraformer::Circle.new [-122.6764, 45.5165], 100
8
+
9
+ assert !c.dirty?
10
+ c.center.x.must_equal -122.6764
11
+ c.center.y.must_equal 45.5165
12
+ c.radius.must_equal 100
13
+ c.resolution.must_equal Terraformer::DEFAULT_BUFFER_RESOLUTION
14
+ end
15
+
16
+ it 'constructs from coordinate' do
17
+ a = Terraformer::Coordinate.new -122.6764, 45.5165
18
+ c = Terraformer::Circle.new a, 100
19
+
20
+ assert !c.dirty?
21
+ c.center.must_equal a
22
+ c.radius.must_equal 100
23
+ c.resolution.must_equal Terraformer::DEFAULT_BUFFER_RESOLUTION
24
+ end
25
+
26
+ it 'constructs from point' do
27
+ a = Terraformer::Coordinate.new -122.6764, 45.5165
28
+ p = Terraformer::Point.new a
29
+ c = Terraformer::Circle.new p, 100
30
+
31
+ assert !c.dirty?
32
+ c.center.must_equal a
33
+ c.radius.must_equal 100
34
+ c.resolution.must_equal Terraformer::DEFAULT_BUFFER_RESOLUTION
35
+ end
36
+
37
+
38
+ it 'constructs from coordinate with res' do
39
+ a = Terraformer::Coordinate.new -122.6764, 45.5165
40
+ c = Terraformer::Circle.new a, 100, 10
41
+
42
+ assert !c.dirty?
43
+ c.center.must_equal a
44
+ c.radius.must_equal 100
45
+ c.resolution.must_equal 10
46
+
47
+ # todo: how to test size of polygon?
48
+ end
49
+ end
50
+
51
+ describe 'modification' do
52
+ before :each do
53
+ @c = Terraformer::Circle.new [-122.6764, 45.5165], 100
54
+ end
55
+
56
+ it 'should be dirty after setting center' do
57
+ @c.center = [-122, 45]
58
+
59
+ assert @c.dirty?
60
+ @c.center.x.must_equal -122
61
+ end
62
+
63
+ end
64
+ end
@@ -2,7 +2,7 @@ require_relative './helper'
2
2
 
3
3
  describe Terraformer::ConvexHull do
4
4
 
5
- EXAMPLES.not(:multi_line_string).each do |key, geo|
5
+ EXAMPLES.reject {|k,v| k == :multi_line_string }.each do |key, geo|
6
6
  it "works on #{key}" do
7
7
  ch = Terraformer.parse(geo).convex_hull
8
8
  ch.dont_be_terrible_ok
@@ -23,4 +23,15 @@ describe Terraformer::ConvexHull do
23
23
  ch.must_be_valid_geojson
24
24
  end
25
25
 
26
+ it 'works on feature collections - jarvis march' do
27
+ Terraformer::ConvexHull.impl = :jarvis_march
28
+
29
+ fc = Terraformer::FeatureCollection.new
30
+ fc << Terraformer.parse(EXAMPLES[:waldocanyon])
31
+ fc << Terraformer.parse(EXAMPLES[:sf_county]).to_feature
32
+ ch = fc.convex_hull
33
+ ch.dont_be_terrible_ok
34
+ ch.must_be_valid_geojson
35
+ end
36
+
26
37
  end
@@ -0,0 +1,269 @@
1
+ require_relative './helper'
2
+
3
+ describe Terraformer::Coordinate do
4
+
5
+ describe 'from' do
6
+ it 'should accept nested arrays' do
7
+ arys = [[100,0], [101,1], [102,2]]
8
+ c = Terraformer::Coordinate.from arys
9
+
10
+ c.class.must_equal Array
11
+ c[0].class.must_equal Terraformer::Coordinate
12
+ c[0].x.must_equal 100
13
+ c[0].y.must_equal 0
14
+ c[1].class.must_equal Terraformer::Coordinate
15
+ c[1].x.must_equal 101
16
+ c[1].y.must_equal 1
17
+ c[2].class.must_equal Terraformer::Coordinate
18
+ c[2].x.must_equal 102
19
+ c[2].y.must_equal 2
20
+ end
21
+
22
+ it 'should accept double nested arrays' do
23
+ arys = [[[100,0], [101,1]], [[102,2], [103,3]]]
24
+ c = Terraformer::Coordinate.from arys
25
+
26
+ c.class.must_equal Array
27
+ c[0].class.must_equal Array
28
+ c[0][0].class.must_equal Terraformer::Coordinate
29
+ c[0][0].x.must_equal 100
30
+ c[0][0].y.must_equal 0
31
+ end
32
+ end
33
+
34
+ describe 'from_array' do
35
+
36
+ it 'should be called from Array.to_c' do
37
+ c = [100,0].to_c
38
+
39
+ c.class.must_equal Terraformer::Coordinate
40
+ c.x.must_equal 100
41
+ c.y.must_equal 0
42
+ end
43
+
44
+ it 'should accept [100, 0]' do
45
+ c = Terraformer::Coordinate.from_array [100, 0]
46
+
47
+ c.class.must_equal Terraformer::Coordinate
48
+ c.x.must_equal 100
49
+ c.y.must_equal 0
50
+ end
51
+
52
+ it 'should accept nested arrays' do
53
+ arys = [[100,0], [101,1], [102,2]]
54
+ c = Terraformer::Coordinate.from_array arys
55
+
56
+ c.class.must_equal Array
57
+ c[0].class.must_equal Terraformer::Coordinate
58
+ c[0].x.must_equal 100
59
+ c[0].y.must_equal 0
60
+ c[1].class.must_equal Terraformer::Coordinate
61
+ c[1].x.must_equal 101
62
+ c[1].y.must_equal 1
63
+ c[2].class.must_equal Terraformer::Coordinate
64
+ c[2].x.must_equal 102
65
+ c[2].y.must_equal 2
66
+ end
67
+
68
+ it 'should accept double nested arrays' do
69
+ arys = [[[100,0], [101,1]], [[102,2], [103,3]]]
70
+ c = Terraformer::Coordinate.from_array arys
71
+
72
+ c.class.must_equal Array
73
+ c[0].class.must_equal Array
74
+ c[0][0].class.must_equal Terraformer::Coordinate
75
+ c[0][0].x.must_equal 100
76
+ c[0][0].y.must_equal 0
77
+ end
78
+
79
+ it 'should accept triple nested arrays' do
80
+ arys = [[[[100,0], [101,1]]]]
81
+ c = Terraformer::Coordinate.from_array arys
82
+
83
+ c.class.must_equal Array
84
+ c[0].class.must_equal Array
85
+ c[0][0].class.must_equal Array
86
+ c[0][0][0].class.must_equal Terraformer::Coordinate
87
+ c[0][0][0].x.must_equal 100
88
+ c[0][0][0].y.must_equal 0
89
+ end
90
+ end
91
+
92
+ describe 'initialize' do
93
+
94
+ it 'accepts numbers as params: x,y' do
95
+ c = Terraformer::Coordinate.new 100, 0
96
+
97
+ c.x.must_equal 100
98
+ c.y.must_equal 0
99
+ c.z.must_equal nil
100
+ #c.to_s.must_equal '100,0'
101
+ end
102
+
103
+ it 'accepts numbers as params: x,y,z' do
104
+ c = Terraformer::Coordinate.new 100, 0, 0
105
+
106
+ c.x.must_equal 100
107
+ c.y.must_equal 0
108
+ c.z.must_equal 0
109
+ #c.to_s.must_equal '100,0,0'
110
+ end
111
+
112
+ it 'accepts strings as params: x,y' do
113
+ c = Terraformer::Coordinate.new '100', '0'
114
+
115
+ c.x.must_equal 100
116
+ c.y.must_equal 0
117
+ c.z.must_equal nil
118
+ #c.to_s.must_equal '100,0'
119
+ end
120
+
121
+ it 'accepts strings as params: x,y,z' do
122
+ c = Terraformer::Coordinate.new '100', '0', '0'
123
+
124
+ c.x.must_equal 100
125
+ c.y.must_equal 0
126
+ c.z.must_equal 0
127
+ #c.to_s.must_equal '100,0,0'
128
+ end
129
+ it 'accepts an array as params: x,y' do
130
+ c = Terraformer::Coordinate.new [100, 0]
131
+
132
+ c.x.must_equal 100
133
+ c.y.must_equal 0
134
+ c.z.must_equal nil
135
+ #c.to_s.must_equal '100,0'
136
+ end
137
+
138
+ it 'accepts an array as params: x,y,z' do
139
+ c = Terraformer::Coordinate.new [100, 0, 0]
140
+
141
+ c.x.must_equal 100
142
+ c.y.must_equal 0
143
+ c.z.must_equal 0
144
+ #c.to_s.must_equal '100,0,0'
145
+ end
146
+
147
+ it 'does not allow mixed params' do
148
+ assert_raises ArgumentError do
149
+ Terraformer::Coordinate.new [100,0], 0
150
+ end
151
+
152
+ assert_raises ArgumentError do
153
+ Terraformer::Coordinate.new 100
154
+ end
155
+
156
+ assert_raises ArgumentError do
157
+ Terraformer::Coordinate.new {}
158
+ end
159
+ end
160
+
161
+ it 'does not allow invalid types' do
162
+ assert_raises ArgumentError do
163
+ Terraformer::Coordinate.new :foo, :bar
164
+ end
165
+ end
166
+
167
+ end
168
+
169
+ describe 'methods' do
170
+ before :each do
171
+ @c = Terraformer::Coordinate.new 100, 0, 0
172
+ end
173
+
174
+ describe 'modification' do
175
+
176
+ it 'should allow modification of x' do
177
+ @c.x = 101
178
+ @c.x.must_equal 101
179
+
180
+ @c.x = '102'
181
+ @c.x.must_equal 102
182
+
183
+ assert_raises ArgumentError do
184
+ @c.x = :foo
185
+ end
186
+ end
187
+
188
+ it 'should allow modification of y' do
189
+ @c.y = 101
190
+ @c.y.must_equal 101
191
+
192
+ @c.y = '102'
193
+ @c.y.must_equal 102
194
+
195
+ assert_raises ArgumentError do
196
+ @c.y = :foo
197
+ end
198
+ end
199
+ end
200
+
201
+ describe 'equality' do
202
+
203
+ it 'should be equal' do
204
+ c2 = Terraformer::Coordinate.new 100, 0, 0
205
+ @c.must_equal c2
206
+ end
207
+
208
+ it 'should not be equal' do
209
+ c2 = Terraformer::Coordinate.new 101, 0, 0
210
+ @c.wont_equal c2
211
+ end
212
+ end
213
+
214
+ describe 'maths' do
215
+ before :each do
216
+ @c2 = Terraformer::Coordinate.new 102, 2
217
+ end
218
+
219
+ it 'should add' do
220
+ @c = @c + [1, 1]
221
+
222
+ @c.x.must_equal 101
223
+ @c.y.must_equal 1
224
+ end
225
+
226
+ it 'should subtract' do
227
+ @c = @c - [1, 1]
228
+
229
+ @c.x.must_equal 99
230
+ @c.y.must_equal -1
231
+ end
232
+
233
+ it 'should calculate euclidean distance' do
234
+ ed = @c.euclidean_distance_to(@c2)
235
+ ed.to_s('F').must_equal "2.82842712474619"
236
+ end
237
+
238
+ it 'should calculate squared euclidean distance' do
239
+ ed = @c.squared_euclidean_distance_to(@c2)
240
+ ed.to_s('F').must_equal "8.0"
241
+ end
242
+
243
+ it 'should calculate haversine distance' do
244
+ ed = @c.haversine_distance_to(@c2)
245
+ ed.to_s('F').must_equal "314475.2493440403"
246
+ end
247
+
248
+ it 'should calculate distance and bearing' do
249
+ db = @c.distance_and_bearing_to(@c2)
250
+ db.class.must_equal Hash
251
+
252
+ db[:distance].to_s('F').must_equal "314037.6739419673"
253
+ db[:bearing][:initial].to_s('F').must_equal "45.17484844056842"
254
+ db[:bearing][:final].to_s('F').must_equal "45.20976211316248"
255
+
256
+ d = @c.distance_to @c2
257
+ d.must_equal db[:distance]
258
+
259
+ ib = @c.initial_bearing_to @c2
260
+ ib.must_equal db[:bearing][:initial]
261
+
262
+ fb = @c.final_bearing_to @c2
263
+ fb.must_equal db[:bearing][:final]
264
+
265
+ end
266
+ end
267
+ end
268
+ end
269
+