ThinkGeo.com    |     Documentation    |     Premium Support

401 unauthorized error WMTS layer

I want to use a WMTS service in my application. But I got 401 unauthorized error. I pass the key value by url. What’s wrong with my code?

private void MapViewMainLoaded(object sender, RoutedEventArgs e)
{

	MapViewMain.MapUnit = GeographyUnit.DecimalDegree;
	MapViewMain.CurrentExtent = new RectangleShape(0, 100, 100, 0);

	var wmtsLayer = new WmtsLayer(new List<Uri>
	{
		new Uri("https://api.os.uk/maps/raster/v1/wmts?key=ZsSPHhEhQUgspZCTyYmvGVqPGAtMg3DN")
	});

	var layerOverlay = new LayerOverlay();
	layerOverlay.Layers.Add(wmtsLayer);
	MapViewMain.Overlays.Clear();
	MapViewMain.Overlays.Add("layerOverlay", layerOverlay);

	MapViewMain.Refresh();
}

Thanks Ghaderiyan,
I looked into it. I think the reason for this error is the key is not correct. You can try with

https://api.os.uk/maps/raster/v1/wmts?key=ZsSPHhEhQUgspZCTyYmvGVqPGAtMg3DN?Service=WMTS&Request=GetCapabilities

You will get an error.

Thanks

Frank

Thanks Frank
please check this url. Your url contains two question marks.

https://api.os.uk/maps/raster/v1/wmts?key=ZsSPHhEhQUgspZCTyYmvGVqPGAtMg3DN&Service=WMTS&Request=GetCapabilities

Thanks Ghaderiyan,
Yes. The url come not correct when the mapsuite request the tile/xml from the WMTS server. I have fixed it by overwrite the url.

By looking into the capabilities xml file. I found the wmts server only provider 14 zoom level layers. So I have to add 7 offset to get the correct tiles. I created a demo project for you. Also I restrict the map zoom level from 7 to 17. The ones after 17 will get some error from the WMTS server. it says “Premium Plan is required to access Premium Data”. You can try with
https://api.os.uk/maps/raster/v1/wmts?service=WMTS&request=GetTile&version=1.0.0&layer=Road_3857&style=default&format=image/png&TileMatrixSet=EPSG:3857&TileMatrix=17&TileRow=43586&TileCol=65494&key=ZsSPHhEhQUgspZCTyYmvGVqPGAtMg3DN

WpfAppDemo.zip (4.4 KB)

Here is what it looks like.

Thanks

Frank

Thanks Frank
I added the map to the software. But the map is not displayed correctly. What is the problem?


Thanks

Thanks Ghaderiyan,
If you set the break point. You will see more detail about the WMTS server capabilities

.

You can find the wmts provide 7 layers. I picked up Road_3857 as the demo project. It only provide 14 zoom level. and start from level 7. That is why I limit the zoom level with the following code.

mapView.ZoomLevelSet = new ZoomLevelSet();
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel07);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel08);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel09);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel10);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel11);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel12);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel13);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel14);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel15);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel16);
mapView.ZoomLevelSet.CustomZoomLevels.Add(mapView.ZoomLevelSet.ZoomLevel17);

But look at your screenshot. seems it is a different case. At your zoom level you should get something with my demo project.

Could you send me the code segment you are using? We could look into more detail.

Thanks

Frank

I used this code to implement.This code uses ThinkGeo version 10.5.0. Is there a problem with the code?

private Overlay GetOsDataHubMapOverlay()
{
    var wmtsLayer = new WmtsLayer();
    wmtsLayer.SendingWebRequest += WmtsLayer_SendingWebRequest;

    wmtsLayer.DrawingExceptionMode = DrawingExceptionMode.DrawException;
    wmtsLayer.WmtsSeverEncodingType = WmtsSeverEncodingType.Kvp;
    wmtsLayer.ServerUris.Add(new Uri("https://api.os.uk/maps/raster/v1/wmts"));
    wmtsLayer.Open();
    wmtsLayer.ActiveLayerName = "Road_3857";
    wmtsLayer.ActiveStyleName = "default";
    wmtsLayer.TileMatrixSetName = "EPSG:3857"; // 3857
    wmtsLayer.OutputFormat = "image/png";

    var layerOverlay = new LayerOverlay();
    layerOverlay.Layers.Add(wmtsLayer);

    if (string.IsNullOrEmpty(Settings.WebProxy)) return new LayerOverlay(new BindingList<Layer> { wmtsLayer });

    wmtsLayer.WebProxy = CreateWebProxy() != null ? CreateWebProxy() : wmtsLayer.WebProxy;
    return layerOverlay;
}

private void WmtsLayer_SendingWebRequest(object sender, SendingWebRequestEventArgs e)
{
    string uri = e.WebRequest.RequestUri.AbsoluteUri;

    if (uri == null) return;

    if (uri.Contains("?"))
    {
        uri = uri + $"&key=ZsSPHhEhQUgspZCTyYmvGVqPGAtMg3DN";
    }
    else
    {
        uri = uri + $"?key=ZsSPHhEhQUgspZCTyYmvGVqPGAtMg3DN";
    }
    // set 7 offset for the zoom level.
    var zIndex = HttpUtility.ParseQueryString(uri).Get("TileMatrix");
    if (zIndex != null)
    {
        try
        {
            var newZindex = int.Parse(zIndex.Replace("EPSG:3857:", "")) + 7;
            uri = uri.Replace($"&TileMatrix={zIndex}&", $"&TileMatrix=EPSG:3857:{newZindex}&");
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    }

    e.WebRequest = HttpWebRequest.Create(uri);
}

Thanks Ghaderiyan,
Could you please try with version 12. You can find the nuget package here.
https://www.nuget.org/packages/ThinkGeo.UI.Wpf/12.2.17

Thanks

Frank

It worked on version 12. Thanks Frank4

Thanks,
Go ahead let us know if you have any more question.

Thanks

Frank