Skip to content

Commit ce84a8d

Browse files
committed
Add implementation of haversine function
1 parent 97bbed4 commit ce84a8d

File tree

4 files changed

+38
-122
lines changed

4 files changed

+38
-122
lines changed

config.m4

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ dnl [ --with-geospatial Include geospatial support])
1313

1414
dnl Otherwise use enable:
1515

16-
dnl PHP_ARG_ENABLE(geospatial, whether to enable geospatial support,
17-
dnl Make sure that the comment is aligned:
18-
dnl [ --enable-geospatial Enable geospatial support])
16+
PHP_ARG_ENABLE(geospatial, whether to enable geospatial support,
17+
[ --enable-geospatial Enable geospatial support])
1918

2019
if test "$PHP_GEOSPATIAL" != "no"; then
2120
dnl Write more examples of tests here...

geospatial.c

Lines changed: 26 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@
2727
#include "ext/standard/info.h"
2828
#include "php_geospatial.h"
2929

30-
/* If you declare any globals in php_geospatial.h uncomment this:
31-
ZEND_DECLARE_MODULE_GLOBALS(geospatial)
32-
*/
33-
34-
/* True global resources - no need for thread safety here */
35-
static int le_geospatial;
30+
ZEND_BEGIN_ARG_INFO(haversine_args,ZEND_SEND_BY_VAL)
31+
ZEND_ARG_INFO(0,fromLatitude)
32+
ZEND_ARG_INFO(0,fromLongitude)
33+
ZEND_ARG_INFO(0,toLatitude)
34+
ZEND_ARG_INFO(0,toLongitude)
35+
ZEND_END_ARG_INFO()
3636

3737
/* {{{ geospatial_functions[]
3838
*
3939
* Every user visible function must have an entry in geospatial_functions[].
4040
*/
4141
const zend_function_entry geospatial_functions[] = {
42-
PHP_FE(confirm_geospatial_compiled, NULL) /* For testing, remove later. */
42+
PHP_FE(haversine, haversine_args) /* For testing, remove later. */
4343
PHP_FE_END /* Must be the last line in geospatial_functions[] */
4444
};
4545
/* }}} */
@@ -53,9 +53,9 @@ zend_module_entry geospatial_module_entry = {
5353
"geospatial",
5454
geospatial_functions,
5555
PHP_MINIT(geospatial),
56-
PHP_MSHUTDOWN(geospatial),
57-
PHP_RINIT(geospatial), /* Replace with NULL if there's nothing to do at request start */
58-
PHP_RSHUTDOWN(geospatial), /* Replace with NULL if there's nothing to do at request end */
56+
NULL,
57+
NULL,
58+
NULL,
5959
PHP_MINFO(geospatial),
6060
#if ZEND_MODULE_API_NO >= 20010901
6161
"0.1", /* Replace with version number for your extension */
@@ -68,62 +68,17 @@ zend_module_entry geospatial_module_entry = {
6868
ZEND_GET_MODULE(geospatial)
6969
#endif
7070

71-
/* {{{ PHP_INI
72-
*/
73-
/* Remove comments and fill if you need to have entries in php.ini
74-
PHP_INI_BEGIN()
75-
STD_PHP_INI_ENTRY("geospatial.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_geospatial_globals, geospatial_globals)
76-
STD_PHP_INI_ENTRY("geospatial.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_geospatial_globals, geospatial_globals)
77-
PHP_INI_END()
78-
*/
79-
/* }}} */
80-
81-
/* {{{ php_geospatial_init_globals
82-
*/
83-
/* Uncomment this function if you have INI entries
84-
static void php_geospatial_init_globals(zend_geospatial_globals *geospatial_globals)
85-
{
86-
geospatial_globals->global_value = 0;
87-
geospatial_globals->global_string = NULL;
88-
}
89-
*/
90-
/* }}} */
91-
9271
/* {{{ PHP_MINIT_FUNCTION
9372
*/
9473
PHP_MINIT_FUNCTION(geospatial)
9574
{
96-
/* If you have INI entries, uncomment these lines
97-
REGISTER_INI_ENTRIES();
98-
*/
9975
return SUCCESS;
10076
}
10177
/* }}} */
10278

10379
/* {{{ PHP_MSHUTDOWN_FUNCTION
10480
*/
10581
PHP_MSHUTDOWN_FUNCTION(geospatial)
106-
{
107-
/* uncomment this line if you have INI entries
108-
UNREGISTER_INI_ENTRIES();
109-
*/
110-
return SUCCESS;
111-
}
112-
/* }}} */
113-
114-
/* Remove if there's nothing to do at request start */
115-
/* {{{ PHP_RINIT_FUNCTION
116-
*/
117-
PHP_RINIT_FUNCTION(geospatial)
118-
{
119-
return SUCCESS;
120-
}
121-
/* }}} */
122-
123-
/* Remove if there's nothing to do at request end */
124-
/* {{{ PHP_RSHUTDOWN_FUNCTION
125-
*/
126-
PHP_RSHUTDOWN_FUNCTION(geospatial)
12782
{
12883
return SUCCESS;
12984
}
@@ -134,43 +89,32 @@ PHP_RSHUTDOWN_FUNCTION(geospatial)
13489
PHP_MINFO_FUNCTION(geospatial)
13590
{
13691
php_info_print_table_start();
137-
php_info_print_table_header(2, "geospatial support", "enabled");
92+
php_info_print_table_header(2, "Geospatial functions", "enabled");
13893
php_info_print_table_end();
139-
140-
/* Remove comments if you have entries in php.ini
141-
DISPLAY_INI_ENTRIES();
142-
*/
14394
}
14495
/* }}} */
14596

14697

147-
/* Remove the following function when you have succesfully modified config.m4
148-
so that your module can be compiled into PHP, it exists only for testing
149-
purposes. */
150-
151-
/* Every user-visible function in PHP should document itself in the source */
152-
/* {{{ proto string confirm_geospatial_compiled(string arg)
153-
Return a string to confirm that the module is compiled in */
154-
PHP_FUNCTION(confirm_geospatial_compiled)
98+
PHP_FUNCTION(haversine)
15599
{
156-
char *arg = NULL;
157-
int arg_len, len;
158-
char *strg;
159-
160-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
100+
double fromLat, fromLong, toLat, toLong, deltaLat, deltaLong;
101+
double latH, longH, result;
102+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dddd", &fromLat, &fromLong, &toLat, &toLong) == FAILURE) {
161103
return;
162104
}
163105

164-
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "geospatial", arg);
165-
RETURN_STRINGL(strg, len, 0);
166-
}
167-
/* }}} */
168-
/* The previous line is meant for vim and emacs, so it can correctly fold and
169-
unfold functions in source code. See the corresponding marks just before
170-
function definition, where the functions purpose is also documented. Please
171-
follow this convention for the convenience of others editing your code.
172-
*/
106+
deltaLat = (fromLat - toLat) * GEO_DEG_TO_RAD;
107+
deltaLong = (fromLong - toLong) * GEO_DEG_TO_RAD;
173108

109+
latH = sin(deltaLat * 0.5);
110+
latH *= latH;
111+
longH = sin(deltaLong * 0.5);
112+
longH *= longH;
113+
114+
result = cos(fromLat * GEO_DEG_TO_RAD) * cos(toLat * GEO_DEG_TO_RAD);
115+
result = GEO_EARTH_RADIUS_IN_METERS * 2.0 * asin(sqrt(latH + result * longH));
116+
RETURN_DOUBLE(result);
117+
}
174118

175119
/*
176120
* Local variables:

php_geospatial.h

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,16 @@ extern zend_module_entry geospatial_module_entry;
3636
#include "TSRM.h"
3737
#endif
3838

39+
#define GEO_DEG_TO_RAD 0.017453292519943295769236907684886
40+
#define GEO_EARTH_RADIUS_IN_METERS 6372.797560856
41+
3942
PHP_MINIT_FUNCTION(geospatial);
40-
PHP_MSHUTDOWN_FUNCTION(geospatial);
41-
PHP_RINIT_FUNCTION(geospatial);
42-
PHP_RSHUTDOWN_FUNCTION(geospatial);
4343
PHP_MINFO_FUNCTION(geospatial);
4444

45-
PHP_FUNCTION(confirm_geospatial_compiled); /* For testing, remove later. */
46-
47-
/*
48-
Declare any global variables you may need between the BEGIN
49-
and END macros here:
50-
51-
ZEND_BEGIN_MODULE_GLOBALS(geospatial)
52-
long global_value;
53-
char *global_string;
54-
ZEND_END_MODULE_GLOBALS(geospatial)
55-
*/
56-
57-
/* In every utility function you add that needs to use variables
58-
in php_geospatial_globals, call TSRMLS_FETCH(); after declaring other
59-
variables used by that function, or better yet, pass in TSRMLS_CC
60-
after the last function argument and declare your utility function
61-
with TSRMLS_DC after the last declared argument. Always refer to
62-
the globals in your function as GEOSPATIAL_G(variable). You are
63-
encouraged to rename these macros something shorter, see
64-
examples in any other php module directory.
65-
*/
66-
67-
#ifdef ZTS
68-
#define GEOSPATIAL_G(v) TSRMG(geospatial_globals_id, zend_geospatial_globals *, v)
69-
#else
70-
#define GEOSPATIAL_G(v) (geospatial_globals.v)
71-
#endif
45+
PHP_FUNCTION(haversine);
7246

7347
#endif /* PHP_GEOSPATIAL_H */
7448

75-
7649
/*
7750
* Local variables:
7851
* tab-width: 4

tests/haversine.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
haversine() function - bastic test for haversine forumla
33
--FILE--
44
<?php
5-
$lon1 = -104.88544
6-
$lat1 = 39.06546
5+
$lat1 = 39.06546;
6+
$lon1 = -104.88544;
77

8-
$lon2 = -104.80
9-
$lat2 = lat1
10-
haversine($lat1, $lon2, $lat2, $lon2);
8+
$lat2 = $lat1;
9+
$lon2 = -104.80;
10+
var_dump(haversine($lat1, $lon1, $lat2, $lon2));
1111
?>
1212
--EXPECT--
13-
float(7.376)
13+
float(7.3785163137969)

0 commit comments

Comments
 (0)