Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Storing Spatial Data Expand / Collapse
Author
Message
Posted Tuesday, April 16, 2013 1:57 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 11:36 PM
Points: 29, Visits: 52
Hi All,

I work with geospatial specialists who provides me with polygons which i save it to my geography field. And any spatial related queries are queried to this field and i get the values as well.

The polygon details are provided as a string of lat and long pairs. For eg (Lat1 Long1, Lat2 Long2,.......Lat1 Long1)
as my first point and last point needs to be same for a polygon.

I have read in many blogs that we need to provide a point as Long Lat and not as Lat long. That is, we recieve the points as Lat Long but we need to reverse the points as Long Lat

So my ploygon value before converting to Geography data type, i reverse the points. The string will be (Long1 Lat1, Long2 Lat2,........Long1 Lat1)

This works fine for me and my queries are fetching the values properly.

Now we have another feature added to the web application wherein the geospatial team would draw a polygon on the google map. When a polygon is drawn, google map API gives me the points of the polygon. We can plot as many points and the user ensures that the polygon ends at the same location where it started. The API provides me the points in Lat long pair. However it gives only distinct points. So what i do is that i take the first point and add at the last to ensure that i dont get an error when i convert it into a geography data. But even then i get "The specified input does not represent a valid geography instance" error.


INSERT INTO SpatialTable (GeogCordinate)
VALUES (geography::STGeomFromText('POLYGON((76.9410753250122 11.05135492884294,76.94399356842041 11.055229898350953, 76.95734024047851 11.053439837277391,76.96186780929565 11.045963581774801,76.94744825363159 11.044805271627415,76.93420886993408 11.048764567419667, 76.9410753250122 11.05135492884294))', 4326));


If i do not reverse my Lat and Long value in a pair and if my query is


INSERT INTO SpatialTable (GeogCordinate)
VALUES (geography::STGeomFromText('POLYGON((11.05135492884294 76.9410753250122,11.055229898350953 76.94399356842041,11.053439837277391 76.95734024047851,11.045963581774801 76.96186780929565,11.044805271627415 76.94744825363159,11.048764567419667 76.93420886993408,11.05135492884294 76.9410753250122))', 4326));

then it works.

I am confused if i need on how to pair the Lat and long value before converting it into a geograpahy data type.

Any help is highly appreciated.



Thanks & Regards

Krishna Kumar. P





Post #1442633
Posted Tuesday, April 16, 2013 9:12 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 3, 2014 8:05 AM
Points: 1,191, Visits: 9,892
Not sure which bit you're confused with. Longitude comes before Latitude for those geography methods, so the last query looks the correct way round....
Post #1442799
Posted Tuesday, April 16, 2013 9:17 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 11:36 PM
Points: 29, Visits: 52
Hi,

Thanks for replying.

Lat: 11.05135492884294
Long: 76.9410753250122

If you see the first insert query, i have the long first followed by Lat but that did not work whereas my second query has the Lat first and then Long.


Thanks & Regards

Krishna
Post #1442805
Posted Tuesday, April 16, 2013 9:34 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 3, 2014 8:05 AM
Points: 1,191, Visits: 9,892
I see. I think it's still longitude first for this, but the problem is that when describing a polygon, the direction of rotation (ring orientation) is important as it defines whether you're defining the polygon as the inside or outside of the coordinates described.

Unfortunately, BOL documentation is sketchy on this, but it follows the "left hand rule", which means that if you imagined walking along the line in the direction that the points are described, your left hand would describe area. So, if you describe them clockwise, you're trying to define a polygon across the whole world, minus the small "hole" you've described, which in 2008 will throw an error as it can't span two hemispheres. Generally you need to define them anti-clockwise to describe an area.

So, if your users are inputting the coordinates clockwise, you'll have to reverse the order.
Post #1442812
Posted Tuesday, April 16, 2013 9:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 11:36 PM
Points: 29, Visits: 52
Hi,

Thank you so very much for the detailed explanation. I think i am not following the left hand rule. I will try this out and will let you know.


Thanks & Regards

Krishna
Post #1442820
Posted Tuesday, April 16, 2013 1:54 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 2:45 PM
Points: 1,080, Visits: 3,170
HowardW (4/16/2013)
Unfortunately, BOL documentation is sketchy on this, but it follows the "left hand rule", which means that if you imagined walking along the line in the direction that the points are described, your left hand would describe area. So, if you describe them clockwise, you're trying to define a polygon across the whole world, minus the small "hole" you've described, which in 2008 will throw an error as it can't span two hemispheres. Generally you need to define them anti-clockwise to describe an area.

So, if your users are inputting the coordinates clockwise, you'll have to reverse the order.

Nice description

One way to ensure you get the orientation right for this is something like the following
It takes advantage of the fact that the Geometry is so strict on the orientation of the exterior ring. The STUnion of the first point causes the geometry to reorient.
;with geom as ( -- used to make the query a bit clearer
SELECT Geometry::STGeomFromText(
'POLYGON((76.9410753250122 11.05135492884294,76.94399356842041 11.055229898350953, 76.95734024047851 11.053439837277391,76.96186780929565 11.045963581774801,76.94744825363159 11.044805271627415,76.93420886993408 11.048764567419667, 76.9410753250122 11.05135492884294))'
,0) g
)
SELECT Geography::STGeomFromWKB(
g.STUnion(g.STPointN(1)).MakeValid().STAsBinary()
,4326)
FROM geom

Post #1442990
Posted Wednesday, April 17, 2013 2:31 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 11:36 PM
Points: 29, Visits: 52
Hi howard and micky,


Thank you so very much. Both the solutions are working now. Micky thanks for saving me an additional work of reversing the string. Now I know the Left Hand Rule clearly. Thank you howard for such a nice description.


Warm Regards

Krishna
Post #1443144
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse