Skip to content

Commit 4ec0a92

Browse files
authored
ajfriend/more v4 funcs (#241)
* get_pentagon_indexes and get_res0_indexes * line * cell area functions
1 parent 298f17f commit 4ec0a92

File tree

5 files changed

+105
-87
lines changed

5 files changed

+105
-87
lines changed

src/h3/_cy/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
compact,
2828
uncompact,
2929
num_hexagons,
30+
get_pentagon_indexes,
31+
get_res0_indexes,
32+
line,
33+
mean_hex_area,
34+
cell_area,
3035
)
3136

3237
from h3fake2._cy import (
@@ -42,12 +47,12 @@
4247
# compact,
4348
# uncompact,
4449
# num_hexagons,
45-
mean_hex_area,
46-
cell_area,
47-
line,
50+
# mean_hex_area,
51+
# cell_area,
52+
# line,
4853
# is_res_class_iii,
49-
get_pentagon_indexes,
50-
get_res0_indexes,
54+
# get_pentagon_indexes,
55+
# get_res0_indexes,
5156
# center_child,
5257
get_faces,
5358
experimental_h3_to_local_ij,

src/h3/_cy/cells.pxd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ cpdef H3int center_child(H3int h, res=*) except 0
1414
cpdef H3int[:] compact(const H3int[:] hu)
1515
cpdef H3int[:] uncompact(const H3int[:] hc, int res)
1616
cpdef int64_t num_hexagons(int resolution) except -1
17-
# cpdef double mean_hex_area(int resolution, unit=*) except -1
18-
# cpdef double cell_area(H3int h, unit=*) except -1
19-
# cpdef H3int[:] line(H3int start, H3int end)
17+
cpdef double mean_hex_area(int resolution, unit=*) except -1
18+
cpdef double cell_area(H3int h, unit=*) except -1
19+
cpdef H3int[:] line(H3int start, H3int end)
2020
cpdef bool is_res_class_iii(H3int h)
21-
# cpdef H3int[:] get_pentagon_indexes(int res)
22-
# cpdef H3int[:] get_res0_indexes()
21+
cpdef H3int[:] get_pentagon_indexes(int res)
22+
cpdef H3int[:] get_res0_indexes()
2323
# cpdef get_faces(H3int h)
2424
# cpdef (int, int) experimental_h3_to_local_ij(H3int origin, H3int h) except *
2525
# cpdef H3int experimental_local_ij_to_h3(H3int origin, int i, int j) except 0

src/h3/_cy/cells.pyx

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -281,87 +281,104 @@ cpdef int64_t num_hexagons(int resolution) except -1:
281281
return num_cells
282282

283283

284-
# cpdef double mean_hex_area(int resolution, unit='km^2') except -1:
285-
# check_res(resolution)
284+
cpdef double mean_hex_area(int resolution, unit='km^2') except -1:
285+
cdef:
286+
h3lib.H3Error err
287+
double area
286288

287-
# area = h3lib.hexAreaKm2(resolution)
289+
check_res(resolution)
288290

289-
# # todo: multiple units
290-
# convert = {
291-
# 'km^2': 1.0,
292-
# 'm^2': 1000*1000.0
293-
# }
291+
err = h3lib.getHexagonAreaAvgKm2(resolution, &area)
294292

295-
# try:
296-
# area *= convert[unit]
297-
# except:
298-
# raise H3ValueError('Unknown unit: {}'.format(unit))
293+
# todo: multiple units
294+
convert = {
295+
'km^2': 1.0,
296+
'm^2': 1000*1000.0
297+
}
299298

300-
# return area
299+
try:
300+
area *= convert[unit]
301+
except:
302+
raise H3ValueError('Unknown unit: {}'.format(unit))
301303

304+
return area
302305

303-
# cpdef double cell_area(H3int h, unit='km^2') except -1:
304-
# check_cell(h)
305306

306-
# if unit == 'rads^2':
307-
# area = h3lib.cellAreaRads2(h)
308-
# elif unit == 'km^2':
309-
# area = h3lib.cellAreaKm2(h)
310-
# elif unit == 'm^2':
311-
# area = h3lib.cellAreaM2(h)
312-
# else:
313-
# raise H3ValueError('Unknown unit: {}'.format(unit))
307+
cpdef double cell_area(H3int h, unit='km^2') except -1:
308+
cdef:
309+
h3lib.H3Error err
310+
double area
314311

315-
# return area
312+
check_cell(h)
316313

314+
if unit == 'rads^2':
315+
err = h3lib.cellAreaRads2(h, &area)
316+
elif unit == 'km^2':
317+
err = h3lib.cellAreaKm2(h, &area)
318+
elif unit == 'm^2':
319+
err = h3lib.cellAreaM2(h, &area)
320+
else:
321+
raise H3ValueError('Unknown unit: {}'.format(unit))
317322

318-
# cpdef H3int[:] line(H3int start, H3int end):
319-
# check_cell(start)
320-
# check_cell(end)
323+
return area
321324

322-
# n = h3lib.h3LineSize(start, end)
323325

324-
# if n < 0:
325-
# s = "Couldn't find line between cells {} and {}"
326-
# s = s.format(hex(start), hex(end))
327-
# raise H3ValueError(s)
326+
cpdef H3int[:] line(H3int start, H3int end):
327+
cdef:
328+
h3lib.H3Error err
329+
int64_t n
328330

329-
# ptr = create_ptr(n)
330-
# flag = h3lib.h3Line(start, end, ptr)
331-
# mv = create_mv(ptr, n)
331+
check_cell(start)
332+
check_cell(end)
332333

333-
# if flag != 0:
334-
# s = "Couldn't find line between cells {} and {}"
335-
# s = s.format(hex(start), hex(end))
336-
# raise H3ValueError(s)
334+
err = h3lib.gridPathCellsSize(start, end, &n)
337335

338-
# return mv
336+
if err:
337+
s = "Couldn't find line between cells {} and {}"
338+
s = s.format(hex(start), hex(end))
339+
raise H3ValueError(s)
340+
341+
ptr = create_ptr(n)
342+
err = h3lib.gridPathCells(start, end, ptr)
343+
mv = create_mv(ptr, n)
344+
345+
if err:
346+
s = "Couldn't find line between cells {} and {}"
347+
s = s.format(hex(start), hex(end))
348+
raise H3ValueError(s)
349+
350+
return mv
339351

340352
cpdef bool is_res_class_iii(H3int h):
341353
return h3lib.isResClassIII(h) == 1
342354

343355

344-
# cpdef H3int[:] get_pentagon_indexes(int res):
345-
# check_res(res)
356+
cpdef H3int[:] get_pentagon_indexes(int res):
357+
cdef:
358+
h3lib.H3Error err
346359

347-
# n = h3lib.pentagonIndexCount()
360+
check_res(res)
348361

349-
# ptr = create_ptr(n)
350-
# h3lib.getPentagonIndexes(res, ptr)
351-
# mv = create_mv(ptr, n)
362+
n = h3lib.pentagonCount()
352363

353-
# return mv
364+
ptr = create_ptr(n)
365+
err = h3lib.getPentagons(res, ptr)
366+
mv = create_mv(ptr, n)
354367

368+
return mv
355369

356-
# cpdef H3int[:] get_res0_indexes():
357-
# n = h3lib.res0IndexCount()
358370

359-
# ptr = create_ptr(n)
360-
# h3lib.getRes0Indexes(ptr)
361-
# mv = create_mv(ptr, n)
371+
cpdef H3int[:] get_res0_indexes():
372+
cdef:
373+
h3lib.H3Error err
362374

363-
# return mv
375+
n = h3lib.res0CellCount()
364376

377+
ptr = create_ptr(n)
378+
err = h3lib.getRes0Cells(ptr)
379+
mv = create_mv(ptr, n)
380+
381+
return mv
365382

366383
# cpdef get_faces(H3int h):
367384
# check_cell(h)

src/h3/_cy/h3lib.pxd

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,21 @@ cdef extern from "h3api.h":
6060
const int res
6161
) nogil
6262

63-
H3Error getNumCells(int res, int64_t *out)
63+
H3Error getNumCells(int res, int64_t *out) nogil
64+
int pentagonCount() nogil
65+
int res0CellCount() nogil
66+
H3Error getPentagons(int res, H3Index *out) nogil
67+
H3Error getRes0Cells(H3Index *out) nogil
68+
69+
H3Error gridPathCellsSize(H3Index start, H3Index end, int64_t *size) nogil
70+
H3Error gridPathCells(H3Index start, H3Index end, H3Index *out) nogil
71+
72+
H3Error getHexagonAreaAvgKm2(int res, double *out) nogil
73+
H3Error getHexagonAreaAvgM2(int res, double *out) nogil
74+
75+
H3Error cellAreaRads2(H3Index h, double *out) nogil
76+
H3Error cellAreaKm2(H3Index h, double *out) nogil
77+
H3Error cellAreaM2(H3Index h, double *out) nogil
6478

6579
# ctypedef struct GeoBoundary:
6680
# int num_verts "numVerts"
@@ -122,14 +136,6 @@ cdef extern from "h3api.h":
122136

123137
# void h3ToString(H3Index h, char *str, size_t sz)
124138

125-
# int pentagonIndexCount()
126-
127-
# void getPentagonIndexes(int res, H3Index *out)
128-
129-
# int res0IndexCount()
130-
131-
# void getRes0Indexes(H3Index *out)
132-
133139
# int h3IndexesAreNeighbors(H3Index origin, H3Index destination)
134140

135141
# H3Index getH3UnidirectionalEdge(H3Index origin, H3Index destination)
@@ -144,22 +150,12 @@ cdef extern from "h3api.h":
144150

145151
# void getH3UnidirectionalEdgeBoundary(H3Index edge, GeoBoundary *gb)
146152

147-
# int h3LineSize(H3Index start, H3Index end)
148-
# int h3Line(H3Index start, H3Index end, H3Index *out)
149-
150153
# int maxFaceCount(H3Index h3)
151154
# void h3GetFaces(H3Index h3, int *out)
152155

153156
# int experimentalH3ToLocalIj(H3Index origin, H3Index h3, CoordIJ *out)
154157
# int experimentalLocalIjToH3(H3Index origin, const CoordIJ *ij, H3Index *out)
155158

156-
# double hexAreaKm2(int res) nogil
157-
# double hexAreaM2(int res) nogil
158-
159-
# double cellAreaRads2(H3Index h) nogil
160-
# double cellAreaKm2(H3Index h) nogil
161-
# double cellAreaM2(H3Index h) nogil
162-
163159
# double edgeLengthKm(int res) nogil
164160
# double edgeLengthM(int res) nogil
165161

tests/test_cells_and_edges.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ def test_num_hexagons():
269269

270270
def test_hex_area():
271271
expected_in_km2 = {
272-
0: 4250546.848,
273-
1: 607220.9782,
274-
2: 86745.85403,
275-
9: 0.1053325,
276-
15: 9e-07,
272+
0: 4357449.416078381,
273+
1: 609788.441794133,
274+
2: 86801.780398997,
275+
9: 0.105332513,
276+
15: 8.95311e-07,
277277
}
278278

279279
out = {

0 commit comments

Comments
 (0)