Web Tiles

Working with XYZ Tiles

The XYZ Tile (“Slippy Map”) is an approach for building mapping on the web. The world map is divided into parts called tiles. All tiles are stored in Web Mapping Service tile services like Openstreetmaps, Google Hybrid, Bing, OpenCycleMap, Thunderforest, etc. And Aspose.GIS C# library lets you work with the XYZ Tiles.

Render one xyz tile

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var mapPath = Path.Combine(RunExamples.GetDataDir(), "out_osm_tile.png");
// we use the osm tile server
string url = "http://tile.openstreetmap.org/{z}/{x}/{y}.png";
using (var layer = Drivers.XyzTiles.OpenLayer(new XyzConnection(url)))
{
// print tile info
var tile = layer.GetTile(2, 3, 1);
Console.WriteLine($"CellX: {tile.CellX}, CellY: {tile.CellY}" );
Console.WriteLine($"Path: {tile.AsPath()}");
// render tile
var resampling = new RasterMapResampling() { Height = 256, Width = 256 };
using (var map = new Map(800, 800))
{
var raster = tile.AsRaster();
map.Add(new RasterMapLayer(raster){Resampling = resampling});
map.Render(mapPath, Renderers.Png);
}
Console.WriteLine($"Rendered Map: {mapPath}");
}

osm xyz tile

Render xyz tiles by extent

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
var mapPath = Path.Combine(RunExamples.GetDataDir(), "out_osm_tiles.png");
// we use the osm tile server
string url = "http://tile.openstreetmap.org/{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}");
}

osm xyz tiles

Open xyz tile from folder

// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
string url = "C://tiles/{z}/{x}/{y}.png";
using (var layer = Drivers.XyzTiles.OpenLayer(new XyzConnection(url)))
{
// print tile info
var tile = layer.GetTile(0, 0, 0);
Console.WriteLine($"CellX: {tile.CellX}, CellY: {tile.CellY}");
Console.WriteLine($"Path: {tile.AsPath()}");
}