public void GetConvertedPoint()
        {
            Double fromX, fromY;
            WebCoordinateSystem fromCoordinateSystem, toCoordinateSystem;
            WebPoint            fromPoint, toPoint;

            fromX                   = 1727060.905; // 1644820;
            fromY                   = 7453389.762; // 6680450;
            fromPoint               = new WebPoint(fromX, fromY);
            fromCoordinateSystem    = new WebCoordinateSystem();
            fromCoordinateSystem.Id = CoordinateSystemId.Rt90_25_gon_v;
            foreach (CoordinateSystemId coordinateSystemId in Enum.GetValues(typeof(CoordinateSystemId)))
            {
                toCoordinateSystem    = new WebCoordinateSystem();
                toCoordinateSystem.Id = coordinateSystemId;
                if (toCoordinateSystem.Id == CoordinateSystemId.None)
                {
                    toCoordinateSystem.WKT = ArtDatabanken.Settings.Default.Rt90_25_gon_v_WKT;
                }
                toPoint = GetCoordinateConversionManager(true).GetConvertedPoint(fromPoint, fromCoordinateSystem, toCoordinateSystem);
                Assert.IsNotNull(toPoint);
                if (fromCoordinateSystem.GetWkt() == toCoordinateSystem.GetWkt())
                {
                    Assert.IsTrue(Math.Abs(fromX - toPoint.X) < 2);
                    Assert.IsTrue(Math.Abs(fromY - toPoint.Y) < 2);
                }
                else
                {
                    Assert.IsTrue(Math.Abs(fromX - toPoint.X) > 1);
                    Assert.IsTrue(Math.Abs(fromY - toPoint.Y) > 1);
                }
            }
        }
 /// <summary>
 /// Get region geography cache key.
 /// </summary>
 /// <param name="regionUniqueString">String that is unique for a region.</param>
 /// <param name="coordinateSystem">Coordinate system used in returned geography information.</param>
 /// <returns>Region geography cache key.</returns>
 private String GetRegionGeographyCacheKey(String regionUniqueString,
                                           WebCoordinateSystem coordinateSystem)
 {
     return(Settings.Default.RegionGeographyCacheKey +
            Settings.Default.CacheKeyDelimiter +
            regionUniqueString +
            Settings.Default.CacheKeyDelimiter +
            coordinateSystem.GetWkt());
 }
        /// <summary>
        /// Check that data is valid.
        /// </summary>
        /// <param name="coordinateSystem">This coordinate system.</param>
        public static void CheckData(this WebCoordinateSystem coordinateSystem)
        {
            String wkt;

            coordinateSystem.CheckNotNull("coordinateSystem");
            wkt = coordinateSystem.GetWkt();
            wkt.CheckNotEmpty("coordinateSystem.wkt");
            try
            {
                CoordinateSystemWktReader.Parse(wkt);
            }
            catch
            {
                throw new ArgumentException("Wrong format in coordinate system: " + wkt);
            }
        }
Example #4
0
        /// <summary>
        /// Convert polygons from provided coordinate
        /// system to a Elasticsearch coordinate system.
        /// </summary>
        /// <param name="context">Web service request context.</param>
        /// <param name="inputPolygons">Input polygons.</param>
        /// <param name="regionGuids">Region GUIDs.</param>
        /// <param name="inputCoordinateSystem">Which coordinate system the coordinates should be converted from.</param>
        /// <returns>Polygons in Elasticsearch coordinate system.</returns>
        public List <WebPolygon> ConvertToElasticSearchCoordinates(WebServiceContext context,
                                                                   List <WebPolygon> inputPolygons,
                                                                   List <String> regionGuids,
                                                                   WebCoordinateSystem inputCoordinateSystem)
        {
            List <WebPolygon>         outputPolygons;
            List <WebRegionGeography> regionsGeography;
            WebCoordinateSystem       speciesObservationCoordinateSystem;

            outputPolygons = null;
            speciesObservationCoordinateSystem    = new WebCoordinateSystem();
            speciesObservationCoordinateSystem.Id = CoordinateSystemId.WGS84;
            if (inputPolygons.IsNotEmpty())
            {
                if (inputCoordinateSystem.GetWkt().ToLower() == speciesObservationCoordinateSystem.GetWkt().ToLower())
                {
                    outputPolygons = inputPolygons;
                }
                else
                {
                    outputPolygons = WebServiceData.CoordinateConversionManager.GetConvertedPolygons(inputPolygons,
                                                                                                     inputCoordinateSystem,
                                                                                                     speciesObservationCoordinateSystem);
                }
            }

            if (regionGuids.IsNotEmpty())
            {
                regionsGeography = WebServiceData.RegionManager.GetRegionsGeographyByGuids(context,
                                                                                           regionGuids,
                                                                                           speciesObservationCoordinateSystem);
                if (outputPolygons.IsNull())
                {
                    outputPolygons = new List <WebPolygon>();
                }

                foreach (WebRegionGeography regionGeography in regionsGeography)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    outputPolygons.AddRange(regionGeography.MultiPolygon.Polygons);
                }
            }

            return(outputPolygons);
        }
Example #5
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);
        }
        public void GetConvertedPoints()
        {
            Double              fromX1, fromX2, fromY1, fromY2;
            List <WebPoint>     fromPoints, toPoints;
            WebCoordinateSystem fromCoordinateSystem, toCoordinateSystem;

            fromX1     = 1644820;
            fromY1     = 6680450;
            fromX2     = 1634243;
            fromY2     = 6653434;
            fromPoints = new List <WebPoint>();
            fromPoints.Add(new WebPoint(fromX1, fromY1));
            fromPoints.Add(new WebPoint(fromX2, fromY2));
            fromCoordinateSystem    = new WebCoordinateSystem();
            fromCoordinateSystem.Id = CoordinateSystemId.Rt90_25_gon_v;
            foreach (CoordinateSystemId coordinateSystemId in Enum.GetValues(typeof(CoordinateSystemId)))
            {
                toCoordinateSystem    = new WebCoordinateSystem();
                toCoordinateSystem.Id = coordinateSystemId;
                if (toCoordinateSystem.Id == CoordinateSystemId.None)
                {
                    toCoordinateSystem.WKT = ArtDatabanken.Settings.Default.Rt90_25_gon_v_WKT;
                }
                toPoints = GetCoordinateConversionManager(true).GetConvertedPoints(fromPoints, fromCoordinateSystem, toCoordinateSystem);
                Assert.IsTrue(toPoints.IsNotEmpty());
                Assert.AreEqual(fromPoints.Count, toPoints.Count);
                if (fromCoordinateSystem.GetWkt() == toCoordinateSystem.GetWkt())
                {
                    Assert.IsTrue(Math.Abs(fromX1 - toPoints[0].X) < 2);
                    Assert.IsTrue(Math.Abs(fromY1 - toPoints[0].Y) < 2);
                    Assert.IsTrue(Math.Abs(fromX2 - toPoints[1].X) < 2);
                    Assert.IsTrue(Math.Abs(fromY2 - toPoints[1].Y) < 2);
                }
                else
                {
                    Assert.IsTrue(Math.Abs(fromX1 - toPoints[0].X) > 1);
                    Assert.IsTrue(Math.Abs(fromY1 - toPoints[0].Y) > 1);
                    Assert.IsTrue(Math.Abs(fromX2 - toPoints[1].X) > 1);
                    Assert.IsTrue(Math.Abs(fromY2 - toPoints[1].Y) > 1);
                }
            }
        }
Example #7
0
        /// <summary>
        /// Get a SqlGeometry instance with same information as
        /// property MultiPolygon in provided WebRegionGeography.
        /// </summary>
        /// <param name="regionGeography">This region geography.</param>
        /// <param name="context">Web service request context.</param>
        /// <param name="coordinateSystem">Coordinate system used in region.</param>
        /// <returns>
        /// A SqlGeometry instance with same information as
        /// property MultiPolygon in provided WebRegionGeography.
        /// </returns>
        private static SqlGeometry GetMultiPolygonGeometry(this WebRegionGeography regionGeography,
                                                           WebServiceContext context,
                                                           WebCoordinateSystem coordinateSystem)
        {
            SqlGeometry geometryMultiPolygon;
            String      cacheKey;

            // Get cached information.
            cacheKey = "RegionSqlGeometry:" +
                       regionGeography.Id +
                       ":CoordinateSystem:" +
                       coordinateSystem.GetWkt();
            geometryMultiPolygon = (SqlGeometry)context.GetCachedObject(cacheKey);

            if (geometryMultiPolygon.IsNull())
            {
                geometryMultiPolygon = regionGeography.MultiPolygon.GetGeometry();

                // Add information to cache.
                context.AddCachedObject(cacheKey, geometryMultiPolygon, new TimeSpan(1, 0, 0), CacheItemPriority.BelowNormal);
            }

            return(geometryMultiPolygon);
        }