/// <summary>
        /// Convert polygon from one coordinate system to
        /// another coordinate system.
        /// </summary>
        /// <param name="polygon">Polygon that should be converted.</param>
        /// <param name="fromCoordinateSystem">From coordinate system.</param>
        /// <param name="toCoordinateSystem">To coordinate system.</param>
        /// <returns>Polygon with coordinates according to toCoordinateSystem</returns>
        public virtual WebPolygon GetConvertedPolygon(WebPolygon polygon,
                                                      WebCoordinateSystem fromCoordinateSystem,
                                                      WebCoordinateSystem toCoordinateSystem)
        {
            Int32 linearRingIndex, toLinearRingIndex;
            List <WebLinearRing> fromLinearRings, toLinearRings;
            WebPolygon           toPolygon;

            toPolygon = null;
            if (polygon.IsNotNull())
            {
                toPolygon       = new WebPolygon();
                fromLinearRings = new List <WebLinearRing>();
                for (linearRingIndex = 0; linearRingIndex < polygon.LinearRings.Count; linearRingIndex++)
                {
                    fromLinearRings.Add(polygon.LinearRings[linearRingIndex]);
                }
                toLinearRings         = GetConvertedLinearRings(fromLinearRings, fromCoordinateSystem, toCoordinateSystem);
                toLinearRingIndex     = 0;
                toPolygon.LinearRings = new List <WebLinearRing>();
                for (linearRingIndex = 0; linearRingIndex < polygon.LinearRings.Count; linearRingIndex++)
                {
                    toPolygon.LinearRings.Add(toLinearRings[toLinearRingIndex]);
                    toLinearRingIndex++;
                }
            }

            return(toPolygon);
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
 /// <summary>
 /// Convert polygon to JSON format.
 /// </summary>
 /// <param name="polygon">Plygon that should be converted.</param>
 /// <returns>Polygon in JSON format.</returns>
 public static String GetJson(this WebPolygon polygon)
 {
     if (polygon.IsNotNull())
     {
         return(polygon.LinearRings.GetJson());
     }
     else
     {
         return(String.Empty);
     }
 }
Exemple #4
0
 /// <summary>
 /// Check that data is valid.
 /// </summary>
 /// <param name="polygon">The polygon.</param>
 public static void CheckData(this WebPolygon polygon)
 {
     if (polygon.IsNotNull())
     {
         polygon.LinearRings.CheckNotEmpty("LinearRings");
         foreach (WebLinearRing linearRing in polygon.LinearRings)
         {
             linearRing.CheckData();
         }
     }
 }