64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using OSMDatastructure;
|
|
using OSMDatastructure.Graph;
|
|
|
|
namespace OSMImporter
|
|
{
|
|
public class RegionManager
|
|
{
|
|
private string workingDirectory { get; }
|
|
private readonly Dictionary<ulong, Region> _regions = new();
|
|
|
|
public RegionManager(string workingDirectory)
|
|
{
|
|
this.workingDirectory = workingDirectory;
|
|
}
|
|
|
|
public Region? GetRegion(Coordinates coordinates)
|
|
{
|
|
return GetRegion(Coordinates.GetRegionHashCode(coordinates));
|
|
}
|
|
|
|
public Region? GetRegion(ulong id)
|
|
{
|
|
if(_regions.TryGetValue(id, out Region? value))
|
|
return value;
|
|
else
|
|
{
|
|
Region? loadedRegion = LoadRegion(id);
|
|
if(loadedRegion is not null)
|
|
_regions.Add(loadedRegion!.regionHash, value: loadedRegion);
|
|
return loadedRegion;
|
|
}
|
|
}
|
|
|
|
public Region[] GetAllRegions()
|
|
{
|
|
return this._regions.Values.ToArray();
|
|
}
|
|
|
|
private Region? LoadRegion(Coordinates coordinates)
|
|
{
|
|
return LoadRegion(Coordinates.GetRegionHashCode(coordinates));
|
|
}
|
|
|
|
private Region? LoadRegion(ulong id)
|
|
{
|
|
Console.WriteLine($"Load Region {id}");
|
|
return Region.FromId(workingDirectory, id);
|
|
}
|
|
|
|
public OsmNode? GetNode(Coordinates coordinates)
|
|
{
|
|
Region? regionWithNode = GetRegion(coordinates);
|
|
if (regionWithNode is not null)
|
|
return regionWithNode.GetNode(coordinates);
|
|
else return null;
|
|
}
|
|
|
|
public OsmNode? GetNode(ulong nodeId, ulong regionId)
|
|
{
|
|
Region? r = GetRegion(regionId);
|
|
return r?.GetNode(nodeId);
|
|
}
|
|
}
|
|
} |