Link to home
Start Free TrialLog in
Avatar of Paul Mauriello
Paul MaurielloFlag for United States of America

asked on

using a SQL Server Geography variable. How do I reverse ARC a SQL SERVER POLYGON. So it accounts for the earth shape in opposite direction?

DECLARE @geog4326 AS geography
SELECT @geog4326 = geography::STGeomFromText('POLYGON((-66.82289098119743 44.9491105596001 ,
                                            -61.98466506087445 43.18048820166788 ,
                                                                  -43.20279822053235 48.34198315747771 ,
                                                                  -74.49995232361809 74.3559573642212 ,
                                                                  -99.90895644651711 74.7065222116312 ,
                                                                  -96.15083389519661 52.62792365767956 ,
                                                                  -66.82289098119743 44.9491105596001  ))',4326)      


I am taking KML file coordinates which render perfectly in Google Earth as a polygon and bringing them into SQL SERVER 2014.

However it is taking account of the earth's shape in the wrong direction. How do I flip that? I cant believe it should be that difficult.
Avatar of arnold
arnold
Flag of United States of America image

Double check your point coordinates in google earth match the latitude, longitude
East us positive, west is negative
North us positive, south us negative.
Double check the coordinated match longitude,latitude.

If you break the nap into quarters based 0,0 position
west/north,east/north
West/south,east/south

Is your polygon is in the wrong quadrant, or in the right quadrant but misshaped the points ?
Avatar of Paul Mauriello

ASKER

Everything is where its should be in terms of coordinates. The weirdness comes into play when I draw a polygon on google earth, the great circle is accounted for in one direction and the sql geography accounts for in the opposite.  Run the above query and compare it to the image I created on google earth and see attached.
Google-Earth_2017-03-24_08-18-30.jpg
The curvature is on the eastern side of the polygon?
The SQL geospatial uses a left-handed rule versus Google possibly using right hand rule.

Reverse instead of your current point organization of  1,2,3,4,5,6,1 try 1,6,5,4,3,2,1

DECLARE @geog4326 AS geography
SELECT @geog4326 = geography::STGeomFromText('POLYGON((-66.82289098119743 44.9491105596001 ,
                                            -96.15083389519661 52.62792365767956 ,
                                                                   -99.90895644651711 74.7065222116312 ,
                                                                  -74.49995232361809 74.3559573642212 ,
                                                                  -43.20279822053235 48.34198315747771 ,
                                                                   -61.98466506087445 43.18048820166788 ,
                                                                  -66.82289098119743 44.9491105596001  ))',4326)
Here is the kml reference from I think they point out they are using right hand rule.....

https://developers.google.com/kml/documentation/kmlreference
I appreciate your response. Reversing the points does not change the SQL Geography but merely reverses the selection of the same area.

 See attached screenshot when I ran your query. Can I switch SQL Geography to right handed rule? Is there a way?
Reverse-the-points.jpg
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try the following

DECLARE @geog4326 AS geography
SELECT @geog4326 = geography::STGeomFromText('POLYGON(( 44.9491105596001 -66.82289098119743 , 
                                        52.62792365767956    -96.15083389519661 ,
                                                                 74.7065222116312 -99.90895644651711  ,
                                                                 74.3559573642212  -74.49995232361809 ,
                                                                48.34198315747771  -43.20279822053235 ,
                                                                  43.18048820166788 -61.98466506087445 , 
                                                             44.9491105596001     -66.82289098119743  ))',4326)

Open in new window

Thanks for your response, I tried the above query, but it resulted in error due to invalid latitudes. see attached.
tried-query.jpg
the point entry is in longitude latitude format.
ok instead of transposing, we need to alter the order of 1 6 5 4 3 2 1 where points
Point 1 is southern most.
point 2 is western most
point 3 is north western
point 4 is north eastern
point 5 is  eastern most

Your graphs seem to have fewer points then your query, or that can be visible,

With that said you have curvature on the point 3 to point 4 and point 4 to point 5 but you want the curvature on points 1 to 2 and 2 to 3.
the one way to achieve this is to shift the starting point of your polygon.

so instead of 1 6 5 4 3 2 1 presumably this is the picture you have, try
                        5 4 3 2 1 6 5

with that said try
DECLARE @geog4326 AS geography
SELECT @geog4326 = geography::STGeomFromText('POLYGON(( -99.90895644651711 74.7065222116312 ,
                                                                  -74.49995232361809 74.3559573642212 ,
                                                                  -43.20279822053235 48.34198315747771 ,
                                                                   -61.98466506087445 43.18048820166788 ,
                                                                  -66.82289098119743 44.9491105596001,
                                                                    -96.15083389519661 52.62792365767956 ,
                                                                       -99.90895644651711 74.7065222116312 ,   ))',4326)

Post the image you get.
The shape remains the same but the area around the shape is being selected rather than switching the hand-rule
Spatial-query.jpg
Unfortunately, I did not have a way to render. Could you post the image?
Another option is to use the CurvePolygon it does not https://docs.microsoft.com/en-us/sql/relational-databases/spatial/curvepolygon

You have six points, but the image appears to have 5. The "illusion" of a curvature might be because of the missing point.


Your SQL graph grid spacing is also throwing .. if you can set the grid .....
No that did work, Sql Server seems bound to Left-Hand Rule. How about we simplify this and instead of an polygon we look at a simple line.

The line is rendered using the Left-Hand Rule, It boggles my mind that SQL Server has no way to switch rules. Does this simpler example give you any ideas?


DECLARE @Line AS geography
SELECT @Line = geography::STGeomFromText('LINESTRING(-43.20279822053235 48.34198315747771 ,      -74.49995232361809 74.3559573642212  )',4243)      
SELECT @Line
I do not have a way to render it, but based on left hand rule the curvature is in the correct place at the end of the south east Point if I understand it right ( contrary to the curvature of the earth.
If not mistaken wiki has the representation
Left hand z thumb x index  y middle
Right hand z thumb x index y middle

If you draw the same line on Google, it will curve in e right north West Point.

You can not change the rule used from left to right, but you can shift the starting point of your shape to achieve the curvature you need.
Try the new point arrangement on google earth and it too will reflect the curvature in the wrong place.
I did try In Google and the KML Polygon is in Right-Hand rule.
Sql Server is using Left-Hand Rule and no change in the ordering of points is altering that. No matter which way I arrange the points the curvature is not altered. I cannot change the hemisphere of the polygon because it needs to be located at a particular place.
Thank you for your assistance.