From ba6cc38703800157a4b6c923e8d0238b6be9fcca Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 6 Aug 2015 16:04:58 +0800 Subject: [PATCH] add GEO function get_line_distance_km --- HISTORY | 3 +++ libfastcommon.spec | 2 +- php-fastcommon/fastcommon.c | 34 ++++++++++++++++++++++++++++++++++ php-fastcommon/fastcommon.h | 1 + src/shared_func.c | 19 +++++++++++++++++++ src/shared_func.h | 3 +++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/HISTORY b/HISTORY index 8248200..e8a113b 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.20 2015-08-06 + * add GEO function get_line_distance_km + Version 1.19 2015-07-24 * correct logger rotate time near 0 clock diff --git a/libfastcommon.spec b/libfastcommon.spec index 6973dc0..6de3d78 100644 --- a/libfastcommon.spec +++ b/libfastcommon.spec @@ -2,7 +2,7 @@ %define LibFastcommonDevel libfastcommon-devel Name: libfastcommon -Version: 1.0.19 +Version: 1.0.20 Release: 1%{?dist} Summary: c common functions library extracted from my open source projects FastDFS License: GPL diff --git a/php-fastcommon/fastcommon.c b/php-fastcommon/fastcommon.c index 692967d..6a8fffd 100644 --- a/php-fastcommon/fastcommon.c +++ b/php-fastcommon/fastcommon.c @@ -21,6 +21,7 @@ #include "logger.h" #include "hash.h" #include "sockopt.h" +#include "shared_func.h" #include "fastcommon.h" #define MAJOR_VERSION 1 @@ -40,6 +41,7 @@ const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, N ZEND_FE(fastcommon_gethostaddrs, NULL) ZEND_FE(fastcommon_time33_hash, NULL) ZEND_FE(fastcommon_simple_hash, NULL) + ZEND_FE(fastcommon_get_line_distance_km, NULL) {NULL, NULL, NULL} /* Must be the last line */ }; @@ -240,3 +242,35 @@ ZEND_FUNCTION(fastcommon_simple_hash) RETURN_LONG(simple_hash(str, str_len) & 0x7FFFFFFF); } +/* +double fastcommon_get_line_distance_km(double lat1, double lon1, + double lat2, double lon2) +return line distance in KM +*/ +ZEND_FUNCTION(fastcommon_get_line_distance_km) +{ + int argc; + double lat1; + double lon1; + double lat2; + double lon2; + + argc = ZEND_NUM_ARGS(); + if (argc != 4) { + logError("file: "__FILE__", line: %d, " + "fastcommon_get_line_distance_km parameters count: %d is invalid", + __LINE__, argc); + RETURN_BOOL(false); + } + + if (zend_parse_parameters(argc TSRMLS_CC, "dddd", &lat1, &lon1, + &lat2, &lon2) == FAILURE) + { + logError("file: "__FILE__", line: %d, " + "zend_parse_parameters fail!", __LINE__); + RETURN_BOOL(false); + } + + RETURN_DOUBLE(get_line_distance_km(lat1, lon1, lat2, lon2)); +} + diff --git a/php-fastcommon/fastcommon.h b/php-fastcommon/fastcommon.h index b6a29fa..6f6b973 100644 --- a/php-fastcommon/fastcommon.h +++ b/php-fastcommon/fastcommon.h @@ -21,6 +21,7 @@ ZEND_FUNCTION(fastcommon_version); ZEND_FUNCTION(fastcommon_gethostaddrs); ZEND_FUNCTION(fastcommon_time33_hash); ZEND_FUNCTION(fastcommon_simple_hash); +ZEND_FUNCTION(fastcommon_get_line_distance_km); #ifdef __cplusplus } diff --git a/src/shared_func.c b/src/shared_func.c index 74c2ddf..fa246bc 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "shared_func.h" #include "logger.h" #include "sockopt.h" @@ -2092,3 +2093,21 @@ int ignore_signal_pipe() return 0; } +double get_line_distance_km(const double lat1, const double lon1, + const double lat2, const double lon2) +{ +#define FAST_ABS(v) ((v) >= 0 ? (v) : -1 * (v)) +#define DISTANCE_PER_LATITUDE 111.111 + + double lat_value; + double lng_distance; + double lat_distance; + + lat_value = FAST_ABS(lat1) < FAST_ABS(lat2) ? lat1 : lat2; + lat_distance = FAST_ABS(lat1 - lat2) * DISTANCE_PER_LATITUDE; + lng_distance = FAST_ABS(lon1 - lon2) * DISTANCE_PER_LATITUDE * + cos(lat_value * 3.1415926 / 180.0); + + return sqrt(lat_distance * lat_distance + lng_distance * lng_distance); +} + diff --git a/src/shared_func.h b/src/shared_func.h index 212d20c..e88d90b 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -512,6 +512,9 @@ int set_file_utimes(const char *filename, const time_t new_time); */ int ignore_signal_pipe(); +double get_line_distance_km(const double lat1, const double lon1, + const double lat2, const double lon2); + #ifdef __cplusplus } #endif