Skip to content

Instantly share code, notes, and snippets.

@rbrundritt
Created October 28, 2022 17:10
Show Gist options
  • Save rbrundritt/9ddc0005dfbb16935abaebf6bf337fbd to your computer and use it in GitHub Desktop.
Save rbrundritt/9ddc0005dfbb16935abaebf6bf337fbd to your computer and use it in GitHub Desktop.
A simple code example that uses the Azure Maps .NET client library to retrieve a vector tile, parse it using the NetTopologySuite.IO.VectorTile libraries, and then extract the unique road names. Uses the https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/maps and https://github.com/NetTopologySuite/NetTopologySuite.IO.VectorTiles libraries.
/*
* A simple code example that uses the Azure Maps .NET client library to retrieve a vector tile,
* parse it using the NetTopologySuite.IO.VectorTile libraries, and then extract the unique road names.
*/
using Azure;
using NetTopologySuite.IO.VectorTiles.Mapbox;
//Input lat/lon, and zoom.
double latitude = 47.6;
double longitude = -122.33;
int zoom = 16;
//A list of all the layers within the Azure Maps vector tiles that contain road information.
var roadLayers = new List<string>
{
"Motorway",
"International road",
"Major road",
"Secondary road",
"Connecting road",
"Major local road",
"Local road",
"Minor local road",
"Toll motorway",
"Toll international road",
"Toll major road",
"Toll secondary road",
"Toll connecting road",
"Toll major local road",
"Toll local road",
"Toll minor local road",
"Toll motorway tunnel",
"Toll international road tunnel",
"Toll major road tunnel",
"Toll secondary road tunnel",
"Toll connecting road tunnel",
"Toll major local road tunnel",
"Toll local road tunnel",
"Toll minor local road tunnel",
"Motorway tunnel",
"International road tunnel",
"Major road tunnel",
"Secondary road tunnel",
"Connecting road tunnel",
"Major local road tunnel",
"Local road tunnel",
"Minor local road tunnel",
//"Ferry road",
"Pedestrian road",
"Non public road",
"Walkway road",
"Parking road"
};
//Define which tile you want to read.
var tileDefinition = NetTopologySuite.IO.VectorTiles.Tiles.Tile.CreateAroundLocation(latitude, longitude, zoom);
//Create a client to connect to the Azure Maps rendering service.
var creds = new AzureKeyCredential("<Your Azure Maps Key>");
var client = new Azure.Maps.Rendering.MapsRenderingClient(creds);
//Create a tile index for Azure Maps using the tile definition.
var tileIdx = new Azure.Maps.Rendering.MapTileIndex(tileDefinition.X, tileDefinition.Y, tileDefinition.Zoom);
//Retrieve the vector tile from Azure Maps.
var tileResponse = await client.GetMapTileAsync(new Azure.Maps.Rendering.GetMapTileOptions(Azure.Maps.Rendering.MapTileSetId.MicrosoftBase, tileIdx));
//Create a MapboxTileReader to parse the tile stream response.
var reader = new MapboxTileReader();
//Read the vector tile.
var vt = reader.Read(tileResponse.Value, tileDefinition);
//Capture the unique road names in a list.
var roadNames = new List<string>();
//Loop through each layer in the vector tile.
foreach (var l in vt.Layers)
{
//Check to see if the layer is a road layer.
if (roadLayers.Contains(l.Name)) {
//Access the features of the layer and do something with them.
var features = l.Features;
foreach (var f in features)
{
//Get the "name" attribute of the feature, if it has one.
var name = f.Attributes.Exists("name") ? f.Attributes["name"].ToString() : null;
//If there is a unique name, capture it and write it.
if (name != null && !roadNames.Contains(name)) {
roadNames.Add(name);
Console.WriteLine(name);
}
}
}
}
Console.ReadLine();
@rbrundritt
Copy link
Author

rbrundritt commented Oct 28, 2022

Information on the vector tiles layers and feature properties/attributes of the different types of Azure Maps vector tiles can be found here (TomTom is the data provider):

@rbrundritt
Copy link
Author

Here is a useful tool for inspecting the base road vector tiles in Azure Maps to get an idea of what's available: https://samples.azuremaps.com/?search=inspect&sample=inspect-features-under-the-mouse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment