imposm3/geom/geos/geos_wrap.go

90 lines
1.9 KiB
Go
Raw Normal View History

2013-05-06 21:24:49 +04:00
package geos
/*
#cgo LDFLAGS: -lgeos_c
#include "geos_c.h"
2013-08-09 12:08:30 +04:00
#include <stdint.h>
2013-05-06 21:24:49 +04:00
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
2013-08-09 12:08:30 +04:00
#include <string.h>
2013-05-06 21:24:49 +04:00
2013-05-23 19:53:58 +04:00
extern void goLogString(char *msg);
2013-05-06 21:24:49 +04:00
void debug_wrap(const char *fmt, ...) {
va_list a_list;
va_start(a_list, fmt);
char buf[1024];
2013-05-06 21:24:49 +04:00
vsnprintf(buf, sizeof(buf), fmt, a_list);
va_end(a_list);
2013-05-23 19:53:58 +04:00
goLogString((char *)&buf);
2013-05-06 21:24:49 +04:00
}
2013-06-05 14:21:55 +04:00
void devnull(const char *fmt, ...) {
}
2013-05-06 21:24:49 +04:00
GEOSContextHandle_t initGEOS_r_debug() {
2013-06-05 14:21:55 +04:00
return initGEOS_r(devnull, debug_wrap);
2013-05-06 21:24:49 +04:00
}
2013-05-15 11:49:38 +04:00
void initGEOS_debug() {
2013-06-05 14:21:55 +04:00
return initGEOS(devnull, debug_wrap);
2013-05-15 11:49:38 +04:00
}
2013-05-24 14:28:22 +04:00
2013-08-09 12:08:30 +04:00
typedef struct {
uint32_t num;
uint32_t *arr;
uint32_t arrCap;
} queryResult;
void queryResultAppend(queryResult *r, int idx) {
2013-08-09 16:26:21 +04:00
if ((r->num+1) >= r->arrCap) {
uint32_t newCap = r->arrCap > 0 ? r->arrCap * 2 : 2;
2013-08-09 12:08:30 +04:00
uint32_t *newArr = malloc(sizeof(uint32_t) * newCap);
if (r->arrCap == 0) {
r->arr = newArr;
} else {
2013-08-09 16:26:21 +04:00
memcpy(newArr, r->arr, sizeof(uint32_t) * r->arrCap);
2013-08-09 12:08:30 +04:00
free(r->arr);
r->arr = newArr;
}
r->arrCap = newCap;
}
r->arr[r->num] = idx;
2013-08-09 16:26:21 +04:00
r->num += 1;
2013-08-09 12:08:30 +04:00
}
void IndexQueryCallback(void *item, void *userdata) {
int idx = (size_t)item;
queryResult *result = (queryResult *)userdata;
queryResultAppend(result, idx);
2013-05-24 14:28:22 +04:00
}
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);
}
2013-08-09 12:08:30 +04:00
2013-05-24 14:28:22 +04:00
// query with our custom callback
2013-08-09 12:08:30 +04:00
uint32_t *IndexQuery(
2013-05-24 14:28:22 +04:00
GEOSContextHandle_t handle,
GEOSSTRtree *tree,
const GEOSGeometry *g,
2013-08-09 12:08:30 +04:00
uint32_t *num)
2013-05-24 14:28:22 +04:00
{
2013-08-09 12:08:30 +04:00
queryResult result = {0};
GEOSSTRtree_query_r(handle, tree, g, IndexQueryCallback, &result);
*num = result.num;
return result.arr;
}
2013-05-06 21:24:49 +04:00
*/
import "C"