/* * distance calculation using big-circle formula */ public static double calDistance(IKmlFolder trip) { double distance = 0; IKmlObjectList points = trip.getFeatures().getChildNodes(); IKmlPlacemark prev = null; for (int i = 0; i != (points.getLength()); i++) { IKmlPlacemark curr = (IKmlPlacemark)points.item(i); if (prev != null) { double d = DistanceAlgorithm.DistanceBetweenPlaces((double)getCoordinates(curr)["lat"], (double)getCoordinates(curr)["lon"] , (double)getCoordinates(prev)["lat"], (double)getCoordinates(prev)["lon"]); distance += d; } prev = curr; } return(Math.Round(distance, 3)); }
public void writeTextToFile(String filePath) { //Once the user has completed editing, we will write the text file //Now the write will be a combination of the Viewer file which the user was editing and the total file which wasn't being edited. //So we'll go through all the trips in the total database and if we're on the trip that we were editing we'll use the values from our Viewer. String day; int i = 0; day = databaseViewerTotal.Rows[0]["Day"].ToString(); String[] dayStartCoordinates = new String[2]; String[] dayFinishCoordinates = new String[2]; dayStartCoordinates[0] = databaseViewerTotal.Rows[i]["sLongitude"].ToString(); dayStartCoordinates[1] = databaseViewerTotal.Rows[i]["sLatitude"].ToString(); while (i != databaseViewerTotal.Rows.Count) { if (databaseViewerTotal.Rows[i]["Day"].Equals(day)) { if (databaseViewerTotal.Rows[i]["Day"].Equals(GlobalDay)) { for (int j = 0; j != databaseViewer.Rows.Count; ++j) { //We will use data from the Viewer dataTable DataRow row = writeToFile.NewRow(); row = copyDataRowContents(databaseViewer.Rows[j], row); dayFinishCoordinates[0] = databaseViewer.Rows[j]["fLongitude"].ToString(); dayFinishCoordinates[1] = databaseViewer.Rows[j]["fLatitude"].ToString(); writeToFile.Rows.Add(row); } //Once all the data for this file is written we should fill out the other information //But before that we need to find the next set of trip while (databaseViewerTotal.Rows[i]["Day"].Equals(GlobalDay)) { ++i; } DataRow row1 = writeToFile.NewRow(); row1[1] = "The return distance back to the start is " + Math.Round(DistanceAlgorithm.DistanceBetweenPlaces(Convert.ToDouble(dayStartCoordinates[1]), Convert.ToDouble(dayStartCoordinates[0]), Convert.ToDouble(dayFinishCoordinates[1]), Convert.ToDouble(dayFinishCoordinates[0])), 2) + " km."; writeToFile.Rows.Add(row1); dayStartCoordinates[0] = databaseViewerTotal.Rows[i]["sLongitude"].ToString(); dayStartCoordinates[1] = databaseViewerTotal.Rows[i]["sLatitude"].ToString(); row1 = writeToFile.NewRow(); row1[1] = "The distance between the days is " + Math.Round(DistanceAlgorithm.DistanceBetweenPlaces(Convert.ToDouble(dayFinishCoordinates[1]), Convert.ToDouble(dayFinishCoordinates[0]), Convert.ToDouble(dayStartCoordinates[1]), Convert.ToDouble(dayStartCoordinates[0])), 2) + " km."; writeToFile.Rows.Add(row1); day = databaseViewerTotal.Rows[i]["Day"].ToString(); } else { DataRow row = writeToFile.NewRow(); row = copyDataRowContents(databaseViewerTotal.Rows[i], row); writeToFile.Rows.Add(row); ++i; } } else { //If a new trip has started. Then we should first fill in some information DataRow row = writeToFile.NewRow(); dayFinishCoordinates[0] = databaseViewerTotal.Rows[i - 1]["fLongitude"].ToString(); dayFinishCoordinates[1] = databaseViewerTotal.Rows[i - 1]["fLatitude"].ToString(); row[1] = "The return distance back to the start is " + Math.Round(DistanceAlgorithm.DistanceBetweenPlaces(Convert.ToDouble(dayStartCoordinates[1]), Convert.ToDouble(dayStartCoordinates[0]), Convert.ToDouble(dayFinishCoordinates[1]), Convert.ToDouble(dayFinishCoordinates[0])), 2) + " km."; writeToFile.Rows.Add(row); dayStartCoordinates[0] = databaseViewerTotal.Rows[i]["sLongitude"].ToString(); dayStartCoordinates[1] = databaseViewerTotal.Rows[i]["sLatitude"].ToString(); row = writeToFile.NewRow(); row[1] = "The distance between the days is " + Math.Round(DistanceAlgorithm.DistanceBetweenPlaces(Convert.ToDouble(dayFinishCoordinates[1]), Convert.ToDouble(dayFinishCoordinates[0]), Convert.ToDouble(dayStartCoordinates[1]), Convert.ToDouble(dayStartCoordinates[0])), 2) + " km."; writeToFile.Rows.Add(row); day = databaseViewerTotal.Rows[i]["Day"].ToString(); } } //Now once we've collected all the information in the datatable we'll write it out to a file DateTime currentDateTime = DateTime.Now; String tempDateTime = currentDateTime.ToString("yyMMdd_HHmmss"); String newFilePath = filePath.Split('.')[0] + "_" + tempDateTime + ".txt"; String newFileText = buildStringForWriting(writeToFile); //First we'll copy the contents of the current file, into the new file File.Move(filePath, newFilePath); //Then we will copy the clear the contents of the current file System.IO.StreamWriter file = new System.IO.StreamWriter(filePath); file.WriteLine(newFileText); file.Close(); MessageBox.Show("The text file has been written to " + filePath + "\nThe previous version was saved in " + newFilePath); }