Skip to content

Commit

Permalink
Prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengYen-Tang committed Nov 6, 2018
1 parent 401132e commit 8e67387
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
84 changes: 84 additions & 0 deletions Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
}
93 changes: 92 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,103 @@
using System;
using System.Collections.Generic;
using System.Globalization;

namespace CoordinateUUIDConverter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
if(args.Length < 2)
{
var consoleColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wrong number of parameters");
Console.ForegroundColor = consoleColor;
Environment.Exit( 0 );
}

if (args[0].ToLower() != "-touuid" && args[0].ToLower() != "-tocoordinate")
{
var consoleColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wrong of parameters");
Console.ForegroundColor = consoleColor;
Environment.Exit( 0 );
}

if (args[0].ToLower() == "-touuid")
{
List<Coordinate> coordinates = new List<Coordinate>();
for (int i = 1; i < args.Length; i++)
try
{
string[] buffer = args[i].Split(',');

if (buffer.Length == 2)
coordinates.Add(new Coordinate(
float.Parse(buffer[0], CultureInfo.InvariantCulture.NumberFormat),
float.Parse(buffer[1], CultureInfo.InvariantCulture.NumberFormat),
1));
else
coordinates.Add(new Coordinate (
float.Parse(buffer[0], CultureInfo.InvariantCulture.NumberFormat),
float.Parse(buffer[1], CultureInfo.InvariantCulture.NumberFormat),
float.Parse(buffer[2], CultureInfo.InvariantCulture.NumberFormat)
));
}
catch(Exception ex)
{
var consoleColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("The {0} coordinate format is incorrect. {1}", i, ex.Message);
Console.ForegroundColor = consoleColor;
}

ToUUIDProcess(coordinates);
}

if (args[0].ToLower() == "-tocoordinate")
{
List<Guid> ids = new List<Guid>();
for (int i = 1; i < args.Length; i++)
try
{
ids.Add(Guid.Parse(args[i]));
}
catch
{
var consoleColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("UUID format is incorrect. {0}" ,args[i]);
Console.ForegroundColor = consoleColor;
}

ToCoordinateProcess(ids);
}
}

private static void ToUUIDProcess(List<Coordinate> coordinates)
{
foreach(Coordinate coordinate in coordinates)
Console.WriteLine("({0},{1},{2}): {3}",
coordinate.Latitude,
coordinate.Longitude,
coordinate.Floor,
coordinate.ToUUID());
}

private static void ToCoordinateProcess(List<Guid> ids)
{
foreach (Guid id in ids)
{
Coordinate coordinate = id.ToCoordinate();
Console.WriteLine("UUID: {0}, ({1},{2},{3})",
id.ToString(),
coordinate.Latitude,
coordinate.Longitude,
coordinate.Floor);
}
}
}
}
8 changes: 8 additions & 0 deletions Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"CoordinateUUIDConverter": {
"commandName": "Project",
"commandLineArgs": "-touuid 25.15502,121.68507,2"
}
}
}

0 comments on commit 8e67387

Please sign in to comment.