File: osm.cc

package info (click to toggle)
pyosmium 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: python: 4,400; cpp: 2,504; makefile: 20
file content (321 lines) | stat: -rw-r--r-- 14,961 bytes parent folder | download | duplicates (4)
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
/* SPDX-License-Identifier: BSD-2-Clause
 *
 * This file is part of pyosmium. (https://osmcode.org/pyosmium/)
 *
 * Copyright (C) 2025 Sarah Hoffmann <lonvia@denofr.de> and others.
 * For a full list of authors see the git log.
 */
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>

#include <osmium/osm/entity_bits.hpp>

#include "cast.h"
#include "osm_base_objects.h"

namespace py = pybind11;

#if PYBIND11_VERSION_MINOR >= 11 || PYBIND11_VERSION_MAJOR > 2
/*
Work-around false positive added by pybind/pybind11@f701654 change:
ItemIterator/CollectionIterator ARE copy/move constructible, even if their template
parameter is not. Indeed, those iterators iterate over low-level memory representation
of the objects, without relying on their constructors.

For eg.
// static_assert(std::is_move_constructible<osmium::memory::CollectionIterator<osmium::RelationMember const>>::value);
// static_assert(!std::is_copy_constructible<osmium::RelationMember>::value);

The work-around relies on officially exposed pybind11::detail::is_copy_constructible/is_copy_constructible: 
https://github.com/pybind/pybind11/pull/4631
*/
namespace pybind11 {
namespace detail {
template <typename T>
struct is_copy_constructible<osmium::memory::CollectionIterator<T>>
    : std::true_type {};
template <typename T>
struct is_move_constructible<osmium::memory::CollectionIterator<T>>
    : std::true_type {};
template <typename T>
struct is_copy_constructible<osmium::memory::ItemIterator<T>>
    : std::true_type {};
template <typename T>
struct is_move_constructible<osmium::memory::ItemIterator<T>>
    : std::true_type {};
} // namespace detail
} // namespace pybind11
#endif

namespace {

using TagIterator = osmium::TagList::const_iterator;
using MemberIterator = osmium::RelationMemberList::const_iterator;

static py::object tag_iterator_next(TagIterator &it, TagIterator const &cend)
{
    if (it == cend)
        throw pybind11::stop_iteration();

    static auto const tag = pybind11::module_::import("osmium.osm.types").attr("Tag");
    auto const value = tag(it->key(), it->value());
    ++it;

    return value;
}


static py::object member_iterator_next(MemberIterator &it, MemberIterator const &cend)
{
    if (it == cend)
        throw pybind11::stop_iteration();

    static auto const obj = pybind11::module_::import("osmium.osm.types").attr("RelationMember");
    auto const value = obj(it->ref(), item_type_to_char(it->type()), it->role());
    ++it;

    return value;
}

using OuterRingIterator = osmium::memory::ItemIteratorRange<osmium::OuterRing const>::const_iterator;
using InnerRingIterator = osmium::memory::ItemIteratorRange<osmium::InnerRing const>::const_iterator;

template <typename T>
T const *ring_iterator_next(typename osmium::memory::ItemIteratorRange<T const>::const_iterator &it)
{
    if (!it)
        throw pybind11::stop_iteration();

    return &(*it++);
}

static pybind11::object get_node_item(osmium::NodeRefList const *list, Py_ssize_t idx)
{
    auto const sz = list->size();

    osmium::NodeRefList::size_type const iout =
        (idx >= 0 ? idx : (Py_ssize_t) sz + idx);

    if (iout >= sz || iout < 0) {
        throw pybind11::index_error("Bad index.");
    }

    auto const &node = (*list)[iout];

    static auto const node_ref_t = pybind11::module_::import("osmium.osm.types").attr("NodeRef");

    return node_ref_t(node.location(), node.ref());
}

template <typename T, typename P>
void make_node_list(py::module_ &m, char const *class_name)
{
    py::class_<T>(m, class_name)
        .def("size", [](T const *o, P const &parent)
            { parent.get(); return o->size(); })
        .def("get", [](T const *o, P const &parent, Py_ssize_t idx)
            { parent.get(); return get_node_item(o, idx); })
        .def("is_closed", [](T const *o, P const &parent)
            { parent.get(); return o->is_closed(); })
        .def("ends_have_same_location", [](T const *o, P const &parent)
            { parent.get(); return o->ends_have_same_location(); })
    ;
}


template <typename COSMObject>
py::class_<COSMObject> make_osm_object_class(py::module_ &m, char const *class_name)
{
    return py::class_<COSMObject>(m, class_name)
        .def("id", [](COSMObject const &o) { return o.get()->id(); })
        .def("deleted", [](COSMObject const &o) { return o.get()->deleted(); })
        .def("visible", [](COSMObject const &o) { return o.get()->visible(); })
        .def("version", [](COSMObject const &o) { return o.get()->version(); })
        .def("changeset", [](COSMObject const &o) { return o.get()->changeset(); })
        .def("uid", [](COSMObject const &o) { return o.get()->uid(); })
        .def("timestamp", [](COSMObject const &o) { return o.get()->timestamp(); })
        .def("user", [](COSMObject const &o) { return o.get()->user(); })
        .def("positive_id", [](COSMObject const &o) { return o.get()->positive_id(); })
        .def("user_is_anonymous", [](COSMObject const &o) { return o.get()->user_is_anonymous(); })
        .def("tags_size", [](COSMObject const &o) { return o.get()->tags().size(); })
        .def("tags_get_value_by_key", [](COSMObject const &o, char const *key, char const *def)
            { return o.get()->tags().get_value_by_key(key, def); })
        .def("tags_has_key", [](COSMObject const &o, char const *key)
            { return o.get()->tags().has_key(key); })
        .def("tags_begin", [](COSMObject const &o) { return o.get()->tags().cbegin(); })
        .def("tags_next", [](COSMObject const &o, TagIterator &it)
            { return tag_iterator_next(it, o.get()->tags().cend()); })
        .def("is_valid", &COSMObject::is_valid)
    ;
}

} // namespace

#ifdef Py_GIL_DISABLED
PYBIND11_MODULE(_osm, m, py::mod_gil_not_used())
#else
PYBIND11_MODULE(_osm, m)
#endif
{
    py::enum_<osmium::osm_entity_bits::type>(m, "osm_entity_bits")
        .value("NOTHING", osmium::osm_entity_bits::nothing)
        .value("NODE", osmium::osm_entity_bits::node)
        .value("WAY", osmium::osm_entity_bits::way)
        .value("RELATION", osmium::osm_entity_bits::relation)
        .value("AREA", osmium::osm_entity_bits::area)
        .value("OBJECT", osmium::osm_entity_bits::object)
        .value("CHANGESET", osmium::osm_entity_bits::changeset)
        .value("ALL", osmium::osm_entity_bits::all)
        .export_values()
        .def("__or__", &osmium::osm_entity_bits::operator|)
        .def("__and__", &osmium::osm_entity_bits::operator&)
        .def("__invert__", &osmium::osm_entity_bits::operator~)
        .def("__bool__", [](osmium::osm_entity_bits::type e) { return e != osmium::osm_entity_bits::nothing; })
    ;


    py::class_<osmium::Box>(m, "Box",
        "A bounding box around a geographic area. It is defined by an "
        ":py:class:`osmium.osm.Location` for the bottem-left corner and an "
        "``osmium.osm.Location`` for the top-right corner. Those locations may "
        " be invalid in which case the box is considered invalid, too.")
        .def(py::init<double, double, double, double>())
        .def(py::init<osmium::Location, osmium::Location>())
        .def_property_readonly("bottom_left",
                               (osmium::Location& (osmium::Box::*)())
                                   &osmium::Box::bottom_left,
                               py::return_value_policy::reference_internal,
             "(read-only) Bottom-left corner of the bounding box.")
        .def_property_readonly("top_right",
                               (osmium::Location& (osmium::Box::*)())
                                   &osmium::Box::top_right,
                               py::return_value_policy::reference_internal,
             "(read-only) Top-right corner of the bounding box.")
        .def("extend",
             (osmium::Box& (osmium::Box::*)(osmium::Location const&))
                 &osmium::Box::extend,
             py::arg("location"),
             py::return_value_policy::reference_internal,
             "Extend the box to include the given location. If the location "
             "is invalid the box remains unchanged. If the box is invalid, it "
             "will contain only the location after the operation. "
             "Returns a reference to itself.")
        .def("extend",
             (osmium::Box& (osmium::Box::*)(osmium::Box const &))
                 &osmium::Box::extend,
             py::arg("box"),
             py::return_value_policy::reference_internal,
             "Extend the box to include the given box. If the box to be added "
             "is invalid the input box remains unchanged. If the input box is invalid, it "
             "will become equal to the box that was added. "
             "Returns a reference to itself.")
        .def("valid", &osmium::Box::valid,
             "Check if the box coordinates are defined and with the usual bounds.")
        .def("size", &osmium::Box::size,
             "Return the size in square degrees.")
        .def("contains", &osmium::Box::contains, py::arg("location"),
             "Check if the given location is inside the box.")\
    ;


    py::class_<osmium::Location>(m, "Location",
        "A geographic coordinate in WGS84 projection. A location doesn't "
         "necessarily have to be valid.")
        .def(py::init<>())
        .def(py::init<double, double>())
        .def(py::self == py::self)
        .def_property_readonly("x", &osmium::Location::x,
             "(read-only) X coordinate (longitude) as a fixed-point integer.")
        .def_property_readonly("y", &osmium::Location::y,
             "(read-only) Y coordinate (latitude) as a fixed-point integer.")
        .def_property_readonly("lon", &osmium::Location::lon,
             "(read-only) Longitude (x coordinate) as floating point number."
             "Raises an :py:class:`osmium.InvalidLocationError` when the "
             "location is invalid.")
        .def_property_readonly("lat", &osmium::Location::lat,
             "(read-only) Latitude (y coordinate) as floating point number."
             "Raises an :py:class:`osmium.InvalidLocationError` when the "
             "location is invalid.")
        .def("valid", &osmium::Location::valid,
             "Check that the location is a valid WGS84 coordinate, i.e. "
             "that it is within the usual bounds.")
        .def("lat_without_check", &osmium::Location::lat_without_check,
             "Return latitude (y coordinate) without checking if the location "
             "is valid.")
        .def("lon_without_check", &osmium::Location::lon_without_check,
             "Return longitude (x coordinate) without checking if the location "
             "is valid.")
    ;


    py::class_<TagIterator>(m, "CTagListIterator");
    py::class_<MemberIterator>(m, "CRelationMemberListIterator");
    py::class_<OuterRingIterator>(m, "COuterRingIterator");
    py::class_<InnerRingIterator>(m, "CInnerRingIterator");


    make_osm_object_class<pyosmium::COSMNode>(m, "COSMNode")
        .def("location", [](pyosmium::COSMNode const &o) { return o.get()->location(); })
    ;

    make_osm_object_class<pyosmium::COSMWay>(m, "COSMWay")
        .def("is_closed", [](pyosmium::COSMWay const &o) { return o.get()->is_closed(); })
        .def("ends_have_same_location", [](pyosmium::COSMWay const &o) { return o.get()->ends_have_same_location(); })
        .def("nodes", [](pyosmium::COSMWay const &o) { return &o.get()->nodes(); },
             py::return_value_policy::reference)
    ;


    make_osm_object_class<pyosmium::COSMRelation>(m, "COSMRelation")
        .def("members_size", [](pyosmium::COSMRelation const &o) { return o.get()->members().size(); })
        .def("members_begin", [](pyosmium::COSMRelation const &o) { return o.get()->members().cbegin(); })
        .def("members_next", [](pyosmium::COSMRelation const &o, MemberIterator &it)
            { return member_iterator_next(it, o.get()->members().cend()); })

    ;

    make_osm_object_class<pyosmium::COSMArea>(m, "COSMArea")
        .def("from_way", [](pyosmium::COSMArea const &o) { return o.get()->from_way(); })
        .def("orig_id", [](pyosmium::COSMArea const &o) { return o.get()->orig_id(); })
        .def("is_multipolygon", [](pyosmium::COSMArea const &o) { return o.get()->is_multipolygon(); })
        .def("num_rings", [](pyosmium::COSMArea const &o) { return o.get()->num_rings(); })
        .def("outer_begin", [](pyosmium::COSMArea const &o) { return o.get()->outer_rings().cbegin(); })
        .def("outer_next", [](pyosmium::COSMArea const &o, OuterRingIterator &it) {
            o.get();
            return ring_iterator_next<osmium::OuterRing>(it);
        },
             py::return_value_policy::reference)
        .def("inner_begin", [](pyosmium::COSMArea const &o, osmium::OuterRing const &ring)
            { return o.get()->inner_rings(ring).cbegin(); })
        .def("inner_next", [](pyosmium::COSMArea const &o, InnerRingIterator &it) {
            o.get();
            return ring_iterator_next<osmium::InnerRing>(it);
        },
             py::return_value_policy::reference)
    ;

    py::class_<pyosmium::COSMChangeset>(m, "COSMChangeset")
        .def("id", [](pyosmium::COSMChangeset const &o) { return o.get()->id(); })
        .def("uid", [](pyosmium::COSMChangeset const &o) { return o.get()->uid(); })
        .def("created_at", [](pyosmium::COSMChangeset const &o) { return o.get()->created_at(); })
        .def("closed_at", [](pyosmium::COSMChangeset const &o) { return o.get()->closed_at(); })
        .def("open", [](pyosmium::COSMChangeset const &o) { return o.get()->open(); })
        .def("num_changes", [](pyosmium::COSMChangeset const &o) { return o.get()->num_changes(); })
        .def("user", [](pyosmium::COSMChangeset const &o) { return o.get()->user(); })
        .def("user_is_anonymous", [](pyosmium::COSMChangeset const &o) { return o.get()->user_is_anonymous(); })
        .def("bounds", [](pyosmium::COSMChangeset const &o) { return o.get()->bounds(); })
        .def("tags_size", [](pyosmium::COSMChangeset const &o) { return o.get()->tags().size(); })
        .def("tags_get_value_by_key", [](pyosmium::COSMChangeset const &o, char const *key, char const *def)
            { return o.get()->tags().get_value_by_key(key, def); })
        .def("tags_has_key", [](pyosmium::COSMChangeset const &o, char const *key)
            { return o.get()->tags().has_key(key); })
        .def("tags_begin", [](pyosmium::COSMChangeset const &o) { return o.get()->tags().cbegin(); })
        .def("tags_next", [](pyosmium::COSMChangeset const &o, TagIterator &it)
            { return tag_iterator_next(it, o.get()->tags().cend()); })
        .def("is_valid", &pyosmium::COSMChangeset::is_valid)
    ;

    make_node_list<osmium::WayNodeList, pyosmium::COSMWay>(m, "CWayNodeList");
    make_node_list<osmium::OuterRing, pyosmium::COSMArea>(m, "COuterRing");
    make_node_list<osmium::InnerRing, pyosmium::COSMArea>(m, "CInnerRing");
}