< Summary

Class:Itinero.IO.Osm.Tiles.Tile
Assembly:Itinero.IO.Osm.Tiles
File(s):/home/runner/work/routing2/routing2/src/Itinero.IO.Osm.Tiles/Tile.cs
Covered lines:40
Uncovered lines:32
Coverable lines:72
Total lines:181
Line coverage:55.5% (40 of 72)
Covered branches:0
Total branches:6
Branch coverage:0% (0 of 6)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
CalculateBounds()100%1100%
get_X()100%1100%
get_Y()100%1100%
get_Zoom()100%1100%
get_Top()100%1100%
get_Bottom()100%1100%
get_Left()100%1100%
get_Right()100%1100%
UpdateToLocalId(...)100%10%
get_LocalId()100%1100%
FromLocalId(...)100%1100%
MaxLocalId(...)100%10%
ToLocalCoordinates(...)100%10%
FromLocalCoordinates(...)100%10%
WorldToTile(...)100%1100%
IsInside(...)0%60%
ToString()100%10%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.IO.Osm.Tiles/Tile.cs

#LineLine coverage
 1using System;
 2
 3namespace Itinero.IO.Osm.Tiles;
 4
 5internal class Tile
 6{
 27    public Tile(uint x, uint y, int zoom)
 28    {
 29        this.X = x;
 210        this.Y = y;
 211        this.Zoom = zoom;
 12
 213        this.CalculateBounds();
 214    }
 15
 16    private void CalculateBounds()
 217    {
 218        var n = Math.PI - 2.0 * Math.PI * this.Y / Math.Pow(2.0, this.Zoom);
 219        this.Left = this.X / Math.Pow(2.0, this.Zoom) * 360.0 - 180.0;
 220        this.Top = 180.0 / Math.PI * Math.Atan(Math.Sinh(n));
 21
 222        n = Math.PI - 2.0 * Math.PI * (this.Y + 1) / Math.Pow(2.0, this.Zoom);
 223        this.Right = (this.X + 1) / Math.Pow(2.0, this.Zoom) * 360.0 - 180.0;
 224        this.Bottom = 180.0 / Math.PI * Math.Atan(Math.Sinh(n));
 225    }
 26
 27    /// <summary>
 28    /// Gets X.
 29    /// </summary>
 830    public uint X { get; private set; }
 31
 32    /// <summary>
 33    /// Gets Y.
 34    /// </summary>
 835    public uint Y { get; private set; }
 36
 37    /// <summary>
 38    /// Gets the zoom level.
 39    /// </summary>
 1240    public int Zoom { get; private set; }
 41
 42    /// <summary>
 43    /// Gets the top.
 44    /// </summary>
 245    public double Top { get; private set; }
 46
 47    /// <summary>
 48    /// Get the bottom.
 49    /// </summary>
 250    public double Bottom { get; private set; }
 51
 52    /// <summary>
 53    /// Get the left.
 54    /// </summary>
 255    public double Left { get; private set; }
 56
 57    /// <summary>
 58    /// Gets the right.
 59    /// </summary>
 260    public double Right { get; private set; }
 61
 62    /// <summary>
 63    /// Updates the data in this tile to correspond with the given local tile id.
 64    /// </summary>
 65    /// <param name="localId">The local tile id.</param>
 66    public void UpdateToLocalId(ulong localId)
 067    {
 068        var xMax = (ulong)(1 << (int)this.Zoom);
 69
 070        this.X = (uint)(localId % xMax);
 071        this.Y = (uint)(localId / xMax);
 072    }
 73
 74    /// <summary>
 75    /// Gets the local tile id.
 76    /// </summary>
 77    /// <remarks>This is the id relative to the zoom level.</remarks>
 78    public uint LocalId
 79    {
 80        get
 181        {
 182            var xMax = 1 << (int)this.Zoom;
 83
 184            return (uint)(this.Y * xMax + this.X);
 185        }
 86    }
 87
 88    /// <summary>
 89    /// Returns the tile for the given local id and zoom level.
 90    /// </summary>
 91    /// <param name="localId">The local id.</param>
 92    /// <param name="zoom">The zoom level.</param>
 93    /// <returns>The tile.</returns>
 94    public static Tile FromLocalId(ulong localId, int zoom)
 195    {
 196        var xMax = (ulong)(1 << zoom);
 97
 198        return new Tile((uint)(localId % xMax),
 199            (uint)(localId / xMax), zoom);
 1100    }
 101
 102    /// <summary>
 103    /// Calculates the maximum local id for the given zoom level.
 104    /// </summary>
 105    /// <param name="zoom">The zoom level.</param>
 106    /// <returns>The maximum local id for the given zoom level</returns>
 107    public static ulong MaxLocalId(int zoom)
 0108    {
 0109        var xMax = (ulong)(1 << zoom);
 110
 0111        return xMax * xMax;
 0112    }
 113
 114    /// <summary>
 115    /// Converts a lat/lon pair to a set of local coordinates.
 116    /// </summary>
 117    /// <param name="latitude">The latitude.</param>
 118    /// <param name="longitude">The longitude.</param>
 119    /// <param name="resolution">The resolution.</param>
 120    /// <returns>A local coordinate pair.</returns>
 121    public (int x, int y) ToLocalCoordinates(double longitude, double latitude, int resolution)
 0122    {
 0123        var latStep = (this.Top - this.Bottom) / resolution;
 0124        var lonStep = (this.Right - this.Left) / resolution;
 0125        var top = this.Top;
 0126        var left = this.Left;
 127
 0128        return ((int)((longitude - left) / lonStep), (int)((top - latitude) / latStep));
 0129    }
 130
 131    /// <summary>
 132    /// Converts a set of local coordinates to a lat/lon pair.
 133    /// </summary>
 134    /// <param name="x"></param>
 135    /// <param name="y"></param>
 136    /// <param name="resolution"></param>
 137    /// <returns>A global coordinate pair.</returns>
 138    public (double longitude, double latitude) FromLocalCoordinates(int x, int y, int resolution)
 0139    {
 0140        var latStep = (this.Top - this.Bottom) / resolution;
 0141        var lonStep = (this.Right - this.Left) / resolution;
 0142        var top = this.Top;
 0143        var left = this.Left;
 144
 0145        return (left + lonStep * x, top - y * latStep);
 0146    }
 147
 148    /// <summary>
 149    /// Gets the tile at the given coordinates for the given zoom level.
 150    /// </summary>
 151    /// <param name="latitude">The latitude.</param>
 152    /// <param name="longitude">The longitude.</param>
 153    /// <param name="zoom">The zoom level.</param>
 154    /// <returns>The tile a the given coordinates.</returns>
 155    public static Tile WorldToTile(double longitude, double latitude, int zoom)
 1156    {
 1157        var n = (int)Math.Floor(Math.Pow(2, zoom)); // replace by bitshifting?
 158
 1159        var rad = latitude / 180d * Math.PI;
 160
 1161        var x = (uint)((longitude + 180.0f) / 360.0f * n);
 1162        var y = (uint)(
 1163            (1.0f - Math.Log(Math.Tan(rad) + 1.0f / Math.Cos(rad))
 1164                / Math.PI) / 2f * n);
 165
 1166        return new Tile(x, y, zoom);
 1167    }
 168
 169    public bool IsInside(double longitude, double latitude)
 0170    {
 0171        return !(this.Top <= latitude ||
 0172                 this.Bottom >= latitude ||
 0173                 this.Left >= longitude ||
 0174                 this.Right <= longitude);
 0175    }
 176
 177    public override string ToString()
 0178    {
 0179        return $"{this.X},{this.Y}@{this.Zoom}";
 0180    }
 181}