00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Drawing;
00006 using System.IO;
00007 using System.Xml;
00008
00009 namespace UtmConvert {
00010 class Parser {
00011
00012
00013 private string _fileName;
00014 protected string FileName {
00015 get { return _fileName; }
00016 }
00017
00018 protected string _fileString;
00019 public string FileString {
00020 get { return _fileString; }
00021 }
00022 public Parser() {
00023
00024 }
00025
00026 public Parser(string fileName) {
00027 _fileName = fileName;
00028 }
00029
00030 public void readFile(string fileName) {
00031 _fileName = fileName;
00032 if (File.Exists(fileName)) {
00033 StreamReader sRead = new StreamReader(fileName);
00034 _fileString = sRead.ReadToEnd();
00035 sRead.Close();
00036 }
00037 }
00038 }
00039
00040 class XmlParser : Parser {
00041
00042 string _type = "";
00043 string _datum = "NAD83/WGS84";
00044 public string Type {
00045 get { return _type; }
00046 }
00047
00048 string _status = "ok";
00049 public string Status {
00050 get { return _status; }
00051 }
00052
00053 string _locationName = "";
00054 public string LocationName {
00055 get { return _locationName; }
00056 }
00057
00058 ConvertLatLonUtm con;
00059 List<Point> _points;
00060 public List<Point> Points {
00061 get { return _points; }
00062 }
00063
00064 XmlDocument doc;
00065
00066 public XmlParser() {
00067
00068 }
00069
00070 private bool datumMatch(Datum d) {
00071 return d.Name == _datum;
00072 }
00073
00074 public void Parse() {
00075 doc = new XmlDocument();
00076 con = new ConvertLatLonUtm();
00077 _points = new List<Point>();
00078
00079 doc.LoadXml(_fileString);
00080 foreach (XmlNode n in doc.ChildNodes) {
00081 if (n.Name == "lat_lon_point_set") {
00082 _datum = n.Attributes[1].Value;
00083 con.Datum = Datums.datumList.Find(datumMatch);
00084 _type = n.Name;
00085 storeLatLonPoints(n);
00086 break;
00087 }
00088 }
00089 }
00090
00091 private void storeLatLonPoints(XmlNode node) {
00092 _locationName = node.Attributes[0].Value;
00093 double lat = 0, lon = 0;
00094 foreach (XmlNode n in node) {
00095 if (n.Name == "ddd_dd") {
00096 lat = ConvertDegRad.getRadians(n.Attributes[0].Value, n.Attributes[1].Value == "s");
00097 lon = ConvertDegRad.getRadians(n.Attributes[2].Value, n.Attributes[3].Value == "w");
00098 } else if (n.Name == "ddd_mm_mm") {
00099 string s = n.Attributes[0].Value;
00100 string f1 = s.Substring(0, s.IndexOf(" "));
00101 string f2 = s.Substring(s.IndexOf(" ") + 1, (s.Length - s.IndexOf(" ")) - 1);
00102 s = n.Attributes[2].Value;
00103 string f3 = s.Substring(0, s.IndexOf(" "));
00104 string f4 = s.Substring(s.IndexOf(" ") + 1, (s.Length - s.IndexOf(" ") - 1));
00105 lat = ConvertDegRad.getRadians(f1, f2, n.Attributes[1].Value == "s");
00106 lon = ConvertDegRad.getRadians(f3, f4, n.Attributes[3].Value == "w");
00107 } else if (n.Name == "ddd_mm_ss_ss") {
00108 string s = n.Attributes[0].Value;
00109 string f1 = s.Substring(0, s.IndexOf(" "));
00110 string f2 = s.Substring(s.IndexOf(" ") + 1, (s.IndexOf(" ", s.IndexOf(" ")) - s.IndexOf(" ") + 1));
00111 string f3 = s.Substring(s.IndexOf(" ", s.IndexOf(" ") + 1) + 1
00112 , s.Length - s.IndexOf(" ", s.IndexOf(" ") + 1) - 1);
00113 s = n.Attributes[2].Value;
00114 string f4 = s.Substring(0, s.IndexOf(" "));
00115 string f5 = s.Substring(s.IndexOf(" ") + 1, (s.IndexOf(" ", s.IndexOf(" ")) - s.IndexOf(" ") + 1));
00116 string f6 = s.Substring(s.IndexOf(" ", s.IndexOf(" ") + 1) + 1
00117 , s.Length - s.IndexOf(" ", s.IndexOf(" ") + 1) - 1);
00118 lat = ConvertDegRad.getRadians(f1, f2, f3, n.Attributes[1].Value == "s");
00119 lon = ConvertDegRad.getRadians(f4, f5, f6, n.Attributes[3].Value == "w");
00120 }
00121 if (ConvertDegRad.Status == "ok") {
00122 if (n.Name == "ddd_dd" || n.Name == "ddd_mm_mm" || n.Name == "ddd_mm_ss_ss") {
00123 con.convertLatLonToUtm(lat, lon);
00124 _points.Add(new Point(con.x, con.y));
00125 }
00126 } else {
00127 _status = "Invalid input, malformed xml data";
00128 }
00129 }
00130
00131 }
00132 }
00133 }