wookay / H3.jl

H3.jl ⬡ provides a Julia version of H3, Hexagonal hierarchical geospatial indexing system. https://github.com/uber/h3
Other
25 stars 4 forks source link

Is the Regions API implemented? #12

Closed hs-ye closed 3 years ago

hs-ye commented 3 years ago

Hi, just checking if the APIs documented in the C version https://h3geo.org/docs/api/regions is implemented or is there plans to?

Can't see to see it in the source, not sure if i'm missing something?

wookay commented 3 years ago

you could find them in H3.Lib (they are low level apis that need pointer, Ref things). test code are here https://github.com/wookay/H3.jl/blob/master/test/h3/lib/regions.jl

using H3.Lib
using .Lib: H3Index, GeoCoord, Geofence, GeoPolygon
sfVerts = [GeoCoord(deg2rad(37.813318999983238), deg2rad(-122.4089866999972145)),
           GeoCoord(deg2rad(37.7198061999978478), deg2rad(-122.3544736999993603)),
           GeoCoord(deg2rad(37.8151571999998453), deg2rad(-122.4798767000009008))]
sfGeofence = Geofence(length(sfVerts), pointer(sfVerts))
sfGeoPolygon = GeoPolygon(sfGeofence, 0, C_NULL)
res = 7
hexagons = Lib.polyfill(Ref(sfGeoPolygon), res)
julia> filter(!iszero, hexagons)
7-element Vector{UInt64}:
 0x0872830828ffffff
 0x087283082effffff
 0x087283082affffff
 0x087283082bffffff
 0x0872830876ffffff
 0x0872830820ffffff
 0x0872830870ffffff
hs-ye commented 3 years ago

Ah yeah that works great, thanks!

Any idea why the underlying implementation returns a lot of 0's when polyfill is called, or how it's used by the other higher level APIs?

hs-ye commented 3 years ago

A better way to ask my question is, why is it:

wookay commented 3 years ago

well, H3.jl needs to update the h3 release (from v3.6.3 to v3.7.2)

I found that some polyfill changes since after v3.6.3. [3.6.4] - 2020-06-19 Again implement new polyfill algorithm. https://github.com/uber/h3/releases/tag/v3.6.4

you could compare it among other h3 implementations.

wookay commented 3 years ago

I have updated the h3 v3.7.2 to the master branch.

here's example code (test/h3/lib/regions.jl) https://github.com/wookay/H3.jl/blob/4c246787ecf613efdc4ec6964ce82e6e34571f02/test/h3/lib/regions.jl#L42

# https://h3geo.org/docs/api/regions

to_geocoord((lat, lon)) = GeoCoord(deg2rad(lat), deg2rad(lon))

sfVerts = map(to_geocoord, [
    [37.813318999983238, -122.4089866999972145],
    [37.7198061999978478, -122.3544736999993603],
    [37.8151571999998453, -122.4798767000009008]
])
res = 7

sfGeofence = Geofence(length(sfVerts), pointer(sfVerts))
sfGeoPolygon = GeoPolygon(sfGeofence, 0, C_NULL)
numHexagons = Lib.maxPolyfillSize(Ref(sfGeoPolygon), res)
# @test numHexagons == 100

p_hexagons = Libc.calloc(numHexagons, sizeof(H3Index))
Lib.polyfill(Ref(sfGeoPolygon), res, p_hexagons)
p = Base.unsafe_convert(Ptr{H3Index}, p_hexagons)
w = unsafe_wrap(Vector{H3Index}, p, numHexagons)
# @test filter(!iszero, w) == [0x087283082bffffff, 0x0872830870ffffff, 0x0872830820ffffff, 0x087283082effffff, 0x0872830828ffffff, 0x087283082affffff, 0x0872830876ffffff]
Libc.free(p_hexagons)

you need Julia 1.6 and H3_jll

hs-ye commented 3 years ago

Thanks, will give it a shot later. Closed as I think the original intent of this issue is very much covered!