Skip to content

Commit

Permalink
Funkcje przeksztalcajace pliki z danymi dla Staszka,
Browse files Browse the repository at this point in the history
stworzenie slownika na abstract linki,
lekki refactoring kodu.
  • Loading branch information
snakoneczny committed May 18, 2013
1 parent edf96e4 commit 3247ddf
Show file tree
Hide file tree
Showing 8 changed files with 123,204 additions and 54,998 deletions.
220 changes: 209 additions & 11 deletions Code/Graph/CityGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@ namespace Graph
{
public class CityGraph
{
// AdjecencyGraph from QuickGraph library
private AdjacencyGraph<MyNode, MyEdge> graph;

// dictionary with which you can get node id based on node openid
private Dictionary<string, int> idDictionary = new Dictionary<string, int>();

// dictionary of middle points belonging to links
Dictionary<int, List<int>> linksDictionary = new Dictionary<int, List<int>>();

public CityGraph(String path)
public CityGraph(String graphPath, String linksPath)
{
createGraphFromXML(graphPath);
createLinksDictionary(linksPath);
}

// creating graph from XML file
public void createGraphFromXML(String graphPath)
{
int id = 0;
Dictionary<int, double> latitudeDic = new Dictionary<int, double>();
Expand All @@ -27,8 +41,8 @@ public CityGraph(String path)
double lanes = 0;
double avgcard = 0;

XmlTextReader xmlReader = new XmlTextReader(path);
XmlTextReader xmlReader = new XmlTextReader(graphPath);

while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "graph")
Expand All @@ -51,7 +65,7 @@ public CityGraph(String path)
if (xmlReader.GetAttribute("key") == "v_latitude")
{
xmlReader.Read();
latitudeDic[id] = double.Parse(xmlReader.Value.Replace(".",","));
latitudeDic[id] = double.Parse(xmlReader.Value.Replace(".", ","));
}
if (xmlReader.GetAttribute("key") == "v_longitude")
{
Expand All @@ -67,11 +81,11 @@ public CityGraph(String path)
xmlReader.Read();
} //end if
} // end while
graph.AddVertex(new MyNode(id, longitudeDic[id], longitudeDic[id], openIdDic[id]));
graph.AddVertex(new MyNode(id, latitudeDic[id], longitudeDic[id], openIdDic[id]));
idDictionary.Add(openIdDic[id], id);
} //end if
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "edge")
{
if (graph.EdgeCount % 1000 == 0) Console.WriteLine(graph.EdgeCount);
startNode = Int32.Parse(xmlReader.GetAttribute("source").Substring(1));
endNode = Int32.Parse(xmlReader.GetAttribute("target").Substring(1));
while (xmlReader.NodeType != XmlNodeType.EndElement)
Expand All @@ -97,14 +111,198 @@ public CityGraph(String path)
}
xmlReader.Read();
xmlReader.Read();
} //end if
} // end if
} // end while
graph.AddEdge(new MyEdge(
graph.Vertices.ElementAt(startNode),
graph.Vertices.ElementAt(endNode),
distance, lanes, avgcard));
} //end if
} // end if
} // end while
}
}
}
} // end function

// creating dictionary of middle points on links
public void createLinksDictionary(String sourcePath)
{
List<int> links = new List<int>(); // list of nodes from one link

string line; // one line of rebuild file
string[] elements; // elements of that line
int actualIndex = 1; // index of actual link from file
int actualTime = 1; // time of actualIndex

FileStream sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader linksReader = new StreamReader(sourceStream);

// first line of description
linksReader.ReadLine();

// read file to it's end
while ((line = linksReader.ReadLine()) != null)
{
elements = line.Split(' ');

// if we are on the next link
if (Int32.Parse(elements[1]) != actualIndex)
{
// add link to dictionary
linksDictionary.Add(actualIndex, links);

// if we have added all links
if (actualTime != Int32.Parse(elements[0]))
break; ;

// clear list
links.Clear();
// add first element of new link
links.Add(Int32.Parse(elements[2]));

// update parameters
actualIndex = Int32.Parse(elements[1]);
actualTime = Int32.Parse(elements[0]);
}

// searching edge from file in the graph
foreach(MyEdge e in graph.OutEdges(graph.Vertices.ElementAt(idDictionary[elements[2]])))
{
if(e.endNode.openid == elements[3])
{
// add new node to our list
links.Add(Int32.Parse(elements[3]));
break;
}
}
} // end while

linksReader.Close();
sourceStream.Close();
} // end function

public void rebuildAvgVelocity(String toRebuild, String outPath)
{
string line; // one line of rebuild file
string[] elements; // elements of that line
int actualIndex = 1; // index of actual link from file
int actualTime = 60; // time of actualIndex
double congestion = 0; // summary congestion at link
double velocity = 0; // summary velocity at link
double weight = 0; // weight(sum of distances) of edges from link

// creating out file
if (File.Exists(outPath))
{
File.Delete(outPath);
}
FileStream tmp = File.Create(outPath);
tmp.Close();

FileStream sourceStream = new FileStream(toRebuild, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader linksReader = new StreamReader(sourceStream);

FileStream outStream = new FileStream(outPath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
StreamWriter linksWriter = new StreamWriter(outStream);

// first line of description
linksReader.ReadLine();
linksWriter.WriteLine("Time Name Velocity NrOfCars");

// read file to it's end
while ((line = linksReader.ReadLine()) != null)
{
elements = line.Split(' ');

// if we are on the next link
if (Int32.Parse(elements[1]) != actualIndex)
{
linksWriter.WriteLine(actualTime + " " + actualIndex + " " + Math.Round(velocity / weight, 3) + " " +
Math.Round(congestion / weight, 3));
congestion = velocity = weight = 0;

actualIndex = Int32.Parse(elements[1]);
actualTime = Int32.Parse(elements[0]);
}

// searching edge from file in the graph
foreach(MyEdge e in graph.OutEdges(graph.Vertices.ElementAt(idDictionary[elements[2]])))
{
if(e.endNode.openid == elements[3])
{
// update parameters
congestion += e.distance * Double.Parse(elements[5]);
velocity += e.distance * Double.Parse(elements[4]);
weight += e.distance;
break;
}
}
} // end while

linksReader.Close();
sourceStream.Close();

linksWriter.Close();
outStream.Close();
} // end function

public void rebuildCongestion(String toRebuild, String outPath)
{
string line; // one line of rebuild file
string[] elements; // elements of that line
int actualIndex = 1; // index of actual link from file
int actualTime = 1; // time of actualIndex
double congestion = 0; // summary congestion at link
double weight = 0; // weight(sum of distances) of edges from link

// creating out file
if (File.Exists(outPath))
{
File.Delete(outPath);
}
FileStream tmp = File.Create(outPath);
tmp.Close();

FileStream sourceStream = new FileStream(toRebuild, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader linksReader = new StreamReader(sourceStream);

FileStream outStream = new FileStream(outPath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
StreamWriter linksWriter = new StreamWriter(outStream);

// first line of description
linksReader.ReadLine();
linksWriter.WriteLine("Time(minutes) Name NrOfCars");

// read file to it's end
while ((line = linksReader.ReadLine()) != null)
{
elements = line.Split(' ');

// if we are on the next link
if (Int32.Parse(elements[1]) != actualIndex)
{
linksWriter.WriteLine(actualTime + " " + actualIndex + " " + Math.Round(congestion / weight, 3));
congestion = weight = 0;

actualIndex = Int32.Parse(elements[1]);
actualTime = Int32.Parse(elements[0]);
}

// searching edge from file in the graph
foreach(MyEdge e in graph.OutEdges(graph.Vertices.ElementAt(idDictionary[elements[2]])))
{
if(e.endNode.openid == elements[3])
{
// update parameters
congestion += e.distance * Double.Parse(elements[4]);
weight += e.distance;
break;
}
}
} // end while

linksReader.Close();
sourceStream.Close();

linksWriter.Close();
outStream.Close();
} // end function
} // end class
} // end namespace
4 changes: 2 additions & 2 deletions Code/Graph/MyEdge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Graph
public class MyEdge : IEdge<MyNode>
{
private MyNode startNode { get; set; }
private MyNode endNode { get; set; }
private double distance { get; set; }
public MyNode endNode { get; set; }
public double distance { get; set; }
private double lanes { get; set; }
private double avgcard { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Code/Graph/MyNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MyNode
private int id { get; set; }
private double latitude { get; set; }
private double longitude { get; set; }
private string openid { get; set; }
public string openid { get; set; }

public MyNode(int new_id)
{
Expand Down
Loading

0 comments on commit 3247ddf

Please sign in to comment.