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 public class Parser {
00011
00012
00013
00014 private string _fileName;
00015 protected string FileName {
00016 get { return _fileName; }
00017 }
00018
00019 protected string _fileString;
00020 public string FileString {
00021 get { return _fileString; }
00022 }
00023 public Parser() {
00024
00025 }
00026
00027 public Parser(string fileName) {
00028 _fileName = fileName;
00029 }
00030
00031 public void readFile(string fileName) {
00032 _fileName = fileName;
00033 if (File.Exists(fileName)) {
00034 StreamReader sRead = new StreamReader(fileName);
00035 _fileString = sRead.ReadToEnd();
00036 sRead.Close();
00037 }
00038 }
00039
00040 }
00041
00042 public class XmlParser : Parser {
00043
00044 public delegate void updateErrorEventHandler(Exception error);
00045 public event updateErrorEventHandler updateErrorEvent;
00046
00047 string _datum = "NAD83/WGS84";
00048
00049 string _format = "";
00050 public string Format {
00051 get { return _format; }
00052 }
00053
00054 string _note = "";
00055 public string Note {
00056 get { return _note; }
00057 }
00058
00059 string _status = "ok";
00060 public string Status {
00061 get { return _status; }
00062 }
00063
00064 ConvertLatLonUtm con;
00065 public XmlParser() {
00066 con = new ConvertLatLonUtm();
00067 }
00068
00069 private bool datumMatch(Datum d) {
00070 return d.Name == _datum;
00071 }
00072
00073 List<UtmPointSet> _utmPointSetList;
00074 public List<UtmPointSet> UtmPointSetList {
00075 get { return _utmPointSetList; }
00076 }
00077
00078 List<LatLonPointSet> _latLonPointSetList;
00079 public List<LatLonPointSet> LatLonPointSetList {
00080 get { return _latLonPointSetList; }
00081 }
00082
00083 public void parseGPX() {
00084 XmlDocument doc = new XmlDocument();
00085 doc.LoadXml(_fileString);
00086 _utmPointSetList = new List<UtmPointSet>();
00087 _latLonPointSetList = new List<LatLonPointSet>();
00088 XmlNode node = doc.ChildNodes[0];
00089 foreach (XmlNode n in doc.ChildNodes) {
00090 if (n.Name == "gpx") {
00091 node = n;
00092 break;
00093 }
00094 }
00095 foreach (XmlNode n in node) {
00096 if (n.Name == "rte") {
00097 node = n;
00098 break;
00099 }
00100 }
00101
00102 if (node.Name == "rte") {
00103 UtmPointSet ps1 = new UtmPointSet();
00104 LatLonPointSet ps2 = new LatLonPointSet();
00105 ps1.LocationName = node.ChildNodes[0].InnerText;
00106 ps1.Datum = "NAD83/WGS84";
00107 string format = "ddd_dd";
00108 foreach (XmlNode n in node) {
00109 if (n.Name == "rtept") {
00110 string slat = n.Attributes[0].Value;
00111 string slon = n.Attributes[1].Value;
00112 char ns = 'N', ew = 'E';
00113 if (slat.Contains("-")) {
00114 slat = slat.Replace("-", "");
00115 ns = 'S';
00116 }
00117 if (slon.Contains("-")) {
00118 slon = slon.Replace("-", "");
00119 ew = 'W';
00120 }
00121 double lat = double.Parse(slat);
00122 double lon = double.Parse(slon);
00123 string note = n.Attributes[2].Value;
00124 string handle = n.Attributes[3].Value;
00125 ps2.Points.Add(new LatLonPoint(lat, ns.ToString()
00126 , lon, ew.ToString(), format, note, handle));
00127 if (_status != "ok") break;
00128 ps1.Points.Add(new UtmPoint(convertLatLonToUtm
00129 (slat, ns, slon, ew, format, ps1.Datum), con.Zone
00130 , note, handle));
00131 }
00132 }
00133 _utmPointSetList.Add(ps1);
00134 _latLonPointSetList.Add(ps2);
00135 }
00136 }
00137
00138 public void Parse(string format) {
00139 try {
00140 XmlDocument doc = new XmlDocument();
00141 doc.LoadXml(_fileString);
00142 _utmPointSetList = new List<UtmPointSet>();
00143 _latLonPointSetList = new List<LatLonPointSet>();
00144 foreach (XmlNode node in doc.ChildNodes) {
00145 if (node.Name == "lat_lon_point_set") {
00146 UtmPointSet ps1 = new UtmPointSet();
00147 LatLonPointSet ps2 = new LatLonPointSet();
00148 ps1.LocationName = node.Attributes[0].Value;
00149 ps1.Datum = node.Attributes[1].Value;
00150 foreach (XmlNode n in node) {
00151 ps2.Points.Add(new LatLonPoint(double.Parse(
00152 n.Attributes[0].Value), n.Attributes[1].Value
00153 , double.Parse(n.Attributes[2].Value)
00154 , n.Attributes[3].Value, format
00155 , n.Attributes[4].Value, n.Attributes[5].Value));
00156 if (_status != "ok") break;
00157 if (n.Name == "ddd_dd" || n.Name == "ddd_mm_mm"
00158 || n.Name == "ddd_mm_ss_ss") {
00159 ps1.Points.Add(new UtmPoint(convertLatLonToUtm
00160 (n.Attributes[0].Value, n.Attributes[1].Value[0],
00161 n.Attributes[2].Value, n.Attributes[3].Value[0]
00162 , n.Name, node.Attributes[1].Value), con.Zone
00163 , n.Attributes[4].Value, n.Attributes[5].Value));
00164 }
00165 }
00166 _utmPointSetList.Add(ps1);
00167 _latLonPointSetList.Add(ps2);
00168 } else if (node.Name == "utm_point_set") {
00169 UtmPointSet ps1 = new UtmPointSet();
00170 LatLonPointSet ps2 = new LatLonPointSet();
00171 ps2.LocationName = node.Attributes[0].Value;
00172 ps2.Datum = node.Attributes[1].Value;
00173 foreach (XmlNode n in node) {
00174 if (_status != "ok") break;
00175 if (n.Name == "dd_dd") {
00176
00177 ps1.Points.Add(new UtmPoint(new Point(double.Parse(
00178 n.Attributes[0].Value), double.Parse(
00179 n.Attributes[1].Value)), n.Attributes[2].Value
00180 , n.Attributes[3].Value, n.Attributes[4].Value));
00181
00182 con.convertUtmToLatLon
00183 (double.Parse(n.Attributes[0].Value)
00184 , double.Parse(n.Attributes[1].Value)
00185 , n.Attributes[2].Value);
00186 string ns = "N", ew = "E";
00187 if (con.Latitude < 0) ns = "S";
00188 if (con.Longitude < 0) ew = "W";
00189 ps2.Points.Add(new LatLonPoint(con.Latitude
00190 , ns, con.Longitude, ew, format
00191 , n.Attributes[3].Value
00192 , n.Attributes[4].Value));
00193 }
00194 }
00195 _utmPointSetList.Add(ps1);
00196 _latLonPointSetList.Add(ps2);
00197 }
00198 }
00199 } catch (Exception e) {
00200 if (updateErrorEvent != null)
00201 updateErrorEvent.Invoke(e);
00202 _status = "Invalid input, malformed data: " + e.Message
00203 + "\r\n" + e.StackTrace;
00204 }
00205 }
00206
00207 public Point convertLatLonToUtm(string lat, char ns, string lon
00208 , char ew, string format, string datum) {
00209 _datum = datum;
00210 _status = "ok";
00211 _format = format;
00212 Point pt = new Point(-1, -1);
00213 double radLat = 0, radLon = 0;
00214 try {
00215 con.Datum = Datums.datumList.Find(datumMatch);
00216 if((ns != 'N' && ns != 'S') || (ew != 'E' && ew != 'W'))
00217 throw new Exception("Direction string wrong format");
00218 if (format == "ddd_dd") {
00219 radLat = ConvertDegRad.getRadians(lat, ns);
00220 if (ConvertDegRad.Status != "ok")
00221 throw new Exception(ConvertDegRad.Status);
00222 radLon = ConvertDegRad.getRadians(lon, ew);
00223 } else if (format == "ddd_mm_mm") {
00224 string s = lat;
00225 string f1 = s.Substring(0, s.IndexOf(" "));
00226 string f2 = s.Substring(s.IndexOf(" ") + 1,
00227 (s.Length - s.IndexOf(" ")) - 1);
00228 s = lon;
00229 string f3 = s.Substring(0, s.IndexOf(" "));
00230 string f4 = s.Substring(s.IndexOf(" ") + 1,
00231 (s.Length - s.IndexOf(" ") - 1));
00232 radLat = ConvertDegRad.getRadians(f1, f2, ns);
00233 if (ConvertDegRad.Status != "ok")
00234 throw new Exception(ConvertDegRad.Status);
00235 radLon = ConvertDegRad.getRadians(f3, f4, ew);
00236 } else if (format == "ddd_mm_ss_ss") {
00237 string s = lat;
00238 string f1 = s.Substring(0, s.IndexOf(" "));
00239 string f2 = s.Substring(s.IndexOf(" ") + 1, (s.IndexOf(" "
00240 , s.IndexOf(" ")) - s.IndexOf(" ") + 1));
00241 string f3 = s.Substring(s.IndexOf(" ", s.IndexOf(" ") + 1) + 1
00242 , s.Length - s.IndexOf(" ", s.IndexOf(" ") + 1) - 1);
00243 s = lon;
00244 string f4 = s.Substring(0, s.IndexOf(" "));
00245 string f5 = s.Substring(s.IndexOf(" ") + 1, (s.IndexOf(" "
00246 , s.IndexOf(" ")) - s.IndexOf(" ") + 1));
00247 string f6 = s.Substring(s.IndexOf(" ", s.IndexOf(" ") + 1) + 1
00248 , s.Length - s.IndexOf(" ", s.IndexOf(" ") + 1) - 1);
00249 radLat = ConvertDegRad.getRadians(f1, f2, f3, ns);
00250 if (ConvertDegRad.Status != "ok")
00251 throw new Exception(ConvertDegRad.Status);
00252 radLon = ConvertDegRad.getRadians(f4, f5, f6, ew);
00253 }
00254 if ((format == "ddd_dd" || format == "ddd_mm_mm"
00255 || format == "ddd_mm_ss_ss") && ConvertDegRad.Status == "ok") {
00256 con.convertLatLonToUtm(radLat, radLon);
00257 pt = new Point(con.x, con.y);
00258 return pt;
00259 }
00260 _status = "Invalid input, malformed data";
00261 return pt;
00262 } catch (Exception e) {
00263 if (updateErrorEvent != null)
00264 updateErrorEvent.Invoke(e);
00265 _status = "Invalid input, malformed data: " + e.Message
00266 + "\r\n" + e.StackTrace;
00267 return pt;
00268 }
00269 }
00270 }
00271 }