-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBingMaps.cs
126 lines (110 loc) · 5.01 KB
/
BingMaps.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// This file contains a pair of Bing Maps API requests! One for color, and
// one for elevation. It'll execute these asynchronously, so they don't block
// the application, and will notify a callback method with a texture
// generated from the data, as well as the size of the data received.
//
// The data structures ImageryRequest and ElevationRequest have a whole pile
// of parameters packed away in them, so these are great places to start
// messing around to see what additional information and configuration you
// can get!
using BingMapsRESTToolkit;
using StereoKit;
using System;
using System.IO;
using System.Threading.Tasks;
static class BingMaps
{
///////////////////////////////////////////
public static async Task RequestColor(string apiKey, ImageryType imageryType, BoundingBox regionBounds, Action<Tex, Vec3, Vec2> OnReceivedColor)
{
// Request an image from the maps API! This is the request package
// that gets sent to the server, details about the arguments can be
// found here:
// https://github.com/microsoft/BingMapsRESTToolkit/blob/master/Docs/API%20Reference.md#ImageryRequest
ImageryRequest request = new ImageryRequest() {
MapArea = regionBounds,
MapWidth = 1024,
MapHeight = 1024,
ImagerySet = imageryType,
BingMapsKey = apiKey
};
// We need the meta response as well as the image response, since the
// image API doesn't care too much about what we actually request!
// The results it sends back can differ in size, bounds, image format,
// so we need to know what we got!
Task<Response> metaTask = ServiceManager.GetResponseAsync(request);
Task<Stream> colorTask = ServiceManager.GetImageAsync (request);
await Task.WhenAll(metaTask, colorTask);
Response meta = await metaTask;
Stream stream = await colorTask;
// StatusCode is a web response status code, where 200-299 means
// success. Details here:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
if (meta.StatusCode < 200 || meta.StatusCode >= 300)
{
Log.Warn("Bing Maps API error:\n" + string.Join('\n', meta.ErrorDetails));
return;
}
// We need the result as a MemoryStream so we can grab the result as
// an array of bytes.
MemoryStream memStream = null;
if (stream is MemoryStream) memStream = (MemoryStream)stream;
else stream.CopyTo(memStream);
// Send the image over to StereoKit, and turn it into a texture!
Tex texture = Tex.FromMemory(memStream.ToArray());
texture.AddressMode = TexAddress.Mirror;
// Convert the image's bounds from lat/lon information into our
// world's meters.
BoundingBox bounds = new BoundingBox(meta.ResourceSets[0].Resources[0].BoundingBox);
Geo.BoundsToWorld(regionBounds, bounds, out Vec3 size, out Vec2 center);
// Done! Pass the results back.
OnReceivedColor(texture, size, center);
}
///////////////////////////////////////////
public static async Task RequestHeight(string apiKey, BoundingBox regionBounds, Action<Tex, Vec3, Vec2> OnReceivedHeight)
{
// Here's an elevation request! This doesn't provide an image, rather,
// it gives us a grid of height values. It's limited to a maximum of
// 1024 values per request, so we're only asking for a grid of 32x32
// elevations.
// However! This request does work exactly within the bounds provided,
// so we're getting what we expect from the results of this request.
// Details about the request can be found here:
// https://github.com/microsoft/BingMapsRESTToolkit/blob/master/Docs/API%20Reference.md#ElevationRequest
ElevationRequest request = new ElevationRequest() {
Bounds = regionBounds,
Row = 32,
Col = 32,
BingMapsKey = apiKey
};
Response response = await ServiceManager.GetResponseAsync(request);
// StatusCode is a web response status code, where 200-299 means
// success. Details here:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
if (response.StatusCode < 200 || response.StatusCode >= 300)
{
Log.Warn("Bing Maps API error:\n" + string.Join('\n', response.ErrorDetails));
return;
}
// Convert the elevation data we've received into a heightmap texture!
ElevationData data = response.ResourceSets[0].Resources[0] as ElevationData;
float[] heights = new float[32 * 32];
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 32; x++) {
// Height data is provided upside-down, so we're flipping it with
// this index on the Y axis.
heights[x+(31-y)*32] = data.Elevations[x+y*32] / Geo.EarthTallest;
}}
// Create a texture from the elevation data! We're storing it as
// R32 to preserve floating point precision in the height values.
Tex texture = new Tex(TexType.ImageNomips, TexFormat.R32);
texture.SetColors(32, 32, heights);
texture.AddressMode = TexAddress.Clamp;
// Our bounds should be correct, but we still need it in StereoKit
// units, so convert!
Geo.BoundsToWorld(regionBounds, regionBounds, out Vec3 size, out Vec2 center);
// Done! Pass the results back.
OnReceivedHeight(texture, size, center);
}
///////////////////////////////////////////
}