使用 SQLite 计算大圆距离
这是我的问题,我有一个包含位置和纬度/经度的 SQLite 表。基本上我需要:
SELECT location, HAVERSINE(lat, lon) AS distance FROM location ORDER BY distance ASC;
HAVERSINE()
是一个 PHP 函数,它应该返回 大圆距离(以英里或公里为单位)给定一对纬度和经度值。 其中一对应由 PHP 提供,另一对应由 locations
表中可用的每个纬度/经度行提供。
由于 SQLite 没有任何地理空间扩展(AFAIK SpatiaLite< /a> 存在但仍然...)我猜最好的方法是使用自定义函数与 PDO 方法之一:
我认为对于这种情况 PDO::sqliteCreateFunction()
就足够了,但是我对此函数的有限经验可以简化为类似于 PHP 手册中提供的用例:
$db = new PDO('sqlite:geo.db');
function md5_and_reverse($string) { return strrev(md5($string)); }
$db->sqliteCreateFunction('md5rev', 'md5_and_reverse', 1);
$rows = $db->query('SELECT md5rev(filename) FROM files')->fetchAll();
我在弄清楚如何获得 SQLite 用户定义函数 < strong>同时处理来自 PHP 的数据和表数据,如果有人能帮助我解决这个问题,同时更好地理解 SQLite UDF(SQLite IMO 的一大胜利),我将不胜感激。
提前致谢!
Here is my problem, I have a SQLite table with locations and latitudes / longitudes. Basically I need to:
SELECT location, HAVERSINE(lat, lon) AS distance FROM location ORDER BY distance ASC;
HAVERSINE()
is a PHP function that should return the Great-Circle Distance (in miles or km) given a pair of latitude and longitude values. One of these pairs should be provided by PHP and the other pair should be provided by each latitude / longitude row available in the locations
table.
Since SQLite doesn't has any Geo Spatial extension (AFAIK SpatiaLite exists but still...) I'm guessing the best approach would be to use a custom function with either one of the PDO methods:
I think for this case PDO::sqliteCreateFunction()
would be enough, however my limited experience with this function can be reduced to usage cases similar to the one provided in the PHP Manual:
$db = new PDO('sqlite:geo.db');
function md5_and_reverse($string) { return strrev(md5($string)); }
$db->sqliteCreateFunction('md5rev', 'md5_and_reverse', 1);
$rows = $db->query('SELECT md5rev(filename) FROM files')->fetchAll();
I'm having some trouble figuring out how can I get an SQLite user defined function to process data from PHP and table data at the same time and I would appreciate if someone could help me solve this problem while also understanding SQLite UDFs (a big win of SQLite IMO) a little bit better.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
到目前为止我只能想到这个解决方案:
然后执行以下冗长的查询:
如果有人能想到更好的解决方案,请告诉我。
我刚刚发现了这个有趣的链接,我明天会尝试一下。< /强>
So far I could only think of this solution:
And then execute the following lengthy query:
If anyone can think of a better solution please let me know.
I just found this interesting link, I'll try it tomorrow.
来自您的“有趣的链接”。
然后使用以下命令进行查询:
编辑(通过 QOP): 我终于再次需要这个,这个解决方案效果很好,我只是最终修改了代码一点,它有点不那么冗长,并且可以处理非- 优雅的数值,这里是:
From your "interesting link".
Then do a query with:
EDIT (by QOP): I finally needed this again and this solution worked out great, I just ended up modifying the code a bit to it is a bit less verbose and handles non-numeric values gracefully, here it is:
建立阿利克斯的答案......
我想这将允许您在问题中指定的查询工作。
Building off Alix's answer...
I would imagine that this would allow the query that you specified in your question to work.