Skip to content

Commit c59a2a8

Browse files
committed
Add get_node_location() function to middle
Before we had only a function to retrieve a list of locations from a list of nodes which is inconvenient if you only need a single location. The new function is needed in the context of retrieving locations for member nodes of relations. There are probably ways to improve on this: * We are using the get_node_list prepared statement to get the single id when a simpler prepared statement would do. But then we'd need another prepared statement. * When using this it might be possible to first assemble a list of several ids and get them all in one go. But its more difficult to write code like this and there aren't that many relations around that have lots of node members, so we'll leave it at that and optimize this later if needed.
1 parent 69f4849 commit c59a2a8

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

src/middle-pgsql.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,38 @@ std::size_t middle_query_pgsql_t::get_way_node_locations_flatnodes(
345345
return count;
346346
}
347347

348+
osmium::Location middle_query_pgsql_t::get_node_location_db(osmid_t id) const
349+
{
350+
auto const res =
351+
m_sql_conn.exec_prepared("get_node_list", "{{{}}}"_format(id));
352+
if (res.num_tuples() == 0) {
353+
return osmium::Location{};
354+
}
355+
356+
return osmium::Location{(int)strtol(res.get_value(0, 1), nullptr, 10),
357+
(int)strtol(res.get_value(0, 2), nullptr, 10)};
358+
}
359+
360+
osmium::Location
361+
middle_query_pgsql_t::get_node_location_flatnodes(osmid_t id) const
362+
{
363+
if (id >= 0) {
364+
return m_persistent_cache->get(id);
365+
}
366+
return osmium::Location{};
367+
}
368+
369+
osmium::Location middle_query_pgsql_t::get_node_location(osmid_t id) const
370+
{
371+
auto const loc = m_cache->get(id);
372+
if (loc.valid()) {
373+
return loc;
374+
}
375+
376+
return m_persistent_cache ? get_node_location_flatnodes(id)
377+
: get_node_location_db(id);
378+
}
379+
348380
size_t middle_query_pgsql_t::nodes_get_list(osmium::WayNodeList *nodes) const
349381
{
350382
return m_persistent_cache ? get_way_node_locations_flatnodes(nodes)

src/middle-pgsql.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class middle_query_pgsql_t : public middle_query_t
3737
std::string const &conninfo, std::shared_ptr<node_locations_t> cache,
3838
std::shared_ptr<node_persistent_cache> persistent_cache);
3939

40+
osmium::Location get_node_location(osmid_t id) const override;
41+
4042
size_t nodes_get_list(osmium::WayNodeList *nodes) const override;
4143

4244
bool way_get(osmid_t id, osmium::memory::Buffer *buffer) const override;
@@ -51,6 +53,8 @@ class middle_query_pgsql_t : public middle_query_t
5153
void exec_sql(std::string const &sql_cmd) const;
5254

5355
private:
56+
osmium::Location get_node_location_flatnodes(osmid_t id) const;
57+
osmium::Location get_node_location_db(osmid_t id) const;
5458
std::size_t get_way_node_locations_flatnodes(osmium::WayNodeList *nodes) const;
5559
std::size_t get_way_node_locations_db(osmium::WayNodeList *nodes) const;
5660

src/middle-ram.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ void middle_ram_t::relation(osmium::Relation const &relation)
184184
}
185185
}
186186

187+
osmium::Location middle_ram_t::get_node_location(osmid_t id) const
188+
{
189+
return m_node_locations.get(id);
190+
}
191+
187192
std::size_t middle_ram_t::nodes_get_list(osmium::WayNodeList *nodes) const
188193
{
189194
assert(nodes);

src/middle-ram.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class middle_ram_t : public middle_t, public middle_query_t
5454
void way(osmium::Way const &way) override;
5555
void relation(osmium::Relation const &) override;
5656

57+
osmium::Location get_node_location(osmid_t id) const override;
58+
5759
std::size_t nodes_get_list(osmium::WayNodeList *nodes) const override;
5860

5961
bool way_get(osmid_t id, osmium::memory::Buffer *buffer) const override;

src/middle.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ struct middle_query_t : std::enable_shared_from_this<middle_query_t>
2828
{
2929
virtual ~middle_query_t() = 0;
3030

31+
/**
32+
* Retrieves node location for the given id.
33+
*/
34+
virtual osmium::Location get_node_location(osmid_t id) const = 0;
35+
3136
/**
3237
* Retrieves node locations for the given node list.
3338
*

tests/test-middle.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ static testing::pg::tempdb_t db;
2828

2929
namespace {
3030

31-
void expect_location(osmium::Location loc, osmium::Node const &expected)
31+
void check_locations_are_equal(osmium::Location a, osmium::Location b)
3232
{
33-
CHECK(loc.lat() == Approx(expected.location().lat()));
34-
CHECK(loc.lon() == Approx(expected.location().lon()));
33+
CHECK(a.lat() == Approx(b.lat()));
34+
CHECK(a.lon() == Approx(b.lon()));
3535
}
3636

3737
} // namespace
@@ -136,11 +136,18 @@ TEMPLATE_TEST_CASE("middle import", "", options_slim_default,
136136

137137
// get it back
138138
REQUIRE(mid_q->nodes_get_list(&nodes) == nodes.size());
139-
expect_location(nodes[0].location(), node);
139+
check_locations_are_equal(nodes[0].location(), node.location());
140140

141141
// other nodes are not retrievable
142142
auto &n2 = buffer.add_way("w3 Nn1,n2,n1235").nodes();
143143
REQUIRE(mid_q->nodes_get_list(&n2) == 0);
144+
145+
// check the same thing again using get_node_location() function
146+
check_locations_are_equal(mid_q->get_node_location(1234),
147+
node.location());
148+
CHECK_FALSE(mid_q->get_node_location(1).valid());
149+
CHECK_FALSE(mid_q->get_node_location(2).valid());
150+
CHECK_FALSE(mid_q->get_node_location(1235).valid());
144151
}
145152

146153
SECTION("Set and retrieve a single way")

0 commit comments

Comments
 (0)