├── EXPERIMENTAL ├── CREDITS ├── tests ├── haversine.phpt └── 001.phpt ├── config.w32 ├── geospatial.php ├── .gitignore ├── LICENSE ├── php_geospatial.h ├── config.m4 └── geospatial.c /EXPERIMENTAL: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /CREDITS: -------------------------------------------------------------------------------- 1 | PHP geospatial extension 2 | 3 | Finally commenced at PHPNW12 4 | 5 | Marcus Deglos 6 | Michael Maclean 7 | Ryan Mauger 8 | 9 | -------------------------------------------------------------------------------- /tests/haversine.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | haversine() function - bastic test for haversine forumla 3 | --FILE-- 4 | 12 | --EXPECT-- 13 | float(7.3785163137969) 14 | -------------------------------------------------------------------------------- /config.w32: -------------------------------------------------------------------------------- 1 | // $Id$ 2 | // vim:ft=javascript 3 | 4 | // If your extension references something external, use ARG_WITH 5 | // ARG_WITH("geospatial", "for geospatial support", "no"); 6 | 7 | // Otherwise, use ARG_ENABLE 8 | // ARG_ENABLE("geospatial", "enable geospatial support", "no"); 9 | 10 | if (PHP_GEOSPATIAL != "no") { 11 | EXTENSION("geospatial", "geospatial.c"); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /geospatial.php: -------------------------------------------------------------------------------- 1 | "; 3 | 4 | if(!extension_loaded('geospatial')) { 5 | dl('geospatial.' . PHP_SHLIB_SUFFIX); 6 | } 7 | $module = 'geospatial'; 8 | $functions = get_extension_funcs($module); 9 | echo "Functions available in the test extension:$br\n"; 10 | foreach($functions as $func) { 11 | echo $func."$br\n"; 12 | } 13 | echo "$br\n"; 14 | $function = 'confirm_' . $module . '_compiled'; 15 | if (extension_loaded($module)) { 16 | $str = $function($module); 17 | } else { 18 | $str = "Module $module is not compiled into PHP"; 19 | } 20 | echo "$str\n"; 21 | ?> 22 | -------------------------------------------------------------------------------- /tests/001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Check for geospatial presence 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 20 | --EXPECT-- 21 | geospatial extension is available 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.deps 2 | /Makefile 3 | /*.lo 4 | /*.loT 5 | /*.slo 6 | /*.mk 7 | /*.la 8 | /.libs 9 | /libs.mk 10 | /ac*.m4 11 | /build 12 | /config.h 13 | /config.h.in 14 | /config.nice 15 | /config.sub 16 | /configure 17 | /configure.in 18 | /config.status 19 | /config.cache 20 | /conftest 21 | /conftest.c 22 | /core 23 | /dynlib.m4 24 | /install-sh 25 | /ltmain.sh 26 | /include 27 | /Makefile.fragments 28 | /Makefile.global 29 | /Makefile.objects 30 | /missing 31 | /mkinstalldirs 32 | /modules 33 | /scan_makefile_in.awk 34 | /config.guess 35 | /*swp 36 | /config.log 37 | /libtool 38 | /Debug 39 | /Release 40 | /Debug_TS 41 | /Release_TS 42 | /*.plg 43 | /*.patch 44 | /*.tgz 45 | /*.ncb 46 | /*.opt 47 | /*.dsw 48 | /autom4te.cache 49 | /run-tests-config.php 50 | /run-tests.php 51 | /tmp-php.ini 52 | .svn 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Marcus Deglos. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of the nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | 26 | -------------------------------------------------------------------------------- /php_geospatial.h: -------------------------------------------------------------------------------- 1 | /* 2 | +----------------------------------------------------------------------+ 3 | | PHP Version 5 | 4 | +----------------------------------------------------------------------+ 5 | | Copyright (c) 1997-2012 The PHP Group | 6 | +----------------------------------------------------------------------+ 7 | | This source file is subject to version 3.01 of the PHP license, | 8 | | that is bundled with this package in the file LICENSE, and is | 9 | | available through the world-wide-web at the following url: | 10 | | http://www.php.net/license/3_01.txt | 11 | | If you did not receive a copy of the PHP license and are unable to | 12 | | obtain it through the world-wide-web, please send a note to | 13 | | license@php.net so we can mail you a copy immediately. | 14 | +----------------------------------------------------------------------+ 15 | | Author: | 16 | +----------------------------------------------------------------------+ 17 | */ 18 | 19 | /* $Id$ */ 20 | 21 | #ifndef PHP_GEOSPATIAL_H 22 | #define PHP_GEOSPATIAL_H 23 | 24 | extern zend_module_entry geospatial_module_entry; 25 | #define phpext_geospatial_ptr &geospatial_module_entry 26 | 27 | #ifdef PHP_WIN32 28 | # define PHP_GEOSPATIAL_API __declspec(dllexport) 29 | #elif defined(__GNUC__) && __GNUC__ >= 4 30 | # define PHP_GEOSPATIAL_API __attribute__ ((visibility("default"))) 31 | #else 32 | # define PHP_GEOSPATIAL_API 33 | #endif 34 | 35 | #ifdef ZTS 36 | #include "TSRM.h" 37 | #endif 38 | 39 | #define GEO_DEG_TO_RAD 0.017453292519943295769236907684886 40 | #define GEO_EARTH_RADIUS_IN_METERS 6372.797560856 41 | 42 | PHP_MINIT_FUNCTION(geospatial); 43 | PHP_MINFO_FUNCTION(geospatial); 44 | 45 | PHP_FUNCTION(haversine); 46 | 47 | #endif /* PHP_GEOSPATIAL_H */ 48 | 49 | /* 50 | * Local variables: 51 | * tab-width: 4 52 | * c-basic-offset: 4 53 | * End: 54 | * vim600: noet sw=4 ts=4 fdm=marker 55 | * vim<600: noet sw=4 ts=4 56 | */ 57 | -------------------------------------------------------------------------------- /config.m4: -------------------------------------------------------------------------------- 1 | dnl $Id$ 2 | dnl config.m4 for extension geospatial 3 | 4 | dnl Comments in this file start with the string 'dnl'. 5 | dnl Remove where necessary. This file will not work 6 | dnl without editing. 7 | 8 | dnl If your extension references something external, use with: 9 | 10 | dnl PHP_ARG_WITH(geospatial, for geospatial support, 11 | dnl Make sure that the comment is aligned: 12 | dnl [ --with-geospatial Include geospatial support]) 13 | 14 | dnl Otherwise use enable: 15 | 16 | PHP_ARG_ENABLE(geospatial, whether to enable geospatial support, 17 | [ --enable-geospatial Enable geospatial support]) 18 | 19 | if test "$PHP_GEOSPATIAL" != "no"; then 20 | dnl Write more examples of tests here... 21 | 22 | dnl # --with-geospatial -> check with-path 23 | dnl SEARCH_PATH="/usr/local /usr" # you might want to change this 24 | dnl SEARCH_FOR="/include/geospatial.h" # you most likely want to change this 25 | dnl if test -r $PHP_GEOSPATIAL/$SEARCH_FOR; then # path given as parameter 26 | dnl GEOSPATIAL_DIR=$PHP_GEOSPATIAL 27 | dnl else # search default path list 28 | dnl AC_MSG_CHECKING([for geospatial files in default path]) 29 | dnl for i in $SEARCH_PATH ; do 30 | dnl if test -r $i/$SEARCH_FOR; then 31 | dnl GEOSPATIAL_DIR=$i 32 | dnl AC_MSG_RESULT(found in $i) 33 | dnl fi 34 | dnl done 35 | dnl fi 36 | dnl 37 | dnl if test -z "$GEOSPATIAL_DIR"; then 38 | dnl AC_MSG_RESULT([not found]) 39 | dnl AC_MSG_ERROR([Please reinstall the geospatial distribution]) 40 | dnl fi 41 | 42 | dnl # --with-geospatial -> add include path 43 | dnl PHP_ADD_INCLUDE($GEOSPATIAL_DIR/include) 44 | 45 | dnl # --with-geospatial -> check for lib and symbol presence 46 | dnl LIBNAME=geospatial # you may want to change this 47 | dnl LIBSYMBOL=geospatial # you most likely want to change this 48 | 49 | dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL, 50 | dnl [ 51 | dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $GEOSPATIAL_DIR/lib, GEOSPATIAL_SHARED_LIBADD) 52 | dnl AC_DEFINE(HAVE_GEOSPATIALLIB,1,[ ]) 53 | dnl ],[ 54 | dnl AC_MSG_ERROR([wrong geospatial lib version or lib not found]) 55 | dnl ],[ 56 | dnl -L$GEOSPATIAL_DIR/lib -lm 57 | dnl ]) 58 | dnl 59 | dnl PHP_SUBST(GEOSPATIAL_SHARED_LIBADD) 60 | 61 | PHP_NEW_EXTENSION(geospatial, geospatial.c, $ext_shared) 62 | fi 63 | -------------------------------------------------------------------------------- /geospatial.c: -------------------------------------------------------------------------------- 1 | /* 2 | +----------------------------------------------------------------------+ 3 | | PHP Version 5 | 4 | +----------------------------------------------------------------------+ 5 | | Copyright (c) 1997-2012 The PHP Group | 6 | +----------------------------------------------------------------------+ 7 | | This source file is subject to version 3.01 of the PHP license, | 8 | | that is bundled with this package in the file LICENSE, and is | 9 | | available through the world-wide-web at the following url: | 10 | | http://www.php.net/license/3_01.txt | 11 | | If you did not receive a copy of the PHP license and are unable to | 12 | | obtain it through the world-wide-web, please send a note to | 13 | | license@php.net so we can mail you a copy immediately. | 14 | +----------------------------------------------------------------------+ 15 | | Author: | 16 | +----------------------------------------------------------------------+ 17 | */ 18 | 19 | /* $Id$ */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ini.h" 27 | #include "ext/standard/info.h" 28 | #include "php_geospatial.h" 29 | 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 | 37 | /* {{{ geospatial_functions[] 38 | * 39 | * Every user visible function must have an entry in geospatial_functions[]. 40 | */ 41 | const zend_function_entry geospatial_functions[] = { 42 | PHP_FE(haversine, haversine_args) /* For testing, remove later. */ 43 | PHP_FE_END /* Must be the last line in geospatial_functions[] */ 44 | }; 45 | /* }}} */ 46 | 47 | /* {{{ geospatial_module_entry 48 | */ 49 | zend_module_entry geospatial_module_entry = { 50 | #if ZEND_MODULE_API_NO >= 20010901 51 | STANDARD_MODULE_HEADER, 52 | #endif 53 | "geospatial", 54 | geospatial_functions, 55 | PHP_MINIT(geospatial), 56 | NULL, 57 | NULL, 58 | NULL, 59 | PHP_MINFO(geospatial), 60 | #if ZEND_MODULE_API_NO >= 20010901 61 | "0.1", /* Replace with version number for your extension */ 62 | #endif 63 | STANDARD_MODULE_PROPERTIES 64 | }; 65 | /* }}} */ 66 | 67 | #ifdef COMPILE_DL_GEOSPATIAL 68 | ZEND_GET_MODULE(geospatial) 69 | #endif 70 | 71 | /* {{{ PHP_MINIT_FUNCTION 72 | */ 73 | PHP_MINIT_FUNCTION(geospatial) 74 | { 75 | return SUCCESS; 76 | } 77 | /* }}} */ 78 | 79 | /* {{{ PHP_MSHUTDOWN_FUNCTION 80 | */ 81 | PHP_MSHUTDOWN_FUNCTION(geospatial) 82 | { 83 | return SUCCESS; 84 | } 85 | /* }}} */ 86 | 87 | /* {{{ PHP_MINFO_FUNCTION 88 | */ 89 | PHP_MINFO_FUNCTION(geospatial) 90 | { 91 | php_info_print_table_start(); 92 | php_info_print_table_header(2, "Geospatial functions", "enabled"); 93 | php_info_print_table_end(); 94 | } 95 | /* }}} */ 96 | 97 | 98 | PHP_FUNCTION(haversine) 99 | { 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) { 103 | return; 104 | } 105 | 106 | deltaLat = (fromLat - toLat) * GEO_DEG_TO_RAD; 107 | deltaLong = (fromLong - toLong) * GEO_DEG_TO_RAD; 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 | } 118 | 119 | /* 120 | * Local variables: 121 | * tab-width: 4 122 | * c-basic-offset: 4 123 | * End: 124 | * vim600: noet sw=4 ts=4 fdm=marker 125 | * vim<600: noet sw=4 ts=4 126 | */ 127 | --------------------------------------------------------------------------------