从 python 调用 boost rtree

Calling boost rtree from python

我正在尝试调整此 boost rtree example 以便 python 调用 c++ 库。这是我第一次尝试从 python 调用 c++。我在 ubuntu 16.04.7 LTS 上使用 cppyy 1.8.5(python 版本 3.6.7)和 boost rtree header-only 库(boost 版本 1.58)。这是我到目前为止的代码。

import cppyy
cppyy.set_debug()
import cppyy.ll
cppyy.ll.set_signals_as_exception(True)

cppyy.add_include_path('/usr/include')
cppyy.include('boost/geometry.hpp')
cppyy.include('boost/geometry/geometries/point.hpp')
cppyy.include('boost/geometry/geometries/box.hpp')
cppyy.include('boost/geometry/index/rtree.hpp')
cppyy.include('vector')
cppyy.include('iostream')
cppyy.include('boost/foreach.hpp')

cpp = cppyy.gbl
std = cpp.std
bg = cpp.boost.geometry
bgi = cpp.boost.geometry.index

point = bg.model.point[float, 2, bg.cs.cartesian]
box = bg.model.box[point]
value = std.pair[box, 'unsigned']
rtree = bgi.rtree[value, bgi.quadratic(16)]

for i in range(10):
    b = box(point(i + 0.0, i + 0.0), point(i + 0.5, i + 0.5))
    print(bg.wkt[box](b))
    with cppyy.ll.signals_as_exception():
        rtree.insert(std.make_pair(b, i))

在 for 循环中,框已创建,但 rtree.insert() 调用产生错误。

$ python try_cppyy_rtree.py
TClass::GetClass: Header Parsing - The representation of __cppyy_internal was not found in the type system. A lookup in the interpreter is about to be tried: this can cause parsing. This can be avoided selecting __cppyy_internal in the linkdef/selection file.
...<trimmed>...
TClass::GetClass: Header Parsing - The representation of boost::geometry::wkt_manipulator<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> > > was not found in the type system. A lookup in the interpreter is about to be tried: this can cause parsing. This can be avoided selecting boost::geometry::wkt_manipulator<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> > > in the linkdef/selection file.
POLYGON((0 0,0 0.5,0.5 0.5,0.5 0,0 0))
TClass::GetClass: Header Parsing - The representation of std::make_pair was not found in the type system. A lookup in the interpreter is about to be tried: this can cause parsing. This can be avoided selecting std::make_pair in the linkdef/selection file.
TClass::GetClass: Header Parsing - The representation of std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,int> was not found in the type system. A lookup in the interpreter is about to be tried: this can cause parsing. This can be avoided selecting std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,int> in the linkdef/selection file.
TClass::GetClass: Header Parsing - The representation of std::__pair_base<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,int> was not found in the type system. A lookup in the interpreter is about to be tried: this can cause parsing. This can be avoided selecting std::__pair_base<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,int> in the linkdef/selection file.
In file included from input_line_19:1:
In file included from /usr/include/boost/geometry.hpp:17:
In file included from /usr/include/boost/geometry/geometry.hpp:43:
In file included from /usr/include/boost/geometry/core/radian_access.hpp:28:
In file included from /usr/include/boost/geometry/util/math.hpp:28:
In file included from /usr/include/boost/math/constants/constants.hpp:18:
In file included from /usr/include/boost/lexical_cast.hpp:30:
In file included from /usr/include/boost/range/iterator_range_core.hpp:38:
In file included from /usr/include/boost/range/functions.hpp:18:
/usr/include/boost/range/begin.hpp:47:17: error: member reference type 'std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, int> *const' is a pointer; did you mean
  to use '->'?
        return c.begin();
               ~^
                ->
/usr/include/boost/range/begin.hpp:111:12: note: in instantiation of function template specialization 'boost::range_detail::range_begin<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, int> *const>' requested here
    return range_begin( r );
           ^
/usr/include/boost/range/begin.hpp:127:46: note: in instantiation of function template specialization 'boost::range_adl_barrier::begin<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, int> *>' requested here
            return boost::range_adl_barrier::begin( r );
                                             ^
/usr/include/boost/geometry/index/rtree.hpp:1417:30: note: in instantiation of function template specialization 'boost::range_adl_barrier::const_begin<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, int> *>' requested here
        for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it )
                             ^
/usr/include/boost/geometry/index/rtree.hpp:624:15: note: in instantiation of function template specialization 'boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, unsigned int>, boost::geometry::index::quadratic<16, 4>,
  boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >,
  boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >,
  std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >
  >::insert_dispatch<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, int> *>' requested here
        this->insert_dispatch(conv_or_rng, is_conv_t());
              ^
note: in instantiation of function template specialization 'boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, unsigned int>, boost::geometry::index::quadratic<16, 4>,
  boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >,
  boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >,
  std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >
  >::insert<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, int> *>' requested here
In file included from input_line_19:1:
In file included from /usr/include/boost/geometry.hpp:17:
In file included from /usr/include/boost/geometry/geometry.hpp:43:
In file included from /usr/include/boost/geometry/core/radian_access.hpp:28:
In file included from /usr/include/boost/geometry/util/math.hpp:28:
In file included from /usr/include/boost/math/constants/constants.hpp:18:
In file included from /usr/include/boost/lexical_cast.hpp:30:
In file included from /usr/include/boost/range/iterator_range_core.hpp:38:
In file included from /usr/include/boost/range/functions.hpp:18:
/usr/include/boost/range/begin.hpp:47:18: error: no member named 'begin' in 'std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, int>'
        return c.begin();
               ~ ^
In file included from input_line_19:1:
In file included from /usr/include/boost/geometry.hpp:17:
In file included from /usr/include/boost/geometry/geometry.hpp:43:
In file included from /usr/include/boost/geometry/core/radian_access.hpp:28:
In file included from /usr/include/boost/geometry/util/math.hpp:28:
In file included from /usr/include/boost/math/constants/constants.hpp:18:
In file included from /usr/include/boost/lexical_cast.hpp:30:
In file included from /usr/include/boost/range/iterator_range_core.hpp:38:
In file included from /usr/include/boost/range/functions.hpp:19:
/usr/include/boost/range/end.hpp:48:21: error: member reference type 'std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, int> *const' is a pointer; did you mean
  to use '->'?
            return c.end();
                   ~^
                    ->
/usr/include/boost/range/end.hpp:105:12: note: in instantiation of function template specialization 'boost::range_detail::range_end<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, int> *const>' requested here
    return range_end( r );
           ^
/usr/include/boost/range/end.hpp:121:46: note: in instantiation of function template specialization 'boost::range_adl_barrier::end<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, int> *>' requested here
            return boost::range_adl_barrier::end( r );
                                             ^
/usr/include/boost/geometry/index/rtree.hpp:1417:61: note: in instantiation of function template specialization 'boost::range_adl_barrier::const_end<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, int> *>' requested here
        for ( It it = boost::const_begin(rng); it != boost::const_end(rng) ; ++it )
                                                            ^
/usr/include/boost/geometry/index/rtree.hpp:624:15: note: in instantiation of function template specialization 'boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, unsigned int>, boost::geometry::index::quadratic<16, 4>,
  boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >,
  boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >,
  std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >
  >::insert_dispatch<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, int> *>' requested here
        this->insert_dispatch(conv_or_rng, is_conv_t());
              ^
note: in instantiation of function template specialization 'boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2,
  boost::geometry::cs::cartesian> >, unsigned int>, boost::geometry::index::quadratic<16, 4>,
  boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >,
  boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >,
  std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, unsigned int> >
  >::insert<std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, int> *>' requested here
In file included from input_line_19:1:
In file included from /usr/include/boost/geometry.hpp:17:
In file included from /usr/include/boost/geometry/geometry.hpp:43:
In file included from /usr/include/boost/geometry/core/radian_access.hpp:28:
In file included from /usr/include/boost/geometry/util/math.hpp:28:
In file included from /usr/include/boost/math/constants/constants.hpp:18:
In file included from /usr/include/boost/lexical_cast.hpp:30:
In file included from /usr/include/boost/range/iterator_range_core.hpp:38:
In file included from /usr/include/boost/range/functions.hpp:19:
/usr/include/boost/range/end.hpp:48:22: error: no member named 'end' in 'std::pair<boost::geometry::model::box<boost::geometry::model::point<float, 2, boost::geometry::cs::cartesian> >, int>'
            return c.end();
                   ~ ^
Traceback (most recent call last):
  File "try_cppyy_rtree.py", line 58, in <module>
    rtree.insert(std.make_pair(b, i))
TypeError: Template method resolution failed:
  void boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::insert(const boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::value_type& value) =>
TypeError: unbound method rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::insert must be called with a rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > > instance as first argument
  void boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::insert(const std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,int>& conv_or_rng) =>
TypeError: unbound method rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::insert must be called with a rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > > instance as first argument
  void boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::insert(std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,int>*const& conv_or_rng) =>
TypeError: unbound method rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::insert must be called with a rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > > instance as first argument
  void boost::geometry::index::rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::insert(const std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,int>& conv_or_rng) =>
TypeError: unbound method rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > >::insert must be called with a rtree<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int>,boost::geometry::index::quadratic<16,4>,boost::geometry::index::indexable<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,boost::geometry::index::equal_to<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> >,std::allocator<std::pair<boost::geometry::model::box<boost::geometry::model::point<float,2,boost::geometry::cs::cartesian> >,unsigned int> > > instance as first argument

关于如何让它工作的任何见解?是否建议将 cppyy 用于我正在尝试的操作?我最初选择 cppyy 是因为我看到使用其他方法的示例似乎需要目标文件,但 boost rtree 是一个仅包含头文件的库。

使用 cppyy 1.8.5 有什么理由吗?脚本的问题与 cppyy 版本无关,但我只验证了它可以在 2.0.0(基于较新版本的 Clang)上正确运行。

这一行有问题:

rtree = bgi.rtree[value, bgi.quadratic(16)]

使得rtree仍然只是一个类型对象。将语法与您之前(正确)音译的前面的 typedef 进行比较。要获得 bgi::rtree 的实际实例,您需要实例化类型:

rtree = bgi.rtree[value, bgi.quadratic(16)]()

注意额外的 ()

我检查过 b 被复制了,所以循环创建框应该没问题。否则,使用 Python,因为 b 将被垃圾收集(与原始示例中的 C++ 相同,实际上,因为它们是堆栈对象),它的生命周期应该被管理(例如通过使用 smart指针)。

以下代码有效:

import cppyy
cppyy.set_debug()
import cppyy.ll
cppyy.ll.set_signals_as_exception(True)

cppyy.add_include_path('/usr/include')
cppyy.include('boost/geometry.hpp')
cppyy.include('boost/geometry/geometries/point.hpp')
cppyy.include('boost/geometry/geometries/box.hpp')
cppyy.include('boost/geometry/index/rtree.hpp')
cppyy.include('vector')
cppyy.include('iostream')
cppyy.include('boost/foreach.hpp')

cpp = cppyy.gbl 
std = cpp.std
bg = cpp.boost.geometry
bgi = cpp.boost.geometry.index

point = bg.model.point[float, 2, bg.cs.cartesian]   
box = bg.model.box[point]
value = std.pair[box, 'unsigned']
rtree = bgi.rtree[value, bgi.quadratic(16)]

for i in range(10):
    b = box(point(i + 0.0, i + 0.0), point(i + 0.5, i + 0.5))
    print(bg.wkt[box](b))
    with cppyy.ll.signals_as_exception():
        rtree.insert(std.make_pair(b, i))

# find values intersecting some area defined by a box
query_box = box(point(0,0), point(5,5))
result_s = std.vector[value]()
rtree.query(bgi.intersects(query_box), std.back_inserter(result_s))

# find 5 nearest values to a point
result_n = std.vector[value]()
rtree.query(bgi.nearest(point(0,0), 5), 
std.back_inserter(result_n))

# display results
print('spatial query box:')
print(bg.wkt[box](query_box))
print('spatial query result')
for v in result_s:
    print(str(bg.wkt[box](v[0])) + " - " + str(v[1]))

print('knn query point:') 
print(bg.wkt[point](point(0,0)))
print('knn query result')
for v in result_n:
    print(str(bg.wkt[box](v[0])) + " - " + str(v[1]))