Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

SQL Server genera geografia spaziale casuale intorno al punto?

Ho trovato una risposta da questo post https://gis.stackexchange.com/a/25883/ 37373

Ho adattato la risposta al codice di 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(@[email protected] AS VARCHAR(MAX))+' '+CAST(@[email protected] AS VARCHAR(MAX))+')',4326)
--Convert the distance back to miles to validate
SELECT @geo.STDistance(@newgeo)/1609.34