Free Support Forum -

Modifying code sample for Render xyz tiles by extent


I am looking at the code sample on the requesting of XYZ webtiles using an extent and rendering of a map:

var mapPath = Path.Combine(RunExamples.GetDataDir(), "out_osm_tiles.png");

// we use the osm tile server
string url = "{z}/{x}/{y}.png";
using (var layer = Drivers.XyzTiles.OpenLayer(new XyzConnection(url)))
    // print tiles info
    var extent = new Extent(-90, -40, 90, 40) {SpatialReferenceSystem = SpatialReferenceSystem.Wgs84};

var tiles = layer.GetTiles(2, extent).ToList();

// render tiles
var resampling = new RasterMapResampling() { Height = 800, Width = 800 };
using (var map = new Map(800, 800))
    foreach (var tile in tiles)
        var raster = tile.AsRaster();
        map.Add(new RasterMapLayer(raster) { Resampling = resampling });
    map.Render(mapPath, Renderers.Png);
Console.WriteLine($"Rendered Map: {mapPath}");

Similarly, I should be able to open/create a geometry, or read from WKT or shapefile and add to a layer, then refer to the geometry of the layer by using layer.GetExtent method.

However, if I change the above sample by using
var tiles = layer.GetTiles(2, mylayer.GetExtent).ToList();
and then add both the rastermaplayer and vectorlayer in rendering the map, the resulting image does not align the way I assumed the two layers would align.

Am I missing something simple? I would have thought I could overlay the vector layer on top of the rastermaplayer, and then even add icon marker images on top.


Hi. @yemoku

Please, check the value of mylayer.GetExtent.SpatialReferenceSystem. Its value can be null if the SpatialReferenceSystem is unknown. You need to specify this manually.

Otherwise mylayer.GetExtent.SpatialReferenceSystem is different from Wgs84. In this case, we have an error that needs to be automatically converted to Wgs84, but this conversion was not done. As workaround you maybe need to perform the GetTransformed method for mylayer.GetExten.


Best Regrads

Thanks for the pointer. The geometry sources I am using are all optimized Topojson format which doesnt support the SpatialReferenceSystem property.

I fixed the issue by doing an in memory conversion of the layer to Geojson and then creating the required SpatialReference on the new layer.

Note that the SpatialReferenceSystem property is readonly hence the “hack” to create an entirely new layer.

Yes, this then worked for me.

Many thanks