Sunday, November 26, 2017

How to find the centre of a polygon in SQL Server

     
  SQL Server has many built-in spatial functions. I am going to focus on the STCentroid() function in this post. STCentroid returns the geometric center of a geometric instance. Your object must be geometry data type to use this function. It returns a point that is in geometry data type. Geometry instance must be Polygon, CurvePolygon or MultiPolygon for STCentroid to work. In other words, you can't use this function to find the center of a line.

   In the following example, I am going to create a circular polygon and use STCentroid function to find its center.


declare @miles int = 100 / 0.0006213712
declare @userloc geometry = geometry::Point(40, -82, 4326).STBuffer(@miles)

select @userloc, @userloc.STCentroid().ToString() as centre

     I have used (40,-82) as a center point when i created the circular polygon. As you can see STCentroid returned pretty close coordination when i look for the center of my polygon after I create it.

    Here is the polygon I have created.


Here is the center of the polygon found by STCentroid function.



No comments:

Post a Comment