使用NHibernate Spatial获取半径内的空间点 [英] Get spatial points within radius using NHibernate Spatial

查看:132
本文介绍了使用NHibernate Spatial获取半径内的空间点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图天真地获取一组点的k个近邻,给定值k,用作中心的坐标和用作在其中找到点的最大距离的半径.我正在MSSQL 2008数据库上使用地理位置(SRID 4326).

I'm currently trying to naivly get the k-nearest neighbors of a set of points, given a value k, a coordinate to use as center and a radius serving as the max distance to find points within. I'm using geographical points (SRID 4326) on a MSSQL 2008 database.

天真地发现了邻居,根据到该点的距离对查询进行排序并限制了结果. 我的麻烦开始于将点限制为给定半径.距离函数返回的距离比预期的要大得多,我理解这是由SRID 4326引起的.这很好,因为它仅用于订购,但是当我必须将这些值与相对距离进行比较时,比如说200米,那么大的数字就行不通了.

The neighbors are naivly found ordering the query by the distance to the point and limiting the result. My trouble starts at limiting the points by the given radius. The distances returned by the Distance function are much larger than expected, which I understand to be a caused by the SRID 4326. This is OK as along as it is only used for ordering, but when I have to compare these values to a relative distance, say 200 metres, these large numbers wont do.

然后我的问题是:是否有更聪明的方法使用NHibernate Spatial查询通过半径限制点,还是有一种方法可以将此半径转换为类似于Distance函数所使用的某种度量?

My question then is: is there a smarter way of limiting the points by a radius using NHibernate Spatial queries, or is there a way to convert this radius into the some meassurement similar to that used by the Distance function?

这是我现在的查询:

output = NHibernateSession.GetSession().CreateQuery(@"select p from POI p 
                                        where NHSP.Distance(p.PointCoord, :coord) <= :maxDistance 
                                        order by NHSP.Distance(p.PointCoord, :coord)")
                                    .SetParameter("coord", coord,
                                        NHibernateUtil.Custom(typeof(Wgs84GeographyType)))
                                    .SetParameter<double>("maxDistance", radius)
                                    .SetMaxResults(k)
                                    .Enumerable<POI>();

作为一个例子,我有两点: 要点(7 1) 要点(7 3)

Just as an example I have these two points: POINT(7 1) POINT(7 3)

我的预期距离是2,但是mssql STDistance函数计算的距离结果为221151.479533501.我只是不明白这一点.

My expected distance is 2, but the distance calculated by the mssql STDistance function gives 221151.479533501 as a result. I just cant get my mind to make sense about this.

推荐答案

之所以会发生这种情况,是因为地理数据类型和几何数据类型之间存在差异.

this happens because of the difference between the geography data-type and geometry data type.

最好举例说明.

declare @point1 as geography
declare @point2 as geography

set @point1 = geography::STGeomFromText('POINT (7 1)', 4326)
set @point2 = geography::STGeomFromText('POINT (7 3)', 4326)
select @point1.STDistance(@point2)

declare @point3 as geometry
declare @point4 as geometry

set @point3 = geometry::STGeomFromText('POINT (7 1)', 4326)
set @point4 = geometry::STGeomFromText('POINT (7 3)', 4326)
select @point3.STDistance(@point4)

如果直接在SQL Server Management Studio中运行此程序,则第一个结果将得到221151.479533501,第二个结果将得到2.

If you run this directly in SQL Server Management Studio you get 221151.479533501 in the first result and 2 in the second.

这基本上是因为在地理数据类型中,根据提供的SRID选择了单位.您的情况是4326,以米为单位.因此,您需要的是坐标(lon:7; lat:1)和(lon:7; lat:3)之间的距离(以米为单位).它返回约221公里.

This is basically because in the geography data type the unit is chosen according to the supplied SRID. In your case, being 4326, its in meters. So, you're asking for the distance, in meters, between the coordinates (lon:7; lat:1) and (lon:7; lat:3). It returns about 221 Km.

使用几何类型(第二个示例)时,它是一个平面投影,距离在此投影中按您的预期工作,因此返回2.

When using the geometry type (second example), it's a planar projection where the distance works as you would expect, thus returning 2.

关于您的NH Spatial代码,似乎还可以.只需提供以米为单位的maxDistance参数,就可以了.

Regarding your NH Spatial code, seems ok. Just supply the maxDistance parameter in meters and you should be fine.

这篇关于使用NHibernate Spatial获取半径内的空间点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆