In >=SQL Server 2012, the ReorientObject()ReorientObject()
method should accomplish this. For <SQL Server 2012, below is an alternative method.
For an existing SQL geography @g, @g
, the code below will extract the points and re-create a polygon with points (vertices) in reverse order:
(NOTE 1: works for simple polygons, not for multipolygons or polygons with rings/centroids)
(NOTE 2: using SRID 4326 (WGS 84) coordinate system)
--For existing geography @g
DECLARE @GeometryText varchar(max), @ReversedPolygon geography
DECLARE @GeometryType varchar(20) = 'POLYGON', @Count int
SET @Count = @g.STNumPoints()
WHILE @Count > 0
BEGIN
SET @GeometryText = @GeometryText + CONVERT(varchar(30),CONVERT(decimal(12,8),@g.STPointN(@Count).Long)) + ' ' + CONVERT(varchar(30),CONVERT(decimal(12,8),@g.STPointN(@Count).Lat))
SET @Count = @Count - 1
IF @Count > 0 SET @GeometryText = @GeometryText + ','
END
SET @GeometryText = @GeometryType +'((' + @GeometryText + '))'
SET @ReversedPolygon = geography::STGeomFromText(@GeometryText, 4326);
-- For existing geography @g
DECLARE @GeometryText varchar(max), @ReversedPolygon geography
DECLARE @GeometryType varchar(20) = 'POLYGON', @Count int
SET @Count = @g.STNumPoints()
WHILE @Count > 0
BEGIN
SET @GeometryText = @GeometryText + CONVERT(varchar(30),CONVERT(decimal(12,8),@g.STPointN(@Count).Long)) + ' ' + CONVERT(varchar(30),CONVERT(decimal(12,8),@g.STPointN(@Count).Lat))
SET @Count = @Count - 1
IF @Count > 0 SET @GeometryText = @GeometryText + ','
END
SET @GeometryText = @GeometryType +'((' + @GeometryText + '))'
SET @ReversedPolygon = geography::STGeomFromText(@GeometryText, 4326);
- It only works for simple polygons, not for multipolygons or polygons with rings/centroids.
- It uses the SRID 4326 (WGS 84) coordinate system. (See last line.)