mapper 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mapper.rb +24 -0
- data/test/map.png +0 -0
- data/test/mapper_test.rb +40 -0
- metadata +72 -0
data/lib/mapper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Mapper < Array
|
2
|
+
attr_reader :reference_point1, :reference_point2, :zero_point
|
3
|
+
attr_reader :x_factor, :y_factor
|
4
|
+
|
5
|
+
def initialize(reference_point1={:lat=>0.0,:lon=>0.0,:x=>0,:y=>0}, reference_point2={:lat=>0.0,:lon=>0.0,:x=>0,:y=>0})
|
6
|
+
@reference_point1 = reference_point1; @reference_point2 = reference_point2
|
7
|
+
@x_factor = (reference_point1[:x] - reference_point2[:x]) / (reference_point1[:lon] - reference_point2[:lon]).to_f
|
8
|
+
@y_factor = (reference_point1[:y] - reference_point2[:y]) / (reference_point1[:lat] - reference_point2[:lat]).to_f
|
9
|
+
@zero_point = {:lon => reference_point1[:lon] - reference_point1[:x] / @x_factor, :x => 0,
|
10
|
+
:lat => reference_point1[:lat] - reference_point1[:y] / @y_factor, :y => 0}
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_coords( point={:lon=>0.0,:lat=>0.0} )
|
14
|
+
point.merge!(:x => (point[:lon] - @zero_point[:lon]) * @x_factor) if point[:lon] && !point[:x]
|
15
|
+
point.merge!(:y => (point[:lat] - @zero_point[:lat]) * @y_factor) if point[:lat] && !point[:y]
|
16
|
+
point.merge!(:lon => (point[:x] / @x_factor) + @zero_point[:lon]) if point[:x] && !point[:lon]
|
17
|
+
point.merge!(:lat => (point[:y] / @y_factor) + @zero_point[:lat]) if point[:y] && !point[:lat]
|
18
|
+
point
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_point(point)
|
22
|
+
self << get_coords(point)
|
23
|
+
end
|
24
|
+
end
|
data/test/map.png
ADDED
Binary file
|
data/test/mapper_test.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.join(File.expand_path(File.join(File.dirname(__FILE__))), '..', 'lib', 'mapper')
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class MapperTest < Test::Unit::TestCase
|
5
|
+
MUNICH = {:lat => 48.13, :lon => 11.57, :x => 278, :y => 503}
|
6
|
+
BERLIN = {:lat => 52.52, :lon => 13.41, :x => 361, :y => 191}
|
7
|
+
HAMBURG = {:lat => 53.55, :lon => 9.99, :x => 206, :y => 114}
|
8
|
+
MANNHEIM = {:lat => 49.48, :lon => 8.46, :x => 136, :y => 410}
|
9
|
+
|
10
|
+
def test_initialization
|
11
|
+
mapper = Mapper.new(MUNICH, HAMBURG)
|
12
|
+
assert (45.52 .. 45.62).include?(mapper.x_factor), 'wrong x_factor' # ~ 45.57
|
13
|
+
assert (-71.82 .. -71.72).include?(mapper.y_factor), 'wrong x_factor' # ~ -71.77
|
14
|
+
assert (55.09 .. 55.19).include?(mapper.zero_point[:lat]), 'wrong zero_point[:lat]' # ~ 55.14
|
15
|
+
assert (5.42 .. 5.52).include?(mapper.zero_point[:lon]), 'wrong zero_point[:lon]' # ~ 5.47
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_add_new_geo_coordinate
|
19
|
+
mapper = Mapper.new(MUNICH, HAMBURG)
|
20
|
+
mapper.add_point(:lat=>BERLIN[:lat], :lon=>BERLIN[:lon])
|
21
|
+
assert (BERLIN[:x]-5 .. BERLIN[:x]+5).include?(mapper.first[:x])
|
22
|
+
assert (BERLIN[:y]-5 .. BERLIN[:y]+5).include?(mapper.first[:y])
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_add_new_map_coordinate
|
26
|
+
mapper = Mapper.new(MUNICH, HAMBURG)
|
27
|
+
mapper.add_point(:x=>BERLIN[:x], :y=>BERLIN[:y])
|
28
|
+
assert (BERLIN[:lat]-0.1 .. BERLIN[:lat]+0.1).include?(mapper.first[:lat])
|
29
|
+
assert (BERLIN[:lon]-0.1 .. BERLIN[:lon]+0.1).include?(mapper.first[:lon])
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_add_incomplete_coordinate
|
33
|
+
mapper = Mapper.new(MUNICH, HAMBURG)
|
34
|
+
mapper.add_point(:lat=>BERLIN[:lat])
|
35
|
+
assert_equal BERLIN[:lat], mapper.first[:lat]
|
36
|
+
assert (BERLIN[:y]-5 .. BERLIN[:y]+5).include?(mapper.first[:y])
|
37
|
+
assert !mapper.first[:x]
|
38
|
+
assert !mapper.first[:lon]
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: "1.0"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- pachacamac
|
13
|
+
- naema
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-25 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A tiny geo coordinate converter - basically for plotting latitude/longitude pairs to an image
|
23
|
+
email:
|
24
|
+
- pachacamac@lavabit.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/mapper.rb
|
33
|
+
- test/map.png
|
34
|
+
- test/mapper_test.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: https://github.com/pachacamac/mapper
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 17
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 3
|
62
|
+
- 5
|
63
|
+
version: 1.3.5
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: mapper
|
67
|
+
rubygems_version: 1.4.2
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: A tiny geo coordinate converter
|
71
|
+
test_files: []
|
72
|
+
|