2023-04-01 13:18:54 +02:00
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
2023-03-14 17:00:59 +01:00
|
|
|
using OSMDatastructure.Graph;
|
|
|
|
|
2023-02-06 17:32:55 +01:00
|
|
|
namespace OSMDatastructure;
|
2023-02-02 22:30:43 +01:00
|
|
|
|
2023-03-30 18:24:57 +02:00
|
|
|
[Serializable]
|
2023-02-06 17:32:55 +01:00
|
|
|
public class Region
|
|
|
|
{
|
2023-03-31 21:54:32 +02:00
|
|
|
[NonSerialized]public const float RegionSize = 0.1f;
|
2023-02-06 17:32:55 +01:00
|
|
|
public readonly HashSet<OsmNode> nodes = new();
|
2023-03-30 18:24:57 +02:00
|
|
|
public ulong regionHash { get; }
|
|
|
|
public TagManager tagManager { get; }
|
2023-02-03 21:13:51 +01:00
|
|
|
|
2023-03-30 18:24:57 +02:00
|
|
|
public Region(ulong regionHash)
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
|
|
|
this.regionHash = regionHash;
|
2023-03-30 18:24:57 +02:00
|
|
|
tagManager = new TagManager();
|
2023-02-06 17:32:55 +01:00
|
|
|
}
|
2023-02-05 20:02:22 +01:00
|
|
|
|
2023-03-14 17:00:59 +01:00
|
|
|
public Region(Coordinates coordinates)
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
2023-03-14 17:00:59 +01:00
|
|
|
regionHash = Coordinates.GetRegionHashCode(coordinates);
|
2023-03-30 18:24:57 +02:00
|
|
|
tagManager = new TagManager();
|
2023-02-06 17:32:55 +01:00
|
|
|
}
|
2023-02-05 20:02:22 +01:00
|
|
|
|
2023-04-01 13:18:54 +02:00
|
|
|
public bool ContainsNode(ulong id)
|
2023-02-06 17:32:55 +01:00
|
|
|
{
|
2023-04-01 13:18:54 +02:00
|
|
|
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);
|
2023-02-02 22:30:43 +01:00
|
|
|
}
|
|
|
|
}
|