TileCoords

open class TileCoords

Class to convert from Map Tiles to coordinates and from coordinates to tiles

Coordinates (latitude and longitude) are ALWAYS expressed in degrees. The max latitude that can be converted to tiles is +85.0511 and the minimum is -85.0511 (see https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames).

Zoom level (z) range is from 0 to 19.

The earth is represented by a square that is divided in small pieces (tiles). The number of tiles depends on the zoom value and is equal to: 2^z x 2^z

The values of tiles can be from 0 to 2^z - 1. For instance, for z=10 the max tile would be 1023 (2^10 - 1 = 1024 - 1)

This diagram represents the equivalent lat/long vs tileX/tileY

(-180,85.0511)           (180,85.0511)  <----- coords (lat, long)
0,0                      2^z -1, 0 <---------- Tile number (x,y)
+-------------------------+
|                         |
|            + (0.0,0.0)  |
|                         |
+-------------------------+
0,2^z - 1                 2^z - 1, 2^z - 1
(-180,-85.0511)           (180,-85.0511)

All the wisdom of this class comes from: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames

  • Max value of latitude that can be retrieved with tiles (-85.0511 degrees)

    Declaration

    Swift

    static let maxLatitude: Double
  • Min value of latitude that can be retrieved with tiles (-85.0511 degrees)

    Declaration

    Swift

    static let minLatitude: Double
  • Max value of a longitude (<180.0). Any longitude has to be strictly minor than this value 180.0

    Declaration

    Swift

    static let maxLongitude: Double
  • Min value of a longitude (>=180.0) Any longitude has to be mayor or equal to this value -180.0.

    Declaration

    Swift

    static let minLongitude: Double
  • Max zoom supported in tile servers (19)

    Declaration

    Swift

    static let maxZoom: Zoom
  • Min zoom supported (0)

    Declaration

    Swift

    static let minZoom: Zoom
  • Based on current zoom it indicates what is the max tile

    Declaration

    Swift

    public static func maxTile(forZoom zoom: Zoom) -> TileNumber
  • Validates if longitude is between min and max allowed longitudes

    Throws

    LongitudeError

    See also

    maxLatitude, minLatitude

    Declaration

    Swift

    public static func validate(longitude: Double) throws

    Parameters

    longitude

    the longitude to validate

  • Validates if a latitude is between min and max allowed latitudes. Throws LongitudeError if it is not.

    Declaration

    Swift

    public static func validate(latitude: Double) throws
  • Validate zoom is less or equal to the maxZoom Throws ZoomError if is greater than maxZoom

    Declaration

    Swift

    public static func validate(zoom: Zoom) throws
  • Validates if the tile is within the range for the zoom A tile must be always be less than 2^zoom.

    Declaration

    Swift

    public static func validate(tile: TileNumber, forZoom zoom: Zoom) throws
  • Returns the tile in the X axis for the longitude and zoom. Can throw ZoomError and LongitudeError if these are out of the boundaries.

    Declaration

    Swift

    public static func longitudeToTileX(longitude: Double, zoom: Zoom) throws -> TileNumber
  • Returns the tile in the Y axis for the latitude and zoom. Can throw ZoomError and LongitudeError if these are out of the boundaries.

    Declaration

    Swift

    public static func latitudeToTileY(latitude: Double, zoom: Zoom) throws -> TileNumber
  • Returns the corresponding longitude in degrees for the tileX at zoom level

    Declaration

    Swift

    public static func tileXToLongitude(tileX: TileNumber, zoom: Zoom) throws -> Double
  • Returns the corresponding latitude in degrees for the tileY at zoom level

    Declaration

    Swift

    public static func tileYToLatitude(tileY: TileNumber, zoom: Zoom) throws -> Double
  • Holds the zoom level. For internal use only.

    Declaration

    Swift

    private var _zoom: Zoom
  • Zoom level. Read only. Use setZoom() to change it.

    Declaration

    Swift

    public var zoom: Zoom { get }
  • Holds the actual latitude

    Declaration

    Swift

    private var _latitude: Double
  • Latitude for this tile. Use set() to change it.

    Declaration

    Swift

    public var latitude: Double { get }
  • Holds the actual longitude

    Declaration

    Swift

    private var _longitude: Double
  • Longitude for this tile. Use set() to change it.

    Declaration

    Swift

    public var longitude: Double { get }
  • Holds the actual tileX

    Declaration

    Swift

    private var _tileX: TileNumber
  • Tile in the X axis for current longitude and zoom. Use set() to change it.

    Declaration

    Swift

    public var tileX: TileNumber { get }
  • Holds the actual tileY

    Declaration

    Swift

    private var _tileY: TileNumber
  • Tile in the Y axis for current latitude and zoom. Use set() to change it.

    Declaration

    Swift

    public var tileY: TileNumber { get }
  • Set zoom level. Throws ZoomError if zoom is not valid.

    Declaration

    Swift

    public func set(zoom: Zoom) throws
  • Set tile X and Y values. Throws TileError if latitude or longitude are out of range.

    Declaration

    Swift

    public func set(tileX: TileNumber, tileY: TileNumber) throws
  • Sets latitude and longitude. Throws LatitudeError and LongitudeError if they are out of range.

    Declaration

    Swift

    public func set(latitude: Double, longitude: Double) throws
  • Init a TileCoords instance using tile and zoom info. Will return nil if any of the parameters is out of range.

    Declaration

    Swift

    public init?(tileX: TileNumber, tileY: TileNumber, zoom: Zoom)
  • Init a TileCoords instance using latitude, longitude and zoom info. Will return nil if any of the parameters is out of range.

    Declaration

    Swift

    public init?(latitude: Double, longitude: Double, zoom: Zoom)
  • Creates a new Tile Coord with the same latitude and longitude as the the parameter but with a different zoom.

    Declaration

    Swift

    public init?(_ tileCoords: TileCoords, zoom: Zoom)
  • Returns the maximum tile number for current set zoom.

    Declaration

    Swift

    public func maxTile() -> TileNumber