/// <summary> /// Return shortsest path betweent 2 nodes /// </summary> public List<Node> getShortestPath(Node start, Node finish) { int startID = database.getNearestPoint(start).id; int finishID = database.getNearestPoint(finish).id; dijkstra(startID, finishID); return buildPath(startID, finishID); }
public List<Node> getNodes() { string commandStr = "SELECT * FROM NODES;"; SqlCommand command = new SqlCommand(commandStr, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); List<Node> res = new List<Node>(); while (reader.Read()) { Node curr = new Node(reader.GetDouble(1), reader.GetDouble(2)); curr.id = reader.GetInt32(0); try { curr.prior = reader.GetInt32(3); } catch { curr.prior = 0; } try { curr.zone = reader.GetInt32(4); } catch { curr.zone = 0; } res.Add(curr); } connection.Close(); return res; }
/// <summary> /// Returns nearest point on road in database /// </summary> public Node getNearestPoint(Node curr) { List<Node> nodes = getNodes(); Node res = null; double dist = double.MaxValue; foreach (Node node in nodes) { if (node.prior == 0) continue; double currDist = countDist(curr, node); if (currDist < dist) { res = node; dist = currDist; } } return res; }
public Node getNode(int id) { string commandStr = "SELECT * FROM NODES WHERE id = " + id.ToString() + ";"; SqlCommand command = new SqlCommand(commandStr, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); Node res = null; if (reader.Read()) { res = new Node(reader.GetDouble(1), reader.GetDouble(2)); res.id = reader.GetInt32(0); try { res.prior = reader.GetInt32(3); } catch { res.prior = 0; } try { res.zone = reader.GetInt32(4); } catch { res.zone = 0; } } connection.Close(); return res; }
private double countDist(Node st, Node ed) { double toRad = Math.PI / 180; double lat1 = st.lat * toRad, lng1 = st.lon * toRad, lat2 = ed.lat * toRad, lng2 = ed.lon * toRad; double temp = Math.Sin((lat2 - lat1) / 2); temp *= temp; temp += Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin((lng2 - lng1) / 2) * Math.Sin((lng2 - lng1) / 2); temp = Math.Sqrt(temp); temp = 2 * Math.Asin(temp); return temp * 6372795; }
public Node getNodeByAdress(Address addr) { string commandStr = String.Format("select * from NODES join ADDRESS on NODES.id = ADDRESS.id_node where ADDRESS.id_street = {0}", addr.id_street); if (addr.h_num != -1) commandStr += " and ADDRESS.h_num = " + addr.h_num.ToString(); SqlCommand command = new SqlCommand(commandStr, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); Node res = null; if (reader.Read()) { res = new Node(reader.GetDouble(1), reader.GetDouble(2)); res.id = reader.GetInt32(0); try { res.prior = reader.GetInt32(3); } catch { res.prior = 0; } try { res.zone = reader.GetInt32(4); } catch { res.zone = 0; } } connection.Close(); return res; }