Initial commit

This commit is contained in:
C9Glax 2023-02-02 19:03:00 +01:00
commit f7edc288c1
15 changed files with 372 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

13
.idea/.idea.OSMServer/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
# Default ignored files
/shelf/
/workspace.xml
# Rider ignored files
/projectSettingsUpdater.xml
/contentModel.xml
/modules.xml
/.idea.OSMServer.iml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

42
Importer/Connection.cs Normal file
View File

@ -0,0 +1,42 @@
namespace OSMServer;
public class Connection
{
public Coordinates end { get; }
private Dictionary<string, string> tags;
public Connection(Coordinates end)
{
this.end = end;
this.tags = new Dictionary<string, string>();
}
public Connection(Coordinates end, Dictionary<string, string> tags)
{
this.end = end;
this.tags = tags;
}
public bool AddTag(string key, string value)
{
return this.tags.TryAdd(key, value);
}
public string? TryGetTagValue(string key)
{
return this.tags.ContainsKey(key) ? this.tags[key] : null;
}
public bool ContainsTag(string key)
{
return this.tags.ContainsKey(key);
}
public void UpdateTag(string key, string value)
{
if(!this.AddTag(key, value))
this.tags[key] = value;
}
}

27
Importer/Coordinates.cs Normal file
View File

@ -0,0 +1,27 @@
using System.Diagnostics;
namespace OSMServer;
public class Coordinates
{
public float lat { get; }
public float lon { get; }
public Coordinates(float lat, float lon)
{
this.lat = lat;
this.lon = lon;
}
public uint GetHash()
{
return Convert.ToUInt32((this.lat * 10000f + this.lon) * 10000f);
}
public uint GetRegionHash()
{
float latRegion = this.lat % Importer.regionSize;
float lonRegion = this.lon % Importer.regionSize;
return new Coordinates(latRegion, lonRegion).GetHash();
}
}

138
Importer/Importer.cs Normal file
View File

@ -0,0 +1,138 @@
using System.Xml;
using Newtonsoft.Json;
namespace OSMServer;
public class Importer
{
public const float regionSize = 0.01f;
public static void Split(string xmlFilePath, string outputFolderPath)
{
if (!File.Exists(xmlFilePath))
throw new FileNotFoundException();
FileStream xmlFileStream = File.OpenRead(xmlFilePath);
HashSet<ulong> nodesToImport = ReturnGraphNodes(XmlReader.Create(xmlFileStream));
xmlFileStream.Position = 0;
XmlReader xmlReader = XmlReader.Create(xmlFileStream);
Dictionary<ulong, Node> nodes = new Dictionary<ulong, Node>();
RegionManager regionManager = new RegionManager();
while (xmlReader.ReadToFollowing("node"))
{
ulong id = Convert.ToUInt64(xmlReader.GetAttribute("id"));
if (nodesToImport.Contains(id))
{
float lat = Convert.ToSingle(xmlReader.GetAttribute("lat")!.Replace('.', ','));
float lon = Convert.ToSingle(xmlReader.GetAttribute("lon")!.Replace('.', ','));
Node newNode = new Node(lat, lon);
nodes.Add(id, newNode);
regionManager.GetRegion(newNode).AddNode(id, newNode);
}
}
xmlReader.Close();
xmlFileStream.Position = 0;
xmlReader = XmlReader.Create(xmlFileStream);
while (xmlReader.ReadToFollowing("way"))
{
XmlReader wayReader = xmlReader.ReadSubtree();
HashSet<ulong> nodesInWay = new HashSet<ulong>();
Dictionary<string, string> tags = new Dictionary<string, string>();
while (wayReader.Read())
{
if (wayReader.IsStartElement() && wayReader.Name.Equals("nd"))
{
nodesInWay.Add(Convert.ToUInt64(wayReader.GetAttribute("ref")));
}
else if (wayReader.IsStartElement() && wayReader.Name.Equals("tag"))
{
tags.Add(wayReader.GetAttribute("k")!, wayReader.GetAttribute("v")!);
}
}
if (tags.ContainsKey("highway"))
{
AddConnections(nodes, nodesInWay.ToArray(), tags);
}
}
foreach(Region region in regionManager.GetAllRegions())
WriteRegion(region, outputFolderPath);
}
private static HashSet<ulong> ReturnGraphNodes(XmlReader xmlReader)
{
HashSet<ulong> retSet = new HashSet<ulong>();
while (xmlReader.ReadToFollowing("way"))
{
XmlReader wayReader = xmlReader.ReadSubtree();
HashSet<ulong> tmpList = new HashSet<ulong>();
bool addNodes = false;
while (wayReader.Read())
{
if (wayReader.IsStartElement())
{
if (wayReader.Name.Equals("nd"))
{
tmpList.Add(Convert.ToUInt64(wayReader.GetAttribute("ref")));
}
else if (wayReader.Name.Equals("tag"))
{
if (wayReader.GetAttribute("v")!.Equals("highway"))
addNodes = true;
}
}
}
if(addNodes)
retSet.UnionWith(tmpList);
}
return retSet;
}
private static void AddConnections(Dictionary<ulong, Node> nodes, ulong[] nodeIds,
Dictionary<string, string> tags)
{
string oneWayString = tags.ContainsKey("oneway") ? tags["oneway"] : "no";
bool oneWay = false;
bool forward = true;
switch (oneWayString)
{
case "yes":
oneWay = true;
break;
case "-1":
forward = false;
break;
}
for (int i = 0; i < nodeIds.Length - 1; i++)
{
if (oneWay)
{
nodes[nodeIds[i]].AddConnection(new Connection(nodes[nodeIds[i + 1]], tags));
nodes[nodeIds[i + 1]].AddConnection(new Connection(nodes[nodeIds[i]], tags));
}
else if (forward)
{
nodes[nodeIds[i]].AddConnection(new Connection(nodes[nodeIds[i + 1]], tags));
}
else
{
nodes[nodeIds[i + 1]].AddConnection(new Connection(nodes[nodeIds[i]], tags));
}
}
}
private static void WriteRegion(Region region, string outputFolderPath)
{
if (!Directory.Exists(outputFolderPath))
Directory.CreateDirectory(outputFolderPath);
string jsonString = JsonConvert.SerializeObject(region);
string fileName = region.regionHash.ToString();
File.WriteAllText(Path.Combine(outputFolderPath, fileName), jsonString);
}
}

14
Importer/Importer.csproj Normal file
View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>OSMServer</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
</Project>

21
Importer/Node.cs Normal file
View File

@ -0,0 +1,21 @@
namespace OSMServer;
public class Node : Coordinates
{
private HashSet<Connection> connections = new HashSet<Connection>();
public Node(float lat, float lon) : base(lat, lon)
{
}
public void AddConnection(Connection connection)
{
this.connections.Add(connection);
}
public Connection[] GetConnections()
{
return this.connections.ToArray();
}
}

33
Importer/Region.cs Normal file
View File

@ -0,0 +1,33 @@
namespace OSMServer;
public class Region
{
private Dictionary<ulong, Node> nodesInRegion = new Dictionary<ulong, Node>();
public uint regionHash;
public Region(uint regionHash)
{
this.regionHash = this.regionHash;
}
public Region(Coordinates regionCoordinates)
{
this.regionHash = regionCoordinates.GetRegionHash();
}
public Region(ulong nodeId, Node firstNode)
{
this.regionHash = firstNode.GetRegionHash();
this.nodesInRegion.Add(nodeId, firstNode);
}
public void AddNode(ulong nodeId, Node node)
{
this.nodesInRegion.Add(nodeId, node);
}
public Node[] GetNodes()
{
return this.nodesInRegion.Values.ToArray();
}
}

28
Importer/RegionManager.cs Normal file
View File

@ -0,0 +1,28 @@
namespace OSMServer;
public class RegionManager
{
private Dictionary<uint, Region> regions;
public RegionManager()
{
this.regions = new Dictionary<uint, Region>();
}
public Region GetRegion(Coordinates coordinates)
{
if(this.regions.ContainsKey(coordinates.GetRegionHash()))
return this.regions[coordinates.GetRegionHash()];
else
{
Region newRegion = new Region(coordinates.GetRegionHash());
this.regions.Add(newRegion.regionHash, newRegion);
return newRegion;
}
}
public Region[] GetAllRegions()
{
return this.regions.Values.ToArray();
}
}

16
OSMServer.sln Normal file
View File

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OSMServer", "OSMServer\OSMServer.csproj", "{BE5C111D-AD61-433A-AE7E-F02B78551347}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BE5C111D-AD61-433A-AE7E-F02B78551347}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE5C111D-AD61-433A-AE7E-F02B78551347}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE5C111D-AD61-433A-AE7E-F02B78551347}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE5C111D-AD61-433A-AE7E-F02B78551347}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=_002Fhome_002Fglax_002FRiderProjects_002FGeo_002DGraph_002FGeo_002DGraph_002Fobj_002FDebug_002Fnet6_002E0_002FGeo_002DGraph_002Edll/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

9
Server/Server.cs Normal file
View File

@ -0,0 +1,9 @@
namespace OSMServer;
public class Server
{
public Server()
{
Importer.Split("/home/glax/Downloads/bayern-latest.osm.bz2", "/home/glax/Downloads/split");
}
}

10
Server/Server.csproj Normal file
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>