Create Geometry/Geography Field from Latitude & Longitude fields (SQL Server)

后端 未结 2 1436
野性不改
野性不改 2021-01-05 22:22

I have a view that contains two fields for latitude and longitude, and I would like to create a new view that converts these lat/lon fields into a geometry/geography field (

相关标签:
2条回答
  • 2021-01-05 23:10

    Juan's answer put me on the right track. When dealing with Geometry I initially used

    geometry::Point([Latitude], [Longitude], 4326) as Geom
    

    But when I tried to access the longitude with Geom.STX it would return the latitude value. And Geom.STY would actually return the longitude value.

    I had to use

    geometry::Point([Longitude], [Latitude], 4326) as Geom
    

    Just wanted to provide this for anyone else that runs into some issues involving the geometry type.

    0 讨论(0)
  • 2021-01-05 23:13
      SELECT  *, 
              geography::STGeomFromText('POINT(' + 
                    CAST([Longitude] AS VARCHAR(20)) + ' ' + 
                    CAST([Latitude] AS VARCHAR(20)) + ')', 4326) as GEOM,
    
              geography::Point([Latitude], [Longitude], 4326) as SAME_GEOM
    
      FROM view_name 
      WHERE (latitude <> 0) AND (longitude <> 0)
    
    0 讨论(0)
提交回复
热议问题