72 lines
3.1 KiB
C#
72 lines
3.1 KiB
C#
using OSMDatastructure;
|
|
using OSMDatastructure.Graph;
|
|
|
|
namespace OSMImporter
|
|
{
|
|
public class RegionManager
|
|
{
|
|
private string workingDirectory { get; }
|
|
private readonly Dictionary<int, Region> _regions = new();
|
|
|
|
public RegionManager(string workingDirectory)
|
|
{
|
|
this.workingDirectory = workingDirectory;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks wether the Region is already loaded and returns the Region, or tries to load the Region from file in workingDirectory
|
|
/// </summary>
|
|
/// <param name="coordinates">Coordinates of the Region (or within the Region) to load</param>
|
|
/// <returns>The Region at the specified Coordinates containing Nodes and Connections</returns>
|
|
/// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception>
|
|
public Region GetRegion(Coordinates coordinates)
|
|
{
|
|
if(_regions.ContainsKey(Coordinates.GetRegionHashCode(coordinates)))
|
|
return _regions[Coordinates.GetRegionHashCode(coordinates)];
|
|
else
|
|
{
|
|
Region loadedRegion = LoadRegion(coordinates);
|
|
_regions.Add(loadedRegion.regionHash, value: loadedRegion);
|
|
return loadedRegion;
|
|
}
|
|
}
|
|
|
|
public Region[] GetAllRegions()
|
|
{
|
|
return this._regions.Values.ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="coordinates">Coordinates of the Region (or within the Region) to load</param>
|
|
/// <returns>The Region at the specified Coordinates containing Nodes and Connections</returns>
|
|
/// <exception cref="FileNotFoundException">If the Regionfile can not be found.</exception>
|
|
private Region LoadRegion(Coordinates coordinates)
|
|
{
|
|
string fullPath = Path.Combine(workingDirectory, Coordinates.GetRegionHashCode(coordinates).ToString());
|
|
DateTime startTime = DateTime.Now;
|
|
if (!File.Exists(fullPath))
|
|
{
|
|
throw new FileNotFoundException(string.Format("[{0}] Region does not exist: {1}", startTime, fullPath));
|
|
}
|
|
|
|
FileStream fileStream = new FileStream(fullPath, FileMode.Open);
|
|
long fileStreamLength = fileStream.Length;
|
|
Console.WriteLine("[{0}] Loading [{1}]bytes from {2}", startTime.ToLocalTime(), fileStreamLength, fullPath);
|
|
|
|
byte[] regionBytes = new byte[fileStream.Length];
|
|
int loadedBytesLength = fileStream.Read(regionBytes, 0, regionBytes.Length);
|
|
fileStream.Close();
|
|
|
|
Console.WriteLine("\tLoaded [{0}]bytes ({1:P1}) in [{2}]ms", loadedBytesLength, fileStreamLength / loadedBytesLength,DateTime.Now.Subtract(startTime).TotalMilliseconds);
|
|
return ByteConverter.ToRegion(regionBytes);
|
|
}
|
|
|
|
public OsmNode? GetNode(Coordinates coordinates)
|
|
{
|
|
Region regionWithNode = GetRegion(coordinates);
|
|
return regionWithNode.GetNodeWithCoordinates(coordinates);
|
|
}
|
|
}
|
|
} |