Question

I am first time trying the spatial types of T-SQL And the problem is that I don't know how to verify is point belong to circle which is defined by two pairs of points (lat1, long1; lat2, long2)

I tried to create the geography object:

declare @p1 geography = geography::STGeomFromText('POINT(51,067222 -114,110043)',4326);

Neither of this code works:

declare @p1 geography = geography::STGeomFromText('POINT(51,067222 -114,110043)',4326); declare @p2 geography = geography::STGeomFromText('POINT(51,100004 -113,850491)',4326);

declare @g, @g2 Geometry
set @g = 'Point(51,067222 -114,110043)';
set @g2 = 'Point(51,100004  -113,850491)';
select @g.STBuffer(@g2)

but without success.

Please don't kill me, I am trying this first time ;)

Was it helpful?

Solution

Eventually found the answer. (NOTE: STContains works ONLY in MS SQL Server 2012 or greater)

-- 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)

Very easy :)

OTHER TIPS

Glad you found your own answer. As an alternative, you can also use STIntersects() or STWithin(). For further reading / learning, I've also used an alternative for creating points using lat/long order..

-- 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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top