In this section, you can learn how to work with the OCM custom decoder example.
To decode a tile layer with your custom decoder, you need to create a decoder instance and layer data.
-
Create a DefaultSchemaDecoder
instance and configure it to use the custom decoder for the specific layer.
Note: The layers for which custom decoders are not set are decoded using the protopack
library by default.
auto decoder
= std::make_shared< clientmap::decoder::DefaultSchemaDecoder >( );
decoder->SetDecoder( clientmap::rendering::kRoadAttributeLayerName,
RoadAttributeLayerDecoder( ) );
-
To access data from the HERE platform, create a Network
instance.
#include <olp/core/client/OlpClientSettingsFactory.h>
...
auto network = olp::client::OlpClientSettingsFactory::
CreateDefaultNetworkRequestHandler( );
-
To handle requests asynchronously, create a TaskScheduler
instance and specify the number of threads that the SDK should use.
Note: If you do not define the TaskScheduler
explicitly or set the number of threads to 1, HERE Data SDK for C++ will handle all requests synchronously.
#include <olp/core/client/OlpClientSettingsFactory.h>
...
const std::shared_ptr< olp::thread::TaskScheduler > task_scheduler
= olp::client::OlpClientSettingsFactory::CreateDefaultTaskScheduler( 4u );
-
Create a DataStoreServer
instance that uses the DefaultSchemaDecoder
, Network
, and TaskScheduler
instances.
Note: If you do not set schema_decoder
or set it to nullptr
, the default clientmap::decoder::DefaultSchemaDecoder
instance will be used.
olp::clientmap::datastore::DataStoreServerSettings server_settings;
server_settings.network_request_handler = network;
server_settings.task_scheduler = task_scheduler;
server_settings.schema_decoder = decoder;
const auto server
= std::make_shared< olp::clientmap::datastore::DataStoreServer >( server_settings );
-
Initialize the DataStoreServer
instance and cache.
server->Init();
If the operation is successful, you get the kNone
error.
-
Create a DataStoreClient
instance using a shared pointer to the DataStoreServer
instance.
olp::clientmap::datastore::DataStoreClient client( server, {} );
-
To access data from the OCM catalog, create an AuthenticationCredentials
instance using your platform credentials.
For instructions on how to get platform credentials, see the Get credentials section in the Getting Started Guide.
boost::optional< olp::authentication::AuthenticationCredentials > credentials
= olp::authentication::AuthenticationCredentials::ReadFromFile(
"path-to-credentials-file" );
-
Сreate an AuthenticationSettings
instance using the AuthenticationCredentials
, Network
, and TaskScheduler
instances.
olp::authentication::Settings settings( credentials );
settings.network_request_handler = network;
settings.task_scheduler = task_scheduler;
olp::client::AuthenticationSettings authentication_settings;
authentication_settings.provider
= olp::authentication::TokenProviderDefault( std::move( settings ) );
-
Create a CatalogSettings
instance using the AuthenticationSettings
instance.
olp::clientmap::datastore::CatalogSettings catalog_settings;
catalog_settings.authentication_settings = authentication_settings;
-
Configure DataStoreClient
to get data from a specific version of the OCM catalog.
If you do not set the version explicitly or set it to boost::none
, the catalog version is set internally to the latest version that is available on the HERE platform when the catalog is added.
Note: You cannot add two versions of the same catalog to the same DataStoreClient
instance. To get data from different versions of the same catalog, create a DataStoreClient
instance for each catalog version and add different versions of the catalog to different DataStoreClient
instances.
catalog_settings.version = "version_number";
-
Add a catalog to the DataStoreServer
instance using the HERE Resource Name (HRN) of the catalog and the CatalogSettings
instance.
const auto add_catalog_server =
server->AddCatalog( kCatalogHrn, catalog_settings );
-
Check if the AddCatalog
operation is successful.
if ( !add_catalog_server.IsSuccessful( ) )
{
OLP_SDK_LOG_WARNING_F( kLogTag, "Failed to add a catalog to server - error=%s",
ToString( add_catalog_server.GetError( ) ).c_str( ) );
return EXIT_SUCCESS;
}
The operation can be unsuccessful if one of the following is true:
- The catalog with the requested HRN does not exist.
-
DataStoreClient
fails to check if the catalog with the requested HRN exists. Catalog existence is checked only if the catalog version is not set or set to boost::none
.
-
Add the same catalog to the DataStoreClient
instance using the HRN of the catalog and the CatalogSettings
instance.
const auto add_catalog_result =
client.AddCatalog( kCatalogHrn, catalog_settings );
-
Check if the AddCatalog
operation is successful.
if ( !add_catalog_client.IsSuccessful( ) )
{
OLP_SDK_LOG_WARNING_F( kLogTag, "Failed to add a catalog to client - error=%s",
ToString( add_catalog_client.GetError( ) ).c_str( ) );
return EXIT_SUCCESS;
}
The operation can be unsuccessful if one of the following is true:
- The catalog with the requested HRN does not exist.
-
DataStoreClient
fails to check if the catalog with the requested HRN exists. Catalog existence is checked only if the catalog version is not set or set to boost::none
.
-
If the AddCatalog
operation is successful, to access data from the added catalog, save its CatalogHandle
.
const auto catalog_handle = add_catalog_result.GetResult( );
-
Request the needed layer of a tile.
Note: The RoadArrtibute
layer is used as an example.
clientmap::decoder::ClientMapTile tile;
auto callback = [&]( const olp::clientmap::datastore::ClientMapTiles& catalog_tiles ) {
tile = catalog_tiles[ catalog_handle ];
};
const auto error = client.Load( olp::clientmap::datastore::TileRequest( )
.WithTileKey( kTileKey )
.WithLayerGroup( clientmap::layergroup::kRendering )
.WithLayer( clientmap::rendering::kRoadAttributeLayerName ),
std::move( callback ) );
if ( error != olp::clientmap::datastore::Error::kNone )
{
OLP_SDK_LOG_WARNING_F( kLogTag, "Failed to load the tile %s - error=%s",
kTileKey.ToHereTile( ).c_str( ), ToString( error ).c_str( ) );
return EXIT_SUCCESS;
}
-
Check if the requested layer is present in the tile.
if ( !tile.road_attribute_layer )
{
OLP_SDK_LOG_WARNING_F( kLogTag, "The RoadAttribute layer is not present in the tile %s",
kTileKey.ToHereTile( ).c_str( ) );
return EXIT_SUCCESS;
}
if ( tile.road_attribute_layer->roads_size( ) > 0
&& tile.road_attribute_layer->roads( 0 ).attributes_size( ) > 0 )
{
const auto physical = tile.road_attribute_layer->roads( 0 ).attributes( 0 ).physical( );
OLP_SDK_LOG_INFO_F( kLogTag,
"The physical characteristics for attribute 0 of segment 0 is %" PRIu32,
physical );
}
return EXIT_SUCCESS;