/// <summary>
        /// Convert a multi polygon from one coordinate system to
        /// another coordinate system.
        /// </summary>
        /// <param name="multiPolygon">Multi polygon that should be converted.</param>
        /// <param name="fromCoordinateSystem">From coordinate system.</param>
        /// <param name="toCoordinateSystem">To coordinate system.</param>
        /// <returns>Multi polygon with coordinates according to toCoordinateSystem</returns>
        public virtual WebMultiPolygon GetConvertedMultiPolygon(WebMultiPolygon multiPolygon,
                                                                WebCoordinateSystem fromCoordinateSystem,
                                                                WebCoordinateSystem toCoordinateSystem)
        {
            Int32             polygonIndex, toPolygonIndex;
            WebMultiPolygon   toMultiPolygon;
            List <WebPolygon> fromPolygons, toPolygons;

            toMultiPolygon = null;
            if (multiPolygon.IsNotNull())
            {
                fromPolygons = new List <WebPolygon>();
                for (polygonIndex = 0; polygonIndex < multiPolygon.Polygons.Count; polygonIndex++)
                {
                    fromPolygons.Add(multiPolygon.Polygons[polygonIndex]);
                }
                toPolygons              = GetConvertedPolygons(fromPolygons, fromCoordinateSystem, toCoordinateSystem);
                toPolygonIndex          = 0;
                toMultiPolygon          = new WebMultiPolygon();
                toMultiPolygon.Polygons = new List <WebPolygon>();
                for (polygonIndex = 0; polygonIndex < multiPolygon.Polygons.Count; polygonIndex++)
                {
                    toMultiPolygon.Polygons.Add(toPolygons[toPolygonIndex]);
                    toPolygonIndex++;
                }
            }

            return(toMultiPolygon);
        }
        /// <summary>
        /// Get multi polygon from SqlGeometry.
        /// </summary>
        /// <param name="geometryMultiPolygon">Multi polygon geometry.</param>
        /// <returns>Multi polygon.</returns>
        public static WebMultiPolygon GetMultiPolygon(this SqlGeometry geometryMultiPolygon)
        {
            Int32           polygonIndex;
            SqlGeometry     geometryPolygon;
            WebMultiPolygon multiPolygon;

            multiPolygon          = new WebMultiPolygon();
            multiPolygon.Polygons = new List <WebPolygon>();
            switch (geometryMultiPolygon.GetGeometryType())
            {
            case SqlGeometryType.Polygon:
                geometryPolygon = geometryMultiPolygon;
                multiPolygon.Polygons.Add(GetPolygon(geometryPolygon));
                break;

            case SqlGeometryType.MultiPolygon:
                for (polygonIndex = 1; polygonIndex <= geometryMultiPolygon.STNumGeometries(); polygonIndex++)
                {
                    geometryPolygon = geometryMultiPolygon.STGeometryN(polygonIndex);
                    if (geometryPolygon.GetGeometryType() == SqlGeometryType.Polygon)
                    {
                        multiPolygon.Polygons.Add(GetPolygon(geometryPolygon));
                    }
                    else
                    {
                        throw new Exception("Wrong geometry data type in GetMultiPolygon. Expected type 'Polygon', actual type : " + geometryPolygon.GetGeometryType().ToString());
                    }
                }
                break;

            default:
                throw new Exception("Wrong geometry data type in GetPolygon. Expected type 'Polygon' or 'MultiPolygon', actual type : " + geometryMultiPolygon.GetGeometryType().ToString());
            }
            return(multiPolygon);
        }
Пример #3
0
        /// <summary>
        /// Get a SqlGeometry instance with same
        /// information as provided WebMultiPolygon.
        /// </summary>
        /// <param name="multiPolygon">This multi polygon.</param>
        /// <returns>
        /// A SqlGeometry instance with same
        /// information as provided WebMultiPolygon.
        /// </returns>
        public static SqlGeometry GetGeometry(this WebMultiPolygon multiPolygon)
        {
            StringBuilder wkt = GetGeometryWkt(multiPolygon);

            try
            {
                return(SqlGeometry.Parse(new SqlString(wkt.ToString())));
            }
            catch (Exception)
            {
                //  Debug.WriteLine("SqlGeometry Error in Parse return NULL.");
            }
            return(null);
        }
Пример #4
0
        /// <summary>
        /// Get bounding box for provided polygon.
        /// Currently only 2 dimensions are handled.
        /// </summary>
        /// <param name="multiPolygon">This polygon.</param>
        /// <returns>Bounding box for provided polygon.</returns>
        public static WebBoundingBox GetBoundingBox(this WebMultiPolygon multiPolygon)
        {
            WebBoundingBox boundingBox;

            boundingBox = null;
            if (multiPolygon.Polygons.IsNotEmpty())
            {
                foreach (WebPolygon polygon in multiPolygon.Polygons)
                {
                    if (boundingBox.IsNull())
                    {
                        boundingBox = polygon.GetBoundingBox();
                    }
                    else
                    {
                        boundingBox.Add(polygon.GetBoundingBox());
                    }
                }
            }
            return(boundingBox);
        }
Пример #5
0
        /// <summary>
        /// Get a SqlGeography instance with same
        /// information as provided WebMultiPolygon.
        /// </summary>
        /// <param name="multiPolygon">This multi polygon.</param>
        /// <returns>
        /// A SqlGeography instance with same
        /// information as provided WebMultiPolygon.
        /// </returns>
        public static SqlGeography GetGeography(this WebMultiPolygon multiPolygon)
        {
            Boolean       isFirstLinearRing, isFirstPoint, isFirstPolygon;
            StringBuilder wkt;

            wkt = new StringBuilder("MULTIPOLYGON()");

            try
            {
                if (multiPolygon.Polygons.IsNotEmpty())
                {
                    isFirstPolygon = true;
                    foreach (WebPolygon polygon in multiPolygon.Polygons)
                    {
                        if (polygon.LinearRings.IsNotEmpty())
                        {
                            if (isFirstPolygon)
                            {
                                isFirstPolygon = false;
                                wkt.Insert(13, "()");
                            }
                            else
                            {
                                wkt.Insert(13, "(), ");
                            }

                            isFirstLinearRing = true;
                            foreach (WebLinearRing linearRing in polygon.LinearRings)
                            {
                                if (linearRing.Points.IsNotEmpty())
                                {
                                    if (isFirstLinearRing)
                                    {
                                        isFirstLinearRing = false;
                                        wkt.Insert(14, "()");
                                    }
                                    else
                                    {
                                        wkt.Insert(14, "(), ");
                                    }

                                    String komma = String.Empty;

                                    foreach (WebPoint point in linearRing.Points)
                                    {
                                        wkt.Insert(15, point.X.WebToStringR().Replace(",", ".") + " " + (point.Y.WebToStringR().Replace(",", ".")) + komma);

                                        komma = ", ";
                                    }
                                }
                            }
                        }
                    }
                }

                return(SqlGeography.Parse(new SqlString(wkt.ToString())));
            }
            catch (Exception)
            {
                // Debug.WriteLine("Parse did not work, try other direction...");
            }

            wkt = new StringBuilder("MULTIPOLYGON");
            wkt.Append("(");
            try
            {
                if (multiPolygon.Polygons.IsNotEmpty())
                {
                    isFirstPolygon = true;
                    foreach (WebPolygon polygon in multiPolygon.Polygons)
                    {
                        if (isFirstPolygon)
                        {
                            isFirstPolygon = false;
                        }
                        else
                        {
                            wkt.Append(", ");
                        }
                        wkt.Append("(");
                        if (polygon.LinearRings.IsNotEmpty())
                        {
                            isFirstLinearRing = true;
                            foreach (WebLinearRing linearRing in polygon.LinearRings)
                            {
                                if (isFirstLinearRing)
                                {
                                    isFirstLinearRing = false;
                                }
                                else
                                {
                                    wkt.Append(", ");
                                }
                                wkt.Append("(");
                                if (linearRing.Points.IsNotEmpty())
                                {
                                    isFirstPoint = true;
                                    foreach (WebPoint point in linearRing.Points)
                                    {
                                        if (isFirstPoint)
                                        {
                                            isFirstPoint = false;
                                        }
                                        else
                                        {
                                            wkt.Append(", ");
                                        }
                                        wkt.Append(point.X.WebToStringR().Replace(",", ".") + " " + (point.Y.WebToStringR().Replace(",", ".")));
                                    }
                                }
                                wkt.Append(")");
                            }
                        }
                        wkt.Append(")");
                    }
                }
                wkt.Append(")");

                return(SqlGeography.Parse(new SqlString(wkt.ToString())));
            }
            catch (Exception)
            {
                //  Debug.WriteLine("Parse did not work in this direction either...");
            }
            Debug.WriteLine("SqlGeography, no parse possibel, return NULL");
            return(null);
        }
Пример #6
0
        /// <summary>
        /// Get WebMultiPolygon as a string.
        /// </summary>
        /// <param name="multiPolygon">This multi polygon.</param>
        /// <returns>WebMultiPolygon as a string</returns>
        public static String WebToString(this WebMultiPolygon multiPolygon)
        {
            Boolean       isFirstLinearRing, isFirstPolygon;
            StringBuilder wkt;

            wkt = new StringBuilder();

            if (multiPolygon.IsNotNull())
            {
                wkt.Append("MULTIPOLYGON()");
                if (multiPolygon.Polygons.IsNotEmpty())
                {
                    isFirstPolygon = true;
                    foreach (WebPolygon polygon in multiPolygon.Polygons)
                    {
                        if (polygon.LinearRings.IsNotEmpty())
                        {
                            if (isFirstPolygon)
                            {
                                isFirstPolygon = false;
                                wkt.Insert(13, "()");
                            }
                            else
                            {
                                wkt.Insert(13, "(), ");
                            }

                            isFirstLinearRing = true;
                            foreach (WebLinearRing linearRing in polygon.LinearRings)
                            {
                                if (linearRing.Points.IsNotEmpty())
                                {
                                    if (isFirstLinearRing)
                                    {
                                        isFirstLinearRing = false;
                                        wkt.Insert(14, "()");
                                    }
                                    else
                                    {
                                        wkt.Insert(14, "(), ");
                                    }

                                    String komma = String.Empty;

                                    foreach (WebPoint point in linearRing.Points)
                                    {
                                        wkt.Insert(
                                            15,
                                            point.X.WebToStringR().Replace(",", ".") + " "
                                            + (point.Y.WebToStringR().Replace(",", ".")) + komma);

                                        komma = ", ";
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(wkt.ToString());
        }
Пример #7
0
        /// <summary>
        /// Gets a string instance with same
        /// information (wkt) as provided WebMultiPolygon
        /// </summary>
        /// <param name="multiPolygon">This multi polygon.</param>
        /// <returns>A string instance with same
        /// information as provided WebMultiPolygon.
        /// </returns>
        public static StringBuilder GetGeometryWkt(WebMultiPolygon multiPolygon)
        {
            StringBuilder wkt;
            Boolean       isFirstLinearRing, isFirstPoint, isFirstPolygon;

            wkt = new StringBuilder("MULTIPOLYGON");
            wkt.Append("(");
            if (multiPolygon.Polygons.IsNotEmpty())
            {
                isFirstPolygon = true;
                foreach (WebPolygon polygon in multiPolygon.Polygons)
                {
                    if (isFirstPolygon)
                    {
                        isFirstPolygon = false;
                    }
                    else
                    {
                        wkt.Append(", ");
                    }
                    wkt.Append("(");
                    if (polygon.LinearRings.IsNotEmpty())
                    {
                        isFirstLinearRing = true;
                        foreach (WebLinearRing linearRing in polygon.LinearRings)
                        {
                            if (isFirstLinearRing)
                            {
                                isFirstLinearRing = false;
                            }
                            else
                            {
                                wkt.Append(", ");
                            }
                            wkt.Append("(");
                            if (linearRing.Points.IsNotEmpty())
                            {
                                isFirstPoint = true;
                                foreach (WebPoint point in linearRing.Points)
                                {
                                    if (isFirstPoint)
                                    {
                                        isFirstPoint = false;
                                    }
                                    else
                                    {
                                        wkt.Append(", ");
                                    }
                                    wkt.Append(point.X.WebToStringR().Replace(",", "."));
                                    wkt.Append(" " + point.Y.WebToStringR().Replace(",", "."));
                                }
                            }
                            wkt.Append(")");
                        }
                    }
                    wkt.Append(")");
                }
            }
            wkt.Append(")");
            return(wkt);
        }