-
Notifications
You must be signed in to change notification settings - Fork 156
/
geos_wrap.go
89 lines (76 loc) · 1.91 KB
/
geos_wrap.go
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
package geos
/*
#cgo LDFLAGS: -lgeos_c
#include "geos_c.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
extern void goLogString(char *msg);
void debug_wrap(const char *fmt, ...) {
va_list a_list;
va_start(a_list, fmt);
char buf[1024];
vsnprintf(buf, sizeof(buf), fmt, a_list);
va_end(a_list);
goLogString((char *)&buf);
}
void devnull(const char *fmt, ...) {
}
GEOSContextHandle_t initGEOS_r_debug() {
return initGEOS_r(devnull, debug_wrap);
}
void initGEOS_debug() {
return initGEOS(devnull, debug_wrap);
}
typedef struct {
uint32_t num;
uint32_t *arr;
uint32_t arrCap;
} queryResult;
void queryResultAppend(queryResult *r, int idx) {
if ((r->num+1) >= r->arrCap) {
uint32_t newCap = r->arrCap > 0 ? r->arrCap * 2 : 2;
uint32_t *newArr = malloc(sizeof(uint32_t) * newCap);
if (r->arrCap == 0) {
r->arr = newArr;
} else {
memcpy(newArr, r->arr, sizeof(uint32_t) * r->arrCap);
free(r->arr);
r->arr = newArr;
}
r->arrCap = newCap;
}
r->arr[r->num] = idx;
r->num += 1;
}
void IndexQueryCallback(void *item, void *userdata) {
int idx = (size_t)item;
queryResult *result = (queryResult *)userdata;
queryResultAppend(result, idx);
}
void IndexAdd(
GEOSContextHandle_t handle,
GEOSSTRtree *tree,
const GEOSGeometry *g,
size_t id)
{
// instead of storing a void *, we just store our id
// this is safe since GEOS doesn't access the item pointer
GEOSSTRtree_insert_r(handle, tree, g, (void *)id);
}
// query with our custom callback
uint32_t *IndexQuery(
GEOSContextHandle_t handle,
GEOSSTRtree *tree,
const GEOSGeometry *g,
uint32_t *num)
{
queryResult result = {0};
GEOSSTRtree_query_r(handle, tree, g, IndexQueryCallback, &result);
*num = result.num;
return result.arr;
}
*/
import "C"