/// <summary> /// Get a SqlGeography instance with same /// information as provided WebPolygon. /// </summary> /// <param name="polygon">This polygon.</param> /// <returns> /// A SqlGeography instance with same /// information as provided WebPolygon. /// </returns> public static SqlGeography GetGeography(this WebPolygon polygon) { Boolean isFirstLinearRing; String comma; StringBuilder reverseWkt; try { return(SqlGeography.Parse(new SqlString(polygon.GetWkt()))); } catch (Exception) { } try { // Try with points in reverse order. reverseWkt = new StringBuilder("POLYGON()"); if (polygon.IsNotNull() && polygon.LinearRings.IsNotEmpty()) { isFirstLinearRing = true; foreach (WebLinearRing linearRing in polygon.LinearRings) { if (linearRing.Points.IsNotEmpty()) { if (isFirstLinearRing) { isFirstLinearRing = false; reverseWkt.Insert(8, "()"); } else { reverseWkt.Insert(8, "(), "); } comma = String.Empty; foreach (WebPoint point in linearRing.Points) { reverseWkt.Insert(9, point.X.WebToStringR().Replace(",", ".") + " " + (point.Y.WebToStringR().Replace(",", ".")) + comma); comma = ", "; } } } } return(SqlGeography.Parse(new SqlString(reverseWkt.ToString()))); } catch (Exception) { } return(null); }
/// <summary> /// Get a SqlGeometry instance with same /// information as provided WebPolygon. /// </summary> /// <param name="polygon">This polygon.</param> /// <returns> /// A SqlGeometry instance with same /// information as provided WebPolygon. /// </returns> public static SqlGeometry GetGeometry(this WebPolygon polygon) { return(SqlGeometry.Parse(new SqlString(polygon.GetWkt()))); }