我第一次尝试T-SQL的空间类型,问题是我不知道如何验证点是否属于由两对点(lat1,long1;lat2,long2(定义的圆
我尝试创建地理对象:
declare @p1 geography = geography::STGeomFromText('POINT(51,067222 -114,110043)',4326);
这两个代码都不起作用:
声明@p1 geography=geography::STGeomFromText('POINT(51067222-114110043(',4326(;声明@p2 geography=geography::STGeomFromText('POINT(51100004-113850491(',4326(;
declare @g, @g2 Geometry
set @g = 'Point(51,067222 -114,110043)';
set @g2 = 'Point(51,100004 -113,850491)';
select @g.STBuffer(@g2)
但没有成功。
请不要杀了我,我是第一次尝试;(
最终找到了答案。(注意:STContains仅适用于MS SQL Server 2012或更高版本(
-- First point
declare @p1 geography = geography::STGeomFromText('POINT(-114.110043 51.067222)', 4326);
-- Second point
declare @p2 geography = geography::STGeomFromText('POINT(-113.850491 51.100004)', 4326);
-- Find the distance between points in meters
declare @distanceInMeters float = @p1.STDistance(@p2);
-- Create circle geography object
declare @cicleGeography geography = @p1.STBuffer(@distanceInMeters)
declare @p3 geography = geography::STGeomFromText('POINT(-112.850491 51.100004)', 4326);
-- Returns true if the third point is inside the circle
select Id, @cicleGeography.STContains(@p3)
非常简单:(
很高兴你找到了自己的答案。或者,您也可以使用STIntersects((或STWithin((。为了进一步阅读/学习,我还使用了一种使用lat/long顺序创建点的替代方法。。
-- First point
declare @p1 geography = geography::Point(51.067222, -114.110043, 4326);
-- Second point
declare @p2 geography = geography::Point(51.100004, -113.850491, 4326);
-- Find the distance between points in meters
declare @distanceInMeters float = @p1.STDistance(@p2);
-- Create circle geography object
declare @cicleGeography geography = @p1.STBuffer(@distanceInMeters)
declare @p3 geography = geography::Point(51.100004, -112.850491, 4326);
-- Returns true if the third point is inside the circle
select @id, @cicleGeography.STIntersects(@p3)
-- OR Alternatively
select @id, @cicleGeography.STWithin(@p3)