Question

Suppose I have a point to point line string in SQL Server for instance

geometry::STLineFromText('LINESTRING(0 0, 30 30)', 0)  

Is there a function that will take this line and create a circle or half circle that uses the start and end point of the line as the diameter

Was it helpful?

Solution

Sorry for the late answer, I've only just found the question.

You have to use a combination of functions to achieve this, but it is fairly simple to create a circle. For example:

declare @geom geometry = geometry::STLineFromText('LINESTRING(0 0, 30 30)', 0);

declare @circle geometry = @geom.STEnvelope().STCentroid().STBuffer(@geom.STLength() / 2);

Essentially, we use STEnvelope() to build a polygon around the line, this lets us then use STCentroid() to obtain the centre point of the polygon (and therefore line) which we in turn finally expand with STBuffer() using STLength() / 2 as the buffer distance (which of course is half the length of the line - and therefore the radius of the required circle).

Creating a half (or semi) circle isn't going to be quite so easy. You'll need to consider creating your own function that uses a GeometryBuilder to define a Polygon, starting at your line start point and ending at your line end point, in between calculating the resting coordinate having travelled half the length of your line from the centre point along multiple angles, starting at the angle of your line minus 180 and going clockwise until you reach the angle of your line. The more points you have, the more circular like it will be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top