Skip to content

7 Fill the tiles with our data

Oliver Heilig edited this page Apr 21, 2024 · 11 revisions

Now we can build an sql string from our tile key to query all gemetries which are within the tile.

// calc rect from tile key
var qw = TransformTools.TileToWgs(x, y, z);

// build the sql
var query = FormattableString.Invariant($@"
    SELECT Id, AsBinary(Geometry) FROM WorldGeom 
        WHERE ROWID IN 
            (Select rowid FROM cache_WorldGeom_Geometry 
                WHERE mbr = FilterMbrIntersects({qw.Left}, {qw.Bottom}, {qw.Right}, {qw.Top}))
    ");

using (var command = new SQLiteCommand(query, Global.cn))
using (var reader = await command.ExecuteReaderAsync())
{
    while (await reader.ReadAsync())
    {
        int id = reader.GetInt32(0);
        byte[] wkb = reader[1] as byte[];
...

Next, we need to get a graphics object from our wkb that we can draw into our graphics context. The tools class

https://github.com/ptv-logistics/SpatialTutorial/blob/master/WkbTools.cs

Creates a GDI graphics object directly from the wkb. It also does the transformation on-the-fly, which makes it quite fast.

// create GDI path from wkb
var path = WkbToGdi.Parse(wkb, p => TransformTools.WgsToTile(x, y, z, p));

// fill polygon
var fill = new SolidBrush(Color.FromArgb(168, 0, 0, 255));
graphics.FillPath(fill, path);
fill.Dispose();

// draw outline
graphics.DrawPath(Pens.Black, path);

The result is blue semi-opaque world polygons: https://spatialtutorial.azurewebsites.net/06-SpatialLiteTiles.html