~jl2/cl-h3

ref: c56717579229e9d14ae77fab076d13c2d5713b35 cl-h3/cl-h3.lisp -rw-r--r-- 21.8 KiB
c5671757Jeremiah LaRocco Assorted changes. 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
;; cl-h3.lisp
;;
;; Copyright (c) 2021 Jeremiah LaRocco <jeremiah_larocco@fastmail.com>

;; Permission to use, copy, modify, and/or distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
;; copyright notice and this permission notice appear in all copies.

;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


(in-package :cl-h3)

(defun extract (num pos len)
  (loop
    for i below len
    summing (ash (if (logbitp (+ pos i) num) 1 0) i)))

(defun show-decoded-h3-index (idx &optional (stream t))
  (flet ((show-field-data (name offset bits)
           (let ((value (extract idx offset bits)))
             (format stream "~(~16,a~): ~8d 0x~2,'0x b~8,'0b~%" name value value value))))
    (let ((fields '((:reserved #16r3f 1)
                    (:mode #16r3b 4)
                    (:mode-data #16r38 3)
                    (:resolution  #16r34 4)
                    (:base-cell #16r2d 8)))
          (first-digit-offset #16r2a))
      (loop
        for (name offset bits) in fields
        do
           (show-field-data name offset bits))
      (loop
        for i from 1
        for digit-offset from first-digit-offset downto 0 by 3
        do
           (show-field-data (format nil "digit-~a" i)
                            digit-offset
                            3)))))

(setf (symbol-function 'lat) #'car)

(setf (symbol-function 'lng) #'cdr)

(setf (symbol-function 'lat-lng) #'cons)

(setf (symbol-function 'degs-to-rads) #'clh3i::degs-to-rads)
(setf (symbol-function 'rads-to-degs) #'clh3i::rads-to-degs)

(defgeneric r2d (radians))
(defmethod r2d ((radians number))
  (/ (* 180 radians) pi))

(defmethod r2d ((radians cons))
  (h3:lat-lng (r2d (car radians))
              (r2d (cdr radians))))

(defgeneric d2r (degrees))
(defmethod d2r ((degrees number))
  (/ (* pi degrees) 180))

(defmethod d2r ((degrees cons))
  (h3:lat-lng (d2r (car degrees))
              (d2r (cdr degrees))))

(setf (symbol-function 'cell-area-m2) #'clh3i::cell-area-m2)
(setf (symbol-function 'cell-area-km2) #'clh3i::cell-area-km2)
(setf (symbol-function 'cell-area-rads2) #'clh3i::cell-area-rads2)

(defun cell-to-boundary (index)
  (autowrap:with-many-alloc ((cell-bound 'clh3i::cell-boundary))
    (clh3i::cell-to-boundary index cell-bound)
    (loop
      with num-verts = (clh3i::cell-boundary.num-verts cell-bound)
      for i below num-verts
      ;; TODO: Learn why this is (* 2 i)
      for ll = (clh3i::cell-boundary.verts[] cell-bound (* 2 i))
      collecting (lat-lng (clh3i::lat-lng.lat ll)
                       (clh3i::lat-lng.lng ll)))))


(setf (symbol-function 'cell-to-center-child) #'clh3i::cell-to-center-child)

(defun cell-to-children (index &optional (res (h3:get-resolution index)))
  (autowrap:with-many-alloc ((child-count 'clh3i::int64-t))
    (clh3i::cell-to-children-size index res child-count)
    (let ((child-count (cffi:mem-ref child-count :int64)))
      (autowrap:with-many-alloc ((children 'clh3i:h3index child-count))
        (clh3i::cell-to-children index res children)
        (loop
          for i below child-count
          for offset = (* (cffi:foreign-type-size :uint64) i)
          for cell = (cffi:mem-ref children :uint64 offset)
          when (not (zerop cell))
          collecting cell)))))

(defun cell-to-lat-lng (cell)
  (autowrap:with-many-alloc ((geo 'clh3i::lat-lng))
    (clh3i::cell-to-lat-lng cell geo)
    (lat-lng (clh3i::lat-lng.lat geo)
          (clh3i::lat-lng.lng geo))))

(defun cell-to-lat-lng-degrees (cell)
  (autowrap:with-many-alloc ((geo 'clh3i::lat-lng))
    (clh3i::cell-to-lat-lng cell geo)
    (lat-lng (clh3i::rads-to-degs (clh3i::lat-lng.lat geo))
          (clh3i::rads-to-degs (clh3i::lat-lng.lng geo)))))

(defun cell-to-parent (cell parent-res)
  (autowrap:with-many-alloc ((parent 'clh3i::uint64-t))
    (clh3i::cell-to-parent cell parent-res parent)
    (cffi:mem-ref parent :uint64)))

(defun cell-to-vertex (cell vertex-num)
  (autowrap:with-many-alloc ((vertex 'clh3i::uint64-t))
    (clh3i::cell-to-vertex cell vertex-num vertex)
    (cffi:mem-ref vertex :uint64)))

(defun cell-to-vertexes (cell)
  (autowrap:with-many-alloc ((vertexes 'clh3i::uint64-t 6))
    (clh3i::cell-to-vertexes cell vertexes)
    (loop
      for i below 6
          collecting (cffi:mem-ref vertexes :uint64
                                   (* (cffi:foreign-type-size :uint64) i)))))

(setf (symbol-function 'cells-to-directed-edge) #'clh3i::cells-to-directed-edge)

(defun compact-cells (cells)
  (let ((count (length cells)))
    (autowrap:with-many-alloc ((in-set 'clh3i::int64-t count)
                               (out-set 'clh3i::int64-t count))
      (loop
        for i below count
        for idx in cells
        for offset = (* (cffi:foreign-type-size :uint64) i)
        do
           (setf (cffi:mem-ref in-set :uint64 offset) idx)
           (setf (cffi:mem-ref out-set :uint64 offset) 0))

      (clh3i::compact-cells in-set out-set count)

      (loop
        for i below count
        for offset = (* (cffi:foreign-type-size :uint64) i)
        for val = (cffi:mem-ref out-set :uint64 offset)
        when (not (zerop val))
          collect val))))


(defun directed-edge-to-boundary (edge)
  (autowrap:with-many-alloc ((cell-bound 'clh3i::cell-boundary))
    (clh3i::directed-edge-to-boundary edge cell-bound)
    (loop
      with num-verts = (clh3i::cell-boundary.num-verts cell-bound)
      for i below num-verts
      ;; TODO: Learn why this is (* 2 i)
      for ll = (clh3i::cell-boundary.verts[] cell-bound (* 2 i))
      collecting (lat-lng (clh3i::lat-lng.lat ll)
                       (clh3i::lat-lng.lng ll)))))

(defun directed-edge-to-cells (edge)
  (autowrap:with-many-alloc ((origin-destination 'clh3i::uint64-t 2))
    (clh3i::directed-edge-to-cells edge origin-destination)
    (lat-lng (cffi:mem-ref origin-destination :uint64 0)
          (cffi:mem-ref origin-destination :uint64 (cffi:foreign-type-size :uint64)))))


(defun distance-m (lat1 lng1 lat2 lng2)
  (autowrap:with-many-alloc ((a 'clh3i::lat-lng)
                             (b 'clh3i::lat-lng))
    (setf (clh3i::lat-lng.lat a) lat1)
    (setf (clh3i::lat-lng.lng a) lng1)
    (setf (clh3i::lat-lng.lat b) lat2)
    (setf (clh3i::lat-lng.lng b) lng2)
    (clh3i::distance-m a b)))

(defun distance-km (lat1 lng1 lat2 lng2)
  (autowrap:with-many-alloc ((a 'clh3i::lat-lng)
                             (b 'clh3i::lat-lng))
    (setf (clh3i::lat-lng.lat a) lat1)
    (setf (clh3i::lat-lng.lng a) lng1)
    (setf (clh3i::lat-lng.lat b) lat2)
    (setf (clh3i::lat-lng.lng b) lng2)
    (clh3i::distance-km a b)))

(defun distance-rads (lat1 lng1 lat2 lng2)
  (autowrap:with-many-alloc ((a 'clh3i::lat-lng)
                             (b 'clh3i::lat-lng))
    (setf (clh3i::lat-lng.lat a) lat1)
    (setf (clh3i::lat-lng.lng a) lng1)
    (setf (clh3i::lat-lng.lat b) lat2)
    (setf (clh3i::lat-lng.lng b) lng2)
    (clh3i::distance-km a b)))


(setf (symbol-function 'exact-edge-length-m) #'clh3i::exact-edge-length-m)
(setf (symbol-function 'exact-edge-length-km) #'clh3i::exact-edge-length-km)
(setf (symbol-function 'exact-edge-length-rads) #'clh3i::exact-edge-length-rads)


(defun experimental-h3-to-local-ij (origin pt2)
  (autowrap:with-many-alloc ((res 'clh3i::coord-ij))
    (clh3i::experimental-h3to-local-ij origin pt2 res)
    (cons (clh3i::coord-ij.i res)
          (clh3i::coord-ij.j res))))

(defun experimental-local-ij-to-h3 (origin ci cj)
  (autowrap:with-many-alloc ((cij 'clh3i::coord-ij)
                             (res 'clh3i::h3index))
    (setf (clh3i::coord-ij.i cij) ci)
    (setf (clh3i::coord-ij.j cij) cj)
    (clh3i::experimental-local-ij-to-h3 origin cij res)
    (cffi:mem-ref res :uint64 0)))


(setf (symbol-function 'get-base-cell-number) #'clh3i::get-base-cell-number)
(setf (symbol-function 'get-directed-edge-destination) #'clh3i::get-directed-edge-destination)
(setf (symbol-function 'get-directed-edge-origin) #'clh3i::get-directed-edge-origin)

(setf (symbol-function 'get-hexagon-area-avg-km2) #'clh3i::get-hexagon-area-avg-km2)
(setf (symbol-function 'get-hexagon-area-avg-m2) #'clh3i::get-hexagon-area-avg-m2)
(setf (symbol-function 'get-hexagon-edge-length-avg-km) #'clh3i::get-hexagon-edge-length-avg-km)
(setf (symbol-function 'get-hexagon-edge-length-avg-m) #'clh3i::get-hexagon-edge-length-avg-m)


(defun get-icosahedron-faces (edge)
  (autowrap:with-many-alloc ((face-count :int))
    (setf (cffi:mem-ref face-count :int 0) 0)
    (clh3i::max-face-count edge face-count)
    (let ((max-face-count (cffi:mem-ref face-count :int)))
      (autowrap:with-many-alloc ((faces :int max-face-count))
        (clh3i::get-icosahedron-faces edge faces)
        (loop for i below max-face-count
              for offset = (* (cffi:foreign-type-size :int) i)
              when (>= (cffi:mem-ref faces :int offset) 0)
                collect (cffi:mem-ref faces :int offset))))))


(setf (symbol-function 'get-num-cells) #'clh3i::get-num-cells)


(defun get-pentagon-count ()
  (clh3i::pentagon-count))


(defun get-pentagons (res)
  (autowrap:with-many-alloc ((pentagons 'clh3i::h3index (clh3i::pentagon-count)))
    (clh3i::get-pentagons res pentagons)
    (loop for i below (clh3i::pentagon-count)
          for offset = (* (cffi:foreign-type-size :uint64) i)
          collect (cffi:mem-ref pentagons :uint64 offset))))


(defun get-res-0-cell-count ()
  (clh3i::res0cell-count))

(defun get-res-0-cells ()
  (autowrap:with-many-alloc ((cells 'clh3i::h3index (clh3i::res0cell-count)))
    (clh3i::get-res0cells cells)
    (loop for i below (clh3i::res0cell-count)
          for offset = (* (cffi:foreign-type-size :uint64) i)
          collect (cffi:mem-ref cells :uint64 offset))))


(setf (symbol-function 'get-resolution) #'clh3i::get-resolution)
(setf (symbol-function 'max-grid-disk-size) #'clh3i::max-grid-disk-size)


(defun grid-disk (index k)
  (let ((max-neighbors (max-grid-disk-size k)))
    (autowrap:with-alloc (neighbors 'clh3i::h3index max-neighbors)
      (loop for i below max-neighbors do
        (setf (cffi:mem-ref neighbors :uint64 (* (cffi:foreign-type-size :uint64) i)) 0))
      (clh3i::grid-disk index k neighbors)
      (loop
        for i below max-neighbors
        for neigh = (cffi:mem-ref neighbors :uint64 (* (cffi:foreign-type-size :uint64) i))
        when (not (zerop neigh))
          collect neigh))))

(defun grid-disk-distances (origin k)
  (let ((max-neighbors (max-grid-disk-size k)))
    (autowrap:with-many-alloc ((neighbors 'clh3i::h3index max-neighbors)
                               (distances :int max-neighbors))
      (loop
        for i below max-neighbors
        for offset64 = (* (cffi:foreign-type-size :uint64) i)
        for offset32 = (* (cffi:foreign-type-size :int) i)
        do
           (setf (cffi:mem-ref neighbors :uint64 offset64) 0)
           (setf (cffi:mem-ref distances :int offset32) 0))
      (clh3i::grid-disk-distances origin k neighbors distances)
      (loop
        for i below max-neighbors
        for offset64 = (* (cffi:foreign-type-size :uint64) i)
        for offset32 = (* (cffi:foreign-type-size :int) i)
        for neigh = (cffi:mem-ref neighbors :uint64 offset64)
        for dist =  (cffi:mem-ref distances :int offset32)
        when (not (zerop neigh))
          collect (cons neigh dist)))))

(defun grid-distance (a b)
  (autowrap:with-many-alloc ((dist 'clh3i::uint64-t))
    (clh3i::grid-distance a b dist)
    (cffi:mem-ref dist :uint64)))

(defun grid-path-cells-size (start end)
  (autowrap:with-alloc (max-cells 'clh3i::int64-t)
    (clh3i::grid-path-cells-size start end max-cells)
    (cffi::mem-ref max-cells :int64 0)))

(defun grid-path-cells (start end)
  (let ((max-path-size (grid-path-cells-size start end)))
    (autowrap:with-alloc (cells 'clh3i::h3index max-path-size)
      (loop
        for i below max-path-size
        for offset = (* (cffi:foreign-type-size :uint64) i)
        for cell = (cffi:mem-ref cells :uint64 offset)
        when (not (zerop cell))
          collect cell))))

(defun grid-disks-unsafe (h3set k)
  (let* ((in-size (length h3set))
         (max-neighbors (* in-size (max-grid-disk-size k))))
    (autowrap:with-many-alloc ((neighbors 'clh3i::h3index max-neighbors)
                               (ch3set 'clh3i::h3index in-size))
      (loop
        for i below in-size
        for offset = (* (cffi:foreign-type-size :uint64) i)
        for cell in h3set
        do
           (setf (cffi:mem-ref ch3set :uint64 offset) cell))
      (loop
        for i below max-neighbors
        do
           (setf (cffi:mem-ref neighbors :uint64 (* (cffi:foreign-type-size :uint64) i)) 0))

      (clh3i::grid-disks-unsafe ch3set in-size k neighbors)

      (loop
        for i below max-neighbors
        for neigh = (cffi:mem-ref neighbors :uint64 (* (cffi:foreign-type-size :uint64) i))
        when (not (zerop neigh))
          collect neigh))))

(defun grid-ring-unsafe (index k)
  (let ((max-cells (if (zerop k) 1 (* 6 k))))
    (autowrap:with-alloc (cells 'clh3i::h3index max-cells)
      (loop for i below max-cells do
        (setf (cffi:mem-ref cells :uint64 (* (cffi:foreign-type-size :uint64) i)) 0))
      (clh3i::grid-disk index k cells)
      (loop
        for i below max-cells
        for cell = (cffi:mem-ref cells :uint64 (* (cffi:foreign-type-size :uint64) i))
        when (not (zerop cell))
          collect cell))))

(defun string-to-h3 (str)
  (autowrap:with-alloc (idx 'clh3i::h3index)
    (clh3i::string-to-h3 str idx)
    (cffi:mem-ref idx :uint64 0)))

(defun h3-to-string (index)
  (format nil "~x" index))

(setf (symbol-function 'is-pentagon) #'clh3i::is-pentagon)
(setf (symbol-function 'is-valid-cell) #'clh3i::is-valid-cell)
(setf (symbol-function 'is-res-class-iii) #'clh3i::is-res-class-iii)
(setf (symbol-function 'is-valid-directed-edge) #'clh3i::is-valid-directed-edge)
(setf (symbol-function 'is-valid-vertex) #'clh3i::is-valid-vertex)


(defun lat-lng-to-cell (lat lng resolution)
  (autowrap:with-many-alloc ((geo 'clh3i::lat-lng)
                             (index 'clh3i::h3index))
    (setf (clh3i::lat-lng.lat geo) lat)
    (setf (clh3i::lat-lng.lng geo) lng)
    (clh3i::lat-lng-to-cell geo resolution index)
    (cffi:mem-ref index :uint64)))

(defun latlng-to-cell (latlng resolution)
  (autowrap:with-many-alloc ((geo 'clh3i::lat-lng)
                             (index 'clh3i::h3index))
    (setf (clh3i::lat-lng.lat geo) (lat latlng))
    (setf (clh3i::lat-lng.lng geo) (lng latlng))
    (clh3i::lat-lng-to-cell geo resolution index)
    (cffi:mem-ref index :uint64)))

(defun origin-to-directed-edges (origin)
  (let ((max-cells 6))
    (autowrap:with-alloc (cells 'clh3i::h3index max-cells)
      (loop for i below max-cells do
        (setf (cffi:mem-ref cells :uint64 (* (cffi:foreign-type-size :uint64) i)) 0))
      (clh3i::origin-to-directed-edges origin cells)
      (loop
        for i below max-cells
        for cell = (cffi:mem-ref cells :uint64 (* (cffi:foreign-type-size :uint64) i))
        when (not (zerop cell))
          collect cell))))


(defun polygon-to-cells (polygon res &optional (poly-holes nil))
  (autowrap:with-many-alloc ((geo-polygon 'clh3i::geo-polygon 1)
                             (verts 'clh3i::lat-lng (length polygon))
                             (holes 'clh3i::geo-loop (length poly-holes))
                             (max-size 'clh3i::int64-t 1))
    (setf (clh3i::geo-loop.num-verts (clh3i::geo-polygon.geoloop geo-polygon))
          (length polygon))
    (setf (clh3i::geo-loop.verts (clh3i::geo-polygon.geoloop geo-polygon))
          (clh3i::lat-lng-ptr verts))
    (loop
      for i below (length polygon)
      for pt in polygon
      for lat = (car pt)
      for lng = (cdr pt)
      for offset = (* (autowrap:foreign-type-size 'clh3i::lat-lng) i)
      for lat-lng = (clh3i::make-lat-lng :ptr (cffi:inc-pointer
                                               (clh3i::geo-loop.verts (clh3i::geo-polygon.geoloop geo-polygon))
                                               offset))
      do
         (setf (clh3i::lat-lng.lat lat-lng) lat)
         (setf (clh3i::lat-lng.lng lat-lng) lng))

    (setf (clh3i::geo-polygon.num-holes geo-polygon) (length poly-holes))
    (setf (clh3i::geo-polygon.holes geo-polygon) (clh3i::geo-loop-ptr holes))
    (loop
      for i below (length poly-holes)
      for hole in poly-holes
      for offset = (* (autowrap:foreign-type-size 'clh3i::geo-loop) i)
      for loop-ptr = (autowrap:alloc 'clh3i::geo-loop 1)
      do
         (setf (cffi:mem-ref (clh3i::geo-loop-ptr holes) :pointer offset) loop-ptr)
         (setf (clh3i::geo-loop.num-verts (autowrap:ptr loop-ptr)) (length hole))
         (setf (clh3i::geo-loop.verts (autowrap:ptr loop-ptr))
               (autowrap:alloc 'clh3i::lat-lng (length hole)))
         (loop
           for i below (length hole)
           for pt in hole
           for lat = (car pt)
           for lng = (cdr pt)
           for offset = (* (autowrap:foreign-type-size 'clh3i::lat-lng) i)
           for lat-lng = (clh3i::make-lat-lng
                          :ptr (cffi:inc-pointer
                                loop-ptr offset))
           do
              (setf (clh3i::lat-lng.lat lat-lng) lat)
              (setf (clh3i::lat-lng.lng lat-lng) lng)))
    (clh3i::max-polygon-to-cells-size geo-polygon res max-size)
    (autowrap:with-alloc (cells 'clh3i::h3index (cffi:mem-ref max-size :int64))

      (clh3i::polygon-to-cells geo-polygon res cells)

      (loop
        for i below (cffi:mem-ref max-size :int64)
        for offset = (* (cffi:foreign-type-size :uint64) i)
        for cell = (cffi:mem-ref cells :uint64 offset)
        when (not (zerop cell))
          collect cell))))


(defun h3-set-to-multi-polygon (h3set)
  (when (null h3set)
    (return-from h3-set-to-multi-polygon  nil))

  (autowrap:with-many-alloc ((cells 'clh3i::h3index (length h3set))
                             (lgp 'clh3i::linked-geo-polygon 1))
      ;; Fill in C array
      (loop
        for i below (length h3set)
        for cell in h3set
        for offset = (* (cffi:foreign-type-size :uint64) i)
        do
           (setf (cffi:mem-ref cells :uint64 offset) cell))

      ;; Call C function
      (clh3i::h3set-to-linked-geo cells (length h3set) lgp)

      ;; Unpack linked-geo into lisp-geo list

      (let ((lisp-geo
      ;; Loop over LinkedGeoPolygon
            (loop
              for gp = lgp
                then (if (cffi:pointer-eq (cffi:null-pointer) (clh3i::linked-geo-polygon.next gp))
                         nil
                         (clh3i::make-linked-geo-polygon :ptr (clh3i::linked-geo-polygon.next gp)))
              while gp
              collect
              ;; Loop over LinkedGeoLoop
              (loop
                for lgl = (clh3i::make-linked-geo-loop :ptr (clh3i::linked-geo-polygon.first gp))
                  then (if (cffi:pointer-eq (cffi:null-pointer) (clh3i::linked-geo-loop.next lgl))
                           nil
                           (clh3i::make-linked-geo-loop :ptr (clh3i::linked-geo-loop.next lgl)))
                while lgl
                collect
                ;; Loop over LinkedLatLng
                (loop
                  for lll = (clh3i::make-linked-lat-lng :ptr (clh3i::linked-geo-loop.first lgl))
                    then (if (cffi:pointer-eq (cffi:null-pointer) (clh3i::linked-lat-lng.next lll))
                           nil
                           (clh3i::make-linked-lat-lng :ptr (clh3i::linked-lat-lng.next lll)))
                  while lll
                  for vertex = (clh3i::linked-lat-lng.vertex lll)

                  ;; Collect ( lat . lng )
                  collect (lat-lng (clh3i::lat-lng.lat vertex)
                                (clh3i::lat-lng.lng vertex)))))))

      ;; Destroy linked-geo
      (clh3i::destroy-linked-polygon lgp)

      ;; return list of polygons
      lisp-geo)))

(defun uncompact-cells (compacted-set res &optional (max-cell-count 10000))
  (let ((num-compact (length compacted-set)))
    (autowrap:with-many-alloc ((in-cells 'clh3i::h3index num-compact)
                               (out-cells 'clh3i::h3index max-cell-count))
      ;; Build in-cells
      (loop
        for i below num-compact
        for cell in compacted-set
        for offset = (* (cffi:foreign-type-size :uint64) i)
        do
           (setf (cffi:mem-ref in-cells :uint64 offset) cell))
      ;; Zero out-cells
      (loop
        for i below max-cell-count
        for offset = (* (cffi:foreign-type-size :uint64) i)
        do
           (setf (cffi:mem-ref out-cells :uint64 offset) 0))
      (clh3i::uncompact-cells in-cells num-compact
                              out-cells max-cell-count
                              res)
      (loop
        for i below max-cell-count
        for offset = (* (cffi:foreign-type-size :uint64) i)
        for cell = (cffi:mem-ref out-cells :uint64 offset)
        when (not (zerop cell))
          collect cell))))

(defun vertex-to-lat-lng (vertex)
  (autowrap:with-many-alloc ((geo 'clh3i::lat-lng))
    (clh3i::vertex-to-lat-lng vertex geo)
    (lat-lng (clh3i::lat-lng.lat geo)
          (clh3i::lat-lng.lng geo))))


(defun haversine-distance (th1 ph1 th2 ph2)
  "Find the great circle distance between two points on a sphere.
Parameters are latitude and longitude of the first and second points, in radians.
Returns the great-circle distance in kilometers."
  (let* ((earth-radius 6371.0088)
         (phn (- ph1 ph2))
         (dz (- (sin th1) (sin th2)))
         (dx (- (* (cos phn) (cos th1)) (cos th2)))
         (dy (* (sin phn) (cos th1))))
    (* 2 earth-radius
       (asin (/ (sqrt (+ (* dx dx)
                         (* dy dy)
                         (* dz dz)))
                2)))))