Storing Spatial Data

  • 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

  • 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....

  • 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

  • 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.

  • 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

  • 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

  • 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

  • krishna.kumar - Wednesday, April 17, 2013 2:31 AM

    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 RegardsKrishna

    Or you can do like this to change the order:

    SET @area = geography::Parse('POLYGON((-2.196526 37.277805, -2.195420 37.290393.. .. ))')
    IF @area.EnvelopeAngle() >= 90
    SET @area = @area.ReorientObject()

    I'm new here and have no idea of how to get ride of the smily?

  • thomas 56197 - Sunday, September 30, 2018 4:56 PM

    krishna.kumar - Wednesday, April 17, 2013 2:31 AM

    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 RegardsKrishna

    Or you can do like this to change the order:

    SET @area = geography::Parse('POLYGON((-2.196526 37.277805, -2.195420 37.290393.. .. ))')
    IF @area.EnvelopeAngle() >= 90
    SET @area = @area.ReorientObject()

    I'm new here and have no idea of how to get ride of the smily?

    The Smiley is going to stay there since we are all smiling at the fact that it took you 5 years to respond to the post 😛
    😎
    Much funnier is the fact that the word Parse darkens when smiley enters the equation.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply