我有一个要修改的 .csv 文件。这是文件的格式:
Id, UTMGridEast, UTMGridNorth, LocDate, LocTime, Species
该数据集使用 UTM 坐标,我想将它们转换为纬度/经度并将它们放回各自位置的 .csv 文件中。我已经能够转换 UTM 坐标,该部分已排序!
现在,我已经有了分离这些值并将它们添加到数组列表的代码。我现在需要做的就是编辑内容并将其写回 .csv 文件。
我的 GUI 只包含两个按钮,到目前为止,这是我的代码:
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
}
}
}
}
}
我试图创建一个新的数组列表并使用它来编写它,StreamWriter
但我似乎无法让它正确。
任何帮助将不胜感激,谢谢!