-
Notifications
You must be signed in to change notification settings - Fork 0
Adding h3 functions for working with hexes #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zer0stars
wants to merge
3
commits into
main
Choose a base branch
from
feature/h3support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| package h3 | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/benthosdev/benthos/v4/public/bloblang" | ||
| "github.com/uber/h3-go/v4" | ||
| ) | ||
|
|
||
| func init() { | ||
| latLngToHexSpec := bloblang.NewPluginSpec(). | ||
| Description("Returns hex id for given latitude and longitude at desired resolution."). | ||
| Param(bloblang.NewFloat64Param("lat")). | ||
| Param(bloblang.NewFloat64Param("lng")). | ||
| Param(bloblang.NewInt64Param("resolution")) | ||
|
|
||
| err := bloblang.RegisterFunctionV2("h3_lat_lng_to_hex", latLngToHexSpec, func(args *bloblang.ParsedParams) (bloblang.Function, error) { | ||
| lat, err := args.GetFloat64("lat") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| lng, err := args.GetFloat64("lng") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| resolution, err := args.GetInt64("resolution") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return func() (interface{}, error) { | ||
| if resolution < 0 && resolution < 15 { | ||
| return nil, fmt.Errorf("resolution should be between 0 and 15") | ||
| } | ||
| latLng := h3.NewLatLng(lat, lng) | ||
| cell := h3.LatLngToCell(latLng, int(resolution)) | ||
| return cell.String(), err | ||
| }, nil | ||
| }) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| hexLatLonSpec := bloblang.NewPluginSpec(). | ||
| Description("Returns lat,lon for given hex."). | ||
| Param(bloblang.NewStringParam("hex_id")) | ||
|
|
||
| err = bloblang.RegisterFunctionV2("h3_hex_to_geo", hexLatLonSpec, func(args *bloblang.ParsedParams) (bloblang.Function, error) { | ||
| hex_id, err := args.GetString("hex_id") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return func() (interface{}, error) { | ||
| index := h3.IndexFromString(hex_id) | ||
| cell := h3.Cell(index) | ||
| if !cell.IsValid() { | ||
| return nil, fmt.Errorf("failed to parse hex id") | ||
| } | ||
| return map[string]float64{"latitude": cell.LatLng().Lat, "longitude": cell.LatLng().Lng}, nil | ||
| }, nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| validHexSpec := bloblang.NewPluginSpec(). | ||
| Description("Returns true if the given hex id is valid."). | ||
| Param(bloblang.NewStringParam("hex_id")) | ||
|
|
||
| err = bloblang.RegisterFunctionV2("h3_valid_hex", validHexSpec, func(args *bloblang.ParsedParams) (bloblang.Function, error) { | ||
| hex_id, err := args.GetString("hex_id") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return func() (interface{}, error) { | ||
| index := h3.IndexFromString(hex_id) | ||
| cell := h3.Cell(index) | ||
| return cell.IsValid(), nil | ||
| }, nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| getH3ResolutionSpec := bloblang.NewPluginSpec(). | ||
| Description("Returns the resolution of the given hex id."). | ||
| Param(bloblang.NewStringParam("hex_id")) | ||
|
|
||
| err = bloblang.RegisterFunctionV2("h3_get_resolution", getH3ResolutionSpec, func(args *bloblang.ParsedParams) (bloblang.Function, error) { | ||
| hex_id, err := args.GetString("hex_id") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return func() (interface{}, error) { | ||
| index := h3.IndexFromString(hex_id) | ||
| cell := h3.Cell(index) | ||
| if !cell.IsValid() { | ||
| return nil, fmt.Errorf("failed to parse hex id") | ||
| } | ||
| return cell.Resolution(), nil | ||
| }, nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| getH3ParentIdSpec := bloblang.NewPluginSpec(). | ||
| Description("Returns the parent hex id of the given hex id at the given resolution."). | ||
| Param(bloblang.NewStringParam("hex_id")). | ||
| Param(bloblang.NewInt64Param("resolution")) | ||
|
|
||
| err = bloblang.RegisterFunctionV2("h3_hex_parent_id", getH3ParentIdSpec, func(args *bloblang.ParsedParams) (bloblang.Function, error) { | ||
| hex_id, err := args.GetString("hex_id") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| resolution, err := args.GetInt64("resolution") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return func() (interface{}, error) { | ||
| if resolution < 0 && resolution < 15 { | ||
| return nil, fmt.Errorf("resolution should be between 0 and 15") | ||
| } | ||
| index := h3.IndexFromString(hex_id) | ||
| cell := h3.Cell(index) | ||
| if !cell.IsValid() { | ||
| return nil, fmt.Errorf("failed to parse hex id") | ||
| } | ||
| return cell.Parent(int(resolution)).String(), nil | ||
| }, nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| getH3ParentLatLonSpec := bloblang.NewPluginSpec(). | ||
| Description("Returns the parent hex lat,lon of the given hex id at the given resolution."). | ||
| Param(bloblang.NewStringParam("hex_id")). | ||
| Param(bloblang.NewInt64Param("resolution")) | ||
|
|
||
| err = bloblang.RegisterFunctionV2("h3_hex_parent_to_geo", getH3ParentLatLonSpec, func(args *bloblang.ParsedParams) (bloblang.Function, error) { | ||
| hex_id, err := args.GetString("hex_id") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| resolution, err := args.GetInt64("resolution") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return func() (interface{}, error) { | ||
| if resolution < 0 && resolution < 15 { | ||
| return nil, fmt.Errorf("resolution should be between 0 and 15") | ||
| } | ||
| index := h3.IndexFromString(hex_id) | ||
| cell := h3.Cell(index) | ||
|
|
||
| if !cell.IsValid() { | ||
| return nil, fmt.Errorf("failed to parse hex id") | ||
| } | ||
| parent := cell.Parent(int(resolution)) | ||
| return map[string]float64{"latitude": parent.LatLng().Lat, "longitude": parent.LatLng().Lng}, nil | ||
| }, nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package h3 | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/benthosdev/benthos/v4/public/bloblang" | ||
| ) | ||
|
|
||
| func TestGetH3HexSuccess(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_lat_lng_to_hex(lat: this.latitude, lng: this.longitude, resolution: 5)") | ||
| require.NoError(t, err) | ||
|
|
||
| hexId, err := exec.Query(map[string]any{ | ||
| "latitude": 40.776676, "longitude": -73.971321, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "852a100bfffffff", hexId) | ||
| } | ||
|
|
||
| func TestGetH3LatLon(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_hex_to_geo(hex_id: this.hex)") | ||
| require.NoError(t, err) | ||
|
|
||
| latLng, err := exec.Query(map[string]any{ | ||
| "hex": "852a100bfffffff", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, map[string]float64{"latitude": 40.85293293570688, "longitude": -73.99191613398101}, latLng) | ||
| } | ||
|
|
||
| func TestValidH3Hex(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_valid_hex(hex_id: this.hex)") | ||
| require.NoError(t, err) | ||
|
|
||
| isValid, err := exec.Query(map[string]any{ | ||
| "hex": "852a100bfffffff", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, true, isValid) | ||
| } | ||
|
|
||
| func TestInValidH3Hex(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_valid_hex(hex_id: this.hex)") | ||
| require.NoError(t, err) | ||
|
|
||
| isValid, err := exec.Query(map[string]any{ | ||
| "hex": "0", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, false, isValid) | ||
| } | ||
|
|
||
| func TestGetResolutionSuccess(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_get_resolution(hex_id: this.hex)") | ||
| require.NoError(t, err) | ||
|
|
||
| resolution, err := exec.Query(map[string]any{ | ||
| "hex": "852a100bfffffff", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 5, resolution) | ||
| } | ||
|
|
||
| func TestGetResolutionFail(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_get_resolution(hex_id: this.hex)") | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = exec.Query(map[string]any{ | ||
| "hex": "852a100b", | ||
| }) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestGetParentAtResolutionSuccess(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_hex_parent_id(hex_id: this.hex, resolution: 4)") | ||
| require.NoError(t, err) | ||
|
|
||
| resolution, err := exec.Query(map[string]any{ | ||
| "hex": "852a100bfffffff", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "842a101ffffffff", resolution) | ||
| } | ||
|
|
||
| func TestGetParentAtInvalidHexResolutionFail(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_hex_parent_id(hex_id: this.hex, resolution: 4)") | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = exec.Query(map[string]any{ | ||
| "hex": "852a100", | ||
| }) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestGetParentAtInvalidResolutionFail(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_hex_parent_id(hex_id: this.hex, resolution: 200)") | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = exec.Query(map[string]any{ | ||
| "hex": "852a100", | ||
| }) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestGetParentGeoAtResolutionSuccess(t *testing.T) { | ||
| // It's safe to pass nil in place of a logger for testing purposes | ||
| exec, err := bloblang.Parse("root = h3_hex_parent_to_geo(hex_id: this.hex, resolution: 3)") | ||
| require.NoError(t, err) | ||
|
|
||
| resolution, err := exec.Query(map[string]any{ | ||
| "hex": "852a100bfffffff", | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, map[string]float64{"latitude": 40.85841614742274, "longitude": -73.7819279919521}, resolution) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool staff overall!!! Nit - extracting those anonymous functions to separate ones for readability might be good.