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 +4 -4
- data/.gitignore +2 -0
- data/Gemfile +1 -0
- data/README.md +33 -8
- data/lib/terraformer.rb +0 -1
- data/lib/terraformer/feature.rb +4 -4
- data/lib/terraformer/geometry.rb +2 -2
- data/lib/terraformer/multi_point.rb +3 -3
- data/lib/terraformer/polygon.rb +1 -1
- data/lib/terraformer/version.rb +1 -1
- data/test/circle_spec.rb +64 -0
- data/test/convex_hull_spec.rb +12 -1
- data/test/coordinate_spec.rb +269 -0
- data/test/geometry_spec.rb +23 -115
- data/test/helper.rb +6 -0
- data/test/line_string_spec.rb +154 -0
- data/test/multi_line_string_spec.rb +78 -0
- data/test/multi_point_spec.rb +151 -0
- data/test/point_spec.rb +105 -0
- data/test/polygon_spec.rb +142 -0
- metadata +16 -3
- data/lib/ext/hash.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bdd47e750c746d35d7e55ac4ecbca82548ca9b0
|
4
|
+
data.tar.gz: b5612978bf4ba8145a4cbb4103574680e3e1e8a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de22c0fe889529a57e7cbd8538344b1e2aa73ce6a218c1f0908d239c177b56eb77f9fa47f436f81dd90302ab93f5c3aaec05f28fda7f38a98d8091b084619891
|
7
|
+
data.tar.gz: a7b86a474675c9c63023820afa8131d6288a6158780ed2e03b0f9fcd43c7a13a82d83df287c34cfe04d2057d155859059caf343f8a239d5047903d249d476048
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
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
|
-
|
6
|
+
## Installation
|
7
7
|
|
8
|
-
|
8
|
+
In your application's Gemfile:
|
9
9
|
|
10
|
-
|
10
|
+
```ruby
|
11
|
+
gem 'terraformer'
|
12
|
+
```
|
13
|
+
|
14
|
+
Or install it manually:
|
11
15
|
|
12
|
-
|
16
|
+
```sh
|
17
|
+
$ gem install terraformer
|
18
|
+
```
|
13
19
|
|
14
|
-
|
20
|
+
## Usage
|
15
21
|
|
16
|
-
|
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
data/lib/terraformer/feature.rb
CHANGED
@@ -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
|
83
|
+
features: features.map {|f| f.to_hash *args}
|
84
84
|
}
|
85
85
|
h[:crs] = crs if crs
|
86
86
|
h
|
data/lib/terraformer/geometry.rb
CHANGED
@@ -48,20 +48,20 @@ module Terraformer
|
|
48
48
|
|
49
49
|
def add_point p
|
50
50
|
p = p.coordinates if Point === p
|
51
|
-
raise
|
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
|
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
|
64
|
+
raise ArgumentError unless Coordinate === p
|
65
65
|
coordinates.delete p
|
66
66
|
end
|
67
67
|
|
data/lib/terraformer/polygon.rb
CHANGED
data/lib/terraformer/version.rb
CHANGED
data/test/circle_spec.rb
ADDED
@@ -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
|
data/test/convex_hull_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require_relative './helper'
|
|
2
2
|
|
3
3
|
describe Terraformer::ConvexHull do
|
4
4
|
|
5
|
-
EXAMPLES.
|
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
|
+
|