Skip to content

Commit 9643d14

Browse files
committed
expose add_box and add custom headers to SimpleWriter
Fixes #157.
1 parent 04d6b9e commit 9643d14

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

lib/io.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ PYBIND11_MODULE(io, m)
4343
&osmium::io::Header::set,
4444
py::arg("key"), py::arg("value"),
4545
"Set the value of header option 'key'.")
46+
.def("add_box", &osmium::io::Header::add_box,
47+
py::arg("box"),
48+
py::return_value_policy::reference_internal,
49+
"Add the given bounding box to the list of bounding boxes in the "
50+
" header.")
4651
;
4752

4853
py::class_<osmium::io::Reader>(m, "Reader",

lib/osm.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <pybind11/pybind11.h>
2+
#include <pybind11/operators.h>
23

34
#include <osmium/osm.hpp>
45
#include <osmium/osm/entity_bits.hpp>
@@ -53,6 +54,7 @@ PYBIND11_MODULE(_osm, m) {
5354
"necessarily have to be valid.")
5455
.def(py::init<>())
5556
.def(py::init<double, double>())
57+
.def(py::self == py::self)
5658
.def_property_readonly("x", &osmium::Location::x,
5759
"(read-only) X coordinate (longitude) as a fixed-point integer.")
5860
.def_property_readonly("y", &osmium::Location::y,

lib/simple_writer.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <osmium/osm.hpp>
44
#include <osmium/io/any_output.hpp>
55
#include <osmium/io/writer.hpp>
6+
#include <osmium/io/header.hpp>
67
#include <osmium/memory/buffer.hpp>
78
#include <osmium/builder/osm_object_builder.hpp>
89

@@ -17,8 +18,8 @@ class SimpleWriter
1718
enum { BUFFER_WRAP = 4096 };
1819

1920
public:
20-
SimpleWriter(const char* filename, size_t bufsz=4096*1024)
21-
: writer(filename),
21+
SimpleWriter(const char* filename, size_t bufsz=4096*1024, osmium::io::Header header=osmium::io::Header())
22+
: writer(filename, header),
2223
buffer(bufsz < 2 * BUFFER_WRAP ? 2 * BUFFER_WRAP : bufsz,
2324
osmium::memory::Buffer::auto_grow::yes),
2425
buffer_size(buffer.capacity()) // same rounding to BUFFER_WRAP
@@ -274,6 +275,7 @@ void init_simple_writer(pybind11::module &m)
274275
"parameter allows changing the default buffer size of 4MB. Larger buffers "
275276
"are normally better but you should be aware that there are normally multiple "
276277
"buffers in use during the write process.")
278+
.def(py::init<const char*, unsigned long, osmium::io::Header>())
277279
.def(py::init<const char*, unsigned long>())
278280
.def(py::init<const char*>())
279281
.def("add_node", &SimpleWriter::add_node, py::arg("node"),

test/test_writer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,27 @@ def test_member_object(test_writer, simple_handler):
190190
simple_handler(rel_opl,
191191
relation=lambda o: w.add_relation(O(members=[m for m in o.members
192192
if m.role != 'inner'])))
193+
194+
195+
def test_set_custom_header(tmp_path):
196+
fn = str(tmp_path / 'test.xml')
197+
h = o.io.Header()
198+
h.set('generator', 'foo')
199+
h.add_box(o.osm.Box(0.1, -4, 10, 45))
200+
201+
writer = o.SimpleWriter(fn, 4000, h)
202+
203+
try:
204+
writer.add_node({})
205+
finally:
206+
writer.close()
207+
208+
rd = o.io.Reader(fn)
209+
try:
210+
h = rd.header()
211+
assert h.get('generator') == 'foo'
212+
assert h.box().valid()
213+
assert h.box().bottom_left == o.osm.Location(0.1, -4)
214+
assert h.box().top_right == o.osm.Location(10, 45)
215+
finally:
216+
rd.close()

0 commit comments

Comments
 (0)