forked from lduchesne/cmatopo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
116 lines (93 loc) · 2.53 KB
/
utils.h
File metadata and controls
116 lines (93 loc) · 2.53 KB
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
#ifndef __CMA_UTILS_H
#define __CMA_UTILS_H
#include <geos_c.h>
#include <string>
#include <vector>
#include <cassert>
namespace cma {
extern GEOSContextHandle_t hdl;
void geos_message_function(const char *fmt, ...);
class GEOSHelper
{
public:
GEOSHelper() {
hdl = initGEOS_r(geos_message_function, geos_message_function);
assert (hdl);
if (hdl) {
wkbr = GEOSWKBReader_create_r(hdl);
wktr = GEOSWKTReader_create_r(hdl);
wkbw = GEOSWKBWriter_create_r(hdl);
wktw = GEOSWKTWriter_create_r(hdl);
GEOSWKTWriter_setRoundingPrecision_r(hdl, wktw, 8);
}
}
~GEOSHelper() {
if (wkbr) GEOSWKBReader_destroy_r(hdl, wkbr);
if (wktr) GEOSWKTReader_destroy_r(hdl, wktr);
if (wkbw) GEOSWKBWriter_destroy_r(hdl, wkbw);
if (wktw) GEOSWKTWriter_destroy_r(hdl, wktw);
finishGEOS_r(hdl);
}
GEOSContextHandle_t handle() const {
return hdl;
}
GEOSWKBReader* reader() {
return wkbr;
}
GEOSWKTReader* text_reader() {
return wktr;
}
GEOSWKBWriter* writer() {
return wkbw;
}
GEOSWKTWriter* text_writer() {
return wktw;
}
std::string as_string(const GEOSGeometry* geom) {
assert (geom);
// GEOSWKTWriter_setRoundingPrecision_r(hdl, text_writer(), 15);
char* wkt_c = GEOSWKTWriter_write_r(hdl, text_writer(), geom);
std::string wkt(wkt_c);
GEOSFree_r(hdl, wkt_c);
return wkt;
}
std::string as_hex_string(const GEOSGeometry* geom) {
assert (geom);
// GEOSWKTWriter_setRoundingPrecision_r(hdl, text_writer(), 15);
size_t size;
unsigned char* hex_c = GEOSWKBWriter_writeHEX_r(hdl, writer(), geom, &size);
std::string hex((char*)hex_c, size);
GEOSFree_r(hdl, hex_c);
return hex;
}
void print_geom(const GEOSGeometry* geom);
private:
GEOSWKBReader* wkbr = NULL;
GEOSWKTReader* wktr = NULL;
GEOSWKBWriter* wkbw = NULL;
GEOSWKTWriter* wktw = NULL;
};
template<class C, class T>
bool _is_in(T hay, const C& stack)
{
return (std::find(stack.begin(), stack.end(), hay) != stack.end());
}
template <class T>
bool _is_null(T& val)
{
return (val == std::numeric_limits<T>::max());
}
/**
* Delete all elements of a pointer vector and empty it.
*/
template <class T>
void delete_all(std::vector<T*>& v)
{
for (T* t : v) {
if (!t) continue;
delete t;
}
v.clear();
}
} // namespace cma
#endif // __CMA_UTILS_H