static void addAtomsToRoute(Route route, AtomGenerator generator, int count) { for (int i = 0; i < count; i++) { double scatterLength = 0.0000; double offset = Util.rand.NextDouble(); double firstLegDeltaX = route.routePoints[1].x - route.routePoints[0].x; double firstLegDeltaY = route.routePoints[1].y - route.routePoints[0].y; double length = Math.Sqrt(firstLegDeltaX * firstLegDeltaX + firstLegDeltaY * firstLegDeltaY); double scatterX = (scatterLength / length) * firstLegDeltaX; double scatterY = (scatterLength / length) * firstLegDeltaY; AtomObject atom = new AtomObject(route.name + i, 0, route.routePoints[0].x - scatterX*offset, route.routePoints[0].y - scatterY*offset); // generate new random number for start time - for now between 0:01 to 1:30 int minutes = Util.rand.Next(2); int seconds = Util.rand.Next(1, 60); int speed = Util.rand.Next(3, 11); String secondsString = seconds >= 10 ? seconds.ToString() : "0" + seconds; Activity activity = new Activity(100 + i, atom.guid, 1, 1, "00:0" + minutes + ":" + secondsString, "00:00:01", speed, route.guid, route.routePoints[0].x, route.routePoints[0].y); generator.createAtom(atom); generator.createActivityToAtom(activity, atom); generator.addAtomToTreeObject(atom); } }
public List<Route> readAllRoutes() { List<Route> routes = new List<Route>(); NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM routes", connection); NpgsqlDataReader reader = command.ExecuteReader(); // read the routes themselves while (reader.Read()) { Route route = new Route(); route.routePoints = new List<DPoint>(); route.guid = (reader[0] == DBNull.Value) ? null : (String)reader[0]; route.name = (reader[1] == DBNull.Value) ? null : (String)reader[1]; route.countryId = (reader[2] == DBNull.Value) ? -1 : (int)reader[2]; route.routeTypeId = (reader[3] == DBNull.Value) ? -1 : (int)reader[3]; route.owner = (reader[4] == DBNull.Value) ? null : (String)reader[4]; routes.Add(route); } reader.Close(); // now for all route add his points foreach (Route route in routes) { command.CommandText = "SELECT * FROM routes_points WHERE route_guid=:guid"; command.Parameters.Add(new NpgsqlParameter("guid", route.guid)); reader = command.ExecuteReader(); while (reader.Read()) { DPoint point = new DPoint((double)reader[2], (double)reader[3]); route.routePoints.Add(point); } reader.Close(); } return routes; }
public async Task<Route> generateRouteByShortestPath(String routeName, DPoint source, DPoint dest) { // assumption: source and dest are on a sidewalk shPath path = await RouteWebAPI.FindShortPath("0", source.x, source.y, dest.x, dest.y, false); Route route = new Route(); route.countryId = 0; route.guid = Util.CreateGuid(); route.name = routeName; route.owner = null; route.routeTypeId = 0; route.routePoints = new List<DPoint>(); foreach (shPoint point in path.Points) { route.routePoints.Add(new DPoint(point.x, point.y)); } return route; }
public void saveRouteToDB(Route route) { // add the route itself NpgsqlCommand addRouteCommand = new NpgsqlCommand("INSERT INTO routes(route_guid, route_name, countryid, routetypeid, owner) VALUES (:guid, :name, :countryId, :typeid, :owner)", connection); addRouteCommand.Parameters.Add(new NpgsqlParameter("guid", route.guid)); addRouteCommand.Parameters.Add(new NpgsqlParameter("name", route.name)); addRouteCommand.Parameters.Add(new NpgsqlParameter("countryId", route.countryId)); addRouteCommand.Parameters.Add(new NpgsqlParameter("typeid", route.routeTypeId)); addRouteCommand.Parameters.Add(new NpgsqlParameter("owner", route.owner != null ? route.owner : "")); addRouteCommand.ExecuteNonQuery(); // add its points for (int i = 0; i < route.routePoints.Count; i++) { String query = "INSERT INTO routes_points(route_guid, point_num, pointx, pointy)" + " VALUES (:guid, :point_num, :x, :y)"; NpgsqlCommand addRoutePointsCommand = new NpgsqlCommand(query, connection); addRoutePointsCommand.Parameters.Add(new NpgsqlParameter("guid", route.guid)); addRoutePointsCommand.Parameters.Add(new NpgsqlParameter("point_num", i)); addRoutePointsCommand.Parameters.Add(new NpgsqlParameter("x", route.routePoints[i].x)); addRoutePointsCommand.Parameters.Add(new NpgsqlParameter("y", route.routePoints[i].y)); addRoutePointsCommand.ExecuteNonQuery(); } }
public void generateReversedRoute(String routeName, String reversedRouteName) { RoutesReader routesReader = new RoutesReader(connection); Route route = routesReader.readRouteByName(routeName); Route reversedRoute = new Route(); reversedRoute.guid = Util.CreateGuid(); reversedRoute.name = reversedRouteName; reversedRoute.owner = route.owner; reversedRoute.routeTypeId = route.routeTypeId; reversedRoute.routePoints = new List<DPoint>(); // copy points to reversed route foreach (DPoint point in route.routePoints) { reversedRoute.routePoints.Add(point); } // reverse route points reversedRoute.routePoints.Reverse(); //after reading route generate a new GUID for it, change its name and reverse its route points saveRouteToDB(reversedRoute); }
public Route readRouteByName(String name) { NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM routes WHERE route_name=:name", connection); command.Parameters.Add(new NpgsqlParameter("name", name)); NpgsqlDataReader reader = command.ExecuteReader(); // get route (if there is one, if not YOU GET NOTHING! DEAL WITH IT!) if (!reader.Read()) { reader.Close(); return null; } Route route = new Route(); route.routePoints = new List<DPoint>(); route.guid = (reader[0] == DBNull.Value) ? null : (String)reader[0]; route.name = (reader[1] == DBNull.Value) ? null : (String)reader[1]; route.countryId = (reader[2] == DBNull.Value) ? -1 : (int)reader[2]; route.routeTypeId = (reader[3] == DBNull.Value) ? -1 : (int)reader[3]; route.owner = (reader[4] == DBNull.Value) ? null : (String)reader[4]; reader.Close(); // get route points command.CommandText = "SELECT * FROM routes_points WHERE route_guid=:guid"; command.Parameters.Add(new NpgsqlParameter("guid", route.guid)); reader = command.ExecuteReader(); while (reader.Read()) { DPoint point = new DPoint((double)reader[2], (double)reader[3]); route.routePoints.Add(point); } reader.Close(); return route; }
public List<Route> readRoutesStartingWith(String name) { List<Route> routes = new List<Route>(); NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM routes WHERE route_name LIKE :name", connection); command.Parameters.AddWithValue(":name", name + "%"); NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Route route = new Route(); route.routePoints = new List<DPoint>(); route.guid = (reader[0] == DBNull.Value) ? null : (String)reader[0]; route.name = (reader[1] == DBNull.Value) ? null : (String)reader[1]; route.countryId = (reader[2] == DBNull.Value) ? -1 : (int)reader[2]; route.routeTypeId = (reader[3] == DBNull.Value) ? -1 : (int)reader[3]; route.owner = (reader[4] == DBNull.Value) ? null : (String)reader[4]; routes.Add(route); } reader.Close(); foreach (Route route in routes) { // get route points command.CommandText = "SELECT * FROM routes_points WHERE route_guid=:guid"; command.Parameters.Add(new NpgsqlParameter("guid", route.guid)); reader = command.ExecuteReader(); while (reader.Read()) { DPoint point = new DPoint((double)reader[2], (double)reader[3]); route.routePoints.Add(point); } reader.Close(); } return routes; }