Skip to content

Commit 5300969

Browse files
authored
Tidying (#38)
* Remove unnecessary files. * Move dependencies to gemspec. * Add MIT license. * Move these specs to indexing_spec.rb * Move bindings base. * hex ring needs to handle pentagons. * Functionality improvements. * Update gem version to match H3. * Update readme.
1 parent 02ebc49 commit 5300969

27 files changed

+246
-170
lines changed

.ruby-version

-1
This file was deleted.

Gemfile

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
source "https://rubygems.org"
2-
3-
ruby "2.4.4" # Ideally, the same one as stuart-api.
4-
5-
gem "rake"
6-
gem "rspec", "~> 3.8.0"
7-
gem "ffi", "~> 1.9.6"
8-
gem "rgeo-geojson", "~> 2.1.1"
2+
gemspec

Gemfile.lock

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
PATH
2+
remote: .
3+
specs:
4+
h3 (3.2.0)
5+
ffi (~> 1.9)
6+
rgeo-geojson (~> 2.1)
7+
18
GEM
29
remote: https://rubygems.org/
310
specs:
411
diff-lcs (1.3)
512
ffi (1.9.25)
613
rake (12.3.2)
7-
rgeo (1.0.0)
14+
rgeo (2.0.0)
815
rgeo-geojson (2.1.1)
916
rgeo (>= 1.0.0)
1017
rspec (3.8.0)
@@ -20,18 +27,16 @@ GEM
2027
diff-lcs (>= 1.2.0, < 2.0)
2128
rspec-support (~> 3.8.0)
2229
rspec-support (3.8.0)
30+
yard (0.9.16)
2331

2432
PLATFORMS
2533
ruby
2634

2735
DEPENDENCIES
28-
ffi (~> 1.9.6)
29-
rake
30-
rgeo-geojson (~> 2.1.1)
31-
rspec (~> 3.8.0)
32-
33-
RUBY VERSION
34-
ruby 2.4.4p296
36+
h3!
37+
rake (~> 12.3)
38+
rspec (~> 3.8)
39+
yard (~> 0.9)
3540

3641
BUNDLED WITH
37-
1.17.1
42+
1.17.2

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Stuart Delivery
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 all
13+
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 THE
21+
SOFTWARE.

README.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# H3 Ruby
22

3-
[![Build Status](https://travis-ci.org/StuartApp/h3_ruby.svg?branch=master)](https://travis-ci.org/seanhandley/h3_ruby)
3+
![h3](https://user-images.githubusercontent.com/98526/50283275-48177300-044d-11e9-8337-eba8d3cc88a2.png)
4+
5+
[![Build Status](https://travis-ci.org/StuartApp/h3_ruby.svg?branch=master)](https://travis-ci.org/seanhandley/h3_ruby) [![Maintainability](https://api.codeclimate.com/v1/badges/c55e1f67421eba8af8d0/maintainability)](https://codeclimate.com/repos/5c18b7f49bc79a02a4000d81/maintainability)
46

57
Ruby bindings for Uber's [H3 library](https://uber.github.io/h3/).
68

7-
TODO: Add more wrapper functions.
9+
Please consult the H3 documentation for a full explanation of terminology and concepts.
810

911
## Getting Started
1012

@@ -14,24 +16,26 @@ Install the build dependencies as instructed here: https://github.com/uber/h3#in
1416

1517
Do *not* follow the Compilation Steps. Instead, use the following:
1618

17-
git clone git@github.com:uber/h3.git
18-
cd h3
19+
git clone git@github.com:uber/h3.git h3_build
20+
cd h3_build
1921
cmake . -DBUILD_SHARED_LIBS=true
2022
make
2123
sudo make install
2224

25+
The key difference is the `BUILD_SHARED_LIBS` option.
26+
2327
## Installing
2428

2529
gem install h3
2630

2731
or
2832

2933
# Gemfile
30-
gem "h3", "~> 0.0.1pre"
34+
gem "h3", "~> 3.2"
3135

32-
## Running Specs
36+
## Documentation
3337

34-
bin/rspec spec
38+
https://www.rubydoc.info/github/StuartApp/h3_ruby
3539

3640
## Usage
3741

@@ -41,6 +45,7 @@ H3.geo_to_h3([53.959130, -1.079230], 8).to_s(16)
4145
# => "8819429a9dfffff"
4246
```
4347

44-
## GeoJSON
48+
## Running Specs
49+
50+
rake
4551

46-
Note that methods returning GeoJSON contain coordinates in the form `[lon, lat]`, whereas H3 coordinates use `[lat, lon]`.

bin/console

-6
This file was deleted.

bin/rspec

-3
This file was deleted.

bin/run

-6
This file was deleted.

h3.gemspec

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
$: << File.expand_path("lib", __dir__)
2-
require "h3/version"
1+
require_relative "lib/h3/version"
32

43
Gem::Specification.new do |spec|
54
spec.name = "h3"
65
spec.version = H3::VERSION
7-
spec.licenses = ["Nonstandard"] # Avoids a warning when building the gem.
6+
spec.licenses = ["MIT"]
87
spec.summary = "C Bindings for Uber's H3 library"
98
spec.homepage = "https://github.com/StuartApp/h3_ruby"
109
spec.authors = ["Lachlan Laycock", "Sean Handley"]
1110
spec.email = "l.laycock@stuart.com"
1211

13-
spec.extensions = %w(ext/h3/extconf.rb)
14-
spec.require_paths = %w(lib)
15-
spec.files = %w(
16-
ext/h3/extconf.rb
17-
ext/h3/h3.c
18-
lib/h3.rb
19-
lib/h3/version.rb
20-
)
12+
spec.required_ruby_version = "> 2.3"
13+
spec.files = `git ls-files`.split("\n")
14+
15+
spec.add_runtime_dependency "ffi", "~> 1.9"
16+
spec.add_runtime_dependency "rgeo-geojson", "~> 2.1"
17+
18+
spec.add_development_dependency "rake", "~> 12.3"
19+
spec.add_development_dependency "rspec", "~> 3.8"
20+
spec.add_development_dependency "yard", "~> 0.9"
2121
end

lib/h3.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
require "ffi"
22
require "rgeo/geo_json"
33

4-
require "h3/binding_base"
5-
require "h3/bindings/structs"
6-
require "h3/bindings/private"
4+
require "h3/bindings"
75
require "h3/geo_json"
86
require "h3/hierarchy"
97
require "h3/indexing"

lib/h3/binding_base.rb

-10
This file was deleted.

lib/h3/bindings.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require "h3/bindings/base"
2+
require "h3/bindings/structs"
3+
require "h3/bindings/private"
4+
5+
module H3
6+
# Internal bindings related modules and classes.
7+
#
8+
# These are intended to be used by the library's public methods
9+
# and not to be used directly by client code.
10+
module Bindings
11+
end
12+
end

lib/h3/bindings/base.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module H3
2+
module Bindings
3+
# Base for FFI Bindings.
4+
#
5+
# When extended, this module sets up FFI to use the H3 C library.
6+
module Base
7+
def self.extended(base)
8+
base.extend FFI::Library
9+
base.ffi_lib ["libh3", "libh3.1"]
10+
base.typedef :ulong_long, :h3_index
11+
base.const_set('H3_INDEX', :ulong_long)
12+
end
13+
end
14+
end
15+
end

lib/h3/bindings/private.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Bindings
55
# This module provides bindings that do not have to be invoked directly by clients
66
# of the library. They are used only internally to provide related public interface.
77
module Private
8-
extend H3::BindingBase
8+
extend H3::Bindings::Base
99

1010
attach_function :compact, [:pointer, :pointer, :int], :bool
1111
attach_function :geo_to_h3, :geoToH3, [ Bindings::Structs::GeoCoord.by_ref, :int ], :h3_index
@@ -20,7 +20,7 @@ module Private
2020
attach_function :hex_range, :hexRange, [ :h3_index, :int, :pointer ], :bool
2121
attach_function :hex_range_distances, :hexRangeDistances, [:h3_index, :int, :pointer, :pointer], :bool
2222
attach_function :hex_ranges, :hexRanges, [ :pointer, :int, :int, :pointer ], :bool
23-
attach_function :hex_ring, :hexRing, [:h3_index, :int, :pointer], :void
23+
attach_function :hex_ring, :hexRing, [:h3_index, :int, :pointer], :bool
2424
attach_function :k_ring, :kRing, [:h3_index, :int, :pointer], :void
2525
attach_function :k_ring_distances, :kRingDistances, [:h3_index, :int, :pointer, :pointer], :bool
2626
attach_function :max_polyfill_size, :maxPolyfillSize, [Bindings::Structs::GeoPolygon.by_ref, :int], :int

lib/h3/geo_json.rb

+15-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ def geo_json_to_coordinates(input)
8484
elsif geom.respond_to?(:coordinates) # polygon
8585
geom.coordinates
8686
else
87-
raise "Could not parse given input. Please use a GeoJSON polygon."
87+
failed_to_parse!
8888
end
89-
swap_lat_lon(coordinates)
89+
swap_lat_lon(coordinates) || failed_to_parse!
90+
rescue JSON::ParserError
91+
failed_to_parse!
9092
end
9193

9294
# Convert a nested array of coordinates to a GeoJSON document
@@ -139,12 +141,14 @@ def coordinates_to_geo_json(coordinates)
139141
coordinates = swap_lat_lon(coordinates)
140142
outer_coords, *inner_coords = coordinates
141143
factory = RGeo::Cartesian.simple_factory
142-
exterior = factory.linear_ring(outer_coords.map! { |lon, lat| factory.point(lon, lat) })
144+
exterior = factory.linear_ring(outer_coords.map { |lon, lat| factory.point(lon, lat) })
143145
interior_rings = inner_coords.map do |polygon|
144146
factory.linear_ring(polygon.map { |lon, lat| factory.point(lon, lat) })
145147
end
146148
polygon = factory.polygon(exterior, interior_rings)
147149
RGeo::GeoJSON.encode(polygon).to_json
150+
rescue RGeo::Error::InvalidGeometry, NoMethodError
151+
invalid_coordinates!
148152
end
149153

150154
private
@@ -153,5 +157,13 @@ def coordinates_to_geo_json(coordinates)
153157
def swap_lat_lon(coordinates)
154158
coordinates.map { |polygon| polygon.map { |x, y| [y, x] } }
155159
end
160+
161+
def failed_to_parse!
162+
raise ArgumentError, "Could not parse given input. Please use a GeoJSON polygon."
163+
end
164+
165+
def invalid_coordinates!
166+
raise ArgumentError, "Could not parse given coordinates."
167+
end
156168
end
157169
end

lib/h3/hierarchy.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module H3
33
#
44
# @see https://uber.github.io/h3/#/documentation/api-reference/hierarchy
55
module Hierarchy
6-
extend H3::BindingBase
6+
extend H3::Bindings::Base
77

88
# @!method h3_to_parent(h3_index, parent_resolution)
99
#

lib/h3/indexing.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ module Indexing
2020
#
2121
# @return [Integer] H3 index.
2222
def geo_to_h3(coords, resolution)
23-
raise TypeError unless coords.is_a?(Array)
24-
raise ArgumentError unless coords.count == 2
25-
raise RangeError if coords.any? { |d| d > 1000000 }
23+
raise ArgumentError unless coords.is_a?(Array) && coords.count == 2
2624

2725
lat, lon = coords
26+
27+
if lat > 90 || lat < -90 || lon > 180 || lon < -180
28+
raise(ArgumentError, "Invalid coordinates")
29+
end
30+
2831
coords = Bindings::Structs::GeoCoord.new
2932
coords[:lat] = degs_to_rads(lat)
3033
coords[:lon] = degs_to_rads(lon)

lib/h3/inspection.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ module H3
33
#
44
# @see https://uber.github.io/h3/#/documentation/api-reference/inspection
55
module Inspection
6-
extend H3::BindingBase
6+
extend H3::Bindings::Base
77

88
H3_TO_STR_BUF_SIZE = 32
9+
private_constant :H3_TO_STR_BUF_SIZE
910

1011
# @!method h3_resolution(h3_index)
1112
#

lib/h3/miscellaneous.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module H3
33
#
44
# @see https://uber.github.io/h3/#/documentation/api-reference/miscellaneous
55
module Miscellaneous
6-
extend H3::BindingBase
6+
extend H3::Bindings::Base
77

88
# @!method degs_to_rads(degs)
99
#

lib/h3/regions.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ module H3
33
#
44
# @see https://uber.github.io/h3/#/documentation/api-reference/regions
55
module Regions
6-
extend H3::BindingBase
7-
6+
extend H3::Bindings::Base
87

98
# Derive the maximum number of H3 indexes that could be returned from the input.
109
#
@@ -136,6 +135,7 @@ def polyfill(geo_polygon, resolution)
136135
#
137136
# @return [Array<Array<Array<Float>>>] Nested array of coordinates.
138137
def h3_set_to_linked_geo(h3_indexes)
138+
h3_indexes.uniq!
139139
linked_geo_polygon = Bindings::Structs::LinkedGeoPolygon.new
140140
FFI::MemoryPointer.new(H3_INDEX, h3_indexes.size) do |hexagons_ptr|
141141
hexagons_ptr.write_array_of_ulong_long(h3_indexes)

lib/h3/traversal.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module H3
33
#
44
# @see https://uber.github.io/h3/#/documentation/api-reference/traversal
55
module Traversal
6-
extend H3::BindingBase
6+
extend H3::Bindings::Base
77

88
# @!method max_kring_size(k)
99
#

lib/h3/unidirectional_edges.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module H3
33
#
44
# @see https://uber.github.io/h3/#/documentation/api-reference/unidirectional-edges
55
module UnidirectionalEdges
6-
extend H3::BindingBase
6+
extend H3::Bindings::Base
77

88
# @!method h3_indexes_neighbors?(origin, destination)
99
#

lib/h3/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module H3
2-
VERSION = "0.0.1pre"
2+
VERSION = "3.2.0"
33
end

0 commit comments

Comments
 (0)