• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

利用curl进行逆地理编码_c语言编写动态链接库对PostgreSQL进行扩

mysql 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论

流程: 【1】c语言编写逆地理编码的函数,利用curl库和高德服务器进行地理坐标解析 【2】gcc生成动态链接库 【3】postgreSQL中加载动态链接库中的函数 【4】postgreSQL中将逆地理编码函数的返回类型进行转化 =========================================== 【

流程:
【1】c语言编写逆地理编码的函数,利用curl库和高德服务器进行地理坐标解析
【2】gcc生成动态链接库
【3】postgreSQL中加载动态链接库中的函数
【4】postgreSQL中将逆地理编码函数的返回类型进行转化
===========================================
【1】c语言编写逆地理编码的函数,利用curl库和高德服务器进行地理坐标解析

#include #include #include #include #include "postgres.h"#include "fmgr.h"PG_MODULE_MAGIC;int StringFind(const char

本文来源gao!%daima.com搞$代*!码$网9

*pSrc,const char *pDst)//字符串位置查找,返回源字符串的位置 { int i, j; for (i=0; pSrc[i]!='\0'; i++) { if(pSrc[i]!=pDst[0]) continue; j = 0; while(pDst[j]!='\0' && pSrc[i+j]!='\0') { j++; if(pDst[j]!=pSrc[i+j]) break; } if(pDst[j]=='\0') return i; } return -1; } size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)//回调函数{strcat((char*)userdata, (char*)ptr);return size*nmemb;}char* poi_list(char* longitude, char* latitude) { //主函数 int mPos=0; char* result; char* strLongitude=longitude; char* strLatitude=latitude; char tempLongitude[25]="longitude="; char tempLatitude[25]="&latitude="; char* pstrLongitude; char* pstrLatitude; pstrLongitude=strcat((char*)tempLongitude,strLongitude); pstrLatitude=strcat((char*)tempLatitude,strLatitude); char* locationInfor=strcat(pstrLongitude,pstrLatitude); char finalResult[5120]={'\0'}; int i=0; char* pDst="poi_list"; char szRet[5120] = {'\0'};//结果存储 char szpage[256] = "http://ditu.amap.com/service/regeo?"; char *myurl =strcat(szpage,locationInfor); CURLcode res; res = curl_global_init(CURL_GLOBAL_ALL);//初始化 if (res != CURLE_OK) { result = psprintf( "Failed to global init default [%d]\n", res ); return result; } CURL* pEasyHandle = curl_easy_init(); // 初始化 curl_easy_setopt(pEasyHandle, CURLOPT_URL, myurl);//传入url curl_easy_setopt(pEasyHandle, CURLOPT_WRITEFUNCTION, &write_callback);//调用回调函数 curl_easy_setopt(pEasyHandle, CURLOPT_TIMEOUT, 10); curl_easy_setopt(pEasyHandle, CURLOPT_FORBID_REUSE, 1); curl_easy_setopt(pEasyHandle, CURLOPT_WRITEDATA, szRet);//参数三对应回调函数的参数四 res = curl_easy_perform(pEasyHandle); curl_easy_cleanup(pEasyHandle); result = szRet ; //获取整个json数据 mPos=StringFind(result,pDst); int mNewPos=mPos+10;//过滤掉poi_list字段 while(szRet[mNewPos]!='\0') { finalResult[i]=szRet[mNewPos]; mNewPos++; i++; } i=i-2; finalResult[i]='\0'; char *result0=finalResult; return result0;}

【2】gcc生成动态链接库

hyc@hyc-csu:~/文档/Curl_program$ gcc -fpic -I `pg_config --includedir-server` -c poiOutput.c -lcurlhyc@hyc-csu:~/文档/Curl_program$ gcc -fpic -shared -o poiOutput.so poiOutput.o -lcurlhyc@hyc-csu:~/文档/Curl_program$ sudo cp poiOutput.so `pg_config --libdir`

【3】postgreSQL中加载动态链接库中的函数

gpsDB=# load 'poiOutput.so';LOADgpsDB=# create function poi_list(cstring,cstring)returns cstringas 'poiOutput.so','poi_list'language C immutable strict;CREATE FUNCTIONgpsDB=# select poi_list('118.744607','32.030886');

【4】postgreSQL中将逆地理编码函数的返回类型进行转化【参数null::poiarray,其中poiarray表结构与json结构相对应,详情点击此处】

gpsDB=# select (select poi_list('112.931850','28.169100'))::json;ERROR:  cannot cast type cstring to json    ?//cstring类型转化为text类型,再转化为json类型gpsDB=# select * from json_populate_recordset(null::poiarray,(cast((select poi_list('118.744607','32.030886')) as text))::json);gpsDB=# select * from json_populate_recordset(null::poiarray,(poi_list('118.744607','32.030886')::text)::json);

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:利用curl进行逆地理编码_c语言编写动态链接库对PostgreSQL进行扩
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址