Пример #1
0
        /// <summary>
        /// Load data into the WebMultiPolygon instance.
        /// </summary>
        /// <param name="multiPolygon">This multi polygon.</param>
        /// <param name='dataReader'>An open data reader.</param>
        /// <param name='columnName'>Name of the column in result set to read data from.</param>
        public static void LoadData(this WebMultiPolygon multiPolygon,
                                    DataReader dataReader,
                                    String columnName)
        {
            SqlGeometry multiPolygonGeometry;

            multiPolygonGeometry  = dataReader.GetSqlGeometry(columnName);
            multiPolygon.Polygons = multiPolygonGeometry.GetMultiPolygon().Polygons;
        }
Пример #2
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;
 }
Пример #3
0
        ///// <summary>
        ///// Not yet implemented. Check a list of sql geometries for errors:
        ///// </summary>
        ///// <param name="checkList">A list of Sql Geometries to be checked.</param>
        ///// <returns>Returns true if geometry checks out allright.</returns>
        //public static bool CheckGeometry(List<SqlGeometry> checkList)
        //{
        //    //Todo:
        //    foreach (SqlGeometry sqlGeometry in checkList)
        //    {
        //        //OpenGisGeometryType.Point():
        //               //Check if first point in list == last
        //               //Check if x and y are in the correct order

        //        //OpenGisGeometryType.Polygon():
        //               //Check for gaps

        //        // All
        //               //Check if within Sweden

        //    }
        //    return false;
        //}


        /// <summary>
        /// This method will take a list of Sql Geometries, convert them to WebMultiPolygons
        /// and reproject them from the current coordinat system to the target coordinat system.
        /// </summary>
        /// <param name="toCoordinateSystem">The target coordinate system.</param>
        /// <param name="fromCoordinateSystem">The current coordinate system.</param>
        /// <param name="sqlGeometryToBeConvertedList">The slit of Sql Geometries that are to be converted.</param>
        public static List <WebMultiPolygon> ReProjectMultiPolygon(WebCoordinateSystem toCoordinateSystem,
                                                                   WebCoordinateSystem fromCoordinateSystem,
                                                                   List <SqlGeometry> sqlGeometryToBeConvertedList)
        {
            List <WebMultiPolygon> webMultiPolygonListToBeConverted;
            WebMultiPolygon        webMultiPolygonToBeConverted;
            int sridInsqlGeometryToBeConvertedList = 0;
            int sridInfromCoordinateSystem         = 0;


            webMultiPolygonListToBeConverted = new List <WebMultiPolygon>();
            webMultiPolygonToBeConverted     = new WebMultiPolygon();
            if (sqlGeometryToBeConvertedList != null && sqlGeometryToBeConvertedList.Count > 0)
            {
                sridInsqlGeometryToBeConvertedList = (int)sqlGeometryToBeConvertedList[0].STSrid;


                sridInfromCoordinateSystem = GetSridFromWebCoordinateSystem(fromCoordinateSystem);

                if (!sridInfromCoordinateSystem.Equals(sridInsqlGeometryToBeConvertedList))
                {
                    throw new Exception("There is a mismatch between coordinate systems in Sql Geometry list and fromCoordinateSystem.");
                }

                if (toCoordinateSystem.GetWkt().ToUpper() != fromCoordinateSystem.GetWkt().ToUpper())
                {
                    // Todo: Konvertera sqlGeometryToBeConvertedList till WebMultiPolygon
                    foreach (SqlGeometry geom in sqlGeometryToBeConvertedList)
                    {
                        webMultiPolygonToBeConverted = geom.GetMultiPolygon();
                        webMultiPolygonListToBeConverted.Add(webMultiPolygonToBeConverted);

                        // i++;
                    }

                    // Convert coordinates if needed
                    List <WebMultiPolygon> toGeometryList =
                        WebServiceData.CoordinateConversionManager.GetConvertedMultiPolygons(
                            webMultiPolygonListToBeConverted, fromCoordinateSystem, toCoordinateSystem);

                    return(toGeometryList);
                }
            }
            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>
        /// 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;
        }