是否可以在 Objective-C 中实现半正矢公式并从 SQLite 调用它?

发布于 2024-08-11 05:00:16 字数 614 浏览 7 评论 0原文

据我了解,SQLite 没有数学函数来正确实现直接 SQL 中的 Haversine 公式。我认为这应该可以使用 外部函数 实现,并用 C 语言实现目标

是在 iPhone 中拥有一个 SQLite 数据库,并能够根据到用户当前位置的距离进行排序。我已经搜索过,但找不到任何已完成此操作的示例。我认为困难的部分是使函数声明正确。我希望的最终结果是能够执行如下 SQL 语句:

SELECT * FROM LOCATION loc ORDER BY distance(loc.lat, loc.long, ?, ?)

我有一个 C 半正弦公式。函数定义如下:

float distance( float nLat1, float nLon1, float nLat2, float nLon2 );

有谁知道这是否可能和/或有一些示例代码可以开始?

As I understand, SQLite doesn't have the math functions to properly implement the Haversine formula in straight SQL. I'm thinking this should be possible using an external function, with the implementation being in C.

The goal is to have a SQLite database in an iPhone, and to be able to sort by the distance to the user's current location. I've searched, but I can't find an example of any examples of this being done. I think the difficult parts would be getting the function declarations correct. The end result I'm hoping for, is to be able to execute a SQL statement like:

SELECT * FROM LOCATION loc ORDER BY distance(loc.lat, loc.long, ?, ?)

I have a C Haversine formula. The function definition is as follows:

float distance( float nLat1, float nLon1, float nLat2, float nLon2 );

Does anyone know if this is possible and/or have some example code to start from?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天煞孤星 2024-08-18 05:00:16

我对这篇文章很幸运: http://www.thismuchiknow.co.uk/ ?p=71

I just had good luck with this post: http://www.thismuchiknow.co.uk/?p=71

一片旧的回忆 2024-08-18 05:00:16

这演示了一个 sqlite 函数,它接受一个字符串参数并返回一个字符串结果。

在您的情况下,您需要一个读取四个浮点数并返回一个浮点数的函数,但原理是相同的(您可以将 sqlite3_value_text 替换为 sqlite3_value_doublesqlite3_result_text em> 与 sqlite3_result_double):

#include <stdlib.h>
#include <sqlite3.h>
#include <stdio.h>


void haver(sqlite3_context* ctx,int cnt,sqlite3_value** val)
{
    printf("In SQLite haver implementation, called for value: %s\n", sqlite3_value_text(*val));

    char * resultOfCall = "Result of function call"; //this would call the distance function
    sqlite3_result_text(ctx, resultOfCall, strlen(resultOfCall), NULL);
}
int cback (void* udata,int ncol,char** value,char** colname)
{
    int i=0;
    for(;i<ncol;i++)
    printf("Result column: %s value: %s   \n", colname[i], value[i]);
    return 0;
}
int main()
{

    sqlite3 * handle;
    int res = sqlite3_open("./test.sql", &handle);

    res = sqlite3_create_function(handle, "haver", 1, SQLITE_UTF8, NULL, &haver, NULL, NULL);

    char * errmsg = NULL;   
    res = sqlite3_exec(handle, "select haver(w) from t", &cback, NULL, &errmsg);
    printf("sqlite3_exec result: %d %s\n", res, errmsg != NULL ? errmsg : "No error");

    sqlite3_close(handle);
}

This demonstrates a sqlite function that takes in one string parameter and returns a string result.

In your case you would need a function that reads four floats and returns a float but the principle is the same (you would replace sqlite3_value_text with sqlite3_value_double and sqlite3_result_text with sqlite3_result_double):

#include <stdlib.h>
#include <sqlite3.h>
#include <stdio.h>


void haver(sqlite3_context* ctx,int cnt,sqlite3_value** val)
{
    printf("In SQLite haver implementation, called for value: %s\n", sqlite3_value_text(*val));

    char * resultOfCall = "Result of function call"; //this would call the distance function
    sqlite3_result_text(ctx, resultOfCall, strlen(resultOfCall), NULL);
}
int cback (void* udata,int ncol,char** value,char** colname)
{
    int i=0;
    for(;i<ncol;i++)
    printf("Result column: %s value: %s   \n", colname[i], value[i]);
    return 0;
}
int main()
{

    sqlite3 * handle;
    int res = sqlite3_open("./test.sql", &handle);

    res = sqlite3_create_function(handle, "haver", 1, SQLITE_UTF8, NULL, &haver, NULL, NULL);

    char * errmsg = NULL;   
    res = sqlite3_exec(handle, "select haver(w) from t", &cback, NULL, &errmsg);
    printf("sqlite3_exec result: %d %s\n", res, errmsg != NULL ? errmsg : "No error");

    sqlite3_close(handle);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文