/// <summary> /// Adds a UN Location Code To The List /// </summary> /// <param name="unloccode">UN Location Code String</param> public void AddLocCodeUN(string unloccode) { if (string.IsNullOrEmpty(unloccode)) { throw new ArgumentNullException("Value Must Not Be Null or Empty"); } StringGeoTools.CheckLocCodeUN(unloccode); this.loccodeUN.Add(unloccode); }
/// <summary> /// Adds a SubDivision To The List /// </summary> /// <param name="subdivisionin">SubDivision String</param> public void AddSubDivision(string subdivisionin) { if (string.IsNullOrEmpty(subdivisionin)) { throw new ArgumentException("Input Value Can't Be Null or Empty!"); } StringGeoTools.CheckSubdivision(subdivisionin); this.subdivision.Add(subdivisionin); }
/// <summary> /// Adds a Country To The List /// </summary> /// <param name="countryin">Country String</param> public void AddCountry(string countryin) { if (string.IsNullOrEmpty(countryin)) { throw new ArgumentException("Input Value Can't Be Null or Empty!"); } StringGeoTools.CheckCountry(countryin); this.country.Add(countryin); }
/// <summary> /// Adds a Polygon To The List /// </summary> /// <param name="polyin">Polygon String</param> public void AddPolygon(string polyin) { if (string.IsNullOrEmpty(polyin)) { throw new ArgumentException("Input Value Can't Be Null or Empty!"); } StringGeoTools.CheckPolygon(polyin); this.polygon.Add(polyin); }
/// <summary> /// Adds a Circle To The List /// </summary> /// <param name="circlein">Circle String</param> public void AddCircle(string circlein) { if (string.IsNullOrEmpty(circlein)) { throw new ArgumentException("Input Value Can't Be Null or Empty!"); } StringGeoTools.CheckCircle(circlein); this.circle.Add(circlein); }
/// <summary> /// Reads This IComposable Message From An Existing XML Document /// </summary> /// <param name="rootnode">Node That Points to the TargetArea Element</param> public void ReadXML(XmlNode rootnode) { if (rootnode == (XmlNode)null) { throw new ArgumentNullException("RootNode"); } foreach (XmlNode node in rootnode.ChildNodes) { if (string.IsNullOrEmpty(node.InnerText)) { continue; } switch (node.LocalName) { case "circle": StringGeoTools.CheckCircle(node.InnerText); this.circle.Add(node.InnerText); break; case "polygon": StringGeoTools.CheckPolygon(node.InnerText); this.polygon.Add(node.InnerText); break; case "country": StringGeoTools.CheckCountry(node.InnerText); this.country.Add(node.InnerText); break; case "subdivision": StringGeoTools.CheckSubdivision(node.InnerText); this.subdivision.Add(node.InnerText); break; case "locCodeUN": StringGeoTools.CheckLocCodeUN(node.InnerText); this.loccodeUN.Add(node.InnerText); break; case "#comment": break; default: throw new FormatException("Invalid value: " + node.InnerText + " found in TargetArea Type"); } } }
/// <summary> /// Writes This IComposable Message Into An Existing XML Document /// </summary> /// <param name="xwriter">Existing XML Document Writer</param> public void WriteXML(XmlWriter xwriter) { if (this.circle.Count == 0 && this.polygon.Count == 0 && this.country.Count == 0 && this.subdivision.Count == 0 && this.loccodeUN.Count == 0) { throw new ArgumentNullException("All Elements can't be null"); } xwriter.WriteStartElement("targetArea"); foreach (string circle in this.circle) { StringGeoTools.CheckCircle(circle); xwriter.WriteElementString("circle", circle); } foreach (string polygon in this.polygon) { StringGeoTools.CheckPolygon(polygon); xwriter.WriteElementString("polygon", polygon); } foreach (string country in this.country) { StringGeoTools.CheckCountry(country); xwriter.WriteElementString("country", country); } foreach (string subdivision in this.subdivision) { StringGeoTools.CheckSubdivision(subdivision); xwriter.WriteElementString("subdivision", subdivision); } foreach (string loccodeun in this.loccodeUN) { StringGeoTools.CheckLocCodeUN(loccodeun); xwriter.WriteElementString("locCodeUN", loccodeun); } xwriter.WriteEndElement(); }
/// <summary> /// Validates A Polygon In The Form latitude,longitude latitude,longitude ... /// Constructs a SQLGeography from a string of coordinates, makes it a 'valid' polygon, and returns the modified coordinates in a string. /// Throws unique FormatException with value "Needs Flipped" when the SQLGeography fails to instantiate /// A fork of StringGeoTools.CheckPolygon() /// </summary> /// <param name="s">Space Delimited String of WGS84 Comma Separated Pairs</param> /// <returns>Valid polygon string of points</returns> public static string ValidatePolygon(string s) { // The validated polygon to return, as a string string fixedPoly = string.Empty; if (s == null) { throw new ArgumentNullException("Input string can not be null"); } char[] seperator = new char[1] { ' ' }; char[] seperator2 = new char[1] { ',' }; string[] split2; List <string> split = s.Split(seperator).ToList(); foreach (string point in split) { StringGeoTools.CheckPoint(point); } if (split[0] != split[split.Count - 1]) { throw new FormatException("Invalid Polygon! First and last points must be equal"); } StringBuilder sqlstring = new StringBuilder(); foreach (string point in split) { split2 = point.Split(seperator2); sqlstring.Append(split2[1]); sqlstring.Append(" "); sqlstring.Append(split2[0]); sqlstring.Append(","); } // Remove last comma sqlstring.Remove(sqlstring.Length - 1, 1); StringBuilder fullstr = new StringBuilder(); fullstr.Append("POLYGON (("); fullstr.Append(sqlstring.ToString()); fullstr.Append("))"); SqlChars schars; try { schars = new SqlChars(fullstr.ToString().ToCharArray()); SqlGeography.STPolyFromText(schars, 4326); fixedPoly = s; // everything looks fine, return value is set to the be the same as the input } catch (ArgumentException ae) { if (!ae.ToString().Contains("24205")) { // Force it into a SQL Geometry type, which allows us to make the bad polygon valid schars = new SqlChars(fullstr.ToString().ToCharArray()); SqlGeometry g = SqlGeometry.STGeomFromText(schars, 4326); ////Console.WriteLine("Valid = " + g.STIsValid()); if (!g.STIsValid()) { g = g.MakeValid(); // Write the polygon back out as a string for the XML side for (int i = 1; i <= g.STNumPoints().Value; i++) { fixedPoly += g.STPointN(i).STY.ToString(); fixedPoly += ","; fixedPoly += g.STPointN(i).STX.ToString(); fixedPoly += " "; } // Repeat the first point to close the loop fixedPoly += g.STPointN(1).STY.ToString(); fixedPoly += ","; fixedPoly += g.STPointN(1).STX.ToString(); } else { throw new FormatException("Needs Flipped"); // ArgumentException with 24205 but not isValid() } } else { throw new FormatException("Needs Flipped"); // ArgumentException, but not 24205 } } catch (Exception e) { throw new FormatException("Invalid Polygon! " + e.ToString()); // Other Exception } return(fixedPoly); }