流程: 【1】c語言編寫逆地理編碼的函數(shù),利用curl庫和高德服務器進行地理坐標解析 【2】gcc生成動態(tài)鏈接庫 【3】postgreSQL中加載動態(tài)鏈接庫中的函數(shù) 【4】postgreSQL中將逆地理編碼函數(shù)的返回類型進行轉化 =========================================== 【
流程:
【1】c語言編寫逆地理編碼的函數(shù),利用curl庫和高德服務器進行地理坐標解析
【2】gcc生成動態(tài)鏈接庫
【3】postgreSQL中加載動態(tài)鏈接庫中的函數(shù)
【4】postgreSQL中將逆地理編碼函數(shù)的返回類型進行轉化
===========================================
【1】c語言編寫逆地理編碼的函數(shù),利用curl庫和高德服務器進行地理坐標解析
#include#include #include #include #include "postgres.h" #include "fmgr.h" PG_MODULE_MAGIC; int StringFind(const char *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)//回調(diào)函數(shù) { strcat((char*)userdata, (char*)ptr); return size*nmemb; } char* poi_list(char* longitude, char* latitude) { //主函數(shù) 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);//調(diào)用回調(diào)函數(shù) curl_easy_setopt(pEasyHandle, CURLOPT_TIMEOUT, 10); curl_easy_setopt(pEasyHandle, CURLOPT_FORBID_REUSE, 1); curl_easy_setopt(pEasyHandle, CURLOPT_WRITEDATA, szRet);//參數(shù)三對應回調(diào)函數(shù)的參數(shù)四 res = curl_easy_perform(pEasyHandle); curl_easy_cleanup(pEasyHandle); result = szRet ; //獲取整個json數(shù)據(jù) 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; }
hyc@hyc-csu:~/文檔/Curl_program$ gcc -fpic -I `pg_config --includedir-server` -c poiOutput.c -lcurl hyc@hyc-csu:~/文檔/Curl_program$ gcc -fpic -shared -o poiOutput.so poiOutput.o -lcurl hyc@hyc-csu:~/文檔/Curl_program$ sudo cp poiOutput.so `pg_config --libdir`
gpsDB=# load 'poiOutput.so'; LOAD gpsDB=# create function poi_list(cstring,cstring) returns cstring as 'poiOutput.so','poi_list' language C immutable strict; CREATE FUNCTION gpsDB=# select poi_list('118.744607','32.030886');
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);
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com