Skip to content

Instantly share code, notes, and snippets.

@negri
Last active September 17, 2020 12:05
Show Gist options
  • Save negri/a5bcc789bb317e01d5364b0019f2bb82 to your computer and use it in GitHub Desktop.
Save negri/a5bcc789bb317e01d5364b0019f2bb82 to your computer and use it in GitHub Desktop.
Calculando Distâncias e se um ponto está próximo de outro usando Geography no Sql Server
print 'Uma casa...'
declare @g geography;
SET @g = geography::STGeomFromText('POINT (-45.9068303 -23.7602212)', 4326); -- -- Lon (X), Lat (Y), 4326 é WGS 84 (GPS, Google...)
print @g.ToString();
print @g.Lat;
print @g.Long;
print 'E uma praça...'
declare @g2 geography;
SET @g2 = geography::Point(-23.762204, -45.907704, 4326); -- outro método para criar, mais simples; Lat (Y), Lon (X), SRID
print @g2.ToString();
print @g2.Lat;
print @g2.Long;
print 'Distancia'
print @g.STDistance(@g2); -- distancia em m
print 'Raio de 100m de g1'
declare @r100 geography;
set @r100 = @g.BufferWithTolerance(100, 10, 0); -- raio de 100m, tolerância de 10m, absoluta
print @r100.ToString();
print @g.STWithin(@r100); -- dentro
print @g2.STWithin(@r100); -- fora
print 'Raio de 200m de g1'
declare @r200 geography;
set @r200 = @g.BufferWithTolerance(200, 10, 0); -- raio de 200m, tolerância de 10m, absoluta
print @r200.ToString();
print @g.STWithin(@r200); -- dentro
print @g2.STWithin(@r200); -- fora
print 'Raio de 300m de g1'
declare @r300 geography;
set @r300 = @g.BufferWithTolerance(300, 10, 0); -- raio de 300m, tolerância de 10m, absoluta
print @r300.ToString();
print @g.STWithin(@r300); -- dentro
print @g2.STWithin(@r300); -- dentro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment