geonames-data 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a44320d3a2e77a54cebfe239bcadc6b08609db8d
4
+ data.tar.gz: 9864a5e8d03ff42fde1ac42cf51b6e05ceb2a94a
5
+ SHA512:
6
+ metadata.gz: 2fd9681b61a61cbf253a66512c473bbe4ac23861fe626b1f8c8781c92916754df13d9fb191a9b44e44052e726f6962fc0b14aaf04958077848809a6160fbfc51
7
+ data.tar.gz: 0a93f5a56136abe3ed8436af372741bea6be5514b7101d79aed81d5dc616afd84579967e4ecff1342772717776dae75d3ace3707177cdd0b84ef97552b54df46
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in geonames-data.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 John Carney
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Geonames::Data
2
+
3
+ Library for working with [Geonames data][geonames-data].
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'geonames-data'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install geonames-data
18
+
19
+ ## Usage
20
+
21
+ To load a set of Geonames features:
22
+
23
+ ```ruby
24
+ Geonames::Data.load_features("cities1000.txt")
25
+ ```
26
+
27
+ Some of the Geonames files can be unworkably huge, so you can provide a filter
28
+ block to `load_features`. The following example will load only airports:
29
+
30
+ ```ruby
31
+ Geonames::Data.load_features("allCountries.txt") do |feature|
32
+ feature.feature_code == "AIRP"
33
+ end
34
+ ```
35
+
36
+ ### Indexing
37
+
38
+ Once you've got a set of features, you will probably want to index them
39
+ somehow. There are two classes provided for this: `Geonames::Data::NameIndex`
40
+ and `Geonames::Data::LocationIndex`.
41
+
42
+ #### Name index
43
+
44
+ `Geonames::Data::NameIndex` indexes Geonames features by name, including their
45
+ ASCII name and all alternate names.
46
+
47
+ ```ruby
48
+ cities = Geonames::Data.load_features("cities1000.txt")
49
+ index = Geonames::Data::NameIndex.new(cities)
50
+ index["Melbourne"]
51
+ ```
52
+
53
+ #### Location index
54
+
55
+ `Geonames::Data::LocationIndex` indexes Geonames features by geolocation
56
+ (latitude and longitude).
57
+
58
+ ```ruby
59
+ Geonames::Data.load_features("allCountries.txt") do |feature|
60
+ feature.feature_code == "AIRP"
61
+ end
62
+ index = Geonames::Data::NameIndex.new(cities)
63
+ index.nearest(-37.814, 144.96332)
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( http://github.com/<my-github-username>/geonames-data/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
73
+
74
+ [geonames-data]: http://download.geonames.org/export/dump
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "geonames/data/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "geonames-data"
8
+ spec.version = Geonames::Data::VERSION
9
+ spec.authors = ["John Carney"]
10
+ spec.email = ["john@carney.id.au"]
11
+ spec.summary = %q{Library for working with Geonames data}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.required_ruby_version = '>= 2.1.0'
21
+
22
+ spec.add_runtime_dependency "kdtree"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,12 @@
1
+ require "geonames/data/version"
2
+ require "geonames/data/feature"
3
+ require "geonames/data/name_index"
4
+ require "geonames/data/location_index"
5
+
6
+ module Geonames
7
+ module Data
8
+ def self.load_features(filepath, &filter)
9
+ Feature.load(filepath, &filter)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,58 @@
1
+ require "pathname"
2
+
3
+ module Geonames
4
+ module Data
5
+ class Feature
6
+ def self.field(name)
7
+ @fields ||= []
8
+ index = @fields.size
9
+ @fields << name
10
+ if block_given?
11
+ define_method name do
12
+ yield(@data[index])
13
+ end
14
+ else
15
+ define_method name do
16
+ @data[index]
17
+ end
18
+ end
19
+ end
20
+
21
+ field(:geoname_id, &:to_i)
22
+ field(:name)
23
+ field(:ascii_name)
24
+ field(:alternate_names) { |names| names.split(",") }
25
+ field(:latitude, &:to_f)
26
+ field(:longitude, &:to_f)
27
+ field(:feature_class)
28
+ field(:feature_code)
29
+ field(:country_code)
30
+ field(:cc2) { |cc2| cc2.split(",") }
31
+ field(:admin1_code)
32
+ field(:admin2_code)
33
+ field(:admin3_code)
34
+ field(:admin4_code)
35
+ field(:population, &:to_i)
36
+ field(:elevation, &:to_i)
37
+ field(:dem)
38
+ field(:timezone)
39
+ field(:modification_date)
40
+
41
+ def initialize(data)
42
+ @data = data.dup
43
+ end
44
+
45
+ def names
46
+ @names ||= [ name, ascii_name, *alternate_names ]
47
+ end
48
+
49
+ def self.load(filepath, &filter)
50
+ Pathname.new(filepath).open("r").inject([]) do |features, line|
51
+ feature = Feature.new(line.strip.split("\t"))
52
+ features.push(feature) if !block_given? || yield(feature)
53
+ features
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,23 @@
1
+ require "kdtree"
2
+
3
+ module Geonames
4
+ module Data
5
+ class LocationIndex
6
+ def initialize(features)
7
+ @features = features.inject({}) { |index, feature| index.update(feature.geoname_id => feature) }
8
+ points = features.map do |feature|
9
+ [ feature.latitude, feature.longitude, feature.geoname_id ]
10
+ end
11
+ @index = Kdtree.new(points)
12
+ end
13
+
14
+ def nearest(latitude, longitude)
15
+ @features[@index.nearest(latitude, longitude)]
16
+ end
17
+
18
+ def self.load(filepath, &filter)
19
+ new(Features.load(filepath, filter))
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Geonames
2
+ module Data
3
+ class NameIndex
4
+ def initialize(features)
5
+ @index = features.inject({}) do |index, feature|
6
+ feature.names.inject(index) do |index, name|
7
+ index[name] ||= []
8
+ index[name].push(feature).uniq!
9
+ index
10
+ end
11
+ end
12
+ end
13
+
14
+ def [](name)
15
+ @index[name]
16
+ end
17
+
18
+ def self.load(filepath, &filter)
19
+ new(Feature.load(filepath, &filter))
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Geonames
2
+ module Data
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geonames-data
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Carney
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kdtree
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - john@carney.id.au
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - geonames-data.gemspec
68
+ - lib/geonames/data.rb
69
+ - lib/geonames/data/feature.rb
70
+ - lib/geonames/data/location_index.rb
71
+ - lib/geonames/data/name_index.rb
72
+ - lib/geonames/data/version.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.1.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Library for working with Geonames data
97
+ test_files: []