27
27
#include "ext/standard/info.h"
28
28
#include "php_geospatial.h"
29
29
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 ()
36
36
37
37
/* {{{ geospatial_functions[]
38
38
*
39
39
* Every user visible function must have an entry in geospatial_functions[].
40
40
*/
41
41
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. */
43
43
PHP_FE_END /* Must be the last line in geospatial_functions[] */
44
44
};
45
45
/* }}} */
@@ -53,9 +53,9 @@ zend_module_entry geospatial_module_entry = {
53
53
"geospatial" ,
54
54
geospatial_functions ,
55
55
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 ,
59
59
PHP_MINFO (geospatial ),
60
60
#if ZEND_MODULE_API_NO >= 20010901
61
61
"0.1" , /* Replace with version number for your extension */
@@ -68,62 +68,17 @@ zend_module_entry geospatial_module_entry = {
68
68
ZEND_GET_MODULE (geospatial )
69
69
#endif
70
70
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
-
92
71
/* {{{ PHP_MINIT_FUNCTION
93
72
*/
94
73
PHP_MINIT_FUNCTION (geospatial )
95
74
{
96
- /* If you have INI entries, uncomment these lines
97
- REGISTER_INI_ENTRIES();
98
- */
99
75
return SUCCESS ;
100
76
}
101
77
/* }}} */
102
78
103
79
/* {{{ PHP_MSHUTDOWN_FUNCTION
104
80
*/
105
81
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 )
127
82
{
128
83
return SUCCESS ;
129
84
}
@@ -134,43 +89,32 @@ PHP_RSHUTDOWN_FUNCTION(geospatial)
134
89
PHP_MINFO_FUNCTION (geospatial )
135
90
{
136
91
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" );
138
93
php_info_print_table_end ();
139
-
140
- /* Remove comments if you have entries in php.ini
141
- DISPLAY_INI_ENTRIES();
142
- */
143
94
}
144
95
/* }}} */
145
96
146
97
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 )
155
99
{
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 ) {
161
103
return ;
162
104
}
163
105
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 ;
173
108
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
+ }
174
118
175
119
/*
176
120
* Local variables:
0 commit comments