【问题标题】:How to get the bounding box from a DbGeography MULTIPOINT?如何从 DbGeography MULTIPOINT 获取边界框?
【发布时间】:2013-06-07 13:05:25
【问题描述】:

对不起,如果问题不是 100% 与 SQL Server 本身相关。但我认为它与主题足够接近,可以在此处发布。

我在我的 ASP.NET Web API 项目中使用实体框架。在创建我的 POCO 时,我从浏览器客户端 (Google Maps API) 收到 LatLngBounds。

我从这些传入的坐标创建一个 DbGeography (MULTIPOINT) 实例,它代表我的两个点以重新创建一个矩形。

现在我需要这个 MULTIPOINT 构造的边界框(多边形)。经过一些研究后,我认为 SqlGeometry 程序集中的 STEnvelope 函数可能对此有所帮助,因为我找不到使用 DbGeography 类型执行此操作的方法。在我尝试保存对象之前,代码中的一切看起来都很好。

请参阅this gist,例如代码和来自 EF 的错误消息。请参阅this gist 了解从 EF 生成的 SQL。

以这种方式尝试是否有意义,或者我在这里遗漏了一些重要的东西并且有一个更简单的方法?

【问题讨论】:

    标签: sql-server entity-framework spatial


    【解决方案1】:
     public static Tuple<double, double, double, double> GetEnvelopeFromPolygon(DbGeography polygon)
        {
            if (polygon == null)
            {
                return new Tuple<double, double, double, double>(0, 0, 0, 0);
            }
    
            var points = polygon.GetPointsFromPolygon();
            double minLat = double.MaxValue;
            double maxLat = double.MinValue;
            double minLon = double.MaxValue;
            double maxLon = double.MinValue;
    
            foreach (Tuple<double, double> point in points)
            {
                double lat = point.Item1;
                double lon = point.Item2;
                if (lat < minLat)
                {
                    minLat = lat;
                }
                if (lat > maxLat)
                {
                    maxLat = lat;
                }
                if (lon < minLon)
                {
                    minLon = lon;
                }
                if (lon > maxLon)
                {
                    maxLon = lon;
                }
    
            }
    
            var res = new Tuple<double, double, double, double>(minLat, maxLat, minLon, maxLon);
            return res;
        }
    
    
      public static IEnumerable<Tuple<double, double>> GetPointsFromPolygon(this System.Data.Entity.Spatial.DbGeography geo)
        {
            for (int i = 1; i < geo.PointCount; i++)
            {
                var p = geo.PointAt(i);
                yield return new Tuple<double, double>(p.Latitude.Value, p.Longitude.Value);
            }
    
            var pFinal = geo.PointAt(1);
            yield return new Tuple<double, double>(pFinal.Latitude.Value, pFinal.Longitude.Value);
        }
    

    【讨论】:

      【解决方案2】:

      让它工作。但仍然不确定这是否是没有并发症的方法。但至少存储对象是通过改变来实现的

      poi.Shape = DbGeography.FromGml(envelop.AsGml().Value);
      

      poi.Shape = DbGeography.FromText(envelop.ToString());
      

      让问题悬而未决,也许其他人可以提供更好的解决方案。

      【讨论】:

        猜你喜欢
        • 2020-12-31
        • 2019-05-11
        • 1970-01-01
        • 1970-01-01
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 2015-03-14
        • 1970-01-01
        相关资源
        最近更新 更多