SQL Server has a lot of spatial functions and developers do not use most of them. Why? Most of the spatial functionalities are getting used by BI developers for reports. Most of the maps are available online for free so you don't need to worry about creating a state's or city's map from scratch on SQL Server. In this post, I am going to create 3 circle polygons, and they are going to be in each other. Let's do that first.
declare @miles int = 100 / 0.0006213712 declare @outercir geometry = geometry::Point(40, -82, 4326).STBuffer(@miles) declare @innercir geometry = geometry::Point(40, -82, 4326).STBuffer(@miles/2) declare @centrecir geometry = geometry::Point(40, -82, 4326).STBuffer(@miles/4)
I have created 3 circles here, @centre is the smallest one, @innercir is in the middle and @outercir is the largest one. Here is what they look like.
Central Circle
Middle Circle
Outer Circle
Now, I want to select the area between the outer circle and the middle circle. To do that I am going to use the spatial function named STDifference() function.
select @outercir.STDifference(@innercir) as middleCir
As you can see you can select the area between two geometry data types by using the STDifference() function. Let's find the area between the middle circle and the central circle too.
select @innercir.STDifference(@centrecir) as middlearea
Spatial Data has a lot of potential to fix many kind of business problems rather than querying data for map boundaries.
geom.STDifference takes time on sql server by taking 4-5 seconds. Is there anyway we can increase the query performance?
ReplyDelete