File: python_handler.h

package info (click to toggle)
pyosmium 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,600 kB
  • sloc: python: 4,493; cpp: 2,616; makefile: 21
file content (104 lines) | stat: -rw-r--r-- 3,104 bytes parent folder | download | duplicates (5)
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
/* SPDX-License-Identifier: BSD-2-Clause
 *
 * This file is part of pyosmium. (https://osmcode.org/pyosmium/)
 *
 * Copyright (C) 2024 Sarah Hoffmann <lonvia@denofr.de> and others.
 * For a full list of authors see the git log.
 */
#ifndef PYOSMIUM_PYTHON_HANDLER_HPP
#define PYOSMIUM_PYTHON_HANDLER_HPP

#include <pybind11/pybind11.h>

#include "base_handler.h"
#include "osm_base_objects.h"

namespace pyosmium {

class PythonHandler : public BaseHandler
{
public:
    PythonHandler(pybind11::handle handler)
    : m_handler(handler)
    {
        m_enabled_for = osmium::osm_entity_bits::nothing;
        if (pybind11::hasattr(m_handler, "node")) {
            m_enabled_for |= osmium::osm_entity_bits::node;
        }
        if (pybind11::hasattr(m_handler, "way")) {
            m_enabled_for |= osmium::osm_entity_bits::way;
        }
        if (pybind11::hasattr(m_handler, "relation")) {
            m_enabled_for |= osmium::osm_entity_bits::relation;
        }
        if (pybind11::hasattr(m_handler, "area")) {
            m_enabled_for |= osmium::osm_entity_bits::area;
        }
        if (pybind11::hasattr(m_handler, "changeset")) {
            m_enabled_for |= osmium::osm_entity_bits::changeset;
        }
    }


    bool node(PyOSMNode &n) override
    {
        if (m_enabled_for & osmium::osm_entity_bits::node) {
            auto ret = m_handler.attr("node")(n.get_or_create_python_object());
            if (pybind11::isinstance<pybind11::bool_>(ret) && ret.cast<bool>()) {
                return true;
            }
        }
        return false;
    }

    bool way(PyOSMWay &w) override
    {
        if (m_enabled_for & osmium::osm_entity_bits::way) {
            auto ret = m_handler.attr("way")(w.get_or_create_python_object());
            if (pybind11::isinstance<pybind11::bool_>(ret) && ret.cast<bool>()) {
                return true;
            }
        }
        return false;
    }

    bool relation(PyOSMRelation &r) override
    {
        if (m_enabled_for & osmium::osm_entity_bits::relation) {
            auto ret = m_handler.attr("relation")(r.get_or_create_python_object());
            if (pybind11::isinstance<pybind11::bool_>(ret) && ret.cast<bool>()) {
                return true;
            }
        }
        return false;
    }

    bool changeset(PyOSMChangeset &c) override
    {
        if (m_enabled_for & osmium::osm_entity_bits::changeset) {
            auto ret = m_handler.attr("changeset")(c.get_or_create_python_object());
            if (pybind11::isinstance<pybind11::bool_>(ret) && ret.cast<bool>()) {
                return true;
            }
        }
        return false;
    }

    bool area(PyOSMArea &a) override
    {
        if (m_enabled_for & osmium::osm_entity_bits::area) {
            auto ret = m_handler.attr("area")(a.get_or_create_python_object());
            if (pybind11::isinstance<pybind11::bool_>(ret) && ret.cast<bool>()) {
                return true;
            }
        }
        return false;
    }
private:
    pybind11::handle m_handler;
};

} // namespace

#endif //PYOSMIUM_PYTHON_HANDLER_HPP