iarea 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,13 +2,13 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  iarea (0.3.0)
5
- leveldb-ruby
5
+ msgpack
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
10
  diff-lcs (1.1.2)
11
- leveldb-ruby (0.7)
11
+ msgpack (0.4.5)
12
12
  rake (0.9.2)
13
13
  rdoc (3.8)
14
14
  rspec (2.6.0)
Binary file
Binary file
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_runtime_dependency 'leveldb-ruby'
21
+ s.add_runtime_dependency 'msgpack'
22
22
  s.add_development_dependency 'rspec'
23
23
  s.add_development_dependency 'rdoc'
24
24
  s.add_development_dependency 'rake'
@@ -1,10 +1,11 @@
1
- require 'leveldb'
2
1
  require 'json'
2
+ require 'msgpack'
3
3
  require 'ostruct'
4
4
  require_relative 'iarea/version'
5
5
 
6
6
  module Iarea # :nodoc:
7
- DB = LevelDB::DB.load(File.expand_path("../../db/iareadata", __FILE__)) # :nodoc:
7
+ DB = MessagePack::unpack(File.read File.expand_path("../../db/meta.msgpack", __FILE__)) # :nodoc:
8
+ MESH = MessagePack::unpack(File.read File.expand_path("../../db/mesh.msgpack", __FILE__)) # :nodoc:
8
9
  autoload :Area, File.expand_path('../iarea/area', __FILE__)
9
10
  autoload :Zone, File.expand_path('../iarea/zone', __FILE__)
10
11
  autoload :Prefecture, File.expand_path('../iarea/prefecture', __FILE__)
@@ -15,20 +15,20 @@ module Iarea
15
15
 
16
16
  # Neighbor areas of the area
17
17
  def neighbors
18
- JSON.parse(DB['n:'+self.areacode]).map{|areacode| Area.find areacode}
18
+ @table[:neighbors].map{|areacode| Area.find areacode}
19
19
  end
20
20
 
21
21
  class << self
22
22
  # Find area by <tt>areacode</tt>
23
23
  def find(areacode)
24
- @@areas[areacode] ||= new JSON.parse(DB['a:'+areacode])
24
+ @@areas[areacode] ||= new DB['area'][areacode]
25
25
  end
26
26
 
27
27
  # Find area by latitude and longitude in degrees
28
28
  def find_by_lat_lng(lat, lng)
29
29
  meshcodes = Utils.expand_meshcode(Utils.lat_lng_to_meshcode(lat, lng))
30
30
  meshcodes.each do |meshcode|
31
- if areacode = DB['m:'+meshcode]
31
+ if areacode = MESH[meshcode]
32
32
  return find(areacode)
33
33
  end
34
34
  end
@@ -16,13 +16,12 @@ module Iarea
16
16
  class << self
17
17
  # Find a prefecture by <tt>id</tt>
18
18
  def find(id)
19
- id = id.to_s
20
- @@prefectures[id] ||= new JSON.parse(DB['p:'+id])
19
+ @@prefectures[id.to_i] ||= new DB['prefecture'][id.to_i]
21
20
  end
22
21
 
23
22
  # All prefectures
24
23
  def all
25
- JSON.parse(DB['p']).map{ |id| find(id) }
24
+ DB['prefecture_ids'].map{ |id| find(id) }
26
25
  end
27
26
  end
28
27
  end
@@ -1,3 +1,3 @@
1
1
  module Iarea
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -15,13 +15,12 @@ module Iarea
15
15
  class << self
16
16
  # Find a zone by <tt>id</tt>
17
17
  def find(id)
18
- id = id.to_s
19
- @@zones[id] ||= new JSON.parse(DB['z:'+id])
18
+ @@zones[id.to_i] ||= new DB['zone'][id.to_i]
20
19
  end
21
20
 
22
21
  # All zones
23
22
  def all
24
- JSON.parse(DB['z']).map{ |id| find(id) }
23
+ DB['zone_ids'].map{|id| Zone.find id}
25
24
  end
26
25
  end
27
26
  end
@@ -4,33 +4,27 @@ require 'tmpdir'
4
4
  require 'tempfile'
5
5
 
6
6
  namespace :iarea do
7
- desc "generate database"
8
- task :generate => ["fetch:def", "fetch:data"]
7
+ desc "generate data"
8
+ task :generate do
9
+ # fetch iarea metadata
10
+ uri = "http://www.nttdocomo.co.jp/service/imode/make/content/iarea/domestic/map/js/iarea_def.js"
11
+ tf = Tempfile.open("iarea")
12
+ tf.write URI(uri).read
13
+ tf.close
9
14
 
10
- namespace :fetch do
11
- desc "fetch iarea metadata"
12
- task :def do
13
- uri = "http://www.nttdocomo.co.jp/service/imode/make/content/iarea/domestic/map/js/iarea_def.js"
14
- tf = Tempfile.open("iarea")
15
- tf.write URI(uri).read
16
- tf.close
17
- ruby "./tools/import_areas.rb", tf.path, "db/iareadata"
18
- end
19
-
20
- desc "fetch iareadata.lzh"
21
- task :data do
22
- Dir.mktmpdir("iareadata") do |dir|
23
- Dir.chdir(dir) do
24
- uri = "http://www.nttdocomo.co.jp/binary/archive/service/imode/make/content/iarea/domestic/iareadata.lzh"
25
- File.open("iareadata.lzh", "w") do |f|
26
- f.write URI(uri).read
27
- end
28
- unless system("lha", "xf", "iareadata.lzh")
29
- raise "lha failed"
30
- end
15
+ # fetch and extract iareadata.lzh
16
+ Dir.mktmpdir("iareadata") do |dir|
17
+ Dir.chdir(dir) do
18
+ uri = "http://www.nttdocomo.co.jp/binary/archive/service/imode/make/content/iarea/domestic/iareadata.lzh"
19
+ File.open("iareadata.lzh", "w") do |f|
20
+ f.write URI(uri).read
21
+ end
22
+ unless system("lha", "xf", "iareadata.lzh")
23
+ raise "lha failed"
31
24
  end
32
- ruby "./tools/import_meshes.rb", dir, "db/iareadata"
33
25
  end
26
+
27
+ ruby './tools/generate.rb', 'db', tf.path, dir
34
28
  end
35
29
  end
36
30
  end
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require 'csv'
5
+ require 'json'
6
+
7
+ require 'rubygems'
8
+ require 'bundler/setup'
9
+ require 'msgpack'
10
+
11
+ def import_meshes(src_path)
12
+ mesh_to_iarea_hash = {}
13
+ Dir[File.join(src_path, "iareadata/iarea*.txt")].each do |path|
14
+ data = CSV.open(path, "r:cp932")
15
+ data.each do |row|
16
+ areaid, subareaid, name, west, south, east, north, *mesh_data = row
17
+ areacode = areaid + subareaid
18
+
19
+ name.encode!("UTF-8")
20
+
21
+ mesh_data.pop if mesh_data.last.nil?
22
+ $stderr.puts " importing meshes %s %s" % [areacode, name]
23
+ num_meshes = mesh_data.shift(7).map(&:to_i)
24
+
25
+ expected_num_meshes = num_meshes.inject(&:+)
26
+ if expected_num_meshes != mesh_data.size
27
+ raise "number of meshes mismatch: expected %d, actual %d" % [
28
+ expected_num_meshes, mesh_data.size
29
+ ]
30
+ end
31
+
32
+ # insert meshes
33
+ mesh_data.each do |mesh|
34
+ mesh_to_iarea_hash[mesh] = areacode
35
+ end
36
+ end
37
+ end
38
+ mesh_to_iarea_hash
39
+ end
40
+
41
+ def import_metadata(src_path)
42
+ js = open(src_path, "r:cp932").read
43
+ js.sub!(%r|^var IAreaDef=new function\(\)\{|, "")
44
+ data = {}
45
+ js.scan %r|this\.(.+?)=(.+?);|m do |key, json|
46
+ data[key] = JSON.parse(json)
47
+ end
48
+
49
+ db = Hash.new{|h,k| h[k] = {}}
50
+
51
+ # zones
52
+ data['zones'].each_with_index do |zone, i|
53
+ id = i + 1
54
+ $stderr.puts " importing zone %d %s" % [id, zone['name']]
55
+ areacodes = data['areas'].select {|areacode, area| area['zone'] == i}.keys
56
+ prefecture_ids = data['prefs'].each.with_index.select {|pref, j| pref['zone'] == i}.map{|pref, j| j+1 }
57
+
58
+ db['zone'][id] = {
59
+ :id => id,
60
+ :name => zone['name'],
61
+ :areacodes => areacodes,
62
+ :prefecture_ids => prefecture_ids
63
+ }
64
+ end
65
+
66
+ db['zone_ids'] = data['zones'].map.with_index{|z, i| i + 1}
67
+
68
+ # prefs
69
+ data['prefs'].each_with_index do |pref, i|
70
+ id = i + 1
71
+ $stderr.puts " importing prefecture %d %s" % [id, pref['name']]
72
+ areacodes = data['areas'].select {|areacode, area| area['pref'] == i}.keys
73
+ db['prefecture'][id] = {
74
+ :id => id,
75
+ :name => pref['name'],
76
+ :zone_id => pref['zone'] + 1,
77
+ :areacodes => areacodes
78
+ }
79
+ end
80
+ db['prefecture_ids'] = data['prefs'].map.with_index{|z, i| i + 1}
81
+
82
+ # areas
83
+ data['areas'].each do |areacode, area|
84
+ $stderr.puts " importing area %s %s" % [areacode, area['name']]
85
+ # insert area
86
+ hash = {
87
+ :areaid => areacode[0..2],
88
+ :subareaid => areacode[3..4],
89
+ :areacode => areacode,
90
+ :name => area['name'],
91
+ :west => area['msWest'],
92
+ :south => area['msSouth'],
93
+ :east => area['msEast'],
94
+ :north => area['msNorth'],
95
+ :zone_id => area['zone'] + 1,
96
+ :prefecture_id => area['pref'] + 1,
97
+ :neighbors => area['neighbors']
98
+ }
99
+ # fix .js problem
100
+ if hash[:name] == '新宿1?2丁目'
101
+ hash[:name] = '新宿1〜2丁目'
102
+ end
103
+ db['area'][areacode] = hash
104
+ end
105
+ db
106
+ end
107
+
108
+
109
+ db_dir = ARGV.shift
110
+ js_path = ARGV.shift
111
+ iareadata_dir = ARGV.shift
112
+
113
+ meta = import_metadata(js_path)
114
+ File.open(File.join(db_dir, 'meta.msgpack'), 'w') do |f|
115
+ f.write meta.to_msgpack
116
+ end
117
+
118
+ mesh_to_iarea_hash = import_meshes(iareadata_dir)
119
+ File.open(File.join(db_dir, 'mesh.msgpack'), 'w') do |f|
120
+ f.write mesh_to_iarea_hash.to_msgpack
121
+ end
122
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iarea
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-01 00:00:00.000000000 +09:00
13
- default_executable:
12
+ date: 2011-08-01 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
- name: leveldb-ruby
17
- requirement: &2157498120 !ruby/object:Gem::Requirement
15
+ name: msgpack
16
+ requirement: &2157922240 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: '0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *2157498120
24
+ version_requirements: *2157922240
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: rspec
28
- requirement: &2157497700 !ruby/object:Gem::Requirement
27
+ requirement: &2157921820 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ! '>='
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: '0'
34
33
  type: :development
35
34
  prerelease: false
36
- version_requirements: *2157497700
35
+ version_requirements: *2157921820
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: rdoc
39
- requirement: &2157497280 !ruby/object:Gem::Requirement
38
+ requirement: &2157921400 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,10 +43,10 @@ dependencies:
44
43
  version: '0'
45
44
  type: :development
46
45
  prerelease: false
47
- version_requirements: *2157497280
46
+ version_requirements: *2157921400
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: rake
50
- requirement: &2157496860 !ruby/object:Gem::Requirement
49
+ requirement: &2157920980 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ! '>='
@@ -55,10 +54,10 @@ dependencies:
55
54
  version: '0'
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *2157496860
57
+ version_requirements: *2157920980
59
58
  - !ruby/object:Gem::Dependency
60
59
  name: bundler
61
- requirement: &2157496440 !ruby/object:Gem::Requirement
60
+ requirement: &2157920560 !ruby/object:Gem::Requirement
62
61
  none: false
63
62
  requirements:
64
63
  - - ! '>='
@@ -66,7 +65,7 @@ dependencies:
66
65
  version: '0'
67
66
  type: :development
68
67
  prerelease: false
69
- version_requirements: *2157496440
68
+ version_requirements: *2157920560
70
69
  description: A library for DoCoMo Open Iarea.
71
70
  email:
72
71
  - dara@shidara.net
@@ -83,16 +82,8 @@ files:
83
82
  - README.rdoc
84
83
  - Rakefile
85
84
  - VERSION
86
- - db/iareadata/000013.sst
87
- - db/iareadata/000014.sst
88
- - db/iareadata/000015.sst
89
- - db/iareadata/000016.log
90
- - db/iareadata/000017.sst
91
- - db/iareadata/CURRENT
92
- - db/iareadata/LOCK
93
- - db/iareadata/LOG
94
- - db/iareadata/LOG.old
95
- - db/iareadata/MANIFEST-000004
85
+ - db/mesh.msgpack
86
+ - db/meta.msgpack
96
87
  - iarea.gemspec
97
88
  - lib/iarea.rb
98
89
  - lib/iarea/area.rb
@@ -106,9 +97,7 @@ files:
106
97
  - spec/iarea/util_spec.rb
107
98
  - spec/iarea/zone_spec.rb
108
99
  - spec/spec_helper.rb
109
- - tools/import_areas.rb
110
- - tools/import_meshes.rb
111
- has_rdoc: true
100
+ - tools/generate.rb
112
101
  homepage: http://github.com/darashi/iarea
113
102
  licenses: []
114
103
  post_install_message:
@@ -129,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
118
  version: '0'
130
119
  requirements: []
131
120
  rubyforge_project: iarea
132
- rubygems_version: 1.6.1
121
+ rubygems_version: 1.8.3
133
122
  signing_key:
134
123
  specification_version: 3
135
124
  summary: A library for DoCoMo Open Iarea.
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1 +0,0 @@
1
- MANIFEST-000004
File without changes
@@ -1,27 +0,0 @@
1
- 2011/08/01-06:56:10.243731 7fff7e529960 Recovering log #3
2
- 2011/08/01-06:56:10.245217 7fff7e529960 Level-0 table #5: started
3
- 2011/08/01-06:56:10.247597 7fff7e529960 Level-0 table #5: 137776 bytes OK
4
- 2011/08/01-06:56:10.248902 7fff7e529960 Delete type=0 #3
5
- 2011/08/01-06:56:10.248995 7fff7e529960 Delete type=3 #2
6
- 2011/08/01-06:56:11.052004 1007f2000 Level-0 table #8: started
7
- 2011/08/01-06:56:11.087336 1007f2000 Level-0 table #8: 1540152 bytes OK
8
- 2011/08/01-06:56:11.093042 1007f2000 Delete type=0 #6
9
- 2011/08/01-06:56:11.913693 1007f2000 Level-0 table #10: started
10
- 2011/08/01-06:56:11.961970 1007f2000 Level-0 table #10: 1538749 bytes OK
11
- 2011/08/01-06:56:11.968064 1007f2000 Delete type=0 #7
12
- 2011/08/01-06:56:12.808704 1007f2000 Level-0 table #12: started
13
- 2011/08/01-06:56:12.841190 1007f2000 Level-0 table #12: 1532571 bytes OK
14
- 2011/08/01-06:56:12.850141 1007f2000 Delete type=0 #9
15
- 2011/08/01-06:56:12.850476 1007f2000 Compacting 4@0 + 0@1 files
16
- 2011/08/01-06:56:12.972097 1007f2000 Generated table #13: 109414 keys, 2116092 bytes
17
- 2011/08/01-06:56:13.060578 1007f2000 Generated table #14: 114250 keys, 2115011 bytes
18
- 2011/08/01-06:56:13.077859 1007f2000 Generated table #15: 26061 keys, 513678 bytes
19
- 2011/08/01-06:56:13.077922 1007f2000 Compacted 4@0 + 0@1 files => 4744781 bytes
20
- 2011/08/01-06:56:13.081068 1007f2000 Delete type=2 #5
21
- 2011/08/01-06:56:13.081286 1007f2000 Delete type=2 #8
22
- 2011/08/01-06:56:13.081596 1007f2000 Delete type=2 #10
23
- 2011/08/01-06:56:13.081880 1007f2000 Delete type=2 #12
24
- 2011/08/01-06:56:13.082217 1007f2000 compacted to: files[ 0 3 0 0 0 0 0 ]
25
- 2011/08/01-06:56:13.662966 1007f2000 Level-0 table #17: started
26
- 2011/08/01-06:56:13.700564 1007f2000 Level-0 table #17: 1535854 bytes OK
27
- 2011/08/01-06:56:13.705899 1007f2000 Delete type=0 #11
@@ -1 +0,0 @@
1
- 2011/08/01-06:56:08.209457 7fff7e529960 Delete type=3 #1
Binary file
@@ -1,79 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- require 'json'
5
-
6
- require 'rubygems'
7
- require 'bundler/setup'
8
- require 'leveldb'
9
-
10
- if ARGV.size != 2
11
- puts "usage #$0 [iarea_def.js] [dest_lebeldb_file]"
12
- exit -1
13
- end
14
-
15
- src_path = ARGV.shift
16
- dest_path = ARGV.shift
17
-
18
- db = LevelDB::DB.new dest_path
19
-
20
- js = open(src_path, "r:cp932").read
21
- js.sub!(%r|^var IAreaDef=new function\(\)\{|, "")
22
- data = {}
23
- js.scan %r|this\.(.+?)=(.+?);|m do |key, json|
24
- data[key] = JSON.parse(json)
25
- end
26
- # zones
27
- data['zones'].each_with_index do |zone, i|
28
- id = i + 1
29
- $stderr.puts " importing zone %d %s" % [id, zone['name']]
30
- areacodes = data['areas'].select {|areacode, area| area['zone'] == i}.keys
31
- prefecture_ids = data['prefs'].each.with_index.select {|pref, j| pref['zone'] == i}.map{|pref, j| j+1 }
32
-
33
- db['z:%d' % id] = JSON.dump(
34
- :id => id,
35
- :name => zone['name'],
36
- :areacodes => areacodes,
37
- :prefecture_ids => prefecture_ids
38
- )
39
- end
40
-
41
- db['z'] = JSON.dump(data['zones'].map.with_index{|z, i| i + 1})
42
-
43
- # prefs
44
- data['prefs'].each_with_index do |pref, i|
45
- id = i + 1
46
- $stderr.puts " importing prefecture %d %s" % [id, pref['name']]
47
- areacodes = data['areas'].select {|areacode, area| area['pref'] == i}.keys
48
- db['p:%d' % id] = JSON.dump(
49
- :id => id,
50
- :name => pref['name'],
51
- :zone_id => pref['zone'] + 1,
52
- :areacodes => areacodes
53
- )
54
- end
55
- db['p'] = JSON.dump(data['prefs'].map.with_index{|z, i| i + 1})
56
-
57
- # areas
58
- data['areas'].each do |areacode, area|
59
- $stderr.puts " importing area %s %s" % [areacode, area['name']]
60
- # insert area
61
- hash = {
62
- :areaid => areacode[0..2],
63
- :subareaid => areacode[3..4],
64
- :areacode => areacode,
65
- :name => area['name'],
66
- :west => area['msWest'],
67
- :south => area['msSouth'],
68
- :east => area['msEast'],
69
- :north => area['msNorth'],
70
- :zone_id => area['zone'] + 1,
71
- :prefecture_id => area['pref'] + 1
72
- }
73
- # fix .js problem
74
- if hash[:name] == '新宿1?2丁目'
75
- hash[:name] = '新宿1〜2丁目'
76
- end
77
- db['a:%s' % areacode] = JSON.dump(hash)
78
- db['n:%s' % areacode] = JSON.dump(area['neighbors'])
79
- end
@@ -1,44 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'csv'
4
-
5
- require 'rubygems'
6
- require 'bundler/setup'
7
- require 'leveldb'
8
-
9
-
10
- if ARGV.size != 2
11
- puts "usage #$0 [src_dir_contains_iareadata_dir] [dest_leveldb_file]"
12
- exit -1
13
- end
14
-
15
- src_path = ARGV.shift
16
- dest_path = ARGV.shift
17
-
18
- db = LevelDB::DB.new dest_path
19
-
20
- Dir[File.join(src_path, "iareadata/iarea*.txt")].each do |path|
21
- data = CSV.open(path, "r:cp932")
22
- data.each do |row|
23
- areaid, subareaid, name, west, south, east, north, *mesh_data = row
24
- areacode = areaid + subareaid
25
-
26
- name.encode!("UTF-8")
27
-
28
- mesh_data.pop if mesh_data.last.nil?
29
- $stderr.puts " importing meshes %s %s" % [areacode, name]
30
- num_meshes = mesh_data.shift(7).map(&:to_i)
31
-
32
- expected_num_meshes = num_meshes.inject(&:+)
33
- if expected_num_meshes != mesh_data.size
34
- raise "number of meshes mismatch: expected %d, actual %d" % [
35
- expected_num_meshes, mesh_data.size
36
- ]
37
- end
38
-
39
- # insert meshes
40
- mesh_data.each do |mesh|
41
- db['m:%s' % mesh] = areacode
42
- end
43
- end
44
- end