I am allowing users to draw a polygon in Silverlight by clicking to draw. Then I loop through the points, convert them to longitude and latitude and then save to SQL (in a geography
column).
The problem is that because of the world being round and all that, it only works if the user draws clockwise. Otherwise it tries to make the polygon right round the world and fails.
So how do I do this correctly? Do I have to work out which way they are drawing, and if so how?
You can check, if the result of the EnvelopeAngle()
method for the geography was 180, then use the ReorientObject()
function to correct it.
Here is the sample:
--A CW polygon
DECLARE @G3 GEOGRAPHY = 'POLYGON ((45 45, 44 45, 44 46, 45 46, 45 45))';
SELECT @G3.EnvelopeAngle(); --180
SELECT @G3.ReorientObject().STAsText(); --POLYGON ((44 46, 44 45, 45 45, 45 46, 44 46))
EDIT as stated in the comments you can may correct current geometries, using a simple update command (in the case you are sure they are not correct):
UPDATE foo_table SET bar_column = bar_column.ReorientObject()
WHERE bar_column.EnvelopeAngle() > 90
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With