iarea 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .bundle
2
+ pkg
data/MIT-LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2010 Yoji Shidara
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = Iarea
2
+
3
+ A library for DoCoMo Open Iarea.
4
+
5
+ = Example of use
6
+
7
+ require 'iarea'
8
+
9
+ area = Iarea::Area.find_by_lat_lng(43.0568397222222,141.3478975)
10
+ area.name # => "すすきの"
11
+ area.areacode # => "00208"
12
+
13
+ = References
14
+
15
+ http://www.nttdocomo.co.jp/service/imode/make/content/iarea/domestic/index.html
16
+
17
+ db/iareadata.sqlite3 contains the data extracted from http://www.nttdocomo.co.jp/binary/archive/service/imode/make/content/iarea/domestic/iareadata.lzh .
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ load 'lib/tasks/iarea.rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "iarea"
7
+ gem.summary = "A library for DoCoMo Open Iarea."
8
+ gem.description = "A library for DoCoMo Open Iarea."
9
+ gem.email = "dara@shidara.net"
10
+ gem.homepage = "http://github.com/darashi/iarea"
11
+ gem.authors = ["Yoji Shidara"]
12
+
13
+ gem.add_dependency "sequel", ">= 3.10.0"
14
+ gem.add_dependency "sqlite3", ">= 1.2.5"
15
+
16
+ gem.add_development_dependency "rspec", ">= 1.3.0"
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ Jeweler::GemcutterTasks.new
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:test) do |t|
26
+ t.spec_files = FileList['test/**/*_test.rb']
27
+ t.libs << 'test'
28
+
29
+ t.verbose = true
30
+ end
31
+
32
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
Binary file
data/iarea.gemspec ADDED
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{iarea}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Yoji Shidara"]
12
+ s.date = %q{2010-04-26}
13
+ s.description = %q{A library for DoCoMo Open Iarea.}
14
+ s.email = %q{dara@shidara.net}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "db/iareadata.sqlite3",
25
+ "iarea.gemspec",
26
+ "lib/iarea.rb",
27
+ "lib/tasks/iarea.rake",
28
+ "test/iarea/iarea_test.rb",
29
+ "test/test_helper.rb",
30
+ "tools/import_meshes.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/darashi/iarea}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6.1}
36
+ s.summary = %q{A library for DoCoMo Open Iarea.}
37
+ s.test_files = [
38
+ "test/iarea/iarea_test.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<sequel>, [">= 3.10.0"])
48
+ s.add_runtime_dependency(%q<sqlite3>, [">= 1.2.5"])
49
+ s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
50
+ else
51
+ s.add_dependency(%q<sequel>, [">= 3.10.0"])
52
+ s.add_dependency(%q<sqlite3>, [">= 1.2.5"])
53
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<sequel>, [">= 3.10.0"])
57
+ s.add_dependency(%q<sqlite3>, [">= 1.2.5"])
58
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
59
+ end
60
+ end
61
+
data/lib/iarea.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'sequel'
2
+
3
+ module Iarea
4
+ DB = Sequel.sqlite(File.expand_path("../../db/iareadata.sqlite3", __FILE__))
5
+
6
+ class Area
7
+ attr_reader :areaid, :subareaid, :areacode, :name, :west, :south, :east, :north
8
+
9
+ def initialize(data)
10
+ data.each do |k, v|
11
+ instance_variable_set("@#{k}", v)
12
+ end
13
+ end
14
+
15
+ class << self
16
+ def find_by_lat_lng(lat, lng)
17
+ meshcodes = Utils.expand_meshcode(Utils.lat_lng_to_meshcode(lat, lng))
18
+ area = DB[:meshes].where(:meshcode => meshcodes).join(:areas, :areacode => :areacode).first
19
+ return nil if area.nil?
20
+ area[:name].force_encoding("UTF-8")
21
+ new(area)
22
+ end
23
+ end
24
+ end
25
+
26
+ module Utils
27
+ LAT_WIDTH = [nil,2400000,300000,150000, 75000,37500,18750, 9375 ]
28
+ LON_WIDTH = [nil,3600000,450000,225000,112500,56250,28125,14062.5]
29
+ class << self
30
+ def lat_lng_to_meshcode(lat_deg, lng_deg)
31
+ lat = (lat_deg * 3600000).to_i
32
+ lng = (lng_deg * 3600000).to_i
33
+ meshcode = ''
34
+ la = lat
35
+ lo = lng - 100*60*60*1000
36
+ for i in 1..7
37
+ a = (la / LAT_WIDTH[i]).to_i
38
+ b = (lo / LON_WIDTH[i]).to_i
39
+ la -= a * LAT_WIDTH[i]
40
+ lo -= b * LON_WIDTH[i]
41
+ case i
42
+ when 1
43
+ meshcode = sprintf("%02d%02d",a,b)
44
+ when 2
45
+ meshcode << sprintf("%d%d",a,b)
46
+ else
47
+ meshcode << sprintf("%d",a*2+b)
48
+ end
49
+ end
50
+ return meshcode
51
+ end
52
+
53
+ def expand_meshcode(meshcode)
54
+ (6..12).map do |n|
55
+ meshcode[0, n]
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ =begin
63
+ require 'sequel'
64
+ db = Sequel.sqlite("iareadata.sqlite3")
65
+ meshcodes = Iarea::Utils.expand_meshcode(meshcode)
66
+ puts db[:meshes].where(:meshcode => meshcodes).join(:areas, :areacode => :areacode).first[:name]
67
+ =end
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+ require 'tmpdir'
4
+
5
+ namespace :iarea do
6
+ desc "generate database"
7
+ task :generate => ["fetch:def", "fetch:data"]
8
+
9
+ namespace :fetch do
10
+ desc "fetch iarea metadata"
11
+ task :def do
12
+ uri = "http://www.nttdocomo.co.jp/service/imode/make/content/iarea/domestic/map/js/iarea_def.js"
13
+ js = URI(uri).read.force_encoding('cp932').encode('UTF-8')
14
+ js.sub!(%r|^var IAreaDef=new function\(\)\{|, "")
15
+ data = {}
16
+ js.scan %r|this\.(.+?)=(.+?);|m do |key, json|
17
+ data[key] = JSON.parse(json)
18
+ end
19
+ end
20
+
21
+ desc "fetch iareadata.lzh"
22
+ task :data do
23
+ Dir.mktmpdir("iareadata") do |dir|
24
+ Dir.chdir(dir) do
25
+ uri = "http://www.nttdocomo.co.jp/binary/archive/service/imode/make/content/iarea/domestic/iareadata.lzh"
26
+ File.open("iareadata.lzh", "w") do |f|
27
+ f.write URI(uri).read
28
+ end
29
+ system "lha", "xf", "iareadata.lzh"
30
+ end
31
+ ruby "./tools/import_meshes.rb", dir, "db/iareadata.sqlite3"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+
4
+ describe Iarea::Utils do
5
+ it ".lat_lng_to_meshcode" do
6
+ Iarea::Utils.lat_lng_to_meshcode(42.8840971954764,141.571321907496).should == "64412430032"
7
+ end
8
+ it ".expand_meshcode" do
9
+ Iarea::Utils.expand_meshcode("64412430032").should == ["644124", "6441243", "64412430", "644124300", "6441243003", "64412430032", "64412430032"]
10
+ end
11
+ end
12
+
13
+ describe Iarea::Area do
14
+ context "at 43.0568397222222,141.3478975" do
15
+ subject do
16
+ Iarea::Area.find_by_lat_lng(43.0568397222222,141.3478975)
17
+ end
18
+
19
+ it "#name" do
20
+ subject.name.should == "すすきの"
21
+ end
22
+
23
+ it "#areacode" do
24
+ subject.areacode.should == "00208"
25
+ end
26
+
27
+ it "#areaid" do
28
+ subject.areaid.should == "002"
29
+ end
30
+
31
+ it "#subareaid" do
32
+ subject.subareaid.should == "08"
33
+ end
34
+ end
35
+
36
+ context "at out of Japan" do
37
+ subject do
38
+ Iarea::Area.find_by_lat_lng(0,0)
39
+ end
40
+
41
+ it "should be nil" do
42
+ should be_nil
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ require 'spec'
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
3
+ require 'iarea'
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv'
4
+ require 'json'
5
+ require 'sequel'
6
+
7
+ if ARGV.size != 2
8
+ puts "usage #$0 [src_dir_contains_iareadata_dir] [dest_sqlite3_file]"
9
+ exit -1
10
+ end
11
+
12
+ src_path = ARGV.shift
13
+ dest_path = ARGV.shift
14
+
15
+ db = Sequel.sqlite(dest_path)
16
+
17
+ db.drop_table :meshes if db.table_exists?(:meshes)
18
+ db.create_table :meshes do
19
+ String :meshcode
20
+ String :areacode
21
+ index :meshcode
22
+ index :areacode
23
+ end
24
+
25
+ db.drop_table :areas if db.table_exists?(:areas)
26
+ db.create_table :areas do
27
+ String :areaid
28
+ String :subareaid
29
+ String :areacode
30
+ String :name
31
+ Integer :west
32
+ Integer :south
33
+ Integer :east
34
+ Integer :north
35
+ index :areacode
36
+ end
37
+
38
+ db.transaction do
39
+ meshes = db[:meshes]
40
+ areas = db[:areas]
41
+ Dir[File.join(src_path, "iareadata/iarea*.txt")].each do |path|
42
+ data = CSV.open(path, "r:cp932")
43
+ data.each do |row|
44
+ areaid, subareaid, name, west, south, east, north, *mesh_data = row
45
+ areacode = areaid + subareaid
46
+
47
+ name.encode!("UTF-8")
48
+ # insert area
49
+ area = { :areaid => areaid,
50
+ :subareaid => subareaid,
51
+ :areacode => areacode,
52
+ :name => name,
53
+ :west => west,
54
+ :south => south,
55
+ :east => east,
56
+ :north => north }
57
+ areas.insert(area)
58
+
59
+ mesh_data.pop if mesh_data.last.nil?
60
+ $stderr.puts "importing area %s %s" % [areacode, name]
61
+ num_meshes = mesh_data.shift(7).map(&:to_i)
62
+
63
+ expected_num_meshes = num_meshes.inject(&:+)
64
+ if expected_num_meshes != mesh_data.size
65
+ raise "number of meshes mismatch: expected %d, actual %d" % [
66
+ expected_num_meshes, mesh_data.size
67
+ ]
68
+ end
69
+
70
+ # insert meshes
71
+ mesh_data.each do |mesh|
72
+ meshes.insert(:meshcode => mesh, :areacode => areacode)
73
+ end
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iarea
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Yoji Shidara
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-26 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sequel
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 10
31
+ - 0
32
+ version: 3.10.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: sqlite3
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 2
46
+ - 5
47
+ version: 1.2.5
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 1
60
+ - 3
61
+ - 0
62
+ version: 1.3.0
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: A library for DoCoMo Open Iarea.
66
+ email: dara@shidara.net
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - README.rdoc
73
+ files:
74
+ - .gitignore
75
+ - MIT-LICENSE
76
+ - README.rdoc
77
+ - Rakefile
78
+ - VERSION
79
+ - db/iareadata.sqlite3
80
+ - iarea.gemspec
81
+ - lib/iarea.rb
82
+ - lib/tasks/iarea.rake
83
+ - test/iarea/iarea_test.rb
84
+ - test/test_helper.rb
85
+ - tools/import_meshes.rb
86
+ has_rdoc: true
87
+ homepage: http://github.com/darashi/iarea
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options:
92
+ - --charset=UTF-8
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.6.1
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: A library for DoCoMo Open Iarea.
118
+ test_files:
119
+ - test/iarea/iarea_test.rb
120
+ - test/test_helper.rb