用于将英国操作系统坐标从东向/北向转换为经度和纬度的 SQL 函数
请有人发布一个 SQL 函数来将东距/北距转换为经度/纬度。我知道它非常复杂,但我还没有找到任何人用 T-SQL 记录它。
此 javascript 代码 可以工作,但我无法将其转换为SQL。
我有 16,000 个坐标,需要将它们全部转换为纬度/经度。
这是我到目前为止所拥有的,但它没有超过 while 循环。
DECLARE @east real = 482353,
@north real = 213371
DECLARE @a real = 6377563.396,
@b real = 6356256.910,
@F0 real = 0.9996012717,
@lat0 real = 49*PI()/180,
@lon0 real = -2*PI()/180
DECLARE @N0 real = -100000,
@E0 real = 400000,
@e2 real = 1 - (@b*@b)/(@a*@a),
@n real = (@a-@b)/(@a+@b)
DECLARE @n2 real = @n*@n,
@n3 real = @n*@n*@n
DECLARE @lat real = @lat0,
@M real = 0
WHILE (@north-@N0-@M >= 0.00001)
BEGIN
SET @lat = ((@north-@N0-@M)/(@a*@F0)) + @lat
DECLARE @Ma real = (1 + @n + (5/4)*@n2 + (5/4)*@n3) * (@lat-@lat0),
@Mb real = (3*@n + 3*@n*@n + (21/8)*@n3) * SIN(@lat-@lat0) * COS(@lat+@lat0),
@Mc real = ((15/8)*@n2 + (15/8)*@n3) * SIN(2*(@lat-@lat0)) * COS(2*(@lat+@lat0)),
@Md real = (35/24)*@n3 * SIN(3*(@lat-@lat0)) * COS(3*(@lat+@lat0))
SET @M = @b * @F0 * (@Ma - @Mb + @Mc - @Md)
END
DECLARE @cosLat real = COS(@lat),
@sinLat real = SIN(@lat)
DECLARE @nu real = @a*@F0/sqrt(1-@e2*@sinLat*@sinLat)
DECLARE @rho real = @a*@F0*(1-@e2)/POWER(1-@e2*@sinLat*@sinLat, 1.5)
DECLARE @eta2 real = @nu/@rho-1
DECLARE @tanLat real = tan(@lat)
DECLARE @tan2lat real = @tanLat*@tanLat
DECLARE @tan4lat real = @tan2lat*@tan2lat
DECLARE @tan6lat real = @tan4lat*@tan2lat
DECLARE @secLat real = 1/@cosLat
DECLARE @nu3 real = @nu*@nu*@nu
DECLARE @nu5 real = @nu3*@nu*@nu
DECLARE @nu7 real = @nu5*@nu*@nu
DECLARE @VII real = @tanLat/(2*@rho*@nu)
DECLARE @VIII real = @tanLat/(24*@rho*@nu3)*(5+3*@tan2lat+@eta2-9*@tan2lat*@eta2)
DECLARE @IX real = @tanLat/(720*@rho*@nu5)*(61+90*@tan2lat+45*@tan4lat)
DECLARE @X real = @secLat/@nu
DECLARE @XI real = @secLat/(6*@nu3)*(@nu/@rho+2*@tan2lat)
DECLARE @XII real = @secLat/(120*@nu5)*(5+28*@tan2lat+24*@tan4lat)
DECLARE @XIIA real = @secLat/(5040*@nu7)*(61+662*@tan2lat+1320*@tan4lat+720*@tan6lat)
DECLARE @dE real = (@east-@E0)
DECLARE @dE2 real = @dE*@dE
DECLARE @dE3 real = @dE2*@dE
DECLARE @dE4 real = @dE2*@dE2,
@dE5 real = @dE3*@dE2
DECLARE @dE6 real = @dE4*@dE2,
@dE7 real = @dE5*@dE2
SET @lat = @lat - @VII*@dE2 + @VIII*@dE4 - @IX*@dE6
DECLARE @lon real = @lon0 + @X*@dE - @XI*@dE3 + @XII*@dE5 - @XIIA*@dE7
SELECT @lon, @lat
Please can someone post a SQL function to convert easting/northing to longitude/latitude. I know it's incredibly complicated but I haven't found anyone who has documented it in T-SQL.
This javascript code works but I'm having trouble converting it to SQL.
I have 16,000 coordinates and need them all converted to lat/long.
This is what I have so far but it's not getting past the while loop.
DECLARE @east real = 482353,
@north real = 213371
DECLARE @a real = 6377563.396,
@b real = 6356256.910,
@F0 real = 0.9996012717,
@lat0 real = 49*PI()/180,
@lon0 real = -2*PI()/180
DECLARE @N0 real = -100000,
@E0 real = 400000,
@e2 real = 1 - (@b*@b)/(@a*@a),
@n real = (@a-@b)/(@a+@b)
DECLARE @n2 real = @n*@n,
@n3 real = @n*@n*@n
DECLARE @lat real = @lat0,
@M real = 0
WHILE (@north-@N0-@M >= 0.00001)
BEGIN
SET @lat = ((@north-@N0-@M)/(@a*@F0)) + @lat
DECLARE @Ma real = (1 + @n + (5/4)*@n2 + (5/4)*@n3) * (@lat-@lat0),
@Mb real = (3*@n + 3*@n*@n + (21/8)*@n3) * SIN(@lat-@lat0) * COS(@lat+@lat0),
@Mc real = ((15/8)*@n2 + (15/8)*@n3) * SIN(2*(@lat-@lat0)) * COS(2*(@lat+@lat0)),
@Md real = (35/24)*@n3 * SIN(3*(@lat-@lat0)) * COS(3*(@lat+@lat0))
SET @M = @b * @F0 * (@Ma - @Mb + @Mc - @Md)
END
DECLARE @cosLat real = COS(@lat),
@sinLat real = SIN(@lat)
DECLARE @nu real = @a*@F0/sqrt(1-@e2*@sinLat*@sinLat)
DECLARE @rho real = @a*@F0*(1-@e2)/POWER(1-@e2*@sinLat*@sinLat, 1.5)
DECLARE @eta2 real = @nu/@rho-1
DECLARE @tanLat real = tan(@lat)
DECLARE @tan2lat real = @tanLat*@tanLat
DECLARE @tan4lat real = @tan2lat*@tan2lat
DECLARE @tan6lat real = @tan4lat*@tan2lat
DECLARE @secLat real = 1/@cosLat
DECLARE @nu3 real = @nu*@nu*@nu
DECLARE @nu5 real = @nu3*@nu*@nu
DECLARE @nu7 real = @nu5*@nu*@nu
DECLARE @VII real = @tanLat/(2*@rho*@nu)
DECLARE @VIII real = @tanLat/(24*@rho*@nu3)*(5+3*@tan2lat+@eta2-9*@tan2lat*@eta2)
DECLARE @IX real = @tanLat/(720*@rho*@nu5)*(61+90*@tan2lat+45*@tan4lat)
DECLARE @X real = @secLat/@nu
DECLARE @XI real = @secLat/(6*@nu3)*(@nu/@rho+2*@tan2lat)
DECLARE @XII real = @secLat/(120*@nu5)*(5+28*@tan2lat+24*@tan4lat)
DECLARE @XIIA real = @secLat/(5040*@nu7)*(61+662*@tan2lat+1320*@tan4lat+720*@tan6lat)
DECLARE @dE real = (@east-@E0)
DECLARE @dE2 real = @dE*@dE
DECLARE @dE3 real = @dE2*@dE
DECLARE @dE4 real = @dE2*@dE2,
@dE5 real = @dE3*@dE2
DECLARE @dE6 real = @dE4*@dE2,
@dE7 real = @dE5*@dE2
SET @lat = @lat - @VII*@dE2 + @VIII*@dE4 - @IX*@dE6
DECLARE @lon real = @lon0 + @X*@dE - @XI*@dE3 + @XII*@dE5 - @XIIA*@dE7
SELECT @lon, @lat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我已经在这个问题上挣扎了一段时间了。
我在 OSGB36 中有很多北向/东向点,必须定期进行动态转换。
请注意,下面的 UDF 将 OSGB36(地形测量)投影中的北距/东距转换为 WGS84 投影中的纬度/经度,以便它们可以在 Google 地图中使用。
I've been struggling with this one for a while.
I had a lot of northing/easting points in OSGB36 that have to be converted on the fly on a regular basis.
Please note that the UDF below converts northings/eastings in OSGB36 (Ordnance Survey) projection to latitude/longitude in WGS84 projection so they can be used in Google Maps.
我最终使用以下 javascript 函数来转换值。我知道这不是一个 SQL 解决方案,但它为我完成了这项工作。
I ended up using the following javascript functions to convert the values. I know it's not a SQL solution but it did the job for me.
我需要相同的功能,而 JavaScript 使得与数据库交互变得困难。我已将您的 JS 转换为 PHP,这在更新数据库时可能更有用 - 即:查询表、循环结果集、调用函数、更新表。
I needed the same function, and javascript made it difficult to interact with the DB. I have converted your JS to PHP and this could be more useful when updating your database - ie: query table, loop through result set, call function, update table.
如果有人对非 SQL 解决方案感兴趣,我强烈建议使用这个 http://www.howtocreate。 co.uk/php/gridref.php PHP/JavaScript 类。
这里要提到的一件重要的事情是该库支持 Helmert 转换。
PHP
JavaScript
If anyone's interested in non-SQL solution I strongly recommend using this http://www.howtocreate.co.uk/php/gridref.php PHP/JavaScript class.
One important thing to mention here is the library supports Helmert transformation.
PHP
JavaScript
我在 .NET 中开发了一个库,可以从 transact sql 调用 将 WGS84/UTM 坐标转换为纬度和经度
它的作用恰恰相反,但由于它使用 CooperativeSharp,您可以下载代码并轻松更改它以从纬度/经度转换为改为 wgs84。
您可以从github下载它:
https://github.com/jv-garcia/UTM2LATITUDE
I have developed a library in .NET to be called from transact sql Converts WGS84/UTM coordinates to Latitude and Longitude
It does just the opposite, but as it uses CoordinateSharp you can download the code and change it easily to convert from lat/long to wgs84 instead.
You can download it from github:
https://github.com/j-v-garcia/UTM2LATITUDE
基于 @Niall 的答案,此 PHP 函数还包括从 OSGB36 到 WSG84 的最终转换。
当前的前 3 个答案(SQL、JavaScript 和 PHP)都使用相同的算法(我认为)来生成 OSGB36 纬度和经度。这是 1836 年的标准,基于当时对地球曲率的理解。 WSG84是1984年的标准,其中坐标精确定位的位置最多可能相差约150m。
资料来源:http://www.movable-type.co。英国/scripts/latlong-os-gridref.html
Building upon @Niall's answer, this PHP function also includes the final transform from OSGB36 to WSG84.
The current top 3 answers (SQL, JavaScript and PHP) all use the same algorithm (I think) which produces OSGB36 latitude and longitude. That is an 1836 standard, based on the understanding of the curvature of the Earth at the time. WSG84 is a 1984 standard, in which the location pinpointed by the co-ordinates may differ by up to about 150m.
Source: http://www.movable-type.co.uk/scripts/latlong-os-gridref.html
基于之前的答案(谢谢!)我必须在 Google Bigquery 中执行此操作。
我使用以下命令在 BQ 中运行它,并在临时函数末尾添加一些 SQL:
Building on the previous answer (thank you!) I had to do this in Google Bigquery.
I used the following to run it in BQ with some SQL on the end of a temp function: