Skip to content

Commit

Permalink
move docs of remaining classes to pyi files
Browse files Browse the repository at this point in the history
  • Loading branch information
lonvia committed Sep 12, 2024
1 parent 52914ad commit db1e7e9
Show file tree
Hide file tree
Showing 13 changed files with 552 additions and 217 deletions.
13 changes: 2 additions & 11 deletions lib/area.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,13 @@ PYBIND11_MODULE(area, m)
py::class_<AreaManagerBufferHandler, pyosmium::BaseHandler>(m,
"AreaManagerBufferHandler");

py::class_<AreaManager, pyosmium::BaseHandler>(m, "AreaManager",
"Object manager class that manages building area objects from "
"ways and relations.")
py::class_<AreaManager, pyosmium::BaseHandler>(m, "AreaManager")
.def(py::init<>())
.def("first_pass_handler", &AreaManager::first_pass_handler,
"Return the handler object used for the first pass of the "
"file, which collects information about the relations.",
py::return_value_policy::reference)
.def("second_pass_handler", &AreaManager::second_pass_handler,
"Return the handler object used for the second pass of the "
"file, where areas are assembled. Pass the handlers that "
"should handle the areas.",
py::return_value_policy::take_ownership, py::keep_alive<1, 2>())
.def("second_pass_to_buffer", &AreaManager::second_pass_to_buffer,
py::keep_alive<1, 2>(),
"Return a handler object for the second pass of the file. "
"The handler holds a buffer, which can be iterated over.")
py::keep_alive<1, 2>())
;
}
59 changes: 19 additions & 40 deletions lib/geom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ using WKTFactory = og::WKTFactory<>;
using GeoJSONFactory = og::GeoJSONFactory<>;

template <typename Factory>
void make_factory_class(py::module_ &m, char const *name, char const *doc)
void make_factory_class(py::module_ &m, char const *name)
{
py::class_<Factory>(m, name, doc)
py::class_<Factory>(m, name)
.def(py::init<>())
.def_property_readonly("epsg", &Factory::epsg,
"(read-only) EPSG number of the output geometry.")
.def_property_readonly("proj_string", &Factory::proj_string,
"(read-only) projection string of the output geometry.")
.def_property_readonly("epsg", &Factory::epsg)
.def_property_readonly("proj_string", &Factory::proj_string)
.def("create_point",
[](Factory &f, py::object const &o) {
if (py::isinstance<osmium::Location>(o)) {
Expand All @@ -55,8 +53,7 @@ void make_factory_class(py::module_ &m, char const *name, char const *doc)

return f.create_point(o.attr("location").cast<osmium::Location const &>());
},
py::arg("pt"),
"Create a point geometry from a :py:class:`osmium.osm.Node`.")
py::arg("pt"))
.def("create_linestring",
[](Factory &f, py::object const &o, og::use_nodes un, og::direction dir) {
auto const *way = pyosmium::try_cast<pyosmium::COSMWay>(o);
Expand All @@ -67,14 +64,12 @@ void make_factory_class(py::module_ &m, char const *name, char const *doc)
return f.create_linestring(pyosmium::cast_list<osmium::WayNodeList>(o), un, dir);
},
py::arg("list"), py::arg("use_nodes")=og::use_nodes::unique,
py::arg("direction")=og::direction::forward,
"Create a LineString geometry from a :py:class:`osmium.osm.Way`.")
py::arg("direction")=og::direction::forward)
.def("create_multipolygon",
[](Factory &f, py::object const &o) {
return f.create_multipolygon(*pyosmium::cast<pyosmium::COSMArea>(o).get());
},
py::arg("area"),
"Create a MultiPolygon geometry from a :py:class:`osmium.osm.Area`.")
py::arg("area"))
;
}

Expand All @@ -94,49 +89,33 @@ PYBIND11_MODULE(geom, m)
.export_values()
;

py::class_<osmium::geom::Coordinates>(m, "Coordinates",
"Class representing coordinates")
py::class_<osmium::geom::Coordinates>(m, "Coordinates")
.def(py::init<>())
.def(py::init<double, double>())
.def(py::init<osmium::Location const &>())
.def_readonly("x", &osmium::geom::Coordinates::x,
"(read-only) X coordinate.")
.def_readonly("y", &osmium::geom::Coordinates::y,
"(read-only) Y coordinate.")
.def("valid", &osmium::geom::Coordinates::valid,
"True if coordinates are valid")
.def_readonly("x", &osmium::geom::Coordinates::x)
.def_readonly("y", &osmium::geom::Coordinates::y)
.def("valid", &osmium::geom::Coordinates::valid)
;

m.def("haversine_distance",
[](py::object const &o) { return og::haversine::distance(pyosmium::cast_list<osmium::WayNodeList>(o)); },
py::arg("list"),
"Compute the distance using the Haversine algorithm which takes the "
"curvature of earth into account. If a :py:class:`WayNodeList` is given "
"as a parameter the total length of the way in meters is computed.");
py::arg("list"));

m.def("haversine_distance",
static_cast<double (*)(og::Coordinates const &, og::Coordinates const &)>(&og::haversine::distance),
"Compute the Haversine distance between two coordinates.");
static_cast<double (*)(og::Coordinates const &, og::Coordinates const &)>(&og::haversine::distance));
m.def("haversine_distance",
[](osmium::Location const &l1, osmium::Location const &l2) {
return og::haversine::distance(og::Coordinates(l1), og::Coordinates(l2));
},
"Compute the Haversine distance between two osmium locations.");
});

m.def("lonlat_to_mercator", &og::lonlat_to_mercator,
py::arg("coordinate"),
"Convert coordinates from WGS84 to Mercator projection.");
m.def("lonlat_to_mercator", &og::lonlat_to_mercator);

m.def("mercator_to_lonlat", &og::mercator_to_lonlat,
py::arg("coordinate"),
"Convert coordinates from WGS84 to Mercator projection.");
m.def("mercator_to_lonlat", &og::mercator_to_lonlat);

make_factory_class<WKBFactory>(m, "WKBFactory",
"Factory that creates WKB from osmium geometries.");
make_factory_class<WKBFactory>(m, "WKBFactory");

make_factory_class<WKTFactory>(m, "WKTFactory",
"Factory that creates WKT from osmium geometries.");
make_factory_class<WKTFactory>(m, "WKTFactory");

make_factory_class<GeoJSONFactory>(m, "GeoJSONFactory",
"Factory that creates GeoJSON geometries from osmium geometries.");
make_factory_class<GeoJSONFactory>(m, "GeoJSONFactory");
}
45 changes: 15 additions & 30 deletions lib/index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,20 @@ PYBIND11_MODULE(index, m)
using IndexFactory =
osmium::index::MapFactory<osmium::unsigned_object_id_type, osmium::Location>;

py::class_<LocationTable>(m, "LocationTable",
"A map from a node ID to a location object. This implementation works "
"only with positive node IDs.")
.def("set", &LocationTable::set, py::arg("id"), py::arg("loc"),
"Set the location for a given node id.")
.def("get", &LocationTable::get, py::arg("id"),
"Return the location for a given id.")
.def("used_memory", &LocationTable::used_memory,
"Return the size (in bytes) currently allocated by this location table.")
.def("clear", &LocationTable::clear,
"Remove all entries from the location table.")
py::class_<LocationTable>(m, "LocationTable")
.def("set", &LocationTable::set, py::arg("id"), py::arg("loc"))
.def("get", &LocationTable::get, py::arg("id"))
.def("used_memory", &LocationTable::used_memory)
.def("clear", &LocationTable::clear)
.def ("__setitem__", &LocationTable::set)
.def ("__getitem__", &LocationTable::get)
;

m.def("create_map", [](const std::string& config_string) {
const auto& map_factory = IndexFactory::instance();
return map_factory.create_map(config_string);
},
py::arg("map_type"),
"Create a new location store. The string parameter takes the type "
"and, where required, additional arguments separated by comma. For "
"example, to create an array cache backed by a file ``foo.store``, "
"the map_type should be ``dense_file_array,foo.store``.");
py::arg("map_type"));

m.def("map_types", []() {
const auto& map_factory = IndexFactory::instance();
Expand All @@ -52,24 +44,17 @@ PYBIND11_MODULE(index, m)
l.append(e);

return l;
},
"Return a list of strings with valid types for the location table.");
});

using IdSet = osmium::index::IdSetDense<osmium::unsigned_object_id_type>;

py::class_<IdSet>(m, "IdSet",
"Compact storage for a set of IDs.")
py::class_<IdSet>(m, "IdSet")
.def(py::init<>())
.def("set", &IdSet::set,
"Add an ID to the storage. Does nothing if the ID is already contained.")
.def("unset", &IdSet::unset,
"Remove an ID from the storage. Does nothing if the ID is not in the storage.")
.def("get", &IdSet::get,
"Check if the given ID is in the storage.")
.def("empty", &IdSet::empty,
"Return true if no IDs are stored.")
.def("clear", &IdSet::clear,
"Remove all IDs from the storage.")
.def("set", &IdSet::set)
.def("unset", &IdSet::unset)
.def("get", &IdSet::get)
.def("empty", &IdSet::empty)
.def("clear", &IdSet::clear)
.def("__len__", &IdSet::size)
.def("__contains__", &IdSet::get)
;
Expand Down
62 changes: 20 additions & 42 deletions lib/io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,63 +24,48 @@ class FileBuffer : public osmium::io::File

PYBIND11_MODULE(io, m)
{
py::class_<osmium::io::File>(m, "File",
"A data file.")
py::class_<osmium::io::File>(m, "File")
.def(py::init<std::string>())
.def(py::init<std::string, std::string>())
.def_property("has_multiple_object_versions",
&osmium::io::File::has_multiple_object_versions,
&osmium::io::File::set_has_multiple_object_versions,
"True if there may be more than one version of the same "
"object in the file. This happens normally only in history "
"files.")
.def("parse_format", &osmium::io::File::parse_format,
"Set the format of the file from a format string.")
&osmium::io::File::set_has_multiple_object_versions)
.def("parse_format", &osmium::io::File::parse_format)
;


py::class_<FileBuffer>(m, "FileBuffer",
"A buffer containing an OSM file.")
py::class_<FileBuffer>(m, "FileBuffer")
.def(py::init<>([] (pybind11::buffer const &buf, std::string const &format) {
pybind11::buffer_info info = buf.request();
return new FileBuffer(reinterpret_cast<const char *>(info.ptr),
static_cast<size_t>(info.size), format.c_str());
}), py::keep_alive<1, 2>())
.def_property("has_multiple_object_versions",
&osmium::io::File::has_multiple_object_versions,
&osmium::io::File::set_has_multiple_object_versions)
.def("parse_format", &osmium::io::File::parse_format)
;


py::class_<osmium::io::Header>(m, "Header",
"File header with global information about the file.")
py::class_<osmium::io::Header>(m, "Header")
.def(py::init<>())
.def_property("has_multiple_object_versions",
&osmium::io::Header::has_multiple_object_versions,
&osmium::io::Header::set_has_multiple_object_versions,
"True if there may be more than one version of the same "
"object in the file. This happens normally only in history "
"files.")
.def("box", &osmium::io::Header::box,
"Return the bounding box of the data in the file or an invalid "
"box if the information is not available.")
&osmium::io::Header::set_has_multiple_object_versions)
.def("box", &osmium::io::Header::box)
.def("get", (std::string (osmium::io::Header::*)(std::string const &, std::string const &))
&osmium::io::Header::get,
py::arg("key"), py::arg("default")="",
"Get the value of header option 'key' or default value if "
"there is no header option with that name. The default cannot be "
"None.")
py::arg("key"), py::arg("default")="")
.def("set",
(void (osmium::io::Header::*)(const std::string&, const char*))
&osmium::io::Header::set,
py::arg("key"), py::arg("value"),
"Set the value of header option 'key'.")
py::arg("key"), py::arg("value"))
.def("add_box", &osmium::io::Header::add_box,
py::arg("box"),
py::return_value_policy::reference_internal,
"Add the given bounding box to the list of bounding boxes in the "
" header.")
py::return_value_policy::reference_internal)
;

py::class_<osmium::io::Reader>(m, "Reader",
"A class that reads OSM data from a file.")
py::class_<osmium::io::Reader>(m, "Reader")
.def(py::init<std::string>())
.def(py::init<std::string, osmium::osm_entity_bits::type>())
.def(py::init<FileBuffer>(),
Expand All @@ -91,25 +76,18 @@ PYBIND11_MODULE(io, m)
py::keep_alive<1, 2>())
.def(py::init<osmium::io::File, osmium::osm_entity_bits::type>(),
py::keep_alive<1, 2>())
.def("eof", &osmium::io::Reader::eof,
"Check if the end of file has been reached.")
.def("close", &osmium::io::Reader::close,
"Close any open file handles. The reader is unusable afterwards.")
.def("header", &osmium::io::Reader::header,
"Return the header with file information, see :py:class:`osmium.io.Header`.")
.def("eof", &osmium::io::Reader::eof)
.def("close", &osmium::io::Reader::close)
.def("header", &osmium::io::Reader::header)
.def("__enter__", [](py::object const &self) { return self; })
.def("__exit__", [](osmium::io::Reader &self, py::args args) { self.close(); })
;

py::class_<osmium::io::Writer>(m, "Writer",
"Class for writing OSM data to a file. This class just encapsulates an "
"OSM file. Have a look `osmium.SimpleWriter` for a high-level interface "
"for writing out data.")
py::class_<osmium::io::Writer>(m, "Writer")
.def(py::init<std::string>())
.def(py::init<osmium::io::File>())
.def(py::init<std::string, osmium::io::Header>())
.def(py::init<osmium::io::File, osmium::io::Header>())
.def("close", &osmium::io::Writer::close,
"Close any open file handles. The writer is unusable afterwards.")
.def("close", &osmium::io::Writer::close)
;
}
Loading

0 comments on commit db1e7e9

Please sign in to comment.