SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Storing Spatial Data


Storing Spatial Data

Author
Message
krishna.kumar
krishna.kumar
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 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
HowardW
HowardW
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2255 Visits: 9892
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....
krishna.kumar
krishna.kumar
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 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
HowardW
HowardW
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2255 Visits: 9892
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.
krishna.kumar
krishna.kumar
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 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
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1592 Visits: 3317
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


krishna.kumar
krishna.kumar
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search