using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Drawing; using System.Text; using System.IO; using MasterEdit; public partial class _Default : System.Web.UI.Page { public locationList listLocation = new locationList(); List cLocation= new List(); protected void Page_Load(object sender, EventArgs e) { //read location data TextReader fileReader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv"); string line = fileReader.ReadLine(); string[] bits; if (line == null) { bits = "".Split(','); } else { bits = line.Split(','); } while (bits[0] != "") { if(bits.Length <6) addLocation(bits[0], bits[1], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4]), bits[2]); line = fileReader.ReadLine(); if (line == null) { bits = "".Split(','); } else { bits = line.Split(','); } } fileReader.Close(); lblNotification.Text = ""; if (!IsPostBack){ //empty the .csv file FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv", FileMode.Create); stream.Close(); hdnSetCoorIndex.Value = "Marker"; Page.RegisterStartupScript("myscript", ""); } } protected void btnConfirm_Click(object sender, EventArgs e) { int total = Convert.ToInt16(hdnTotal.Value), errorCount=0; string txtError = "", check; string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv"; StreamWriter writeLocation= new StreamWriter(path, true); for (int i = 1; i <= total; i++) { string name = Request.Form["txtName" + i]; double lat = Convert.ToDouble(Request.Form["hdnLat" + i]); double lng = Convert.ToDouble(Request.Form["hdnLng" + i]); if (name != "" && lat != null && lng != null) { check = checkLocationData(name, lat, lng); if (check == "") { try { string desc = Request.Form["txtDesc" + i]; string type = Request.Form["ddlType" + i]; addLocation(name, desc, lat, lng, type); writeLocation.WriteLine(name + "," + desc + "," + type + "," + lat + "," + lng); ListItem addList = new ListItem(); addList.Text = name; addList.Value = desc + "~" + lat + "~" + lng; if (type == "Time Point") lstTimePoint.Items.Add(addList); else if (type == "Bus Stop") lstBusStop.Items.Add(addList); else if (type == "Interchange") lstInterchange.Items.Add(addList); } catch { writeLocation.Close(); } } else { txtError += check + "
"; errorCount++; } } else { errorCount++; } } writeLocation.Close(); lblNotification.Text = (total - errorCount) + " location(s) is/are added to the list(s).
"; if (txtError != "") lblNotification.Text += "Failed to Add:
" + txtError; drawMarker(""); } public string createUniqueName() { // create a writer and open the file DateTime time = DateTime.Now; string timeConvert = time + ""; timeConvert = timeConvert.Replace("/", ""); timeConvert = timeConvert.Replace(":", ""); timeConvert = timeConvert.Replace(" ", ""); timeConvert = timeConvert.Replace("AM", ""); timeConvert = timeConvert.Replace("PM", ""); return timeConvert; } protected void lnkbtnMap_Click(object sender, EventArgs e) { drawMarker(""); } public void drawMarker(string script) { //find all from listbox and put in map int numTP = lstTimePoint.Items.Count; int numBS = lstBusStop.Items.Count; int numI = lstInterchange.Items.Count; string locationName, javascriptCode = "resetMarker();DrawMap();dragMarker(4,0, '', '');"; for (int i = 0; i < numTP; i++) { locationName = lstTimePoint.Items[i].Text; javascriptCode += addLocationList(locationName,0); } for (int i = 0; i < numBS; i++) { locationName = lstBusStop.Items[i].Text; javascriptCode += addLocationList(locationName,1); } for (int i = 0; i < numI; i++) { locationName = lstInterchange.Items[i].Text; javascriptCode += addLocationList(locationName,2); } Page.RegisterStartupScript("myscript", ""); } public void addLocation(string name, string description, double latitude, double longitude, string type) { MasterEdit.location addLocation = new MasterEdit.location(); addLocation.name = name; addLocation.latitude = latitude; addLocation.longitude = longitude; addLocation.description = description; addLocation.type = type; listLocation.aLocation.Add(addLocation); } public string addLocationList(string locationName, int type) { double lat, lng; MasterEdit.location aLocation; aLocation = listLocation.aLocationAt(locationName); if (aLocation != null) { lat = aLocation.latitude; lng = aLocation.longitude; return "dragMarker(" + type + ", " + (type+1) + "," + lat + " , " + lng + ");"; } else return ""; } protected void btnCreate_Click(object sender, EventArgs e) { if (txtFileName.Text == "") { Response.Write(""); } else { if (tpCount.Text != "0" || bsCount.Text != "0" || iCount.Text != "0") { string fileName = txtFileName.Text + ".csv"; string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileSave"; string pathName = @Path.Combine(path, fileName); createFile(fileName, pathName); string linkTo = Request.ApplicationPath + @"/FileSave/" + fileName; //save the file. Response.Redirect(linkTo); lblNotification.Text = "Filename: " + fileName + " is saved to your download directory."; } else { Response.Write(""); } } drawMarker(""); } public void createFile(string name, string path) { string locationName; MasterEdit.location aLocation; StreamWriter newFile = new StreamWriter(path, true); newFile.WriteLine("Map Location"); newFile.WriteLine(); newFile.WriteLine(); newFile.WriteLine("Name,Address,Type,Latitude,Longitude"); for (int i = 0; i < lstTimePoint.Items.Count; i++) { locationName=lstTimePoint.Items[i].Text; aLocation = listLocation.aLocationAt(locationName); newFile.WriteLine(aLocation.name +"," + aLocation.description +","+aLocation.type+","+aLocation.latitude+","+aLocation.longitude); } for (int i = 0; i < lstBusStop.Items.Count; i++) { locationName = lstBusStop.Items[i].Text; aLocation = listLocation.aLocationAt(locationName); newFile.WriteLine(aLocation.name + "," + aLocation.description + "," + aLocation.type + "," + aLocation.latitude + "," + aLocation.longitude); } for (int i = 0; i < lstInterchange.Items.Count; i++) { locationName = lstInterchange.Items[i].Text; aLocation = listLocation.aLocationAt(locationName); newFile.WriteLine(aLocation.name + "," + aLocation.description + "," + aLocation.type + "," + aLocation.latitude + "," + aLocation.longitude); } newFile.Close(); } protected void btnLoad_Click(object sender, EventArgs e) { try { // Get the HttpFileCollection if (FileUploadSingle.HasFile) { string path = Server.MapPath("~/FileUpload/"); string fileName = Path.GetFileName(FileUploadSingle.PostedFile.FileName); // check for the valid file extension string fileExtension = Path.GetExtension(fileName).ToLower(); if (fileExtension.Equals(".csv")) { // now save the file to the disk FileUploadSingle.SaveAs(path + fileName); string loadpath = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "FileUpload\\"; string error = loadData(fileName, path); deleteFile(fileName, path); lblNotification.Text = "Filename: " + fileName + " is loaded.
"; if (error != "") lblNotification.Text += error; } else { Response.Write(""); } } else { Response.Write(""); } } catch (Exception ex) { Response.Write(""); } drawMarker(""); } public string loadData(string filename, string path) { string check, txtError = ""; try { TextReader fileReader = new StreamReader(path + filename); //clear all in list if (chkMerge.Checked == false) { lstBusStop.Items.Clear(); lstTimePoint.Items.Clear(); lstInterchange.Items.Clear(); listLocation.aLocation.Clear(); //empty the .csv file FileStream stream = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv", FileMode.Create); stream.Close(); } string path2 = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv"; StreamWriter writeLocation = new StreamWriter(path2, true); string title = fileReader.ReadLine(); title = fileReader.ReadLine(); string[] bits = title.Split(','); string line = fileReader.ReadLine(); line = fileReader.ReadLine(); string[] info = line.Split(','); if (info[0] == "Name" && info[1] == "Address" && info[2] == "Type" && info[3] == "Latitude" && info[4] == "Longitude") { string line2 = fileReader.ReadLine(); bits = line2.Split(','); while (bits[0] != "") { check = checkLocationData(bits[0], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4])); if (check == "") { ListItem addList = new ListItem(); addList.Text = bits[0]; addList.Value = bits[1] + "~" + bits[3] + "~" + bits[4] ; if (bits[2].ToLower() == "time point") { addLocation(bits[0], bits[1], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4]), bits[2]); lstTimePoint.Items.Add(addList); } else if (bits[2].ToLower() == "bus stop") { addLocation(bits[0], bits[1], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4]), bits[2]); lstBusStop.Items.Add(addList); } else if (bits[2].ToLower() == "interchange") { addLocation(bits[0], bits[1], Convert.ToDouble(bits[3]), Convert.ToDouble(bits[4]), bits[2]); lstInterchange.Items.Add(addList); } writeLocation.WriteLine(bits[0] + "," + bits[1] + "," + bits[2] + "," + Convert.ToDouble(bits[3]) + "," + Convert.ToDouble(bits[4])); } else txtError += check + "
"; //new line line2 = fileReader.ReadLine(); if (line2 != null) { bits = line2.Split(','); } else { bits = "".Split(','); } } try { //loadDataRoute(fileReader); } catch { } //check whether got route list writeLocation.Close(); fileReader.Close(); } else { writeLocation.Close(); fileReader.Close(); try { deleteFile(filename, path); } catch { } Response.Write(""); } } catch { try { deleteFile(filename, path); } catch { } Response.Write(""); } return txtError; } public void deleteFile(string fileName, string path) { //delete file DirectoryInfo directory = new DirectoryInfo(path); foreach (FileInfo file in directory.GetFiles()) { if (file.Name == fileName) { file.Delete(); } } } public string checkLocationData(string locationName, double latitude, double longitude) { MasterEdit.location aLocation; aLocation = listLocation.aLocationAt(locationName); if (aLocation != null) { return "Duplicate Location name: "+ locationName; } aLocation = listLocation.aLocationAt(latitude, longitude); if (aLocation != null) { return "Duplicate Coordinate at Location name: " + locationName; } else return ""; } protected void btnEdit_Click(object sender, EventArgs e) { string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv"; StreamWriter writeLocation = new StreamWriter(path, true); writeLocation.WriteLine(hdnNameTemp.Value + "," + hdnDescTemp.Value + "," + hdnTypeTemp.Value + "," + hdnLatTemp.Value + "," + hdnLngTemp.Value + ",delete"); if (hdnTypeTemp.Value == "Time Point") { lstTimePoint.Items.RemoveAt(Convert.ToInt16(hdnTypeNumTemp.Value)); } else if (hdnTypeTemp.Value == "Bus Stop") { lstBusStop.Items.RemoveAt(Convert.ToInt16(hdnTypeNumTemp.Value)); } else { lstInterchange.Items.RemoveAt(Convert.ToInt16(hdnTypeNumTemp.Value)); } MasterEdit.location aLocation; aLocation = listLocation.aLocationAt(hdnNameTemp.Value); listLocation.aLocation.Remove(aLocation); string txtError = "", check; string name = Request.Form["txtNameEdit"]; double lat = Convert.ToDouble(Request.Form["hdnLatEdit"]); double lng = Convert.ToDouble(Request.Form["hdnLngEdit"]); check = checkLocationData(name, lat, lng); if (check == "") { try { string desc = Request.Form["txtDescEdit"]; string type = Request.Form["ddlTypeEdit"]; addLocation(name, desc, lat, lng, type); writeLocation.WriteLine(name + "," + desc + "," + type + "," + lat + "," + lng); ListItem addList = new ListItem(); addList.Text = name; addList.Value = desc+"~"+lat+"~"+lng; if (type == "Time Point") lstTimePoint.Items.Add(addList); else if (type == "Bus Stop") lstBusStop.Items.Add(addList); else if (type == "Interchange") lstInterchange.Items.Add(addList); } catch { writeLocation.Close(); } } else txtError= check +"
"; writeLocation.Close(); lblNotification.Text = "location: "+name+" is edited.
"; if (txtError != "") lblNotification.Text += "Failed to edit:
" + txtError; drawMarker(""); } protected void btnEditLocation_Click(object sender, EventArgs e) { int index = ddlListType.SelectedIndex,num=0; string locationName = ""; MasterEdit.location aLocation; if (index == 0 && lstTimePoint.SelectedIndex >= 0) { num = lstTimePoint.SelectedIndex; locationName = lstTimePoint.SelectedItem.Text; } else if (index == 1 && lstBusStop.SelectedIndex >= 0) { num = lstBusStop.SelectedIndex; locationName = lstBusStop.SelectedItem.Text; } else if (index == 2 && lstInterchange.SelectedIndex >= 0) { num = lstInterchange.SelectedIndex; locationName = lstInterchange.SelectedItem.Text; } if (locationName != "") { aLocation = listLocation.aLocationAt(locationName); txtNameEdit.Text = aLocation.name; hdnLatEdit.Value = Convert.ToString(aLocation.latitude); hdnLngEdit.Value = Convert.ToString(aLocation.longitude); txtDescEdit.Text = aLocation.description; if (aLocation.type == "Time Point") ddlTypeEdit.SelectedIndex = 0; else if (aLocation.type == "Bus Stop") ddlTypeEdit.SelectedIndex = 1; else ddlTypeEdit.SelectedIndex = 2; hdnNameTemp.Value = aLocation.name; hdnDescTemp.Value = aLocation.description; hdnLatTemp.Value = Convert.ToString(aLocation.latitude); hdnLngTemp.Value = Convert.ToString(aLocation.longitude); hdnTypeTemp.Value = aLocation.type; hdnTypeNumTemp.Value = Convert.ToString(num); drawMarker("editLocation_Click();"); } } protected void btnDeleteLocation_Click(object sender, EventArgs e) { int index = ddlListType.SelectedIndex; string locationName = ""; MasterEdit.location aLocation; if (index == 0 && lstTimePoint.SelectedIndex >= 0) locationName = lstTimePoint.SelectedItem.Text; else if (index == 1 && lstBusStop.SelectedIndex >= 0) locationName = lstBusStop.SelectedItem.Text; else if (index == 2 && lstInterchange.SelectedIndex >= 0) locationName = lstInterchange.SelectedItem.Text; if (locationName != "") { aLocation = listLocation.aLocationAt(locationName); string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\location.csv"; StreamWriter writeLocation = new StreamWriter(path, true); writeLocation.WriteLine(aLocation.name + "," + aLocation.description + "," + aLocation.type + "," + aLocation.latitude + "," + aLocation.longitude + ",delete"); writeLocation.Close(); if (aLocation.type == "Time Point") { lstTimePoint.Items.RemoveAt(lstTimePoint.SelectedIndex); } else if (aLocation.type == "Bus Stop") { lstBusStop.Items.RemoveAt(lstBusStop.SelectedIndex); } else { lstInterchange.Items.RemoveAt(lstInterchange.SelectedIndex); } listLocation.aLocation.Remove(aLocation); } ddlListType.SelectedIndex = 0; drawMarker(""); } }