@@ -48,6 +48,12 @@ ZEND_BEGIN_ARG_INFO_EX(fraction_along_gc_line_args, 0, 0, 3)
48
48
ZEND_ARG_INFO (0 , radius )
49
49
ZEND_END_ARG_INFO ()
50
50
51
+ ZEND_BEGIN_ARG_INFO_EX (initial_bearing_args , 0 , 0 , 2 )
52
+ ZEND_ARG_INFO (0 , geoJsonPointFrom )
53
+ ZEND_ARG_INFO (0 , geoJsonPointTo )
54
+ ZEND_ARG_INFO (0 , radius )
55
+ ZEND_END_ARG_INFO ()
56
+
51
57
ZEND_BEGIN_ARG_INFO_EX (helmert_args , 0 , 0 , 3 )
52
58
ZEND_ARG_INFO (0 , x )
53
59
ZEND_ARG_INFO (0 , y )
@@ -97,6 +103,7 @@ ZEND_END_ARG_INFO()
97
103
*/
98
104
const zend_function_entry geospatial_functions [] = {
99
105
PHP_FE (haversine , haversine_args )
106
+ PHP_FE (initial_bearing , initial_bearing_args )
100
107
PHP_FE (fraction_along_gc_line , fraction_along_gc_line_args )
101
108
PHP_FE (helmert , helmert_args )
102
109
PHP_FE (polar_to_cartesian , polar_to_cartesian_args )
@@ -613,6 +620,47 @@ PHP_FUNCTION(fraction_along_gc_line)
613
620
}
614
621
/* }}} */
615
622
623
+ double php_initial_bearing (double from_lat , double from_long , double to_lat , double to_long )
624
+ {
625
+ /*
626
+ var y = Math.sin(dLon) * Math.cos(lat2);
627
+ var x = Math.cos(lat1)*Math.sin(lat2) -
628
+ Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
629
+ var brng = Math.atan2(y, x).toDeg();
630
+ */
631
+ double x , y ;
632
+
633
+ y = sin (to_long - from_long ) * cos (to_lat );
634
+ x = cos (from_lat ) * sin (to_lat ) - sin (from_lat ) * cos (to_lat ) * cos (to_long - to_long );
635
+
636
+ return atan2 (y , x );
637
+ }
638
+
639
+ /* {{{ proto float initial_bearing(GeoJSONPoint from, GeoJSONPoint to)
640
+ Calculates the initial bearing to from from to to. */
641
+ PHP_FUNCTION (initial_bearing )
642
+ {
643
+ zval * from_geojson , * to_geojson ;
644
+ double from_lat , from_long , to_lat , to_long , bearing ;
645
+
646
+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "aa" , & from_geojson , & to_geojson ) == FAILURE ) {
647
+ return ;
648
+ }
649
+
650
+ geojson_point_to_lon_lat (from_geojson , & from_long , & from_lat );
651
+ geojson_point_to_lon_lat (to_geojson , & to_long , & to_lat );
652
+
653
+ bearing = php_initial_bearing (
654
+ from_lat * GEO_DEG_TO_RAD ,
655
+ from_long * GEO_DEG_TO_RAD ,
656
+ to_lat * GEO_DEG_TO_RAD ,
657
+ to_long * GEO_DEG_TO_RAD
658
+ );
659
+
660
+ RETURN_DOUBLE (bearing / GEO_DEG_TO_RAD );
661
+ }
662
+ /* }}} */
663
+
616
664
geo_array * geo_hashtable_to_array (zval * array )
617
665
{
618
666
geo_array * tmp ;
0 commit comments