Home  >  Article  >  Database  >  EF保存平面数据到SqlServer

EF保存平面数据到SqlServer

WBOY
WBOYOriginal
2016-06-07 15:44:421180browse

前言 公司开展一个项目,需要根据客户手机定位获取周围内的精准广告,具体是管理员在地图上绘制多边形的广告范围,落在范围内的客户就看到此广告。下面将我的实现方法简单叙述一下,以供有相同需求的朋友参考。 EF平面对象DbGeometry DbGeometry可以表示点、

前言

公司开展一个项目,需要根据客户手机定位获取周围内的精准广告,具体是管理员在地图上绘制多边形的广告范围,落在范围内的客户就看到此广告。下面将我的实现方法简单叙述一下,以供有相同需求的朋友参考。

EF平面对象DbGeometry

DbGeometry可以表示点、线、多边形等平面对象,输入对象可以是WKT(Well-known text),我们可以把百度地图上的polygon对象的点转换为多边形的wkt描述,再转为DbGeometry进行保存。

EF保存平面数据到SqlServerEF保存平面数据到SqlServer

    <span>///</span> <span><summary></summary></span>
        <span>///</span><span> 从多点数据生成多边形
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="pointsJson"></span><span>百度地图多边形的点集</span><span></span>
        <span>public</span> <span>static</span> DbGeometry PolygonFromPointsJson(<span>string</span><span> pointsJson)
        {
            </span><span>var</span> points = JsonSerializer.ToEntity<list>><span>(pointsJson);
            </span><span>if</span> (points.First().GetHashCode() !=<span> points.Last().GetHashCode())
            {
                points.Add(points.First());
            }

            </span><span>var</span> closePoints = ((IEnumerable<lnglat>)points).Select(item => <span>string</span>.Format(<span>"</span><span>{0} {1}</span><span>"</span><span>, item.Lng, item.Lat));
            </span><span>var</span> wkt = <span>"</span><span>POLYGON((</span><span>"</span> + <span>string</span>.Join(<span>"</span><span>,</span><span>"</span>, closePoints) + <span>"</span><span>))</span><span>"</span><span>;
            </span><span>return</span><span> DbGeometry.PolygonFromText(wkt, SYSTEM_ID);
        }</span></lnglat></list>
View Code

广告实体里用DbGeometry类型记录多边形范围

EF保存平面数据到SqlServerEF保存平面数据到SqlServer

 <span>///</span> <span><summary></summary></span>
    <span>///</span><span> 广告范围表
    </span><span>///</span> <span></span>
<span>    [Serializable]
    </span><span>public</span> <span>class</span><span> AdvRange : Entity
    {
        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 广告范围名
        </span><span>///</span> <span></span>
<span>        [Required]
        [Length(</span><span>20</span><span>)]
        </span><span>public</span> <span>string</span> Name { <span>get</span>; <span>set</span><span>; }

        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 所属城市标识ID
        </span><span>///</span> <span></span>
        <span>public</span> Guid CityId { <span>get</span>; <span>set</span><span>; }

        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 多边形
     </span><span>///</span> <span></span> 
        <span>public</span> DbGeometry Polygon { <span>get</span>; <span>set</span><span>; }
    }</span>
View Code

从DbGeometry对象获取多边形的点

DbGeometry对象的WellKnownValue属性的可以获取WKT文本,目前我没有找到相关工具转WKT转为Json,所以用正则表达式获取也能满足需要,然后将结果转换为json就可以放到地图上进行展示了。

EF保存平面数据到SqlServerEF保存平面数据到SqlServer

 <span>///</span> <span><summary></summary></span>
        <span>///</span><span> 获取多边形的点
        </span><span>///</span> <span></span>
        <span>///</span> <span><returns></returns></span>
        <span>public</span> <span>static</span> List<lnglat> GetPolygonPoints(<span>this</span><span> DbGeometry geometry)
        {
            </span><span>var</span> list = <span>new</span> List<lnglat><span>();
            </span><span>var</span> points = geometry.WellKnownValue.WellKnownText.Matches(<span>@"</span><span>-?[1-9]\d*\.\d*|-?0\.\d*[1-9]\d*</span><span>"</span>).Select(item => <span>decimal</span><span>.Parse(item)).ToArray();
            </span><span>for</span> (<span>var</span> i = <span>0</span>; i 2<span>)
            {
                list.Add(</span><span>new</span> LngLat { Lng = points[i], Lat = points[i + <span>1</span><span>] });
            }
            </span><span>if</span> (list.First().GetHashCode() ==<span> list.Last().GetHashCode())
            {
                list.RemoveAt(list.Count </span>- <span>1</span><span>);
            }
            </span><span>return</span><span> list;
        }</span></lnglat></lnglat>
View Code


广告范围是否包含客户位置的点

 DbGeometry有很多方法和属性,EF可以将其翻译到对应的sql语句,其中Polygon.Intersects(point)可以判断点是否在多边形内。

 

效果图(中心的红标注为多边形的重心)

 EF保存平面数据到SqlServer

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn