offline_geocoder 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4410ff33ce206d140d141583e7c7e5cc8760db87
4
- data.tar.gz: 100cff63bbc8c2d6dbf7c0d15d64cb618f5134e1
2
+ SHA256:
3
+ metadata.gz: 624ae1437a79be0335573dcec94f1677ed1979636baf41a6610bee14ff42d9f0
4
+ data.tar.gz: f025d8180b5b982684ccd920f91c4fecdb4a831db4a41b1c8ed21ad3a0f867d3
5
5
  SHA512:
6
- metadata.gz: 0a7de65b07395318d3da0ba2b7af18f742a7b3dc75334528f507cd314eabdc7ce6d248b05f54116c756610f2c8db88d85cde2dc25ec90d02c642ff9e26b61805
7
- data.tar.gz: 276388822d2d65a6c9b65a21ff9e897d002d4fd74186ca1277e22f0b2e39b9e79fe0f3c198bf8342c0a1264bca277f7f0bfa86bb703d9573ca15b347a77e0db8
6
+ metadata.gz: 225e5c077fcd9c9fc5c4dfd093683059a8ae54704dbf673920a8037b6a22ac9a1def33c05e240e86fd71013844f89ed447fe4e5d17624d70213e63669d2f73fb
7
+ data.tar.gz: 50cb8b88b0918987d13c8be1cbddfe664da39e970b67c98b809b7199c63b8bdbc9d3626d24b0f6ace42213d97a00e3f7bdb6c010504b684a08c79b287da883e8
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in offline_geocoder.gemspec
4
6
  gemspec
7
+ gem 'coveralls', require: false
data/LICENSE CHANGED
@@ -186,7 +186,7 @@
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright 2015 Electronic Inaka LLC
189
+ Copyright 2020 Roberto Romero
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
data/README.md CHANGED
@@ -1,3 +1,8 @@
1
+ [![Build Status](https://travis-ci.org/sildur/offline_geocoder.svg)](https://travis-ci.org/sildur/offline_geocoder)
2
+ [![Code Climate](https://codeclimate.com/github/sildur/offline_geocoder/badges/gpa.svg)](https://codeclimate.com/github/sildur/offline_geocoder)
3
+ ![Gem](https://img.shields.io/gem/v/offline_geocoder)
4
+ ![Gem](https://img.shields.io/gem/dt/offline_geocoder)
5
+
1
6
  # OfflineGeocoder
2
7
 
3
8
  A gem for offline reverse geocoding. It uses data from the [GeoNames](http://www.geonames.org/) project.
@@ -22,11 +27,8 @@ Or install it yourself as:
22
27
 
23
28
  ```ruby
24
29
  require "offline_geocoder"
25
-
26
30
  geocoder = OfflineGeocoder.new
27
-
28
31
  results = geocoder.search(51.5214588, -0.1729636)
29
-
30
32
  p results
31
33
  ```
32
34
 
@@ -36,9 +38,47 @@ The above code will output this:
36
38
  {:lat=>51.51116, :lon=>-0.18426, :name=>"Bayswater", :admin1=>"England", :admin2=>"Greater London", :cc=>"GB", :country=>"United Kingdom"}
37
39
  ```
38
40
 
41
+ Alternatively, you can use named parameters when searching:
42
+
43
+ ```ruby
44
+ results = geocoder.search(lat: 51.5214588, lon: -0.1729636)
45
+ ```
46
+
47
+ ### Searching for names or attributes
48
+
49
+ You can search for names, countries and such. The first result will be
50
+ returned.
51
+
52
+ Searches are case sensitive and must match entirely. e.g. "York" will
53
+ not find "New York", and "Cote dIvoire" will not match "Cote d'Ivoire".
54
+
55
+ ```ruby
56
+ require "offline_geocoder"
57
+ geocoder = OfflineGeocoder.new
58
+ aus = geocoder.search(name: "Bayswater")
59
+ p aus
60
+ gb = geocoder.search(name: "Bayswater", country: "United Kingdom")
61
+ p gb
62
+ ```
63
+
64
+ The above code will output this:
65
+
66
+ ```ruby
67
+ {:lat=>-37.85, :lon=>145.26667, :name=>"Bayswater", :admin1=>"Victoria", :admin2=>"Knox", :cc=>"AU", :country=>"Australia"}
68
+ {:lat=>51.51116, :lon=>-0.18426, :name=>"Bayswater", :admin1=>"England", :admin2=>"Greater London", :cc=>"GB", :country=>"United Kingdom"}
69
+ ```
39
70
 
40
71
  ## Development
41
72
 
42
73
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
43
74
 
44
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+ To install this gem onto your local machine, run `bundle exec rake install`.
76
+
77
+ ## Contact Us
78
+
79
+ For **questions** or **general comments** regarding the use of this library, please use our public
80
+ [hipchat room](http://inaka.net/hipchat).
81
+
82
+ If you find any **bugs** or have a **problem** while using this library, please [open an issue](https://github.com/inaka/galgo/issues/new) in this repo (or a pull request :)).
83
+
84
+ And you can check all of our open-source projects at [inaka.github.io](http://inaka.github.io)
data/Rakefile CHANGED
@@ -1,5 +1,7 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
- task :default => :spec
7
+ task default: :spec
data/bin/console CHANGED
File without changes
data/bin/setup CHANGED
@@ -3,5 +3,3 @@ set -euo pipefail
3
3
  IFS=$'\n\t'
4
4
 
5
5
  bundle install
6
-
7
- # Do any other automated setup that you need to do here
@@ -1,46 +1,51 @@
1
- require "offline_geocoder/version"
2
- require "csv"
3
- require "kdtree"
1
+ # frozen_string_literal: true
2
+
3
+ require 'offline_geocoder/version'
4
+ require 'csv'
5
+ require 'geokdtree'
4
6
 
5
7
  class OfflineGeocoder
8
+ CSV_PATH = File.expand_path('../og_cities1000.csv', __dir__)
9
+
6
10
  def initialize
7
- unless defined? @@cities
8
- @@cities = []
9
- @@points = []
10
- index = 0
11
- csv_path = File.expand_path("../../og_cities1000.csv", __FILE__)
12
- lines = File.read(csv_path).split("\n")
13
- @@fields = lines[0].split(',').collect(&:to_sym)
14
- lines[1..-1].each {|line|
15
- parsed_line =
16
- if line.include?('"')
17
- CSV.parse(line)[0]
18
- else
19
- line.split(',')
20
- end
21
- parsed_line[0] = parsed_line[0].to_f
22
- parsed_line[1] = parsed_line[1].to_f
23
- @@cities << parsed_line
24
- @@points << [parsed_line[0], parsed_line[1], index]
25
- index += 1
26
- }
27
-
28
- @@tree = Kdtree.new(@@points)
29
- return nil
11
+ return if defined? @@cities
12
+
13
+ @@cities = []
14
+ @@tree = Geokdtree::Tree.new(2)
15
+ @@table = []
16
+ index = 0
17
+ CSV.foreach(CSV_PATH, headers: true, header_converters: :symbol) do |row|
18
+ as_hash = row.to_h
19
+ as_hash[:lat] = as_hash[:lat].to_f
20
+ as_hash[:lon] = as_hash[:lon].to_f
21
+ @@tree.insert([row[:lat], row[:lon]], index)
22
+ @@table << as_hash
23
+ index += 1
30
24
  end
31
25
  end
32
26
 
33
- def search(latitude, longitude)
34
- record = @@cities[@@tree.nearest(latitude.to_f, longitude.to_f)]
35
- hash_record = {}
36
- record.each_with_index do |value, index|
37
- hash_record[@@fields[index]] = value
27
+ def search(query, lon = nil)
28
+ lat, lon = lon.nil? ? [query[:lat], query[:lon]] : [query, lon]
29
+
30
+ if lat && lon
31
+ search_by_latlon(lat.to_f, lon.to_f)
32
+ else
33
+ search_by_attr(query)
38
34
  end
39
- hash_record
40
35
  end
41
36
 
42
37
  # Hide internal variables
43
38
  def inspect
44
- "#<#{self.class}:0x#{'%014x' % (self.object_id << 1)}>"
39
+ "#<#{self.class}:0x#{format('%<id>014x', id: (object_id << 1))}>"
40
+ end
41
+
42
+ private
43
+
44
+ def search_by_latlon(lat, lon)
45
+ @@table[@@tree.nearest([lat, lon]).data.to_i].to_h
46
+ end
47
+
48
+ def search_by_attr(query = {})
49
+ @@table.select { |object| object >= query }.first
45
50
  end
46
51
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class OfflineGeocoder
2
- VERSION = "0.1.0"
4
+ VERSION = '0.2.0'
3
5
  end
@@ -1,26 +1,35 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'offline_geocoder/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "offline_geocoder"
8
+ spec.name = 'offline_geocoder'
8
9
  spec.version = OfflineGeocoder::VERSION
9
- spec.authors = ["Roberto Romero"]
10
- spec.email = ["roberto@inaka.net"]
10
+ spec.authors = ['Roberto Romero']
11
+ spec.date = Time.now.utc.strftime('%Y-%m-%d')
12
+ spec.email = ['roberto.offline_geocoder@nirae.at']
13
+
14
+ spec.summary = 'Offline reverse geocoder'
15
+ spec.description = 'Offline reverse geocoder. Uses GeoNames database'
16
+ spec.homepage = 'https://github.com/sildur/offline_geocoder'
17
+ spec.license = 'Apache-2.0'
11
18
 
12
- spec.summary = %q{Offline reverse geocoder}
13
- spec.description = %q{Offline reverse geocoder. Uses GeoNames database}
14
- spec.homepage = "https://github.com/inaka/offline_geocoder"
15
- spec.license = "Apache-2.0"
19
+ spec.metadata = {
20
+ 'bug_tracker_uri' => 'https://github.com/sildur/offline_geocoder/issues',
21
+ 'homepage_uri' => 'https://github.com/sildur/offline_geocoder',
22
+ 'source_code_uri' => 'https://github.com/sildur/offline_geocoder'
23
+ }
16
24
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
25
+ spec.files =
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec/|\.+)}) }
27
+ spec.bindir = 'bin'
28
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
21
30
 
22
- spec.add_development_dependency "bundler", "~> 1.8"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec", "~> 3.2"
25
- spec.add_runtime_dependency "kdtree", "~> 0.3"
31
+ spec.add_development_dependency 'bundler', '~> 2.0'
32
+ spec.add_development_dependency 'rake', '~> 12.3.3'
33
+ spec.add_development_dependency 'rspec', '~> 3.2'
34
+ spec.add_runtime_dependency 'geokdtree', '~> 0.2'
26
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: offline_geocoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Romero
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-21 00:00:00.000000000 Z
11
+ date: 2020-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.8'
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.8'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: 12.3.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: 12.3.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -53,29 +53,28 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.2'
55
55
  - !ruby/object:Gem::Dependency
56
- name: kdtree
56
+ name: geokdtree
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.3'
61
+ version: '0.2'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.3'
68
+ version: '0.2'
69
69
  description: Offline reverse geocoder. Uses GeoNames database
70
70
  email:
71
- - roberto@inaka.net
72
- executables: []
71
+ - roberto.offline_geocoder@nirae.at
72
+ executables:
73
+ - console
74
+ - setup
73
75
  extensions: []
74
76
  extra_rdoc_files: []
75
77
  files:
76
- - ".gitignore"
77
- - ".rspec"
78
- - ".travis.yml"
79
78
  - CONTRIBUTING.md
80
79
  - Gemfile
81
80
  - LICENSE
@@ -88,10 +87,13 @@ files:
88
87
  - lib/offline_geocoder/version.rb
89
88
  - offline_geocoder.gemspec
90
89
  - og_cities1000.csv
91
- homepage: https://github.com/inaka/offline_geocoder
90
+ homepage: https://github.com/sildur/offline_geocoder
92
91
  licenses:
93
92
  - Apache-2.0
94
- metadata: {}
93
+ metadata:
94
+ bug_tracker_uri: https://github.com/sildur/offline_geocoder/issues
95
+ homepage_uri: https://github.com/sildur/offline_geocoder
96
+ source_code_uri: https://github.com/sildur/offline_geocoder
95
97
  post_install_message:
96
98
  rdoc_options: []
97
99
  require_paths:
@@ -107,8 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
109
  - !ruby/object:Gem::Version
108
110
  version: '0'
109
111
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.4.5
112
+ rubygems_version: 3.1.2
112
113
  signing_key:
113
114
  specification_version: 4
114
115
  summary: Offline reverse geocoder
data/.gitignore DELETED
@@ -1,13 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- /*gem
11
-
12
- # Ignore RubyMine files
13
- /.idea/
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
data/.travis.yml DELETED
@@ -1,3 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.2.1