I have a .csv file I would like to modify. Here is the format of the file:
Id, UTMGridEast, UTMGridNorth, LocDate, LocTime, Species
This dataset uses UTM coordinates, and I would like to convert them to Latitude/Longitude and place them back into the .csv file in their respective locations. I am able to convert the UTM coordinates already, that part is sorted!
Now, I already have code that seperates these values and adds them to an arraylist. All I need to do now is edit the contents and write it back to the .csv file.
My GUI consists of just two buttons, here is my code so far:
public partial class MainWindow : Window
{
private string _filename;
public MainWindow()
{
InitializeComponent();
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Dataset"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Commar Seperated Values (.csv)|*.csv" ; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
_filename = dlg.FileName;
txtFilePath.Text = _filename;
}
}
private void btnConvert_Click(object sender, RoutedEventArgs e)
{
ConvertToLatLong();
}
private void ConvertToLatLong()
{
GeoUTMConverter geoUtmConverter = new GeoUTMConverter();
TextWriter tw = new StreamWriter("starkey.txt");
string textFile = System.IO.File.ReadAllText(_filename);
List<string> lines = new List<string>(textFile.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
for (int iLine = 0; iLine < lines.Count; iLine++)
{
List<string> values = new List<string>(lines[iLine].Split(new[] { "," }, StringSplitOptions.None));
for (int iValue = 0; iValue < values.Count; iValue++)
{
Console.WriteLine(String.Format("Line {0} Value {1} : {2}", iLine, iValue, values[iValue]));
if (iLine > 0)
{
geoUtmConverter.ToLatLon(Convert.ToDouble(values[1]), Convert.ToDouble(values[2]), 11, GeoUTMConverter.Hemisphere.Northern);
Double latitude = geoUtmConverter.Latitude;
Double longitude = geoUtmConverter.Longitude;
Console.WriteLine("Latitude: " + latitude + " Longitude: " + longitude);
//EDIT AND WRITE TO FILE HERE
}
}
}
}
}
I have tried to create a new arraylist and write that using StreamWriter
but I just can't seem to get it correct.
Any help would be GREATLY appreciated, thank you!
The part you seem to be missing is
values[1]=latitude;
values[2]=longitude;
tw.WriteLine(String.Join(",",values);
Dont forget to close writer at the end.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.