Skip to content

Commit 23a7d69

Browse files
committed
Merged pull request #7
2 parents 53918a0 + 85209cb commit 23a7d69

15 files changed

+173
-75
lines changed

geo_array.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2013 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+
| Authors: Derick Rethans <github@derickrethans.nl> |
16+
| Michael Maclean <michael@no-surprises.co.uk> |
17+
| Nathaniel McHugh <nmchugh@inviqa.com> |
18+
| Marcus Deglos <marcus@deglos.com> |
19+
+----------------------------------------------------------------------+
20+
*/
21+
122
#include <stdlib.h>
223

324
#include "geo_array.h"

geo_array.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2013 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+
| Authors: Derick Rethans <github@derickrethans.nl> |
16+
| Michael Maclean <michael@no-surprises.co.uk> |
17+
| Nathaniel McHugh <nmchugh@inviqa.com> |
18+
| Marcus Deglos <marcus@deglos.com> |
19+
+----------------------------------------------------------------------+
20+
*/
21+
122
typedef struct geo_array {
223
double *x;
324
double *y;

geospatial.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
+----------------------------------------------------------------------+
33
| PHP Version 5 |
44
+----------------------------------------------------------------------+
5-
| Copyright (c) 1997-2012 The PHP Group |
5+
| Copyright (c) 1997-2013 The PHP Group |
66
+----------------------------------------------------------------------+
77
| This source file is subject to version 3.01 of the PHP license, |
88
| that is bundled with this package in the file LICENSE, and is |
@@ -12,13 +12,13 @@
1212
| obtain it through the world-wide-web, please send a note to |
1313
| license@php.net so we can mail you a copy immediately. |
1414
+----------------------------------------------------------------------+
15-
| Author: |
15+
| Authors: Derick Rethans <github@derickrethans.nl> |
16+
| Michael Maclean <michael@no-surprises.co.uk> |
17+
| Nathaniel McHugh <nmchugh@inviqa.com> |
18+
| Marcus Deglos <marcus@deglos.com> |
1619
+----------------------------------------------------------------------+
1720
*/
1821

19-
20-
/* $Id$ */
21-
2222
#ifdef HAVE_CONFIG_H
2323
#include "config.h"
2424
#endif
@@ -328,7 +328,7 @@ geo_lat_long cartesian_to_polar(double x, double y, double z, geo_ellipsoid eli)
328328
return polar;
329329
}
330330

331-
/* {{{ proto dms_to_decimal(double degrees, double minutes, double seconds [,string direction])
331+
/* {{{ proto double dms_to_decimal(double degrees, double minutes, double seconds [,string direction])
332332
* Convert degrees, minutes & seconds values to decimal degrees */
333333
PHP_FUNCTION(dms_to_decimal)
334334
{
@@ -353,7 +353,7 @@ PHP_FUNCTION(dms_to_decimal)
353353
}
354354
/* }}} */
355355

356-
/* {{{ proto decimal_to_dms(double decimal, string coordinate)
356+
/* {{{ proto array decimal_to_dms(double decimal, string coordinate)
357357
* Convert decimal degrees value to whole degrees and minutes and decimal seconds */
358358
PHP_FUNCTION(decimal_to_dms)
359359
{
@@ -385,13 +385,14 @@ PHP_FUNCTION(decimal_to_dms)
385385
}
386386
/* }}} */
387387

388-
/* {{{ proto helmert(double x, double y, double z [, long from_reference_ellipsoid, long to_reference_ellipsoid])
389-
* Convert polar ones (latitude, longitude) tp cartesian co-ordiantes (x, y, z) */
388+
/* {{{ proto array helmert(double x, double y, double z [, long from_reference_ellipsoid, long to_reference_ellipsoid])
389+
* Convert cartesian co-ordinates between reference elipsoids */
390390
PHP_FUNCTION(helmert)
391391
{
392392
double x, y, z;
393393
geo_cartesian point;
394-
long from_reference_ellipsoid, to_reference_ellipsoid;
394+
long from_reference_ellipsoid = 0, to_reference_ellipsoid = 0;
395+
395396
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd|ll", &x, &y, &z, &from_reference_ellipsoid, &to_reference_ellipsoid) == FAILURE) {
396397
return;
397398
}
@@ -405,7 +406,7 @@ PHP_FUNCTION(helmert)
405406
}
406407
/* }}} */
407408

408-
/* {{{ proto polar_to_cartesian(double latitude, double longitude[, long reference_ellipsoid])
409+
/* {{{ proto array polar_to_cartesian(double latitude, double longitude[, long reference_ellipsoid])
409410
* Convert polar ones (latitude, longitude) tp cartesian co-ordiantes (x, y, z) */
410411
PHP_FUNCTION(polar_to_cartesian)
411412
{
@@ -426,8 +427,8 @@ PHP_FUNCTION(polar_to_cartesian)
426427
}
427428
/* }}} */
428429

429-
/* {{{ proto cartesian_to_polar(double x, double y, double z [, long reference_ellipsoid])
430-
* Convert cartesian co-ordiantes (x, y, z) to polar ones (latitude, longitude) */
430+
/* {{{ proto array cartesian_to_polar(double x, double y, double z [, long reference_ellipsoid])
431+
* Convert cartesian co-ordinates (x, y, z) to polar ones (latitude, longitude) */
431432
PHP_FUNCTION(cartesian_to_polar)
432433
{
433434
double x, y, z;
@@ -448,44 +449,53 @@ PHP_FUNCTION(cartesian_to_polar)
448449
/* }}} */
449450

450451

451-
/* {{{ proto transform_datum(double latitude, double longitude, long from_reference_ellipsoid, long to_reference_ellipsoid)
452-
* Unified function to transform projection of geo-cordinates between datums */
452+
/* {{{ proto GeoJSONPoint transform_datum(GeoJSONPoint coordinates, long from_reference_ellipsoid, long to_reference_ellipsoid)
453+
* Unified function to transform projection of geo-coordinates between datums */
453454
PHP_FUNCTION(transform_datum)
454455
{
455456
double latitude, longitude;
457+
zval *geojson;
456458
long from_reference_ellipsoid, to_reference_ellipsoid;
457459
geo_cartesian point, converted_point;
458460
geo_lat_long polar;
459461

460-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddll", &latitude, &longitude, &from_reference_ellipsoid, &to_reference_ellipsoid) == FAILURE) {
462+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "all", &geojson, &from_reference_ellipsoid, &to_reference_ellipsoid) == FAILURE) {
461463
return;
462464
}
463465

466+
geojson_point_to_lon_lat(geojson, &longitude, &latitude);
467+
464468
geo_ellipsoid eli_from = get_ellipsoid(from_reference_ellipsoid);
465469
geo_ellipsoid eli_to = get_ellipsoid(to_reference_ellipsoid);
466470
point = polar_to_cartesian(latitude, longitude, eli_from);
467471
geo_helmert_constants helmert_constants = get_helmert_constants(from_reference_ellipsoid, to_reference_ellipsoid);
468472
converted_point = helmert(point.x, point.y, point.z, helmert_constants);
469473
polar = cartesian_to_polar(converted_point.x, converted_point.y, converted_point.z, eli_to);
470-
474+
/*
471475
array_init(return_value);
472476
add_assoc_double(return_value, "lat", polar.latitude);
473477
add_assoc_double(return_value, "long", polar.longitude);
474478
add_assoc_double(return_value, "height", polar.height);
479+
*/
480+
retval_point_from_coordinates(return_value, polar.longitude, polar.latitude);
475481
}
476482
/* }}} */
477483

478-
/* {{{ proto haversine(double fromLat, double fromLong, double toLat, double toLong [, double radius ])
484+
/* {{{ proto double haversine(GeoJSONPoint from, GeoJSONPoint to [, double radius ])
479485
* Calculates the greater circle distance between the two lattitude/longitude pairs */
480486
PHP_FUNCTION(haversine)
481487
{
482-
double from_lat, from_long, to_lat, to_long;
483488
double radius = GEO_EARTH_RADIUS;
489+
zval *from_geojson, *to_geojson;
490+
double from_lat, from_long, to_lat, to_long;
484491

485-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dddd|d", &from_lat, &from_long, &to_lat, &to_long, &radius) == FAILURE) {
492+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa|d", &from_geojson, &to_geojson, &radius) == FAILURE) {
486493
return;
487494
}
488495

496+
geojson_point_to_lon_lat(from_geojson, &from_long, &from_lat);
497+
geojson_point_to_lon_lat(to_geojson, &to_long, &to_lat);
498+
489499
RETURN_DOUBLE(php_geo_haversine(from_lat * GEO_DEG_TO_RAD, from_long * GEO_DEG_TO_RAD, to_lat * GEO_DEG_TO_RAD, to_long * GEO_DEG_TO_RAD) * radius);
490500
}
491501
/* }}} */
@@ -508,7 +518,7 @@ void php_geo_fraction_along_gc_line(double from_lat, double from_long, double to
508518
*res_long = atan2(y, x);
509519
}
510520

511-
/* {{{ proto GeoJSON fraction_along_gc_line(GeoJSONPoint from, GeoJSONPoint to, double fraction [, double radius ])
521+
/* {{{ proto GeoJSONPoint fraction_along_gc_line(GeoJSONPoint from, GeoJSONPoint to, double fraction [, double radius ])
512522
* Calculates a lat/long pair at a fraction (0-1) of the distance along a GC line */
513523
PHP_FUNCTION(fraction_along_gc_line)
514524
{

php_geospatial.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
+----------------------------------------------------------------------+
33
| PHP Version 5 |
44
+----------------------------------------------------------------------+
5-
| Copyright (c) 1997-2012 The PHP Group |
5+
| Copyright (c) 1997-2013 The PHP Group |
66
+----------------------------------------------------------------------+
77
| This source file is subject to version 3.01 of the PHP license, |
88
| that is bundled with this package in the file LICENSE, and is |
@@ -12,24 +12,25 @@
1212
| obtain it through the world-wide-web, please send a note to |
1313
| license@php.net so we can mail you a copy immediately. |
1414
+----------------------------------------------------------------------+
15-
| Author: |
15+
| Authors: Derick Rethans <github@derickrethans.nl> |
16+
| Michael Maclean <michael@no-surprises.co.uk> |
17+
| Nathaniel McHugh <nmchugh@inviqa.com> |
18+
| Marcus Deglos <marcus@deglos.com> |
1619
+----------------------------------------------------------------------+
1720
*/
1821

19-
/* $Id$ */
20-
2122
#ifndef PHP_GEOSPATIAL_H
2223
#define PHP_GEOSPATIAL_H
2324

2425
extern zend_module_entry geospatial_module_entry;
2526
#define phpext_geospatial_ptr &geospatial_module_entry
2627

2728
#ifdef PHP_WIN32
28-
# define PHP_GEOSPATIAL_API __declspec(dllexport)
29+
# define PHP_GEOSPATIAL_API __declspec(dllexport)
2930
#elif defined(__GNUC__) && __GNUC__ >= 4
30-
# define PHP_GEOSPATIAL_API __attribute__ ((visibility("default")))
31+
# define PHP_GEOSPATIAL_API __attribute__ ((visibility("default")))
3132
#else
32-
# define PHP_GEOSPATIAL_API
33+
# define PHP_GEOSPATIAL_API
3334
#endif
3435

3536
#ifdef ZTS
@@ -71,6 +72,7 @@ typedef struct {
7172
* The WGS84 elipsoid semi major axes
7273
*/
7374
const geo_ellipsoid wgs84 = {6378137.000, 6356752.3142};
75+
7476
/**
7577
* The Airy 1830 elipsoid semi major axes
7678
*/
@@ -89,6 +91,7 @@ const geo_helmert_constants wgs84_osgb36 = {
8991
-0.2470,
9092
-0.8421
9193
};
94+
9295
/**
9396
* The values of the 7 variables for performing helmert transformation between
9497
* osgb36 and wgs84 -1 * the values for the reverse transformation
@@ -130,7 +133,7 @@ PHP_FUNCTION(dms_to_decimal);
130133
PHP_FUNCTION(decimal_to_dms);
131134
PHP_FUNCTION(rdp_simplify);
132135

133-
#endif /* PHP_GEOSPATIAL_H */
136+
#endif /* PHP_GEOSPATIAL_H */
134137

135138
/*
136139
* Local variables:

tests/Greenwich.phpt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
--TEST--
2-
WGS84 to OSGB36 for Grennwich Observertory
2+
WGS84 to OSGB36 for Greenwich Observertory
33
--FILE--
44
<?php
55

66
$lat = dms_to_decimal(51, 28.675, 0, 'N');
77
$long = dms_to_decimal(0, 0.089, 0, 'W');
88

9-
$polar = transform_datum($lat, $long, GEO_WGS84, GEO_AIRY_1830);
9+
$from = array('type' => 'Point', 'coordinates' => array( $long, $lat ) );
1010

11-
$airyLong = $polar['long'];
12-
$wgs84Long = $long;
11+
$polar = transform_datum($from, GEO_WGS84, GEO_AIRY_1830);
1312

14-
$diferenceWGS84 = haversine($lat, $long, $lat, 0);
15-
$diferenceAiry = haversine($polar['lat'], $polar['long'], $polar['lat'], 0);
13+
$diferenceWGS84 = haversine(
14+
$from,
15+
array('type' => 'Point', 'coordinates' => array( 0, $lat ) )
16+
);
17+
18+
$diferenceAiry = haversine(
19+
$polar,
20+
array('type' => 'Point', 'coordinates' => array( 0, $polar['coordinates'][1] ) )
21+
);
1622
//lat long of merdian in Airy 1830 ideally long of 0
1723
var_dump($polar);
1824
//distance in m of difference from lat long and meridian
1925
echo round($diferenceWGS84 * 1000, 8),PHP_EOL;
2026
echo round($diferenceAiry * 1000, 8),PHP_EOL;
27+
?>
2128
--EXPECT--
22-
array(3) {
23-
["lat"]=>
24-
float(51.477400823311)
25-
["long"]=>
26-
float(0.00013627354767069)
27-
["height"]=>
28-
float(-21.205192557536)
29+
array(2) {
30+
["type"]=>
31+
string(5) "Point"
32+
["coordinates"]=>
33+
array(2) {
34+
[0]=>
35+
float(0.00013627354767069)
36+
[1]=>
37+
float(51.477400823311)
38+
}
2939
}
3040
102.84185171
31-
9.44816796
41+
9.44816796

tests/JodrellBank.phpt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@ WGS84 to OSGB36
66
$lat = dms_to_decimal(53, 14, 10.5);
77
$long = dms_to_decimal(-2, 18, 25.7);
88

9-
$polar = transform_datum($lat, $long, GEO_WGS84, GEO_AIRY_1830);
9+
$from = array('type' => 'Point', 'coordinates' => array( $long, $lat ) );
1010

11-
echo round($polar['lat'] ,6),PHP_EOL;
12-
echo round($polar['long'] ,6),PHP_EOL;
13-
echo round($polar['height'] ,3),PHP_EOL;
11+
$polar = transform_datum($from, GEO_WGS84, GEO_AIRY_1830);
12+
13+
var_dump($polar);
14+
?>
1415
--EXPECT--
15-
53.235974
16-
-2.305717
17-
-25.649
16+
array(2) {
17+
["type"]=>
18+
string(5) "Point"
19+
["coordinates"]=>
20+
array(2) {
21+
[0]=>
22+
float(-2.3057171628534)
23+
[1]=>
24+
float(53.235974015543)
25+
}
26+
}

tests/OSGB36_to_WGS84.phpt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ OSGB36 to WGS84
66
$lat = dms_to_decimal(53, 14, 10.5, 'N');
77
$long = dms_to_decimal(2, 18, 25.7, 'W');
88

9-
$polar = transform_datum($lat, $long, GEO_AIRY_1830, GEO_WGS84);
9+
$from = array('type' => 'Point', 'coordinates' => array( $long, $lat ) );
1010

11-
var_dump(decimal_to_dms($polar['lat'], 'latitude'));
12-
var_dump(decimal_to_dms($polar['long'] ,'longitude'));
13-
echo round($polar['height'] ,3),PHP_EOL;
11+
$polar = transform_datum($from, GEO_AIRY_1830, GEO_WGS84);
12+
13+
var_dump(decimal_to_dms($polar['coordinates'][1], 'latitude'));
14+
var_dump(decimal_to_dms($polar['coordinates'][0] ,'longitude'));
15+
?>
1416
--EXPECT--
1517
array(4) {
1618
["degrees"]=>
@@ -32,4 +34,3 @@ array(4) {
3234
["direction"]=>
3335
string(1) "W"
3436
}
35-
75.061

0 commit comments

Comments
 (0)