SQL Server - From longitude and latitude to geometry data type [duplicate]

妖精的绣舞 提交于 2019-12-04 06:25:13

问题


Possible Duplicate:
SQL Server 2008 Spatial: find a point in polygon

I am working on an application that uses an SQL Server 2008 database. In this database I have a table named Session that has two fields Longitude and latitude, which indicate the location of a user. On another table called Zone, I have an area attribute which is of type geometry. How can I check if the user's longitude and latitude coordinates belong to a certain geometry?

Thank you


回答1:


You have a geometry, which stores a shape in Euclidean geometry, and you want to associate a point on the globe, represented by a latitude and longitude, with it, to see if it is inside. This won't work, due to the way SQL stores the data. You probably need to use Geography data types to check this - Latitude and longitude are points on a sphere (Actually geodetic data, since the earth is not quite a sphere.)

For more information on why they are different, see this explanation from microsoft. Also this answer on stackoverflow: GEOMETRY and GEOGRAPHY difference SQL Server 2008

To convert your data from geometry to geography, try: Geography::STGeomFromText(cast(GeomCol as varchar(max)), 4326)

Then you can use the STIntersects method, documented by microsoft here.



来源:https://stackoverflow.com/questions/11245645/sql-server-from-longitude-and-latitude-to-geometry-data-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!