25
25
26
26
namespace UnitySlippyMap
27
27
{
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>
31
31
public class GeoHelpers
32
32
{
33
33
public static double OriginShift = 2.0 * Math . PI * 6378137.0 / 2.0 ;
34
34
public static float MetersPerInch = 2.54f / 100.0f ;
35
35
public static double HalfEarthCircumference = 6378137.0 * Math . PI ;
36
36
public static double EarthCircumference = HalfEarthCircumference * 2.0 ;
37
37
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 )
42
46
{
43
47
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 ) ) ;
45
49
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 ) ) ;
47
51
48
52
return p ;
49
53
}
50
54
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 )
55
63
{
56
64
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 ) ) ;
58
66
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 ) ;
60
68
p [ 1 ] = ( 180.0 / Math . PI * Math . Atan ( Math . Sinh ( n ) ) ) ;
61
69
62
70
return p ;
63
71
}
64
72
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>
68
79
public static float MetersPerPixel ( float latitude , float zoomLevel )
69
80
{
70
81
double realLengthInMeters = EarthCircumference * Math . Cos ( Mathf . Deg2Rad * latitude ) ;
71
82
return ( float ) ( realLengthInMeters / Math . Pow ( 2.0 , zoomLevel + 8 ) ) ;
72
83
}
73
84
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>
77
93
public static float MapScaleToOsmZoomLevel ( float mapScale , float latitude , float tileSize , float ppi )
78
94
{
79
95
double realLengthInMeters = EarthCircumference * Math . Cos ( Mathf . Deg2Rad * latitude ) ;
@@ -82,9 +98,14 @@ public static float MapScaleToOsmZoomLevel(float mapScale, float latitude, float
82
98
return ( float ) Math . Log ( zoomLevelExp , 2.0 ) ;
83
99
}
84
100
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>
88
109
public static float OsmZoomLevelToMapScale ( float zoomLevel , float latitude , float tileSize , float ppi )
89
110
{
90
111
double realLengthInMeters = EarthCircumference * Math . Cos ( Mathf . Deg2Rad * latitude ) ;
@@ -93,16 +114,13 @@ public static float OsmZoomLevelToMapScale(float zoomLevel, float latitude, floa
93
114
94
115
return ( float ) ( ( realLengthInMeters * ppi ) / zoomLevelExp / tileSize / MetersPerInch ) ;
95
116
}
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>
106
124
public static double [ ] RaycastHitToWGS84 ( Map map , RaycastHit r )
107
125
{
108
126
double [ ] RaycastHitToEPSG900913 = new double [ ] { ( map . CenterEPSG900913 [ 0 ] ) + ( r . point . x / map . ScaleMultiplier ) , ( map . CenterEPSG900913 [ 1 ] ) + ( r . point . z / map . ScaleMultiplier ) } ;
0 commit comments