SQL Server围绕点生成随机空间地理?

时间:2014-11-16 02:39:49

标签: sql sql-server sql-server-2012

我在开发环境中有几千条记录,每条记录都与特定邮政编码的质心相关联。出于测试目的,我需要将每个SQL Server地理点随机分散到该质心周围0-5英里。

因此,在下面的示例中,我想更新LocationGeo,使其距离各自的ZipGeo 0-5英里。我是否必须使用随机%应用于每个Lon / Lat或者是否有更好的选择?

LocationID int
LocationGeo geography
ZipCode char(5)

ZipCode char(5)
ZipGeo geography

1 个答案:

答案 0 :(得分:2)

我在帖子https://gis.stackexchange.com/a/25883/37373

中找到了答案

我将响应修改为SQL Server代码。

DECLARE @geo as GEOGRAPHY,@newgeo as GEOGRAPHY
SET @geo = (SELECT ZipGeo FROM Location.ZipCodes WHERE ZipCode='90210')

DECLARE @r float,@t float, @w float, @x float, @y float, @u float, @v float;

SET @u=RAND();
SET @v=RAND();

--8046m = ~ 5 miles
SET @r= 8046/(111300*1.0);
SET @w = @r * sqrt(@u);
SET @t = 2 * PI() * @v;
SET @x = @w * cos(@t);
SET @y = @w * sin(@t);
SET @x = @x / cos(@geo.Lat);

SET @newgeo = geography::STPointFromText('POINT('+CAST(@geo.Long+@x AS VARCHAR(MAX))+' '+CAST(@geo.Lat+@y AS VARCHAR(MAX))+')',4326)
--Convert the distance back to miles to validate
SELECT @geo.STDistance(@newgeo)/1609.34