OSMServer/OSMDatastructure/Region.cs
2023-04-01 18:08:59 +02:00

65 lines
1.7 KiB
C#

using System.Runtime.Serialization.Formatters.Binary;
using OSMDatastructure.Graph;
namespace OSMDatastructure;
[Serializable]
public class Region
{
[NonSerialized]public const float RegionSize = 0.025f;
public readonly HashSet<OsmNode> nodes = new();
public ulong regionHash { get; }
public TagManager tagManager { get; }
public Region(ulong regionHash)
{
this.regionHash = regionHash;
tagManager = new TagManager();
}
public Region(Coordinates coordinates)
{
regionHash = Coordinates.GetRegionHashCode(coordinates);
tagManager = new TagManager();
}
public bool ContainsNode(ulong id)
{
return nodes.Any(node => node.nodeId == id);
}
public bool ContainsNode(Coordinates coordinates)
{
return nodes.Any(node => node.coordinates.Equals(coordinates));
}
public OsmNode? GetNode(ulong id)
{
if (ContainsNode(id))
return nodes.First(node => node.nodeId == id);
else return null;
}
public OsmNode? GetNode(Coordinates coordinates)
{
if (ContainsNode(coordinates))
return nodes.First(node => node.coordinates.Equals(coordinates));
else return null;
}
public static Region? FromFile(string filePath)
{
BinaryFormatter bFormatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011
if (File.Exists(filePath))
return (Region)bFormatter.Deserialize(new FileStream(filePath, FileMode.Open));
#pragma warning restore SYSLIB0011
else return null;
}
public static Region? FromId(string path, ulong regionId)
{
string filePath = Path.Join(path, $"{regionId}.region");
return FromFile(filePath);
}
}