Skip to content
This repository was archived by the owner on Mar 11, 2024. It is now read-only.

Commit 0f830bd

Browse files
committed
In-line documentation of the GeoHelpers.
1 parent f87944f commit 0f830bd

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

Assets/UnitySlippyMap/Helpers/GeoHelpers.cs

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,55 +25,71 @@
2525

2626
namespace UnitySlippyMap
2727
{
28-
// <summary>
29-
// Helper class ported mostly from: http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/
30-
// </summary>
28+
/// <summary>
29+
/// Helper class ported mostly from: http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/
30+
/// </summary>
3131
public class GeoHelpers
3232
{
3333
public static double OriginShift = 2.0 * Math.PI * 6378137.0 / 2.0;
3434
public static float MetersPerInch = 2.54f / 100.0f;
3535
public static double HalfEarthCircumference = 6378137.0 * Math.PI;
3636
public static double EarthCircumference = HalfEarthCircumference * 2.0;
3737

38-
// <summary>
39-
// Converts WGS84 LatLon coordinates to OSM tile coordinates.
40-
// </summary>
41-
public static int[] WGS84ToTile(double lon, double lat, int zoom)
38+
/// <summary>
39+
/// Converts WGS84 LatLon coordinates to OSM tile coordinates (<see cref="http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames"/>).
40+
/// </summary>
41+
/// <returns>The tile coordinates.</returns>
42+
/// <param name="lon">Longitude in the WGS84 coordinate system.</param>
43+
/// <param name="lat">Latitude in the WGS84 coordinate system.</param>
44+
/// <param name="zoom">Zoom level.</param>
45+
public static int[] WGS84ToTile(double lon, double lat, int zoomLevel)
4246
{
4347
int[] p = new int[2];
44-
p[0] = (int)((lon + 180.0) / 360.0 * (1 << zoom));
48+
p[0] = (int)((lon + 180.0) / 360.0 * (1 << zoomLevel));
4549
p[1] = (int)((1.0 - Math.Log(Math.Tan(lat * Math.PI / 180.0) +
46-
1.0 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * (1 << zoom));
50+
1.0 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * (1 << zoomLevel));
4751

4852
return p;
4953
}
5054

51-
// <summary>
52-
// Converts OSM tile coordinates to WGS84 LatLon coordinates (upper left corner of the tile).
53-
// </summary>
54-
public static double[] TileToWGS84(int tile_x, int tile_y, int zoom)
55+
/// <summary>
56+
/// Converts OSM tile coordinates to WGS84 LatLon coordinates (upper left corner of the tile).
57+
/// </summary>
58+
/// <returns>The tile coordinates in the WGS84 coordinate system.</returns>
59+
/// <param name="tile_x">X coordinate of the tile.</param>
60+
/// <param name="tile_y">Y coordinate of the tile.</param>
61+
/// <param name="zoom">Zoom level.</param>
62+
public static double[] TileToWGS84(int tile_x, int tile_y, int zoomLevel)
5563
{
5664
double[] p = new double[2];
57-
double n = Math.PI - ((2.0 * Math.PI * tile_y) / Math.Pow(2.0, zoom));
65+
double n = Math.PI - ((2.0 * Math.PI * tile_y) / Math.Pow(2.0, zoomLevel));
5866

59-
p[0] = ((tile_x / Math.Pow(2.0, zoom) * 360.0) - 180.0);
67+
p[0] = ((tile_x / Math.Pow(2.0, zoomLevel) * 360.0) - 180.0);
6068
p[1] = (180.0 / Math.PI * Math.Atan(Math.Sinh(n)));
6169

6270
return p;
6371
}
6472

65-
// <summary>
66-
// Returns the numbers of meters per pixel in respect to the latitude and zoom level of the map.
67-
// </summary>
73+
/// <summary>
74+
/// Returns the number of meters per pixel in respect to the latitude and zoom level of the map.
75+
/// </summary>
76+
/// <returns>The number of meters per pixel.</returns>
77+
/// <param name="latitude">Latitude.</param>
78+
/// <param name="zoomLevel">Zoom level.</param>
6879
public static float MetersPerPixel(float latitude, float zoomLevel)
6980
{
7081
double realLengthInMeters = EarthCircumference * Math.Cos(Mathf.Deg2Rad * latitude);
7182
return (float)(realLengthInMeters / Math.Pow(2.0, zoomLevel + 8));
7283
}
7384

74-
// <summary>
75-
// Returns the Open Street Map zoom level in respect to the map scale, latitude, tile size and resolution.
76-
// </summary>
85+
/// <summary>
86+
/// Returns the Open Street Map zoom level in respect to the map scale, latitude, tile size and resolution.
87+
/// </summary>
88+
/// <returns>The scale to osm zoom level.</returns>
89+
/// <param name="mapScale">Map scale.</param>
90+
/// <param name="latitude">Latitude.</param>
91+
/// <param name="tileSize">Tile size.</param>
92+
/// <param name="ppi">Pixels per inch.</param>
7793
public static float MapScaleToOsmZoomLevel(float mapScale, float latitude, float tileSize, float ppi)
7894
{
7995
double realLengthInMeters = EarthCircumference * Math.Cos(Mathf.Deg2Rad * latitude);
@@ -82,9 +98,14 @@ public static float MapScaleToOsmZoomLevel(float mapScale, float latitude, float
8298
return (float)Math.Log(zoomLevelExp, 2.0);
8399
}
84100

85-
// <summary>
86-
// Returns the map scale in respect to the Open Street Map zoom level, latitude, tile size and resolution.
87-
// </summary>
101+
/// <summary>
102+
/// Returns the map scale in respect to the Open Street Map zoom level, latitude, tile size and resolution.
103+
/// </summary>
104+
/// <returns>The zoom level to map scale.</returns>
105+
/// <param name="zoomLevel">Zoom level.</param>
106+
/// <param name="latitude">Latitude.</param>
107+
/// <param name="tileSize">Tile size.</param>
108+
/// <param name="ppi">Pixels per inch.</param>
88109
public static float OsmZoomLevelToMapScale(float zoomLevel, float latitude, float tileSize, float ppi)
89110
{
90111
double realLengthInMeters = EarthCircumference * Math.Cos(Mathf.Deg2Rad * latitude);
@@ -93,16 +114,13 @@ public static float OsmZoomLevelToMapScale(float zoomLevel, float latitude, floa
93114

94115
return (float)((realLengthInMeters * ppi) / zoomLevelExp / tileSize / MetersPerInch);
95116
}
96-
97-
// <summary>
98-
// Returns WGS84 given a RaycastHit and Map instance.
99-
// </summary>
100-
// <param name='r'>
101-
// The RaycastHit
102-
// </param>
103-
// <param name='m'>
104-
// The Map instance
105-
// </param>
117+
118+
/// <summary>
119+
/// Returns WGS84 given a RaycastHit and Map instance.
120+
/// </summary>
121+
/// <returns>The WGS84 coordinates of the point hit.</returns>
122+
/// <param name="map"><see cref="UnitySlippyMap.Map"/> instance.</param>
123+
/// <param name="r">The red component.</param>
106124
public static double[] RaycastHitToWGS84(Map map, RaycastHit r)
107125
{
108126
double[] RaycastHitToEPSG900913 = new double[]{(map.CenterEPSG900913[0]) + (r.point.x/map.ScaleMultiplier) , (map.CenterEPSG900913[1]) + (r.point.z/map.ScaleMultiplier)};

0 commit comments

Comments
 (0)