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

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

Jun 07, 2016 pm 02:50 PM
curl p use dynamic geography write coding language conduct Link

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

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

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

#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)//回调函数
{
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;
}</curl></string.h></stdlib.h></stdio.h>
Copy after login
【2】gcc生成动态链接库
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`
Copy after login
【3】postgreSQL中加载动态链接库中的函数
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');
Copy after login
【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);
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use magnet links How to use magnet links Feb 18, 2024 am 10:02 AM

Magnet link is a link method for downloading resources, which is more convenient and efficient than traditional download methods. Magnet links allow you to download resources in a peer-to-peer manner without relying on an intermediary server. This article will introduce how to use magnet links and what to pay attention to. 1. What is a magnet link? A magnet link is a download method based on the P2P (Peer-to-Peer) protocol. Through magnet links, users can directly connect to the publisher of the resource to complete resource sharing and downloading. Compared with traditional downloading methods, magnetic

How to download links starting with 115://? Download method introduction How to download links starting with 115://? Download method introduction Mar 14, 2024 am 11:58 AM

Recently, many users have been asking the editor, how to download links starting with 115://? If you want to download links starting with 115://, you need to use the 115 browser. After you download the 115 browser, let's take a look at the download tutorial compiled by the editor below. Introduction to how to download links starting with 115:// 1. Log in to 115.com, download and install the 115 browser. 2. Enter: chrome://extensions/ in the 115 browser address bar, enter the extension center, search for Tampermonkey, and install the corresponding plug-in. 3. Enter in the address bar of 115 browser: Grease Monkey Script: https://greasyfork.org/en/

Convert VirtualBox fixed disk to dynamic disk and vice versa Convert VirtualBox fixed disk to dynamic disk and vice versa Mar 25, 2024 am 09:36 AM

When creating a virtual machine, you will be asked to select a disk type, you can select fixed disk or dynamic disk. What if you choose fixed disks and later realize you need dynamic disks, or vice versa? Good! You can convert one to the other. In this post, we will see how to convert VirtualBox fixed disk to dynamic disk and vice versa. A dynamic disk is a virtual hard disk that initially has a small size and grows in size as you store data in the virtual machine. Dynamic disks are very efficient at saving storage space because they only take up as much host storage space as needed. However, as disk capacity expands, your computer's performance may be slightly affected. Fixed disks and dynamic disks are commonly used in virtual machines

How to get the WeChat video account link? How to add product links to WeChat video account? How to get the WeChat video account link? How to add product links to WeChat video account? Mar 22, 2024 pm 09:36 PM

As part of the WeChat ecosystem, WeChat video accounts have gradually become an important promotion tool for content creators and merchants. Getting links to video accounts on this platform is crucial for sharing and disseminating content. The following will introduce in detail how to obtain the WeChat video account link and how to add product links to the video account to improve the dissemination effect of the content. 1. How to get the WeChat video account link? After posting a video on your WeChat video account, the system will automatically create a video link. Authors can copy the link after publishing to facilitate sharing and dissemination. After logging in to your WeChat video account, you can browse the homepage of your video account. On the home page, each video is accompanied by a corresponding link so you can copy or share it directly. 3. Search video account: Enter the video account name in the WeChat search box

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

3 Ways to Change Language on iPhone 3 Ways to Change Language on iPhone Feb 02, 2024 pm 04:12 PM

It's no secret that the iPhone is one of the most user-friendly electronic gadgets, and one of the reasons why is that it can be easily personalized to your liking. In Personalization, you can change the language to a different language than the one you selected when setting up your iPhone. If you're familiar with multiple languages, or your iPhone's language setting is wrong, you can change it as we explain below. How to Change the Language of iPhone [3 Methods] iOS allows users to freely switch the preferred language on iPhone to adapt to different needs. You can change the language of interaction with Siri to facilitate communication with the voice assistant. At the same time, when using the local keyboard, you can easily switch between multiple languages ​​to improve input efficiency.

What are the requirements for a video link? How to link the video account with goods? What are the requirements for a video link? How to link the video account with goods? Mar 07, 2024 pm 01:13 PM

With the popularity of short video platforms, more and more creators are beginning to use video accounts to create and promote content. Video accounts can not only showcase personal talents, but also realize commercial monetization through product links. However, to add a link to a video account, certain conditions must be met. 1. What are the requirements for a video link? Video account authentication is a prerequisite for adding links to your video account. Currently, major short video platforms such as Douyin and Kuaishou provide certification services, which mainly include two types: personal certification and institutional certification. Personal certification requires the submission of real identity information, while institutional certification requires the provision of certification materials from relevant companies or organizations. After completing the authentication, users can add links to their video accounts to enhance the credibility and authority of their accounts. One of the video link

How to link on Doudian - Tutorial on linking on Doudian How to link on Doudian - Tutorial on linking on Doudian Mar 06, 2024 am 08:40 AM

Many friends still don’t know how to link on Doudian, so the editor below will explain the tutorial on how to link on Doudian. If you are in need, hurry up and take a look. I believe it will be helpful to everyone. Step 1: First open the Doudian computer terminal and enter "Window Product Management" on the left column, as shown in the picture. Step 2: Then click "Add Product" in the upper right corner, as shown in the picture. Step 3: Then copy and paste our product link, as shown in the picture. Step 4: Then click "Confirm Add", as shown in the picture. Step 5: Finally enter the introduction, pictures and other information, and then click "Confirm" to link on Douyin, as shown in the picture. The above is the entire content of how to add links to Doudian brought to you by the editor. I hope it can be helpful to everyone.

See all articles