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 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 public class XmlParser : Parser {
00041
00042 string _datum = "NAD83/WGS84";
00043
00044 string _format = "";
00045 public string Format {
00046 get { return _format; }
00047 }
00048
00049 string _note = "";
00050 public string Note {
00051 get { return _note; }
00052 }
00053
00054 string _status = "ok";
00055 public string Status {
00056 get { return _status; }
00057 }
00058
00059 ConvertLatLonUtm con;
00060 public XmlParser() {
00061 con = new ConvertLatLonUtm();
00062 }
00063
00064 private bool datumMatch(Datum d) {
00065 return d.Name == _datum;
00066 }
00067
00068 List<UtmPointSet> _utmPointSetList;
00069 public List<UtmPointSet> UtmPointSetList {
00070 get { return _utmPointSetList; }
00071 }
00072
00073 List<LatLonPointSet> _latLonPointSetList;
00074 public List<LatLonPointSet> LatLonPointSetList {
00075 get { return _latLonPointSetList; }
00076 }
00077
00078 public void Parse(string format) {
00079 try {
00080 XmlDocument doc = new XmlDocument();
00081 doc.LoadXml(_fileString);
00082 _utmPointSetList = new List<UtmPointSet>();
00083 _latLonPointSetList = new List<LatLonPointSet>();
00084 foreach (XmlNode node in doc.ChildNodes) {
00085 if (node.Name == "lat_lon_point_set") {
00086 UtmPointSet ps = new UtmPointSet();
00087 ps.LocationName = node.Attributes[0].Value;
00088 ps.Datum = node.Attributes[1].Value;
00089 foreach (XmlNode n in node) {
00090 if (_status != "ok") break;
00091 if (n.Name == "ddd_dd" || n.Name == "ddd_mm_mm"
00092 || n.Name == "ddd_mm_ss_ss") {
00093 ps.Points.Add(new UtmPoint(convertLatLonToUtm
00094 (n.Attributes[0].Value, n.Attributes[1].Value[0],
00095 n.Attributes[2].Value, n.Attributes[3].Value[0]
00096 , n.Name, node.Attributes[1].Value), con.Zone
00097 , n.Attributes[4].Value, n.Attributes[5].Value));
00098 }
00099 }
00100 _utmPointSetList.Add(ps);
00101 } else if (node.Name == "utm_point_set") {
00102 LatLonPointSet ps = new LatLonPointSet();
00103 ps.LocationName = node.Attributes[0].Value;
00104 ps.Datum = node.Attributes[1].Value;
00105 foreach (XmlNode n in node) {
00106 if (_status != "ok") break;
00107 if (n.Name == "dd_dd") {
00108 con.convertUtmToLatLon
00109 (double.Parse(n.Attributes[0].Value)
00110 , double.Parse(n.Attributes[1].Value)
00111 , n.Attributes[2].Value);
00112 string ns = "n", ew = "e";
00113 if (con.Latitude < 0) ns = "s";
00114 if (con.Longitude < 0) ew = "w";
00115 ps.Points.Add(new LatLonPoint(con.Latitude
00116 , ns, con.Longitude, ew, format
00117 , n.Attributes[3].Value
00118 , n.Attributes[4].Value));
00119 }
00120 }
00121 _latLonPointSetList.Add(ps);
00122 }
00123 }
00124 } catch (Exception e) {
00125 _status = "Invalid input, malformed data: " + e.Message
00126 + "\r\n" + e.StackTrace;
00127 }
00128 }
00129
00130 public Point convertLatLonToUtm(string lat, char ns, string lon
00131 , char ew, string format, string datum) {
00132 _datum = datum;
00133 _status = "ok";
00134 _format = format;
00135 Point pt = new Point(-1, -1);
00136 double radLat = 0, radLon = 0;
00137 try {
00138 con.Datum = Datums.datumList.Find(datumMatch);
00139 if((ns != 'N' && ns != 'S') || (ew != 'E' && ew != 'W'))
00140 throw new Exception("Direction string wrong format");
00141 if (format == "ddd_dd") {
00142 radLat = ConvertDegRad.getRadians(lat, ns);
00143 if (ConvertDegRad.Status != "ok")
00144 throw new Exception(ConvertDegRad.Status);
00145 radLon = ConvertDegRad.getRadians(lon, ew);
00146 } else if (format == "ddd_mm_mm") {
00147 string s = lat;
00148 string f1 = s.Substring(0, s.IndexOf(" "));
00149 string f2 = s.Substring(s.IndexOf(" ") + 1,
00150 (s.Length - s.IndexOf(" ")) - 1);
00151 s = lon;
00152 string f3 = s.Substring(0, s.IndexOf(" "));
00153 string f4 = s.Substring(s.IndexOf(" ") + 1,
00154 (s.Length - s.IndexOf(" ") - 1));
00155 radLat = ConvertDegRad.getRadians(f1, f2, ns);
00156 if (ConvertDegRad.Status != "ok")
00157 throw new Exception(ConvertDegRad.Status);
00158 radLon = ConvertDegRad.getRadians(f3, f4, ew);
00159 } else if (format == "ddd_mm_ss_ss") {
00160 string s = lat;
00161 string f1 = s.Substring(0, s.IndexOf(" "));
00162 string f2 = s.Substring(s.IndexOf(" ") + 1, (s.IndexOf(" "
00163 , s.IndexOf(" ")) - s.IndexOf(" ") + 1));
00164 string f3 = s.Substring(s.IndexOf(" ", s.IndexOf(" ") + 1) + 1
00165 , s.Length - s.IndexOf(" ", s.IndexOf(" ") + 1) - 1);
00166 s = lon;
00167 string f4 = s.Substring(0, s.IndexOf(" "));
00168 string f5 = s.Substring(s.IndexOf(" ") + 1, (s.IndexOf(" "
00169 , s.IndexOf(" ")) - s.IndexOf(" ") + 1));
00170 string f6 = s.Substring(s.IndexOf(" ", s.IndexOf(" ") + 1) + 1
00171 , s.Length - s.IndexOf(" ", s.IndexOf(" ") + 1) - 1);
00172 radLat = ConvertDegRad.getRadians(f1, f2, f3, ns);
00173 if (ConvertDegRad.Status != "ok")
00174 throw new Exception(ConvertDegRad.Status);
00175 radLon = ConvertDegRad.getRadians(f4, f5, f6, ew);
00176 }
00177 if ((format == "ddd_dd" || format == "ddd_mm_mm"
00178 || format == "ddd_mm_ss_ss") && ConvertDegRad.Status == "ok") {
00179 con.convertLatLonToUtm(radLat, radLon);
00180 pt = new Point(con.x, con.y);
00181 return pt;
00182 }
00183 _status = "Invalid input, malformed data";
00184 return pt;
00185 } catch (Exception e) {
00186 _status = "Invalid input, malformed data: " + e.Message
00187 + "\r\n" + e.StackTrace;
00188 return pt;
00189 }
00190 }
00191 }
00192 }