Obtain assets with LoadAssets API
To refer to assets, OCM uses ResourceID
with the <ocm-layer>/<tile>/<file-name>
formatting.
Example of loading assets by using ResourceID
:
std::mutex single_callback_mutex;
auto single_resource_callback = [&](const AssetPayload& asset_payload ) mutable {
std::lock_guard<std::mutex> lock( single_callback_mutex );
std::cout << "single_resource_callback for " << asset_payload.ResourceId( ) << std::endl;
// asset content is in the asset_payload.Data()
};
std::promise< bool > overall_callback_promise;
auto overall_callback = [&overall_callback_promise]( AssetsResultResponse callback_response ) {
std::stringstream msg;
msg << "LoadAssets overall_callback" << std::endl;
msg << "\tcallback_response: " << ToString( callback_response.GetError( ) ) << std::endl;
if ( callback_response.IsSuccessful( ) )
{
msg << "\tDownloaded:" << std::endl;
const auto& downloaded = callback_response.GetResult( ).GetSuccessfulResources( );
std::for_each(
downloaded.cbegin( ), downloaded.cend( ),
[&msg]( const ResourceId& item ) { msg << "\t - " << item << std::endl; } );
msg << "\tFailed:" << std::endl;
const auto& failed = callback_response.GetResult( ).GetFailedResources( );
std::for_each( failed.cbegin( ), failed.cend( ), [&msg]( const ResourceId& item ) {
msg << "\t - " << item << std::endl;
} );
}
std::cout << msg.str( );
overall_callback_promise.set_value( true );
};
const ResourceIdDefaultParser id_parser;
const ResourcePaths resource_paths{
id_parser( "JunctionViewAssetFutureDay_3x4-2/17/JV_HORIZON_FUTURISTIC_DAY_CITYSKYLINE.png" ),
id_parser( "JunctionViewAssetFutureDay_3x4-2/18/JV_SKY_FUTURISTIC_DAY.jpg" ),
id_parser( "JunctionViewAssetFutureDay_3x4-2/22/JV_TERRAIN_FUTURISTIC_DAY_GREEN.png" ),
};
const auto kTimeOut = std::chrono::seconds( 60 );
const auto token_result
= server->LoadAssets( catalog_handle, AssetsRequest( ).WithResources( resource_paths ),
single_resource_callback, overall_callback );
auto overall_callback_future = overall_callback_promise.get_future( );
if ( token_result.IsSuccessful( ) )
{
const auto overall_callback_future_status = overall_callback_future.wait_for( kTimeOut );
if ( overall_callback_future_status == std::future_status::ready )
{
const auto result = overall_callback_future.get( );
}
}
else
{
std::cout << "LoadAssets returned error: " << ToString( token_result.GetError( ) )
<< std::endl;
}
Note: Single asset callback can be called from different threads. Please ensure that the callback is thread-safe.
Example of how to obtain junction-view ResourceID
In the Navigation
layer group, you can get the asset ResourceID
related to the tile or position from the dedicated layer. The first-level asset can contain more ResourceID
s for further assembling.
Example of how to generate the junction-view ResourceID
:
void
GetSvgzLocation( DataStoreClient& client,
CatalogHandle catalog_handle,
const TileKey& tile_key,
std::string& svgz_location )
{
const auto layer_group = clientmap::layergroup::kNavigation;
const olp::clientmap::datastore::TileRequest::Layers layers{
clientmap::navigation::kJunctionViewLayerName};
ClientMapTile client_map_tile;
auto single_tile_callback = [&client_map_tile]( const ClientMapTile& tile ) {
if (::clientmap::decoder::IsValid( tile ) )
{
client_map_tile = tile;
}
};
// Obtain tile where real junction is
const auto error = client.Load( catalog_handle,
olp::clientmap::datastore::TileRequest( )
.WithTileKey( tile_key )
.WithLayerGroup( std::string( layer_group ) )
.WithLayers( layers )
.WithFetchOption( FetchOptions::OnlineIfNotFound ),
std::move( single_tile_callback ) );
// Got junction view mapped from .proto file
const auto segment_offsets_size = client_map_tile.junction_view_layer->segment_offsets_size( );
for ( uint32_t index = 0; index < segment_offsets_size; ++index )
{
const auto& segment_data = client_map_tile.junction_view_layer->segment_offsets( index );
const auto& map_junction_view_destinations = segment_data.destinations( );
for ( const auto& map_destination : map_junction_view_destinations )
{
if ( map_destination.has_junction_view_file( ) )
{
const auto host_tile_id = map_destination.junction_view_file( ).host_tile_id( );
const std::string& file_name = map_destination.junction_view_file( ).file_name( );
svgz_location = BuildResourceId(
kJunctionViewFileLayerName, kJunctionViewTheme, kJunctionViewAspectRatio,
TileKey::FromQuadKey64( host_tile_id ), file_name );
// There could be several junction views in the tile, just taking the 1st one.
return;
}
}
}
}