/// <summary> /// Given a station it will optimise the station name and encode it to a url format. /// It then uses the Google geocode service to grab the address and gps coordinates. /// </summary> /// <param name="Station">Station</param> /// <returns>GeoCoding result</returns> public GoogleGeoCodeResponse GeoCodeStation(Station Station) { // Trim the station name so only relevant key words are geocoded int index = Station.Name.ToLower().LastIndexOf("station"); index += 7; var name = Station.Name.Substring(0, index); name += " Victoria"; // Build a Url formatted string and send to the Google geocode service var query = "http://maps.googleapis.com/maps/api/geocode/json?address=" + HttpUtility.UrlEncode(name) + "&sensor=false"; var result = new System.Net.WebClient().DownloadString(query); // Deserialize the Json reponse into the appropriate classes in GoogleModels JavaScriptSerializer js = new JavaScriptSerializer(); return (GoogleGeoCodeResponse)js.Deserialize<GoogleGeoCodeResponse>(result); }
/// <summary> /// Reads the html source code of the PTA train time table and extracts /// all relevant data, including, lines, stations times and gps coordinates. /// </summary> /// <param name="Source"></param> /// <returns>Returns a TimeTable object that encapsulates all info extracted</returns> public TimeTable ParseTrainTimeTable(string Source) { // Set up the HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(Source); // Access required information by targeting div elements with unique ids var stations = htmlDocument.DocumentNode.SelectNodes("//div[@id='ttMargin']"); var times = htmlDocument.DocumentNode.SelectNodes("//div[@id='ttBody']"); var line = htmlDocument.DocumentNode.SelectNodes("//div[@id='ttHeadline']"); TimeTable timeTable = new TimeTable(); GeoCode geoCoder = new GeoCode(); // Set Line name timeTable.Line = line.First().FirstChild.FirstChild.InnerText; Station station; // Loop through all the ChildNodes (i.e. All the stations) for (int i = 0; i < stations.First().ChildNodes.Count; i++) { // Create a new station and add it to the list timeTable.Stations.Add(station = new Station { Name = stations.First().ChildNodes[i].ChildNodes[1].FirstChild.InnerText, }); // Set location information station.Geo = geoCoder.GeoCodeStation(station); // Loop through all the ChildNodes (i.e. All the times) and add the time to the station for (int j = 0; j < times.First().ChildNodes[i + 1].ChildNodes.Count; j++) station.Times.Add(j, times.First().ChildNodes[i + 1].ChildNodes[j].FirstChild.InnerText); } return timeTable; }
public int InsertStation(int Id, Station Station) { station station = new station { line_id = Id, name = Station.Name }; _ctx.AddTostations(station); _ctx.SaveChanges(); return station.id; }