Show / Hide Table of Contents

Class Conversions

A set of Geo and Terrain Conversion utils.

Inheritance
System.Object
Conversions
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ToString()
System.Object.ReferenceEquals(System.Object, System.Object)
Namespace:Mapbox.Unity.Utilities
Assembly:cs.temp.dll.dll
Syntax
public static class Conversions

Methods

GeoFromGlobePosition(Vector3, Single)

Declaration
public static Vector2d GeoFromGlobePosition(Vector3 point, float radius)
Parameters
Type Name Description
Vector3 point
System.Single radius
Returns
Type Description
Vector2d

GeoToWorldGlobePosition(Double, Double, Single)

Declaration
public static Vector3 GeoToWorldGlobePosition(double lat, double lon, float radius)
Parameters
Type Name Description
System.Double lat
System.Double lon
System.Single radius
Returns
Type Description
Vector3

GeoToWorldGlobePosition(Vector2d, Single)

Declaration
public static Vector3 GeoToWorldGlobePosition(Vector2d latLong, float radius)
Parameters
Type Name Description
Vector2d latLong
System.Single radius
Returns
Type Description
Vector3

GeoToWorldPosition(Double, Double, Vector2d, Single)

Converts WGS84 lat/lon to x/y meters in reference to a center point

Declaration
public static Vector2d GeoToWorldPosition(double lat, double lon, Vector2d refPoint, float scale = 1F)
Parameters
Type Name Description
System.Double lat

The latitude.

System.Double lon

The longitude.

Vector2d refPoint

A UnityEngine.Vector2d center point to offset resultant xy, this is usually map's center mercator

System.Single scale

Scale in meters. (default scale = 1)

Returns
Type Description
Vector2d

A UnityEngine.Vector2d xy tile ID.

Examples

Converts a Lat/Lon of (37.7749, 122.4194) into Unity coordinates for a map centered at (10,10) and a scale of 2.5 meters for every 1 Unity unit

var worldPosition = Conversions.GeoToWorldPosition(37.7749, 122.4194, new Vector2d(10, 10), (float)2.5);
// worldPosition = ( 11369163.38585, 34069138.17805 )

GeoToWorldPosition(Vector2d, Vector2d, Single)

Declaration
public static Vector2d GeoToWorldPosition(Vector2d latLong, Vector2d refPoint, float scale = 1F)
Parameters
Type Name Description
Vector2d latLong
Vector2d refPoint
System.Single scale
Returns
Type Description
Vector2d

GetAbsoluteHeightFromColor(Color)

Specific formula for mapbox.terrain-rgb to decode height values from pixel values. See: https://www.mapbox.com/blog/terrain-rgb/.

Declaration
public static float GetAbsoluteHeightFromColor(Color color)
Parameters
Type Name Description
Color color

The UnityEngine.Color.

Returns
Type Description
System.Single

Height in meters.

GetAbsoluteHeightFromColor(Single, Single, Single)

Declaration
public static float GetAbsoluteHeightFromColor(float r, float g, float b)
Parameters
Type Name Description
System.Single r
System.Single g
System.Single b
Returns
Type Description
System.Single

GetAbsoluteHeightFromColor32(Color32)

Declaration
public static float GetAbsoluteHeightFromColor32(Color32 color)
Parameters
Type Name Description
Color32 color
Returns
Type Description
System.Single

GetRelativeHeightFromColor(Color, Single)

Gets height from terrain-rgb adjusted for a given scale.

Declaration
public static float GetRelativeHeightFromColor(Color color, float relativeScale)
Parameters
Type Name Description
Color color

The UnityEngine.Color.

System.Single relativeScale

Relative scale.

Returns
Type Description
System.Single

Adjusted height in meters.

GetTileScaleInDegrees(Single, Int32)

Gets the degrees per tile at given zoom level for Web Mercator tile. See: http://wiki.openstreetmap.org/wiki/Zoom_levels.

Declaration
public static float GetTileScaleInDegrees(float latitude, int zoom)
Parameters
Type Name Description
System.Single latitude

The latitude.

System.Int32 zoom

Zoom level.

Returns
Type Description
System.Single

Degrees per tile.

GetTileScaleInMeters(Single, Int32)

Gets the meters per pixels at given latitude and zoom level for a 256x256 tile. See: http://wiki.openstreetmap.org/wiki/Zoom_levels.

Declaration
public static float GetTileScaleInMeters(float latitude, int zoom)
Parameters
Type Name Description
System.Single latitude

The latitude.

System.Int32 zoom

Zoom level.

Returns
Type Description
System.Single

Meters per pixel.

LatitudeLongitudeToTileId(Double, Double, Int32)

Gets the xy tile ID at the requested zoom that contains the WGS84 lat/lon point. See: http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames.

Declaration
public static UnwrappedTileId LatitudeLongitudeToTileId(double latitude, double longitude, int zoom)
Parameters
Type Name Description
System.Double latitude

The latitude.

System.Double longitude

The longitude.

System.Int32 zoom

Zoom level.

Returns
Type Description
UnwrappedTileId

A Mapbox.Map.UnwrappedTileId xy tile ID.

LatitudeLongitudeToUnityTilePosition(Vector2d, UnityTile, UInt64)

Declaration
public static Vector2 LatitudeLongitudeToUnityTilePosition(Vector2d coordinate, UnityTile tile, ulong layerExtent = 4096UL)
Parameters
Type Name Description
Vector2d coordinate
UnityTile tile
System.UInt64 layerExtent
Returns
Type Description
Vector2

LatitudeLongitudeToUnityTilePosition(Vector2d, Int32, Single, UInt64)

Get coordinates for a given latitude/longitude in tile-space. Useful when comparing feature geometry to lat/lon coordinates.

Declaration
public static Vector2 LatitudeLongitudeToUnityTilePosition(Vector2d coordinate, int tileZoom, float tileScale, ulong layerExtent = 4096UL)
Parameters
Type Name Description
Vector2d coordinate

Coordinate.

System.Int32 tileZoom

The zoom level of the tile.

System.Single tileScale

Tile scale. Optional, but recommended. Defaults to a scale of 1.

System.UInt64 layerExtent

Layer extent. Optional, but recommended. Defaults to 4096, the standard for Mapbox Tiles

Returns
Type Description
Vector2

The longitude to tile position.

LatitudeLongitudeToVectorTilePosition(Vector2d, Int32, UInt64)

Get coordinates for a given latitude/longitude in tile-space. Useful when comparing feature geometry to lat/lon coordinates.

Declaration
public static Vector2 LatitudeLongitudeToVectorTilePosition(Vector2d coordinate, int tileZoom, ulong layerExtent = 4096UL)
Parameters
Type Name Description
Vector2d coordinate

Coordinate.

System.Int32 tileZoom

The zoom level of the tile.

System.UInt64 layerExtent

Layer extent. Optional, but recommended. Defaults to 4096, the standard for Mapbox Tiles

Returns
Type Description
Vector2

The longitude to tile position.

LatLonToMeters(Double, Double)

Converts WGS84 lat/lon to Spherical Mercator EPSG:900913 xy meters. SOURCE: http://stackoverflow.com/questions/12896139/geographic-coordinates-converter.

Declaration
public static Vector2d LatLonToMeters(double lat, double lon)
Parameters
Type Name Description
System.Double lat

The latitude.

System.Double lon

The longitude.

Returns
Type Description
Vector2d

A UnityEngine.Vector2d of xy meters.

LatLonToMeters(Vector2d)

Converts Mapbox.Utils.Vector2d struct, WGS84 lat/lon to Spherical Mercator EPSG:900913 xy meters.

Declaration
public static Vector2d LatLonToMeters(Vector2d v)
Parameters
Type Name Description
Vector2d v

The Mapbox.Utils.Vector2d.

Returns
Type Description
Vector2d

A UnityEngine.Vector2d of coordinates in meters.

MetersToLatLon(Vector2d)

Converts Spherical Mercator EPSG:900913 in xy meters to WGS84 lat/lon. Inverse of LatLonToMeters.

Declaration
public static Vector2d MetersToLatLon(Vector2d m)
Parameters
Type Name Description
Vector2d m

A UnityEngine.Vector2d of coordinates in meters.

Returns
Type Description
Vector2d

The Mapbox.Utils.Vector2d in lat/lon.

Examples

Converts EPSG:900913 xy meter coordinates to lat lon

var worldPosition =  new Vector2d (4547675.35434,13627665.27122);
var latlon = Conversions.MetersToLatLon(worldPosition);
// latlon = ( 37.77490, 122.41940 )

MetersToTile(Vector2d, Int32)

Gets the xy tile ID from Spherical Mercator EPSG:900913 xy coords.

Declaration
public static Vector2 MetersToTile(Vector2d m, int zoom)
Parameters
Type Name Description
Vector2d m

UnityEngine.Vector2d XY coords in meters.

System.Int32 zoom

Zoom level.

Returns
Type Description
Vector2

A UnityEngine.Vector2d xy tile ID.

Examples

Converts EPSG:900913 xy meter coordinates to web mercator tile XY coordinates at zoom 12.

var meterXYPosition = new Vector2d (4547675.35434,13627665.27122);
var tileXY = Conversions.MetersToTile (meterXYPosition, 12);
// tileXY = ( 655, 2512 )

StringToLatLon(String)

Convert a simple string to a latitude longitude. Expects format: latitude, longitude

Declaration
public static Vector2d StringToLatLon(string s)
Parameters
Type Name Description
System.String s

string.

Returns
Type Description
Vector2d

The lat/lon as Vector2d.

TileBounds(UnwrappedTileId)

Declaration
public static RectD TileBounds(UnwrappedTileId unwrappedTileId)
Parameters
Type Name Description
UnwrappedTileId unwrappedTileId
Returns
Type Description
RectD

TileBounds(Vector2, Int32)

Gets the tile bounds in Spherical Mercator EPSG:900913 meters from an xy tile ID.

Declaration
public static RectD TileBounds(Vector2 tileCoordinate, int zoom)
Parameters
Type Name Description
Vector2 tileCoordinate

XY tile ID.

System.Int32 zoom

Zoom level.

Returns
Type Description
RectD

A UnityEngine.Rect in meters.

TileIdToBounds(Int32, Int32, Int32)

Gets the Mapbox.Utils.Vector2dBounds of a tile.

Declaration
public static Vector2dBounds TileIdToBounds(int x, int y, int zoom)
Parameters
Type Name Description
System.Int32 x

Tile X position.

System.Int32 y

Tile Y position.

System.Int32 zoom

Zoom level.

Returns
Type Description
Vector2dBounds

The Mapbox.Utils.Vector2dBounds of the tile.

TileIdToCenterLatitudeLongitude(Int32, Int32, Int32)

Gets the WGS84 lat/lon of the center of a tile.

Declaration
public static Vector2d TileIdToCenterLatitudeLongitude(int x, int y, int zoom)
Parameters
Type Name Description
System.Int32 x

Tile X position.

System.Int32 y

Tile Y position.

System.Int32 zoom

Zoom level.

Returns
Type Description
Vector2d

A UnityEngine.Vector2d of lat/lon coordinates.

TileIdToCenterWebMercator(Int32, Int32, Int32)

Gets the Web Mercator x/y of the center of a tile.

Declaration
public static Vector2d TileIdToCenterWebMercator(int x, int y, int zoom)
Parameters
Type Name Description
System.Int32 x

Tile X position.

System.Int32 y

Tile Y position.

System.Int32 zoom

Zoom level.

Returns
Type Description
Vector2d

A UnityEngine.Vector2d of lat/lon coordinates.

TileXToNWLongitude(Int32, Int32)

Gets the WGS84 longitude of the northwest corner from a tile's X position and zoom level. See: http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames.

Declaration
public static double TileXToNWLongitude(int x, int zoom)
Parameters
Type Name Description
System.Int32 x

Tile X position.

System.Int32 zoom

Zoom level.

Returns
Type Description
System.Double

NW Longitude.

TileYToNWLatitude(Int32, Int32)

Gets the WGS84 latitude of the northwest corner from a tile's Y position and zoom level. See: http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames.

Declaration
public static double TileYToNWLatitude(int y, int zoom)
Parameters
Type Name Description
System.Int32 y

Tile Y position.

System.Int32 zoom

Zoom level.

Returns
Type Description
System.Double

NW Latitude.

Back to top © 2017 Mapbox