< Summary

Class:Itinero.Network.Tiles.TileStatic
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/TileStatic.cs
Covered lines:74
Uncovered lines:5
Coverable lines:79
Total lines:135
Line coverage:93.6% (74 of 79)
Covered branches:6
Total branches:8
Branch coverage:75% (6 of 8)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ValidateBox(...)50%260%
ToTile(...)100%1100%
ToLocalId(...)100%1100%
ToLocalId(...)100%1100%
ToLocalTileCoordinates(...)100%1100%
FromLocalTileCoordinates(...)100%1100%
N(...)100%1100%
WorldToTile(...)100%1100%
TileRange()83.33%688%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Tiles/TileStatic.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace Itinero.Network.Tiles;
 5
 6internal static class TileStatic
 7{
 8    private static void ValidateBox(this ((double longitude, double latitude, float? e) topLeft,
 9        (double longitude, double latitude, float? e) bottomRight) box)
 3810    {
 3811        if (box.topLeft.latitude < box.bottomRight.latitude)
 012        {
 013            throw new ArgumentOutOfRangeException($"Top is lower than bottom.");
 14        }
 3815    }
 16
 17    public static (uint x, uint y) ToTile(int zoom, uint tileId)
 193418    {
 193419        var xMax = (ulong)(1 << zoom);
 20
 193421        return ((uint)(tileId % xMax), (uint)(tileId / xMax));
 193422    }
 23
 24    public static uint ToLocalId(uint x, uint y, int zoom)
 46925    {
 46926        var xMax = 1 << (int)zoom;
 46927        return (uint)((y * xMax) + x);
 46928    }
 29
 30    public static uint ToLocalId(double longitude, double latitude, int zoom)
 3831    {
 3832        var (x, y) = WorldToTile(longitude, latitude, zoom);
 3833        return ToLocalId(x, y, zoom);
 3834    }
 35
 36    public static (int x, int y) ToLocalTileCoordinates(int zoom, uint tileId, double longitude, double latitude,
 37        int resolution)
 78838    {
 78839        var tile = ToTile(zoom, tileId);
 40
 78841        var n = Math.PI - (2.0 * Math.PI * tile.y / Math.Pow(2.0, zoom));
 78842        var left = (tile.x / Math.Pow(2.0, zoom) * 360.0) - 180.0;
 78843        var top = 180.0 / Math.PI * Math.Atan(Math.Sinh(n));
 44
 78845        n = Math.PI - (2.0 * Math.PI * (tile.y + 1) / Math.Pow(2.0, zoom));
 78846        var right = ((tile.x + 1) / Math.Pow(2.0, zoom) * 360.0) - 180.0;
 78847        var bottom = 180.0 / Math.PI * Math.Atan(Math.Sinh(n));
 48
 78849        var latStep = (top - bottom) / resolution;
 78850        var lonStep = (right - left) / resolution;
 51
 78852        return ((int)((longitude - left) / lonStep), (int)((top - latitude) / latStep));
 78853    }
 54
 55    public static void FromLocalTileCoordinates(int zoom, uint tileId, int x, int y, int resolution,
 56        out double longitude, out double latitude)
 114657    {
 114658        var tile = ToTile(zoom, tileId);
 59
 114660        var n = Math.PI - (2.0 * Math.PI * tile.y / Math.Pow(2.0, zoom));
 114661        var left = (tile.x / Math.Pow(2.0, zoom) * 360.0) - 180.0;
 114662        var top = 180.0 / Math.PI * Math.Atan(Math.Sinh(n));
 63
 114664        n = Math.PI - (2.0 * Math.PI * (tile.y + 1) / Math.Pow(2.0, zoom));
 114665        var right = ((tile.x + 1) / Math.Pow(2.0, zoom) * 360.0) - 180.0;
 114666        var bottom = 180.0 / Math.PI * Math.Atan(Math.Sinh(n));
 67
 114668        var latStep = (top - bottom) / resolution;
 114669        var lonStep = (right - left) / resolution;
 70
 114671        longitude = left + (lonStep * x);
 114672        latitude = top - (y * latStep);
 114673    }
 74
 75    private static int N(int zoom)
 52776    {
 52777        return (int)Math.Floor(Math.Pow(2, zoom)); // replace by bit shifting?
 52778    }
 79
 80    public static (uint x, uint y) WorldToTile(double longitude, double latitude, int zoom)
 48981    {
 48982        var n = N(zoom);
 48983        var rad = latitude / 180d * Math.PI;
 84
 48985        var x = (uint)((longitude + 180.0f) / 360.0f * n);
 48986        var y = (uint)(
 48987            (1.0f - (Math.Log(Math.Tan(rad) + (1.0f / Math.Cos(rad)))
 48988                / Math.PI)) / 2f * n);
 89
 48990        return (x, y);
 48991    }
 92
 93    public static IEnumerable<(uint x, uint y)> TileRange(
 94        this ((double longitude, double latitude, float? e) topLeft,
 95            (double longitude, double latitude, float? e) bottomRight) box, int zoom)
 3896    {
 3897        box.ValidateBox();
 98
 3899        var n = N(zoom);
 38100        var topLeft = WorldToTile(box.topLeft.longitude, box.topLeft.latitude, zoom);
 38101        var bottomRight = WorldToTile(box.bottomRight.longitude, box.bottomRight.latitude, zoom);
 102
 38103        var x = topLeft.x;
 38104        var y = topLeft.y;
 71105        while (true)
 71106        {
 71107            yield return (x, y);
 108
 50109            if (y == bottomRight.y)
 26110            {
 111                // move on with x.
 26112                if (x == bottomRight.x)
 17113                {
 114                    // both x and y have reached the end.
 17115                    break;
 116                }
 117
 118                // reset y.
 9119                y = topLeft.y;
 120
 121                // move on with x.
 9122                x++;
 9123                if (x == n)
 0124                {
 0125                    x = 0;
 0126                }
 127
 9128                continue;
 129            }
 130
 131            // move on with y.
 24132            y++;
 24133        }
 17134    }
 135}