Skip to content

Commit acdf93a

Browse files
Elevation tile on demand fetching (valhalla#3391)
* Ondemand elevation fetching added * On demand elevation tile fetching * Changes after review v1 * Changes after review v2. Some more tests added * Changes after review v3 * Parallel call removed from sample.cc. get_files removed from sample.cc. Tests added * Public methods for setting endpoints removed * New tests addded * Changes after review v4 * Additional methods * Build method changed * filesystem.h is changed * New tests added * Method make_single_url moved from class tile_getter_t and become class helper funciton. * flag for storing archived elevation tiles removed * Unnecessary comments removed. * Local host returned to any in tile_server * * get_hgt_file_name become util function * get_no_data_value become util function * cache_ become smart pointer * make_single_point_url moved from tilegetter to curl_tilegetter * Sample code thread synchronisation changed * Synchronizaton changed in source method * Test with hash check added * Fixed: Synchronization issue Added: addtional tests to elevation_builder * Added: openssl lib dependencies * Change: file comparison algorithm in elevation_builder.cc * Comments removed * Change: boost -> cxxopt in valhalla_add_elevation.cc * Added: additional checks * Code corrected after review v5 * Code correction after review v6 * Lock added to get method in sample * Update CHANGELOG.md * Update scripts/valhalla_build_config Co-authored-by: Pavel Merkis <[email protected]> * Update src/mjolnir/elevationbuilder.cc Co-authored-by: Pavel Merkis <[email protected]> * Update src/mjolnir/elevationbuilder.cc Co-authored-by: Pavel Merkis <[email protected]> * Update src/skadi/sample.cc Co-authored-by: Pavel Merkis <[email protected]> * Code correction after review v7 * Returned to const ref Co-authored-by: Pavel Merkis <[email protected]>
1 parent 72e986c commit acdf93a

24 files changed

+1453
-343
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ valhalla_tyr_worker
3030
valhalla_associate_segments
3131
valhalla_fetch_transit
3232
valhalla_pack_elevation
33+
valhalla_add_elevation
3334
genfiles/
3435
locales/*.UTF-8
3536
!locales/merge-en.sh

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* ADDED: New options to control what fields of the pbf are returned when pbf format responses are requested [#3207](https://github.com/valhalla/valhalla/pull/3507)
6363
* CHANGED: Rename tripcommon to common [#3516](https://github.com/valhalla/valhalla/pull/3516)
6464
* ADDED: indoor routing - data model, data processing. [#3509](https://github.com/valhalla/valhalla/pull/3509)
65+
* ADDED: On-demand elevation tile fetching [#3391](https://github.com/valhalla/valhalla/pull/3391)
6566

6667
## Release Date: 2021-10-07 Valhalla 3.1.4
6768
* **Removed**

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ set(valhalla_programs valhalla_run_map_match valhalla_benchmark_loki valhalla_be
297297
set(valhalla_data_tools valhalla_build_statistics valhalla_ways_to_edges valhalla_validate_transit
298298
valhalla_benchmark_admins valhalla_build_connectivity valhalla_build_tiles valhalla_build_admins
299299
valhalla_convert_transit valhalla_fetch_transit valhalla_query_transit valhalla_add_predicted_traffic
300-
valhalla_assign_speeds)
300+
valhalla_assign_speeds valhalla_add_elevation)
301301

302302
## Valhalla services
303303
set(valhalla_services valhalla_loki_worker valhalla_odin_worker valhalla_thor_worker)

scripts/valhalla_build_config

+4-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ config = {
6262
}
6363
},
6464
'additional_data': {
65-
'elevation': '/data/valhalla/elevation/'
65+
'elevation': '/data/valhalla/elevation/',
66+
'elevation_url': Optional(str)
6667
},
6768
'loki': {
6869
'actions':['locate','route','height','sources_to_targets','optimized_route','isochrone','trace_route','trace_attributes','transit_available', 'expansion', 'centroid', 'status'],
@@ -331,7 +332,8 @@ help_text = {
331332
}
332333
},
333334
'additional_data': {
334-
'elevation': 'Location of srtmgl1 elevation tiles for using in valhalla_build_tiles'
335+
'elevation': 'Location of elevation tiles',
336+
'elevation_url': 'Http location to read elevations from. this address is used if elevation tiles were not found in the elevation directory. Ex.: http://<your_valhalla_tile_server_host>:<your_valhalla_tile_server_port>/some/Optional/path/{tilePath}?some=Optional&query=params. Valhalla will look for the {tilePath} portion of the url and fill this out with an elevation path when it makes a request for that particular elevation'
335337
},
336338
'loki': {
337339
'actions': 'Comma separated list of allowable actions for the service, one or more of: locate, route, height, optimized_route, isochrone, trace_route, trace_attributes, transit_available, expansion, centroid, status',

src/baldr/graphreader.cc

-2
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,6 @@ class TarballGraphMemory final : public GraphMemory {
549549
// Get a pointer to a graph tile object given a GraphId. Return nullptr
550550
// if the tile is not found/empty
551551
graph_tile_ptr GraphReader::GetGraphTile(const GraphId& graphid) {
552-
// TODO: clear the cache automatically once we become overcommitted by a certain amount
553-
554552
// Return nullptr if not a valid tile
555553
if (!graphid.Is_Valid()) {
556554
return nullptr;

src/baldr/graphtile.cc

+35-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "baldr/graphtile.h"
2+
23
#include "baldr/compression_utils.h"
4+
#include "baldr/curl_tilegetter.h"
35
#include "baldr/datetime.h"
46
#include "baldr/sign.h"
5-
#include "baldr/tilegetter.h"
67
#include "baldr/tilehierarchy.h"
78
#include "filesystem.h"
89
#include "midgard/aabb2.h"
@@ -30,14 +31,6 @@ namespace {
3031
const AABB2<PointLL> world_box(PointLL(-180, -90), PointLL(180, 90));
3132
constexpr float COMPRESSION_HINT = 3.5f;
3233

33-
std::string MakeSingleTileUrl(const std::string& tile_url, const valhalla::baldr::GraphId& graphid) {
34-
auto id_pos = tile_url.find(valhalla::baldr::GraphTile::kTilePathPattern);
35-
return tile_url.substr(0, id_pos) +
36-
valhalla::baldr::GraphTile::FileSuffix(graphid.Tile_Base(),
37-
valhalla::baldr::SUFFIX_NON_COMPRESSED, false) +
38-
tile_url.substr(id_pos + std::strlen(valhalla::baldr::GraphTile::kTilePathPattern));
39-
}
40-
4134
// the point of this function is to avoid race conditions for writing a tile between threads
4235
// so the easiest thing to do is just use the thread id to differentiate
4336
std::string GenerateTmpSuffix() {
@@ -104,9 +97,18 @@ graph_tile_ptr GraphTile::DecompressTile(const GraphId& graphid,
10497
graph_tile_ptr GraphTile::Create(const std::string& tile_dir,
10598
const GraphId& graphid,
10699
std::unique_ptr<const GraphMemory>&& traffic_memory) {
100+
if (!graphid.Is_Valid()) {
101+
LOG_ERROR("Failed to build GraphTile. Error: GraphId is invalid");
102+
return nullptr;
103+
}
107104

108-
// Don't bother with invalid ids
109-
if (!graphid.Is_Valid() || graphid.level() > TileHierarchy::get_max_level() || tile_dir.empty()) {
105+
if (graphid.level() > TileHierarchy::get_max_level()) {
106+
LOG_ERROR("Failed to build GraphTile. Error: GraphId level exceeds tile hierarchy max level");
107+
return nullptr;
108+
}
109+
110+
if (tile_dir.empty()) {
111+
LOG_ERROR("Failed to build GraphTile. Error: Tile dir is empty");
110112
return nullptr;
111113
}
112114

@@ -205,28 +207,38 @@ void GraphTile::SaveTileToFile(const std::vector<char>& tile_data, const std::st
205207
filesystem::remove(tmp_location);
206208
}
207209

210+
void store(const std::string& cache_location,
211+
const GraphId& graphid,
212+
const tile_getter_t* tile_getter,
213+
const std::vector<char>& raw_data) {
214+
if (!cache_location.empty()) {
215+
auto suffix =
216+
valhalla::baldr::GraphTile::FileSuffix(graphid.Tile_Base(),
217+
(tile_getter->gzipped()
218+
? valhalla::baldr::SUFFIX_COMPRESSED
219+
: valhalla::baldr::SUFFIX_NON_COMPRESSED));
220+
auto disk_location = cache_location + filesystem::path::preferred_separator + suffix;
221+
filesystem::save(disk_location, raw_data);
222+
}
223+
}
224+
208225
graph_tile_ptr GraphTile::CacheTileURL(const std::string& tile_url,
209226
const GraphId& graphid,
210227
tile_getter_t* tile_getter,
211228
const std::string& cache_location) {
212229
// Don't bother with invalid ids
213-
if (!graphid.Is_Valid() || graphid.level() > TileHierarchy::get_max_level()) {
230+
if (!graphid.Is_Valid() || graphid.level() > TileHierarchy::get_max_level() || !tile_getter) {
214231
return nullptr;
215232
}
216233

217-
auto uri = MakeSingleTileUrl(tile_url, graphid);
218-
auto result = tile_getter->get(uri);
234+
auto fname = valhalla::baldr::GraphTile::FileSuffix(graphid.Tile_Base(),
235+
valhalla::baldr::SUFFIX_NON_COMPRESSED, false);
236+
auto result = tile_getter->get(baldr::make_single_point_url(tile_url, fname));
219237
if (result.status_ != tile_getter_t::status_code_t::SUCCESS) {
220238
return nullptr;
221239
}
222240
// try to cache it on disk so we dont have to keep fetching it from url
223-
if (!cache_location.empty()) {
224-
auto suffix = FileSuffix(graphid.Tile_Base(),
225-
(tile_getter->gzipped() ? valhalla::baldr::SUFFIX_COMPRESSED
226-
: valhalla::baldr::SUFFIX_NON_COMPRESSED));
227-
auto disk_location = cache_location + filesystem::path::preferred_separator + suffix;
228-
SaveTileToFile(result.bytes_, disk_location);
229-
}
241+
store(cache_location, graphid, tile_getter, result.bytes_);
230242

231243
// turn the memory into a tile
232244
if (tile_getter->gzipped()) {
@@ -248,9 +260,10 @@ void GraphTile::Initialize(const GraphId& graphid) {
248260
char* const tile_ptr = memory_->data;
249261
const size_t tile_size = memory_->size;
250262

251-
if (tile_size < sizeof(GraphTileHeader))
263+
if (tile_size < sizeof(GraphTileHeader)) {
252264
throw std::runtime_error("Invalid tile data size = " + std::to_string(tile_size) +
253265
". Tile file might me corrupted");
266+
}
254267

255268
char* ptr = tile_ptr;
256269
header_ = reinterpret_cast<GraphTileHeader*>(ptr);

0 commit comments

Comments
 (0)