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

流程: 【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>
【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`
【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');
【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);


Statement
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
python中CURL和python requests的相互转换如何实现python中CURL和python requests的相互转换如何实现May 03, 2023 pm 12:49 PM

curl和Pythonrequests都是发送HTTP请求的强大工具。虽然curl是一种命令行工具,可让您直接从终端发送请求,但Python的请求库提供了一种更具编程性的方式来从Python代码中发送请求。将curl转换为Pythonrequestscurl命令的基本语法如下所示:curl[OPTIONS]URL将curl命令转换为Python请求时,我们需要将选项和URL转换为Python代码。这是一个示例curlPOST命令:curl-XPOSThttps://example.com/api

Linux下更新curl版本教程!Linux下更新curl版本教程!Mar 07, 2024 am 08:30 AM

在Linux下更新curl版本,您可以按照以下步骤进行操作:检查当前curl版本:首先,您需要确定当前系统中安装的curl版本。打开终端,并执行以下命令:curl--version该命令将显示当前curl的版本信息。确认可用的curl版本:在更新curl之前,您需要确定可用的最新版本。您可以访问curl的官方网站(curl.haxx.se)或相关的软件源,查找最新版本的curl。下载curl源代码:使用curl或浏览器,下载您选择的curl版本的源代码文件(通常为.tar.gz或.tar.bz2

PHP8.1发布:引入curl多个请求并发处理PHP8.1发布:引入curl多个请求并发处理Jul 08, 2023 pm 09:13 PM

PHP8.1发布:引入curl多个请求并发处理近日,PHP官方发布了最新版本的PHP8.1,其中引入了一个重要的特性:curl多个请求并发处理。这个新特性为开发者提供了一个更加高效和灵活的方式来处理多个HTTP请求,极大地提升了性能和用户体验。在以往的版本中,处理多个请求往往需要通过创建多个curl资源,并使用循环来分别发送和接收数据。这种方式虽然能够实现目

从头到尾:如何使用php扩展cURL进行HTTP请求从头到尾:如何使用php扩展cURL进行HTTP请求Jul 29, 2023 pm 05:07 PM

从头到尾:如何使用php扩展cURL进行HTTP请求引言:在Web开发中,经常需要与第三方API或其他远程服务器进行通信。而使用cURL进行HTTP请求是一种常见而强大的方式。本文将介绍如何使用php扩展cURL来执行HTTP请求,并提供一些实用的代码示例。一、准备工作首先,确保php已安装cURL扩展。可以在命令行执行php-m|grepcurl查

如何利用GitLab进行项目文档管理如何利用GitLab进行项目文档管理Oct 20, 2023 am 10:40 AM

如何利用GitLab进行项目文档管理一、背景介绍在软件开发过程中,项目文档是非常重要的资料,不仅能够帮助开发团队了解项目的需求和设计,还能提供给测试团队和客户参考。为了方便项目文档的版本控制和团队协作,我们可以利用GitLab来进行项目文档管理。GitLab是一个基于Git的版本控制系统,除了支持代码管理,还可以管理项目文档。二、GitLab环境搭建首先,我

PHP Curl中如何处理网页的 301 重定向?PHP Curl中如何处理网页的 301 重定向?Mar 08, 2024 am 11:36 AM

PHPCurl中如何处理网页的301重定向?在使用PHPCurl发送网络请求时,时常会遇到网页返回的301状态码,表示页面被永久重定向。为了正确处理这种情况,我们需要在Curl请求中添加一些特定的选项和处理逻辑。下面将详细介绍在PHPCurl中如何处理网页的301重定向,并提供具体的代码示例。301重定向处理原理301重定向是指服务器返回了一个30

linux curl是什么linux curl是什么Apr 20, 2023 pm 05:05 PM

在linux中,​curl是一个非常实用的、用来与服务器之间传输数据的工具,是一个利用URL规则在命令行下工作的文件传输工具;它支持文件的上传和下载,是综合传输工具。curl提供了一大堆非常有用的功能,包括代理访问、用户认证、ftp上传下载、HTTP POST、SSL连接、cookie支持、断点续传等等。

php curl怎么设置cookiephp curl怎么设置cookieSep 26, 2021 am 09:27 AM

php curl设置cookie的方法:1、创建PHP示例文件;2、通过“curl_setopt”函数设置cURL传输选项;3、在CURL中传递cookie即可。

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment