-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOSMMap.cs
93 lines (74 loc) · 3.39 KB
/
OSMMap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using StereoKit;
namespace DutchSkies
{
public class OSMMap
{
public string id;
// EPSG:4326 (WGS84)
public float min_lat, max_lat;
public float min_lon, max_lon;
public float center_lat, center_lon;
public int zoom;
// Image
public Tex texture;
// EPSG:3857 extents, in meters
public float min_x, max_x;
public float min_y, max_y;
public float x_extent, y_extent;
// Approximate map size in kilometers
public float width, height;
public OSMMap(string id, float minlat, float maxlat, float minlon, float maxlon, int zm=0)
{
this.id = id;
min_lat = minlat;
max_lat = maxlat;
min_lon = minlon;
max_lon = maxlon;
zoom = zm;
center_lat = 0.5f * (min_lat + max_lat);
center_lon = 0.5f * (min_lon + max_lon);
Log.Info($"Center lat {center_lat:F6}, lon {center_lon:F6} (map '{this.id}')");
// Compute width of map at center latitude
// Radius at center latitude in kilometers
float r = Projection.RADIUS_KILOMETERS * MathF.Cos(center_lat / 180.0f * MathF.PI);
width = (max_lon - min_lon) / 360.0f * 2.0f * MathF.PI * r;
// Height based on map aspect in pixels: 360.3565
// Height based on latitudes: 360.1501
//height = 1.0f * current_configuration.image_height / current_configuration.image_width * width;
height = 1.0f * (max_lat - min_lat) / 360.0f * 2.0f * MathF.PI * Projection.RADIUS_KILOMETERS;
Log.Info($"Approximate map size (kilometers) = {width:F3} x {height:F3}");
// This gives an approximate (but reasonable) X, Y range
float xx, yy;
Projection.epsg4326_to_epsg3857(out min_x, out yy, min_lon, center_lat);
Projection.epsg4326_to_epsg3857(out max_x, out yy, max_lon, center_lat);
Projection.epsg4326_to_epsg3857(out xx, out min_y, center_lon, min_lat);
Projection.epsg4326_to_epsg3857(out xx, out max_y, center_lon, max_lat);
Log.Info($"EPSG:3857 X range: {min_x:F6}, {max_x:F6}");
Log.Info($"EPSG:3857 Y range: {min_y:F6}, {max_y:F6}");
x_extent = max_x - min_x;
y_extent = max_y - min_y;
// EPSG:4326 lon 5, lat 52 -> EPSG:3857 556597.45, 6800125.45 -> -18.68516, -17.84164 (NL map centric, center = 5.27, 52.13)
//Project(ref xx, ref yy, 5f, 52f);
//Log.Info($"lon 5, lat 52 -> {xx}, {yy}");
// Image to be set later
texture = null;
}
// Input in WGS84 coordinates
// Output x and y are in *kilometers*, relative to the map center
public void Project(out float x, out float y, float lon, float lat)
{
float xx;
float yy;
Projection.epsg4326_to_epsg3857(out xx, out yy, lon, lat);
//Log.Info($"project() {lon}, {lat} -> x {xx}, y {yy}");
x = ((xx - min_x) / x_extent - 0.5f) * width;
y = ((yy - min_y) / y_extent - 0.5f) * height;
}
public bool OnMapLatLon(float lat, float lon)
{
return lat >= min_lat && lat <= max_lat && lon >= min_lon && lon <= max_lon;
}
}
}