jpm_geo 0.1.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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +17 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +36 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/jpm_geo.gemspec +33 -0
- data/lib/jpm_geo.rb +29 -0
- data/lib/jpm_geo/bounds.rb +51 -0
- data/lib/jpm_geo/point.rb +143 -0
- data/lib/jpm_geo/version.rb +5 -0
- metadata +108 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 48e481ee36d23cda6002fc637fc95fa227e89822820d1d9543b9bf041e957aff
|
|
4
|
+
data.tar.gz: 4ac9d50173f85299038d212e2640c6ac801b3ea92416bbf11cd2d5f299669c57
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b4905cf4ada72268e65e033292b2aeeac2663e2ce505c0ae914d6b08c4791ea0e4a7dbb735eebccec301c974a921061a89cfa922fbabd5bf1bdb03561b80cbc6
|
|
7
|
+
data.tar.gz: 8619c23099c03caf3e690a6932ec085ab6688b5f7f4b0884294e70383de57f0d5c655b74561074623b629301651ce4490fa9c3955ebabb9e8ecf62d817fb2dd9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metrics/LineLength:
|
|
2
|
+
Max: 120
|
|
3
|
+
IgnoredPatterns: ['^\s*#']
|
|
4
|
+
Exclude:
|
|
5
|
+
- "config/initializers/*.rb"
|
|
6
|
+
|
|
7
|
+
Metrics/BlockLength:
|
|
8
|
+
Exclude:
|
|
9
|
+
- "spec/**/*.rb"
|
|
10
|
+
|
|
11
|
+
# The guiding principle of classes is SRP, SRP can't be accurately measured by LoC
|
|
12
|
+
Metrics/ClassLength:
|
|
13
|
+
Max: 1500
|
|
14
|
+
|
|
15
|
+
# compatibility with rufo
|
|
16
|
+
Style/StringLiterals:
|
|
17
|
+
EnforcedStyle: double_quotes
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at sobakasu@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
jpm_geo (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
diff-lcs (1.3)
|
|
10
|
+
rake (10.5.0)
|
|
11
|
+
rspec (3.8.0)
|
|
12
|
+
rspec-core (~> 3.8.0)
|
|
13
|
+
rspec-expectations (~> 3.8.0)
|
|
14
|
+
rspec-mocks (~> 3.8.0)
|
|
15
|
+
rspec-core (3.8.2)
|
|
16
|
+
rspec-support (~> 3.8.0)
|
|
17
|
+
rspec-expectations (3.8.4)
|
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
+
rspec-support (~> 3.8.0)
|
|
20
|
+
rspec-mocks (3.8.1)
|
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
+
rspec-support (~> 3.8.0)
|
|
23
|
+
rspec-support (3.8.2)
|
|
24
|
+
|
|
25
|
+
PLATFORMS
|
|
26
|
+
ruby
|
|
27
|
+
x64-mingw32
|
|
28
|
+
|
|
29
|
+
DEPENDENCIES
|
|
30
|
+
bundler (~> 2.0)
|
|
31
|
+
jpm_geo!
|
|
32
|
+
rake (~> 10.0)
|
|
33
|
+
rspec (~> 3.0)
|
|
34
|
+
|
|
35
|
+
BUNDLED WITH
|
|
36
|
+
2.0.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Andrew Williams
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# JpmGeo
|
|
2
|
+
|
|
3
|
+
Computes the bounding coordinates of all points on the surface of a sphere that have a great circle distance to the given point. This code was ported to ruby directly from http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates, and is owned by Jan Philip Matuschek.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'jpm_geo'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install jpm_geo
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Configuration
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
# use miles as a distance unit (default is kilometers)
|
|
27
|
+
JpmGeo.units = 'm'
|
|
28
|
+
|
|
29
|
+
# specify default radius (default is Earth kilometers)
|
|
30
|
+
JpmGeo.radius = 3389.5 # Mars radius (km)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Creating points
|
|
34
|
+
```
|
|
35
|
+
# lonlat is an object that responds to 'lon' and 'lat' methods
|
|
36
|
+
point = JpmGeo::Point.from_lonlat(lonlat)
|
|
37
|
+
|
|
38
|
+
# from degrees
|
|
39
|
+
point = JpmGeo::Point.from_degrees(lat: 51.50853, lon: -0.12574) # London
|
|
40
|
+
|
|
41
|
+
# from radians
|
|
42
|
+
point = JpmGeo::Point.from_radians(lat: 0.8990, lon: -0.0022) # London (ish)
|
|
43
|
+
|
|
44
|
+
# converting to/from radians/degrees
|
|
45
|
+
point = point.to_radians
|
|
46
|
+
point = point.to_degrees
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Finding distance between points
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
# distance is in the same units as radius
|
|
53
|
+
distance = point.distance_to(point2)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Finding bounding coordinates
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
bounds = point.bounds(100) # km
|
|
60
|
+
p [bounds[0].lat, bounds[0].lon, bounds[1].lat, bounds[1].lon]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Contributing
|
|
64
|
+
|
|
65
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sobakasu/jpm_geo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
70
|
+
|
|
71
|
+
## Code of Conduct
|
|
72
|
+
|
|
73
|
+
Everyone interacting in the JpmGeo project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sobakasu/jpm_geo/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "jpm_geo"
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require "irb"
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/jpm_geo.gemspec
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require "jpm_geo/version"
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = "jpm_geo"
|
|
9
|
+
spec.version = JpmGeo::VERSION
|
|
10
|
+
spec.authors = ["Andrew Williams"]
|
|
11
|
+
spec.email = ["sobakasu@gmail.com"]
|
|
12
|
+
|
|
13
|
+
spec.summary = "Finds points within a distance of a latitude/longitude point using bounding coordinates"
|
|
14
|
+
spec.homepage = "https://github.com/sobakasu/jpm_geo"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/sobakasu/jpm_geo"
|
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/sobakasu/jpm_geo/CHANGELOG.md"
|
|
20
|
+
|
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
25
|
+
end
|
|
26
|
+
spec.bindir = "exe"
|
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
|
+
spec.require_paths = ["lib"]
|
|
29
|
+
|
|
30
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
33
|
+
end
|
data/lib/jpm_geo.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Finds points within a distance of a latitude/longitude point using bounding coordinates.
|
|
4
|
+
# http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates
|
|
5
|
+
module JpmGeo
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
EARTH_RADIUS_KM = 6371.01
|
|
9
|
+
EARTH_RADIUS_M = 3963.19
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
attr_writer :radius
|
|
13
|
+
|
|
14
|
+
def radius
|
|
15
|
+
@radius || EARTH_RADIUS_KM
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def units=(units)
|
|
19
|
+
raise ArgumentError, "invalid units" unless units && %w[km m].include?(units.to_s)
|
|
20
|
+
|
|
21
|
+
@units = units
|
|
22
|
+
@radius = units == "km" ? EARTH_RADIUS_KM : EARTH_RADIUS_M
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
require "jpm_geo/version"
|
|
28
|
+
require "jpm_geo/point"
|
|
29
|
+
require "jpm_geo/bounds"
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JpmGeo
|
|
4
|
+
# Bounds is a bounding box defined by two Points.
|
|
5
|
+
# If the bounding box crosses the 180th meridian, there will be two sets of bounding boxes
|
|
6
|
+
# defined by four Points.
|
|
7
|
+
class Bounds
|
|
8
|
+
attr_reader :points, :radians
|
|
9
|
+
|
|
10
|
+
def [](index)
|
|
11
|
+
to_a[index]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_a
|
|
15
|
+
points
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def size
|
|
19
|
+
points.size
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def to_radians
|
|
23
|
+
return self if radians
|
|
24
|
+
|
|
25
|
+
Bounds.from_points(*points.collect(&:to_radians))
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def to_degrees
|
|
29
|
+
return self unless radians
|
|
30
|
+
|
|
31
|
+
Bounds.from_points(*points.collect(&:to_degrees))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def to_s
|
|
35
|
+
"JpmGeo::Bounds[#{points}]"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
class << self
|
|
39
|
+
def from_points(*points)
|
|
40
|
+
new(*points)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def initialize(*points)
|
|
47
|
+
@points = points
|
|
48
|
+
@radians = points[0].radians
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JpmGeo
|
|
4
|
+
# Point represents a latitude, longitude location
|
|
5
|
+
class Point
|
|
6
|
+
PI = Math::PI
|
|
7
|
+
PI_2 = 2 * PI
|
|
8
|
+
DEG_TO_RAD = PI / 180.0
|
|
9
|
+
RAD_TO_DEG = 180.0 / PI
|
|
10
|
+
MIN_LAT = -PI / 2
|
|
11
|
+
MAX_LAT = PI / 2
|
|
12
|
+
MIN_LON = -PI
|
|
13
|
+
MAX_LON = PI
|
|
14
|
+
|
|
15
|
+
attr_accessor :lat, :lon
|
|
16
|
+
attr_reader :radius, :radians
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
def from_lonlat(lonlat, radius: JpmGeo.radius)
|
|
20
|
+
from_degrees(lon: lonlat.lon, lat: lonlat.lat, radius: radius)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def from_degrees(lon:, lat:, radius: JpmGeo.radius)
|
|
24
|
+
new(lon: lon, lat: lat, radius: radius, radians: false)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def from_radians(lon:, lat:, radius: JpmGeo.radius, validate: true)
|
|
28
|
+
new(lon: lon, lat: lat, radius: radius, radians: true, validate: validate)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_rad(degrees)
|
|
32
|
+
degrees * DEG_TO_RAD
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def to_deg(radians)
|
|
36
|
+
radians * RAD_TO_DEG
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def to_radians
|
|
41
|
+
return self if radians
|
|
42
|
+
|
|
43
|
+
Point.from_radians(lon: Point.to_rad(lon), lat: Point.to_rad(lat), radius: radius)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def to_degrees
|
|
47
|
+
return self unless radians
|
|
48
|
+
|
|
49
|
+
Point.from_degrees(lon: Point.to_deg(lon), lat: Point.to_deg(lat), radius: radius)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def valid?
|
|
53
|
+
point = to_radians
|
|
54
|
+
point.lat >= MIN_LAT && point.lat <= MAX_LAT && point.lon >= MIN_LON && point.lon <= MAX_LON
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def distance_to(point)
|
|
58
|
+
raise ArgumentError, "JpmGeo::Point expected" unless point.is_a?(JpmGeo::Point)
|
|
59
|
+
|
|
60
|
+
radians_distance_to(to_radians, point.to_radians)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# computes bounding coordinates all points that are within the great
|
|
64
|
+
# circle of the given distance from this Point.
|
|
65
|
+
# distance is in the same units as the radius argument.
|
|
66
|
+
def bounding_coordinates(distance)
|
|
67
|
+
raise ArgumentError, "invalid distance" if distance <= 0
|
|
68
|
+
|
|
69
|
+
# angular distance in radians on a great circle
|
|
70
|
+
raddist = distance / radius
|
|
71
|
+
|
|
72
|
+
point = to_radians
|
|
73
|
+
min = latlon(point.lat - raddist, 0)
|
|
74
|
+
max = latlon(point.lat + raddist, 0)
|
|
75
|
+
|
|
76
|
+
if min.lat > MIN_LAT && max.lat < MAX_LAT
|
|
77
|
+
deltalon = Math.asin(Math.sin(raddist) / Math.cos(point.lat))
|
|
78
|
+
min.lon = point.lon - deltalon
|
|
79
|
+
max.lon = point.lon + deltalon
|
|
80
|
+
else
|
|
81
|
+
# a pole is within the distance
|
|
82
|
+
min.lat = [min.lat, MIN_LAT].max
|
|
83
|
+
max.lat = [max.lat, MAX_LAT].min
|
|
84
|
+
min.lon = MIN_LON
|
|
85
|
+
max.lon = MAX_LON
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
bounds = bounds_from_min_max(min, max)
|
|
89
|
+
radians ? bounds : bounds.to_degrees
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def to_s
|
|
93
|
+
units = radians ? " rad" : "°"
|
|
94
|
+
"JpmGeo::Point(#{lat}#{units}, #{lon}#{units})"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def ==(other)
|
|
98
|
+
return false unless other.is_a?(JpmGeo::Point)
|
|
99
|
+
|
|
100
|
+
point = radians ? other.to_radians : other.to_degrees
|
|
101
|
+
point.lon == lon && point.lat == lat && point.radius == radius
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
private
|
|
105
|
+
|
|
106
|
+
def radians_distance_to(point1, point2)
|
|
107
|
+
Math.acos(Math.sin(point1.lat) * Math.sin(point2.lat) +
|
|
108
|
+
Math.cos(point1.lat) * Math.cos(point2.lat) *
|
|
109
|
+
Math.cos(point1.lon - point2.lon)) * radius
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def bounds_from_min_max(min, max)
|
|
113
|
+
# http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates#PolesAnd180thMeridian
|
|
114
|
+
# when the 180th meridian is with the query circle, return two sets of bounding coordinates.
|
|
115
|
+
if min.lon < MIN_LON
|
|
116
|
+
# (latmin, lonmin + 2PI), (latmax, PI) and (latmin, -PI), (latmax, lonmax)
|
|
117
|
+
Bounds.from_points(latlon(min.lat, min.lon + PI_2), latlon(max.lat, PI),
|
|
118
|
+
latlon(min.lat, -PI), latlon(max.lat, max.lon))
|
|
119
|
+
elsif max.lon > MAX_LON
|
|
120
|
+
# (latmin, lonmin), (latmax, PI) and (latmin, -PI), (latmax, lonmax - 2PI).
|
|
121
|
+
Bounds.from_points(latlon(min.lat, min.lon), latlon(max.lat, PI),
|
|
122
|
+
latlon(min.lat, -PI), latlon(max.lat, max.lon - PI_2))
|
|
123
|
+
else
|
|
124
|
+
# does not cross 180th meridian
|
|
125
|
+
Bounds.from_points(min, max)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def latlon(lat, lon)
|
|
130
|
+
Point.from_radians(lat: lat, lon: lon, radius: radius, validate: false)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def initialize(lon:, lat:, radians: false, radius: JpmGeo.radius, validate: true)
|
|
134
|
+
raise ArgumentError, "invalid radius" if radius <= 0
|
|
135
|
+
|
|
136
|
+
@lon = lon
|
|
137
|
+
@lat = lat
|
|
138
|
+
@radius = radius
|
|
139
|
+
@radians = radians
|
|
140
|
+
raise ArgumentError, "invalid coordinates" if validate && !valid?
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jpm_geo
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andrew Williams
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-09-30 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description:
|
|
56
|
+
email:
|
|
57
|
+
- sobakasu@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- ".rspec"
|
|
64
|
+
- ".rubocop.yml"
|
|
65
|
+
- ".travis.yml"
|
|
66
|
+
- CHANGELOG.md
|
|
67
|
+
- CODE_OF_CONDUCT.md
|
|
68
|
+
- Gemfile
|
|
69
|
+
- Gemfile.lock
|
|
70
|
+
- LICENSE.txt
|
|
71
|
+
- README.md
|
|
72
|
+
- Rakefile
|
|
73
|
+
- bin/console
|
|
74
|
+
- bin/setup
|
|
75
|
+
- jpm_geo.gemspec
|
|
76
|
+
- lib/jpm_geo.rb
|
|
77
|
+
- lib/jpm_geo/bounds.rb
|
|
78
|
+
- lib/jpm_geo/point.rb
|
|
79
|
+
- lib/jpm_geo/version.rb
|
|
80
|
+
homepage: https://github.com/sobakasu/jpm_geo
|
|
81
|
+
licenses:
|
|
82
|
+
- MIT
|
|
83
|
+
metadata:
|
|
84
|
+
homepage_uri: https://github.com/sobakasu/jpm_geo
|
|
85
|
+
source_code_uri: https://github.com/sobakasu/jpm_geo
|
|
86
|
+
changelog_uri: https://github.com/sobakasu/jpm_geo/CHANGELOG.md
|
|
87
|
+
post_install_message:
|
|
88
|
+
rdoc_options: []
|
|
89
|
+
require_paths:
|
|
90
|
+
- lib
|
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '0'
|
|
101
|
+
requirements: []
|
|
102
|
+
rubyforge_project:
|
|
103
|
+
rubygems_version: 2.7.6.2
|
|
104
|
+
signing_key:
|
|
105
|
+
specification_version: 4
|
|
106
|
+
summary: Finds points within a distance of a latitude/longitude point using bounding
|
|
107
|
+
coordinates
|
|
108
|
+
test_files: []
|