georuby-ext 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Guardfile +13 -0
- data/README.rdoc +37 -0
- data/Rakefile +10 -0
- data/georuby-ext.gemspec +31 -0
- data/lib/georuby-ext.rb +17 -0
- data/lib/georuby-ext/geokit.rb +25 -0
- data/lib/georuby-ext/georuby/envelope.rb +10 -0
- data/lib/georuby-ext/georuby/geometry.rb +9 -0
- data/lib/georuby-ext/georuby/line_string.rb +41 -0
- data/lib/georuby-ext/georuby/locators.rb +225 -0
- data/lib/georuby-ext/georuby/multi_polygon.rb +9 -0
- data/lib/georuby-ext/georuby/point.rb +73 -0
- data/lib/georuby-ext/georuby/polygon.rb +68 -0
- data/lib/georuby-ext/proj4.rb +11 -0
- data/lib/georuby-ext/rgeo.rb +23 -0
- data/lib/georuby-ext/rspec_helper.rb +61 -0
- data/spec/georuby/line_string_spec.rb +120 -0
- data/spec/georuby/linear_ring_spec.rb +33 -0
- data/spec/georuby/locators_spec.rb +120 -0
- data/spec/georuby/multi_polygon_spec.rb +29 -0
- data/spec/georuby/point_spec.rb +44 -0
- data/spec/georuby/polygon_spec.rb +134 -0
- data/spec/rgeo_spec.rb +81 -0
- data/spec/spec_helper.rb +23 -0
- metadata +227 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'bundler' do
|
5
|
+
watch('Gemfile')
|
6
|
+
watch(/^.+\.gemspec/)
|
7
|
+
end
|
8
|
+
|
9
|
+
guard 'rspec', :version => 2 do
|
10
|
+
watch(%r{^spec/.+_spec\.rb$})
|
11
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
12
|
+
watch('spec/spec_helper.rb') { "spec" }
|
13
|
+
end
|
data/README.rdoc
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
== georuby-ext
|
2
|
+
|
3
|
+
georuby-ext is an extension to Ruby geometry libraries
|
4
|
+
|
5
|
+
=== Summary
|
6
|
+
|
7
|
+
GeoRuby, rgeo, geokit, proj4j (...) are nice ruby libraries which cover the same domain. georuby-ext allows to <b>use them together</b>.
|
8
|
+
|
9
|
+
=== License
|
10
|
+
|
11
|
+
Copyright 2011 Dryade SAS
|
12
|
+
|
13
|
+
All rights reserved.
|
14
|
+
|
15
|
+
Redistribution and use in source and binary forms, with or without
|
16
|
+
modification, are permitted provided that the following conditions are met:
|
17
|
+
|
18
|
+
* Redistributions of source code must retain the above copyright notice,
|
19
|
+
this list of conditions and the following disclaimer.
|
20
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
21
|
+
this list of conditions and the following disclaimer in the documentation
|
22
|
+
and/or other materials provided with the distribution.
|
23
|
+
* Neither the name of the copyright holder, nor the names of any other
|
24
|
+
contributors to this software, may be used to endorse or promote products
|
25
|
+
derived from this software without specific prior written permission.
|
26
|
+
|
27
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
28
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
29
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
30
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
31
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
32
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
33
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
34
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
35
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
36
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
37
|
+
POSSIBILITY OF SUCH DAMAGE.
|
data/Rakefile
ADDED
data/georuby-ext.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "georuby-ext"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.authors = ["Marc Florisson", "Luc Donnet", "Alban Peignier"]
|
8
|
+
s.email = ["marc@dryade.net", "luc@dryade.net", "alban@dryade.net"]
|
9
|
+
s.homepage = "http://github.com/dryade/georuby-ext"
|
10
|
+
s.summary = %q{Extension to Ruby geometry libraries}
|
11
|
+
s.description = %q{Use together GeoRuby, rgeo, geokit, proj4j (and others)}
|
12
|
+
|
13
|
+
s.rubyforge_project = "georuby-ext"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "rspec"
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
s.add_development_dependency "guard"
|
23
|
+
s.add_development_dependency "rcov"
|
24
|
+
|
25
|
+
s.add_runtime_dependency "GeoRuby"
|
26
|
+
s.add_runtime_dependency "geokit"
|
27
|
+
s.add_runtime_dependency "rgeo"
|
28
|
+
s.add_runtime_dependency "proj4rb"
|
29
|
+
|
30
|
+
s.add_runtime_dependency "activesupport"
|
31
|
+
end
|
data/lib/georuby-ext.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'geo_ruby'
|
2
|
+
require 'geokit'
|
3
|
+
require 'rgeo'
|
4
|
+
require 'proj4'
|
5
|
+
|
6
|
+
require 'active_support/core_ext/enumerable'
|
7
|
+
|
8
|
+
require 'georuby-ext/rgeo'
|
9
|
+
require 'georuby-ext/geokit'
|
10
|
+
require 'georuby-ext/proj4'
|
11
|
+
|
12
|
+
require 'georuby-ext/georuby/point'
|
13
|
+
require 'georuby-ext/georuby/line_string'
|
14
|
+
require 'georuby-ext/georuby/polygon'
|
15
|
+
require 'georuby-ext/georuby/multi_polygon'
|
16
|
+
require 'georuby-ext/georuby/envelope'
|
17
|
+
require 'georuby-ext/georuby/locators'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module GeoKit
|
2
|
+
class LatLng
|
3
|
+
|
4
|
+
def valid?
|
5
|
+
self.lat and self.lng
|
6
|
+
end
|
7
|
+
|
8
|
+
def wgs84_to_google
|
9
|
+
self.class.from_pro4j Proj4::Projection.wgs84.transform Proj4::Projection.google, self.proj4_point(Math::PI / 180)
|
10
|
+
end
|
11
|
+
|
12
|
+
def google_to_wgs84
|
13
|
+
self.class.from_pro4j Proj4::Projection.google.transform(Proj4::Projection.wgs84, self.proj4_point), 180 / Math::PI
|
14
|
+
end
|
15
|
+
|
16
|
+
def proj4_point(ratio = 1)
|
17
|
+
Proj4::Point.new(lng * ratio, lat * ratio)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.from_pro4j(point, ratio = 1)
|
21
|
+
new point.lat * ratio, point.lon * ratio
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class GeoRuby::SimpleFeatures::Envelope
|
2
|
+
def to_openlayers
|
3
|
+
OpenLayers::Bounds.new lower_corner.x, lower_corner.y, upper_corner.x, upper_corner.y
|
4
|
+
end
|
5
|
+
|
6
|
+
def to_google
|
7
|
+
Envelope.from_points [lower_corner.to_google, upper_corner.to_google],srid, with_z
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class GeoRuby::SimpleFeatures::LineString
|
2
|
+
|
3
|
+
def change(options)
|
4
|
+
self.class.from_points(options[:points] || points,
|
5
|
+
options[:srid] || srid,
|
6
|
+
options[:with_z] || with_z,
|
7
|
+
options[:with_m] || with_m)
|
8
|
+
# or instead of || requires parenthesis
|
9
|
+
end
|
10
|
+
|
11
|
+
def reverse
|
12
|
+
change :points => points.reverse
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_wgs84
|
16
|
+
change :points => points.map(&:to_wgs84), :srid => 4326
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.merge(lines)
|
20
|
+
merged_points = lines.map(&:points).flatten.uniq
|
21
|
+
if merged_points.size > 1
|
22
|
+
from_points merged_points, lines.first.srid, lines.first.with_z, lines.first.with_m
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_rgeo
|
27
|
+
RGeo::Geos::factory(:srid => srid).line_string(points.collect(&:to_rgeo))
|
28
|
+
end
|
29
|
+
|
30
|
+
def ==(other)
|
31
|
+
other.respond_to?(:points) and points == other.points
|
32
|
+
end
|
33
|
+
|
34
|
+
alias_method :closed?, :is_closed
|
35
|
+
|
36
|
+
def to_ring
|
37
|
+
ring_points = closed? ? points : points + [first]
|
38
|
+
GeoRuby::SimpleFeatures::LinearRing.from_points ring_points, srid, with_z, with_m
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,225 @@
|
|
1
|
+
require 'active_support/memoizable'
|
2
|
+
require 'active_support/core_ext/module/delegation'
|
3
|
+
|
4
|
+
class GeoRuby::SimpleFeatures::MultiLineString
|
5
|
+
|
6
|
+
alias_method :lines, :geometries
|
7
|
+
|
8
|
+
def locate_point(target)
|
9
|
+
nearest_locator = nearest_locator(target)
|
10
|
+
nearest_locator.location + nearest_locator.index
|
11
|
+
end
|
12
|
+
|
13
|
+
def interpolate_point(location)
|
14
|
+
line_index, line_location = location.to_i, location % 1
|
15
|
+
lines[line_index].interpolate_point(line_location)
|
16
|
+
end
|
17
|
+
|
18
|
+
def nearest_locator(target)
|
19
|
+
locators(target).min_by(&:distance_from_line)
|
20
|
+
end
|
21
|
+
|
22
|
+
def locators(point)
|
23
|
+
[].tap do |locators|
|
24
|
+
lines.each_with_index do |line, index|
|
25
|
+
locators << PointLocator.new(point, line, index)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class PointLocator
|
31
|
+
|
32
|
+
attr_reader :target, :line, :index
|
33
|
+
|
34
|
+
def initialize(target, line, index)
|
35
|
+
@target = target
|
36
|
+
@line = line
|
37
|
+
@index = index
|
38
|
+
end
|
39
|
+
|
40
|
+
def location
|
41
|
+
line.locate_point(target)
|
42
|
+
end
|
43
|
+
|
44
|
+
def distance_on_line
|
45
|
+
line.distance_on_line(target)
|
46
|
+
end
|
47
|
+
|
48
|
+
def distance_from_line
|
49
|
+
line.distance_from_line(target)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
class GeoRuby::SimpleFeatures::LineString
|
57
|
+
|
58
|
+
def locate_point(target)
|
59
|
+
distance_on_line(target) / distance
|
60
|
+
end
|
61
|
+
|
62
|
+
def distance_on_line(target)
|
63
|
+
nearest_locator = nearest_locator(target)
|
64
|
+
nearest_locator.distance_on_segment + nearest_locator.segment.line_distance_at_departure
|
65
|
+
end
|
66
|
+
|
67
|
+
def distance_from_line(target)
|
68
|
+
nearest_locator(target).distance_from_segment
|
69
|
+
end
|
70
|
+
|
71
|
+
def nearest_locator(target)
|
72
|
+
locators(target).min_by(&:distance_from_segment)
|
73
|
+
end
|
74
|
+
|
75
|
+
def locators(point)
|
76
|
+
segments.collect { |segment| segment.locator(point) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def segments
|
80
|
+
previous_point = nil
|
81
|
+
distance_from_departure = 0
|
82
|
+
|
83
|
+
points.inject([]) do |segments, point|
|
84
|
+
Segment.new(previous_point, point).tap do |segment|
|
85
|
+
segment.line = self
|
86
|
+
segment.line_distance_at_departure = segments.sum(&:distance)
|
87
|
+
|
88
|
+
segments << segment
|
89
|
+
end if previous_point
|
90
|
+
|
91
|
+
previous_point = point
|
92
|
+
segments
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def distance
|
97
|
+
segments.sum(&:distance)
|
98
|
+
end
|
99
|
+
|
100
|
+
def interpolate_point(location)
|
101
|
+
return points.last if location >= 1
|
102
|
+
return points.first if location <= 0
|
103
|
+
|
104
|
+
distance_on_line = location * distance
|
105
|
+
|
106
|
+
segment = segments.find do |segment|
|
107
|
+
segment.line_distance_at_arrival > distance_on_line
|
108
|
+
end
|
109
|
+
|
110
|
+
location_on_segment =
|
111
|
+
(distance_on_line - segment.line_distance_at_departure) / segment.distance
|
112
|
+
|
113
|
+
segment.interpolate_point location_on_segment
|
114
|
+
end
|
115
|
+
|
116
|
+
class Segment
|
117
|
+
|
118
|
+
attr_reader :departure, :arrival
|
119
|
+
|
120
|
+
def initialize(departure, arrival)
|
121
|
+
@departure, @arrival = departure, arrival
|
122
|
+
end
|
123
|
+
|
124
|
+
attr_accessor :line, :line_distance_at_departure
|
125
|
+
|
126
|
+
def line_distance_at_arrival
|
127
|
+
line_distance_at_departure + distance
|
128
|
+
end
|
129
|
+
|
130
|
+
def locator(target)
|
131
|
+
PointLocator.new target, self
|
132
|
+
end
|
133
|
+
|
134
|
+
def location_in_line
|
135
|
+
if line and line_distance_at_departure
|
136
|
+
line_distance_at_departure / line.distance
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def square_of_distance
|
141
|
+
(arrival.x - departure.x)**2 + (arrival.y - departure.y)**2
|
142
|
+
end
|
143
|
+
|
144
|
+
def distance
|
145
|
+
departure.euclidian_distance(arrival)
|
146
|
+
end
|
147
|
+
|
148
|
+
def to_s
|
149
|
+
"#{departure.x},#{departure.y}..#{arrival.x},#{arrival.y}"
|
150
|
+
end
|
151
|
+
|
152
|
+
def interpolate_point(location)
|
153
|
+
dx, dy =
|
154
|
+
(arrival.x - departure.x)*location, (arrival.y - departure.y)*location
|
155
|
+
GeoRuby::SimpleFeatures::Point.from_x_y departure.x + dx, departure.y + dy, line.srid
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
class PointLocator
|
161
|
+
extend ActiveSupport::Memoizable
|
162
|
+
include Math
|
163
|
+
|
164
|
+
attr_reader :target, :segment
|
165
|
+
delegate :departure, :arrival, :to => :segment
|
166
|
+
|
167
|
+
def initialize(target, segment_or_departure, arrival = nil)
|
168
|
+
@segment =
|
169
|
+
if arrival
|
170
|
+
Segment.new(segment_or_departure, arrival)
|
171
|
+
else
|
172
|
+
segment_or_departure
|
173
|
+
end
|
174
|
+
@target = target
|
175
|
+
|
176
|
+
raise "Target is not defined" unless target
|
177
|
+
end
|
178
|
+
|
179
|
+
def distance_from_segment
|
180
|
+
sin_angle * target_distance_from_departure
|
181
|
+
end
|
182
|
+
|
183
|
+
def distance_on_segment
|
184
|
+
# like cos_angle * target_distance_from_departure
|
185
|
+
# scalar_product / square_of_segment_distance
|
186
|
+
scalar_product / segment_distance
|
187
|
+
end
|
188
|
+
|
189
|
+
def locate_point
|
190
|
+
scalar_product / square_of_segment_distance
|
191
|
+
end
|
192
|
+
|
193
|
+
def scalar_product
|
194
|
+
(target.x-departure.x)*(arrival.x-departure.x) + (target.y-departure.y)*(arrival.y-departure.y).to_f
|
195
|
+
end
|
196
|
+
memoize :scalar_product
|
197
|
+
|
198
|
+
def angle
|
199
|
+
acos cos_angle
|
200
|
+
end
|
201
|
+
|
202
|
+
def sin_angle
|
203
|
+
sin angle
|
204
|
+
end
|
205
|
+
|
206
|
+
def cos_angle
|
207
|
+
scalar_product / segment_distance / target_distance_from_departure
|
208
|
+
end
|
209
|
+
|
210
|
+
def square_of_segment_distance
|
211
|
+
(arrival.x - departure.x)**2 + (arrival.y - departure.y)**2
|
212
|
+
end
|
213
|
+
memoize :square_of_segment_distance
|
214
|
+
|
215
|
+
def segment_distance
|
216
|
+
sqrt square_of_segment_distance
|
217
|
+
end
|
218
|
+
|
219
|
+
def target_distance_from_departure
|
220
|
+
sqrt((target.x - departure.x)**2 + (target.y - departure.y)**2)
|
221
|
+
end
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|