0

I used the envelope function on my geometry shape to get this:

POLYGON ((-179.231086 51.175092, 179.859681 51.175092, 179.859681 71.441059, -179.231086 71.441059, -179.231086 51.175092))

Is there a built-in function that will allow me to get the North-East and South-West corners from that rectangle?

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • I don't know of any built in, but if you roll your own you may want to think about how to use Min(), and Max(). NE happens to be Max(FirstCoord) And Max(SecondCoord), SW is Min(FirstCoord) And Min(SecondCoord). NW is Min, Max, SE would be Max, Min. – Chris L Jun 17 '14 at 20:39

2 Answers2

2

Since you're using an envelope, it will be a box that's aligned to the axes. It looks to me as though it starts enumerating the corners at the SE and goes counter-clockwise. So, we just need to grab the 1st and 3rd points.

declare @g geometry = geometry::STGeomFromText('POLYGON ((-179.231086 51.175092, 179.859681 51.175092, 179.859681 71.441059, -179.231086 71.441059, -179.231086 51.175092))', 0);
select @g.STPointN(1) as [SW], @g.STPointN(3) as [NE]
Ben Thul
  • 31,080
  • 4
  • 45
  • 68
-1

If none exist I provided a code sample below that may point you in the right direction(relates to my comment above), also I used ints instead of floats, but that should be easy to fix:

Declare @temp table(PointNum int, FirstCoord int, SecondCoord int)

Insert Into @temp(PointNum, FirstCoord, SecondCoord)
Values(1, -179, 51)
Insert Into @temp(PointNum, FirstCoord, SecondCoord)
Values(2, 179, 51)
Insert Into @temp(PointNum, FirstCoord, SecondCoord)
Values(3, 179, 71)
Insert Into @temp(PointNum, FirstCoord, SecondCoord)
Values(4, -179, 71)

Select * From @temp

Select Top 1 PointNum as NE From @temp
Order By FirstCoord desc, SecondCoord desc

Select Top 1 PointNum as SW From @temp
Order By FirstCoord , SecondCoord

Select Top 1 PointNum as NW From @temp
Order By FirstCoord, SecondCoord desc

Select Top 1 PointNum as SE From @temp
Order By FirstCoord desc, SecondCoord
Chris L
  • 669
  • 2
  • 10
  • 21