Using Curl for inverse geocoding _c language to write dynamic link libraries to extend PostgreSQL

Source: Internet
Author: User
Tags curl extend postgresql



Process:
"1" C language to write inverse geocoding functions, using Curl Library and the German server for geographical coordinates resolution
"2" GCC build dynamic link library
Functions in "3" PostgreSQL loaded in a dynamic-link library
Conversion of Inverse geocoding function return type in "4" PostgreSQL
===========================================
"1" C language to write inverse geocoding functions, using Curl Library and the German server for geographical coordinates resolution





#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl / curl.h>

#include "postgres.h"
#include "fmgr.h"

PG_MODULE_MAGIC;

int StringFind (const char * pSrc, const char * pDst) // String position search, returns the position of the source string
{
    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) // callback function
{
strcat ((char *) userdata, (char *) ptr);
return size * nmemb;
}

char * poi_list (char * longitude, char * latitude) {// Main function

int mPos = 0;
char * result;
char * strLongitude = longitude;
char * strLatitude = latitude;

char tempLongitude [25] = "longitude =";
char tempLatitude [25] = "& latitude =";
char * pstrLongitude;
char * pstrLatitude;
Ranch
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'}; // result storage
char szpage [256] = "http://ditu.amap.com/service/regeo?";
char * myurl = strcat (szpage, locationInfor);

CURLcode res;
res = curl_global_init (CURL_GLOBAL_ALL); // Init
if (res! = CURLE_OK)
{
result = psprintf ("Failed to global init default [% d] \ n", res);
     return result;
}

CURL * pEasyHandle = curl_easy_init (); // initialize
Ranch
curl_easy_setopt (pEasyHandle, CURLOPT_URL, myurl); // Incoming URL
curl_easy_setopt (pEasyHandle, CURLOPT_WRITEFUNCTION, & write_callback); // Call the callback function
curl_easy_setopt (pEasyHandle, CURLOPT_TIMEOUT, 10);
curl_easy_setopt (pEasyHandle, CURLOPT_FORBID_REUSE, 1);
curl_easy_setopt (pEasyHandle, CURLOPT_WRITEDATA, szRet); // parameter three corresponds to parameter four of the callback

res = curl_easy_perform (pEasyHandle);
curl_easy_cleanup (pEasyHandle);

result = szRet; // Get the entire json data
Ranch
mPos = StringFind (result, pDst);
Ranch
int mNewPos = mPos + 10; // Filter out the poi_list field

while (szRet [mNewPos]! = '\ 0')
{

finalResult [i] = szRet [mNewPos];
mNewPos ++;
i ++;
}
i = i-2;
finalResult [i] = '\ 0';
char * result0 = finalResult;
return result0;
}
"2" GCC build dynamic link library
[email protected]: ~ / documents / Curl_program $ gcc -fpic -I `pg_config --includedir-server` -c poiOutput.c -lcurl
[email protected]: ~ / documents / Curl_program $ gcc -fpic -shared -o poiOutput.so poiOutput.o -lcurl
[email protected]: ~ / documents / Curl_program $ sudo cp poiOutput.so `pg_config --libdir`
functions in "3" PostgreSQL loaded in a dynamic-link library
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');
"4" PostgreSQL converts the return type of the inverse geocoding function to "parameter null::p Oiarray, where the Poiarray table structure corresponds to the JSON structure, click here for details"
gpsDB = # select (select poi_list ('112.931850', '28.169100')) :: json;
ERROR: cannot cast type cstring to json? // cstring type is converted to text type, and then converted to json type
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);  



Using Curl for inverse geocoding _c language to write dynamic link libraries to extend PostgreSQL



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.