-
Notifications
You must be signed in to change notification settings - Fork 0
/
Converter.cs
84 lines (71 loc) · 2.98 KB
/
Converter.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
using System;
namespace CoordinateUUIDConverter
{
public static class Converter
{
public static string ToUUID(this Coordinate coordinate)
{
byte[] lonBytes = BitConverter.GetBytes(coordinate.Longitude);
byte[] latBytes = BitConverter.GetBytes(coordinate.Latitude);
byte[] floorByte = BitConverter.GetBytes(coordinate.Floor);
string[] buffer = new string[4];
for (int i = 0; i < 2; i++)
{
string lonHex = Convert.ToString(lonBytes[i * 2], 16).PadLeft(2, '0') + Convert.ToString(lonBytes[i * 2 + 1], 16).PadLeft(2, '0');
string latHex = Convert.ToString(latBytes[i * 2], 16).PadLeft(2, '0') + Convert.ToString(latBytes[i * 2 + 1], 16).PadLeft(2, '0');
buffer[i + 2] = lonHex;
buffer[i] = latHex;
}
string floorHex = Convert.ToString(floorByte[0], 16).PadLeft(2,'0') +
Convert.ToString(floorByte[1], 16).PadLeft(2, '0') +
Convert.ToString(floorByte[2], 16).PadLeft(2, '0') +
Convert.ToString(floorByte[3], 16).PadLeft(2, '0');
return floorHex + "-0000-" + buffer[0] + "-" + buffer[1] + "-0000" + buffer[2] + buffer[3];
}
public static Coordinate ToCoordinate(this Guid Id)
{
string[] idShards = Id.ToString().Split('-');
string latHexStr = idShards[2] + idShards[3];
string lonHexStr = idShards[4].Substring(4, 8);
string floorHexStr = idShards[0];
return new Coordinate (
HexToFloat(latHexStr),
HexToFloat(lonHexStr),
HexToFloat(floorHexStr)
);
}
/// <summary>
/// Convert hex string content to a float.
/// 0xff20f342 -> 121.564445f
/// </summary>
/// <param name="Hex"></param>
/// <returns></returns>
private static float HexToFloat(string Hex)
{
// Hex string content to a byte array.
byte[] Bytes = new byte[4];
Bytes[0] = Convert.ToByte(Hex.Substring(0, 2), 16);
Bytes[1] = Convert.ToByte(Hex.Substring(2, 2), 16);
Bytes[2] = Convert.ToByte(Hex.Substring(4, 2), 16);
Bytes[3] = Convert.ToByte(Hex.Substring(6, 2), 16);
// byte array to a float.
return BitConverter.ToSingle(Bytes, 0);
}
}
public class Coordinate
{
public float Longitude { get; }
public float Latitude { get; }
public float Floor { get; }
public Coordinate(float latitude, float longitude, float floor)
{
if (latitude < 0 || latitude > 90)
throw new Exception("0 <= Latitude >= 90");
if (longitude < -180 || longitude > 180)
throw new Exception("-180 <= Longitude >= 180");
Longitude = longitude;
Latitude = latitude;
Floor = floor;
}
}
}