Moved RegionManager inside of Importer as RegionStruct,

Moved Regionsize var to Region
This commit is contained in:
C9Glax 2023-02-03 23:34:51 +01:00
parent 7d3d46c505
commit 27cce159a4
4 changed files with 40 additions and 44 deletions

View File

@ -24,10 +24,10 @@ namespace OSMDatastructure
return latHash * offset + lonHash;
}
public ulong GetRegionHash(float regionSize)
public ulong GetRegionHash()
{
float latRegion = this.lat - this.lat % regionSize;
float lonRegion = this.lon - this.lon % regionSize;
float latRegion = this.lat - this.lat % Region.regionSize;
float lonRegion = this.lon - this.lon % Region.regionSize;
ulong latHash = Convert.ToUInt64((latRegion + 90) * decimalCoordsSave);
ulong lonHash = Convert.ToUInt64((lonRegion + 180) * decimalCoordsSave);
return latHash * offset + lonHash;

View File

@ -2,17 +2,18 @@ namespace OSMDatastructure
{
public class Region
{
public const float regionSize = 0.01f;
private readonly Dictionary<ulong, Node> _nodesInRegion = new();
public ulong regionHash { get; }
public Region(Coordinates regionCoordinates, float regionSize)
public Region(Coordinates regionCoordinates)
{
this.regionHash = regionCoordinates.GetRegionHash(regionSize);
this.regionHash = regionCoordinates.GetRegionHash();
}
public Region(ulong nodeId, Node firstNode, float regionSize)
public Region(ulong nodeId, Node firstNode)
{
this.regionHash = firstNode.GetRegionHash(regionSize);
this.regionHash = firstNode.GetRegionHash();
this._nodesInRegion.Add(nodeId, value: firstNode);
}
@ -31,5 +32,10 @@ namespace OSMDatastructure
return this._nodesInRegion;
}
public Node? GetNode(ulong id)
{
return this._nodesInRegion.ContainsKey(id) ? this._nodesInRegion[id] : null;
}
}
}

View File

@ -8,7 +8,6 @@ namespace OSMImporter
public static class Importer
{
public const float regionSize = 0.01f;
private static readonly XmlReaderSettings readerSettings = new()
{
IgnoreWhitespace = true,
@ -30,10 +29,10 @@ namespace OSMImporter
Dictionary<ulong, Node?> nodes = ReturnNodeIdDictionary(XmlReader.Create(xmlFileStream, readerSettings));
xmlFileStream.Position = 0;
RegionManager regionManager = new RegionManager();
RegionStruct regionStruct = new RegionStruct();
Console.WriteLine("Reading nodes...");
LoadNodesIntoDictionary(ref nodes, XmlReader.Create(xmlFileStream, readerSettings), ref regionManager);
LoadNodesIntoDictionary(ref nodes, XmlReader.Create(xmlFileStream, readerSettings), ref regionStruct);
xmlFileStream.Position = 0;
@ -42,7 +41,7 @@ namespace OSMImporter
Console.WriteLine("Writing...");
foreach(Region region in regionManager.GetAllRegions())
foreach(Region region in regionStruct.GetAllRegions())
WriteRegion(region, outputFolderPath);
}
@ -79,7 +78,7 @@ namespace OSMImporter
return retSet;
}
private static void LoadNodesIntoDictionary(ref Dictionary<ulong, Node?> nodes, XmlReader xmlReader, ref RegionManager regionManager)
private static void LoadNodesIntoDictionary(ref Dictionary<ulong, Node?> nodes, XmlReader xmlReader, ref RegionStruct regionStruct)
{
while (xmlReader.ReadToFollowing("node"))
{
@ -90,7 +89,7 @@ namespace OSMImporter
float lon = Convert.ToSingle(xmlReader.GetAttribute("lon")!, coordinateFormat);
Node newNode = new Node(lat, lon);
nodes[id] = newNode;
regionManager.GetRegion(newNode).AddNode(id, newNode);
regionStruct.GetRegion(newNode).AddNode(id, newNode);
}
}
xmlReader.Close();
@ -182,4 +181,26 @@ namespace OSMImporter
fileStream.Close();
}
}
internal class RegionStruct
{
private readonly Dictionary<ulong, Region> _regions = new();
public Region GetRegion(Coordinates coordinates)
{
if(this._regions.ContainsKey(coordinates.GetRegionHash()))
return this._regions[coordinates.GetRegionHash()];
else
{
Region newRegion = new Region(coordinates);
this._regions.Add(newRegion.regionHash, value: newRegion);
return newRegion;
}
}
public Region[] GetAllRegions()
{
return this._regions.Values.ToArray();
}
}
}

View File

@ -1,31 +0,0 @@
using OSMDatastructure;
namespace OSMImporter
{
public class RegionManager
{
private readonly Dictionary<ulong, Region> _regions;
public RegionManager()
{
this._regions = new Dictionary<ulong, Region>();
}
public Region GetRegion(Coordinates coordinates)
{
if(this._regions.ContainsKey(coordinates.GetRegionHash(Importer.regionSize)))
return this._regions[coordinates.GetRegionHash(Importer.regionSize)];
else
{
Region newRegion = new Region(coordinates, Importer.regionSize);
this._regions.Add(newRegion.regionHash, value: newRegion);
return newRegion;
}
}
public Region[] GetAllRegions()
{
return this._regions.Values.ToArray();
}
}
}