Skip to content

Commit ebeb425

Browse files
author
Nathaniel McHugh
committed
clean up names and prototypes
1 parent 49b3e9c commit ebeb425

File tree

8 files changed

+129
-101
lines changed

8 files changed

+129
-101
lines changed

geospatial.c

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ ZEND_BEGIN_ARG_INFO_EX(cartesian_to_polar_args, 0, 0, 4)
5454
ZEND_ARG_INFO(0, reference_ellipsoid)
5555
ZEND_END_ARG_INFO()
5656

57-
ZEND_BEGIN_ARG_INFO_EX(change_datum_args, 0, 0, 4)
57+
ZEND_BEGIN_ARG_INFO_EX(transform_datum_args, 0, 0, 4)
5858
ZEND_ARG_INFO(0, latitude)
5959
ZEND_ARG_INFO(0, longitude)
6060
ZEND_ARG_INFO(0, from_reference_ellipsoid)
6161
ZEND_ARG_INFO(0, to_reference_ellipsoid)
6262
ZEND_END_ARG_INFO()
6363

64-
ZEND_BEGIN_ARG_INFO_EX(to_decimal_args, 0, 0, 4)
64+
ZEND_BEGIN_ARG_INFO_EX(dms_to_decimal_args, 0, 0, 4)
6565
ZEND_ARG_INFO(0, degrees)
6666
ZEND_ARG_INFO(0, minutes)
6767
ZEND_ARG_INFO(0, seconds)
6868
ZEND_ARG_INFO(0, direction)
6969
ZEND_END_ARG_INFO()
7070

71-
ZEND_BEGIN_ARG_INFO_EX(to_dms_args, 0, 0, 2)
71+
ZEND_BEGIN_ARG_INFO_EX(decimal_to_dms_args, 0, 0, 2)
7272
ZEND_ARG_INFO(0, decimal)
7373
ZEND_ARG_INFO(0, coordinate)
7474
ZEND_END_ARG_INFO()
@@ -82,9 +82,9 @@ const zend_function_entry geospatial_functions[] = {
8282
PHP_FE(helmert, helmert_args)
8383
PHP_FE(polar_to_cartesian, polar_to_cartesian_args)
8484
PHP_FE(cartesian_to_polar, cartesian_to_polar_args)
85-
PHP_FE(change_datum, change_datum_args)
86-
PHP_FE(to_decimal, to_decimal_args)
87-
PHP_FE(to_dms, to_dms_args)
85+
PHP_FE(transform_datum, transform_datum_args)
86+
PHP_FE(dms_to_decimal, dms_to_decimal_args)
87+
PHP_FE(decimal_to_dms, decimal_to_dms_args)
8888
/* End of functions */
8989
{ NULL, NULL, NULL }
9090
};
@@ -170,7 +170,7 @@ geo_helmert_constants get_helmert_constants(long from, long to)
170170
}
171171
}
172172

173-
geo_cartesian php_helmert(double x, double y, double z, geo_helmert_constants helmet_consts)
173+
geo_cartesian helmert(double x, double y, double z, geo_helmert_constants helmet_consts)
174174
{
175175
double rX, rY, rZ;
176176
double xOut, yOut, zOut;
@@ -193,24 +193,7 @@ geo_cartesian php_helmert(double x, double y, double z, geo_helmert_constants he
193193
return point;
194194
}
195195

196-
197-
PHP_FUNCTION(helmert)
198-
{
199-
double x, y, z;
200-
geo_cartesian point;
201-
long from_reference_ellipsoid, to_reference_ellipsoid;
202-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd|ll", &x, &y, &z, &from_reference_ellipsoid, &to_reference_ellipsoid) == FAILURE) {
203-
return;
204-
}
205-
array_init(return_value);
206-
geo_helmert_constants helmert_constants = get_helmert_constants(from_reference_ellipsoid, to_reference_ellipsoid);
207-
point = php_helmert(x, y, z, helmert_constants);
208-
add_assoc_double(return_value, "x", point.x);
209-
add_assoc_double(return_value, "y", point.y);
210-
add_assoc_double(return_value, "z", point.z);
211-
}
212-
213-
geo_cartesian php_polar_to_cartesian(double latitude, double longitude, geo_ellipsoid eli)
196+
geo_cartesian polar_to_cartesian(double latitude, double longitude, geo_ellipsoid eli)
214197
{
215198
double x, y, z;
216199

@@ -231,24 +214,8 @@ geo_cartesian php_polar_to_cartesian(double latitude, double longitude, geo_elli
231214
return point;
232215
}
233216

234-
PHP_FUNCTION(polar_to_cartesian)
235-
{
236-
double latitude, longitude;
237-
long reference_ellipsoid;
238-
geo_cartesian point;
239-
240-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd|l", &latitude, &longitude, &reference_ellipsoid) == FAILURE) {
241-
return;
242-
}
243-
geo_ellipsoid eli = get_ellipsoid(reference_ellipsoid);
244-
array_init(return_value);
245-
point = php_polar_to_cartesian(latitude, longitude, eli);
246-
add_assoc_double(return_value, "x", point.x);
247-
add_assoc_double(return_value, "y", point.y);
248-
add_assoc_double(return_value, "z", point.z);
249-
}
250217

251-
geo_lat_long php_cartesian_to_polar(double x, double y, double z, geo_ellipsoid eli)
218+
geo_lat_long cartesian_to_polar(double x, double y, double z, geo_ellipsoid eli)
252219
{
253220

254221
double latitude, longitude;
@@ -275,25 +242,32 @@ geo_lat_long php_cartesian_to_polar(double x, double y, double z, geo_ellipsoid
275242
return polar;
276243
}
277244

278-
PHP_FUNCTION(to_decimal)
245+
/* {{{ proto dms_to_decimal(double degrees, double minutes, double seconds [,string direction])
246+
* Convert degrees, minutes & seconds values to decimal degrees */
247+
PHP_FUNCTION(dms_to_decimal)
279248
{
280249
double degrees, minutes, sign;
281250
double seconds, decimal;
282-
char *direction;
251+
char *direction = "";
283252
int direction_len;
284253

285254
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd|s", &degrees, &minutes, &seconds, &direction, &direction_len) == FAILURE) {
286255
return;
287256
}
288-
289-
sign = strcmp(direction, "S") == 0 || strcmp(direction, "W") == 0 ? -1 : 1;
290-
decimal = degrees + minutes / 60 + seconds / 3600;
257+
if (strcmp("", direction) == 0) {
258+
sign = degrees > 1 ? 1 : -1;
259+
} else {
260+
sign = strcmp(direction, "S") == 0 || strcmp(direction, "W") == 0 ? -1 : 1;
261+
}
262+
decimal = abs(degrees) + minutes / 60 + seconds / 3600;
291263
decimal *= sign;
292264
RETURN_DOUBLE(decimal);
293265
}
266+
/* }}} */
294267

295-
296-
PHP_FUNCTION(to_dms)
268+
/* {{{ proto decimal_to_dms(double decimal, string coordinate)
269+
* Convert decimal degrees value to whole degrees and minutes and decimal seconds */
270+
PHP_FUNCTION(decimal_to_dms)
297271
{
298272
double decimal, seconds;
299273
int degrees, minutes;
@@ -315,13 +289,54 @@ PHP_FUNCTION(to_dms)
315289
degrees = (int) decimal;
316290
minutes = decimal * 60 - degrees * 60;
317291
seconds = decimal * 3600 - degrees * 3600 - minutes * 60;
318-
seconds = (round(seconds *100)) / 100;
319292
add_assoc_long(return_value, "degrees", degrees);
320293
add_assoc_long(return_value, "minutes", minutes);
321294
add_assoc_double(return_value, "seconds", seconds);
322295
add_assoc_string(return_value, "direction", direction, 1);
323296
}
297+
/* }}} */
298+
299+
/* {{{ proto helmert(double x, double y, double z [, long from_reference_ellipsoid, long to_reference_ellipsoid])
300+
* Convert polar ones (latitude, longitude) tp cartesian co-ordiantes (x, y, z) */
301+
PHP_FUNCTION(helmert)
302+
{
303+
double x, y, z;
304+
geo_cartesian point;
305+
long from_reference_ellipsoid, to_reference_ellipsoid;
306+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd|ll", &x, &y, &z, &from_reference_ellipsoid, &to_reference_ellipsoid) == FAILURE) {
307+
return;
308+
}
309+
array_init(return_value);
310+
geo_helmert_constants helmert_constants = get_helmert_constants(from_reference_ellipsoid, to_reference_ellipsoid);
311+
point = helmert(x, y, z, helmert_constants);
312+
add_assoc_double(return_value, "x", point.x);
313+
add_assoc_double(return_value, "y", point.y);
314+
add_assoc_double(return_value, "z", point.z);
315+
}
316+
/* }}} */
317+
318+
/* {{{ proto polar_to_cartesian(double latitude, double longitude[, long reference_ellipsoid])
319+
* Convert polar ones (latitude, longitude) tp cartesian co-ordiantes (x, y, z) */
320+
PHP_FUNCTION(polar_to_cartesian)
321+
{
322+
double latitude, longitude;
323+
long reference_ellipsoid;
324+
geo_cartesian point;
325+
326+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd|l", &latitude, &longitude, &reference_ellipsoid) == FAILURE) {
327+
return;
328+
}
329+
geo_ellipsoid eli = get_ellipsoid(reference_ellipsoid);
330+
array_init(return_value);
331+
point = polar_to_cartesian(latitude, longitude, eli);
332+
add_assoc_double(return_value, "x", point.x);
333+
add_assoc_double(return_value, "y", point.y);
334+
add_assoc_double(return_value, "z", point.z);
335+
}
336+
/* }}} */
324337

338+
/* {{{ proto cartesian_to_polar(double x, double y, double z [, long reference_ellipsoid])
339+
* Convert cartesian co-ordiantes (x, y, z) to polar ones (latitude, longitude) */
325340
PHP_FUNCTION(cartesian_to_polar)
326341
{
327342
double x, y, z;
@@ -333,14 +348,17 @@ PHP_FUNCTION(cartesian_to_polar)
333348
}
334349
geo_ellipsoid eli = get_ellipsoid(reference_ellipsoid);
335350
array_init(return_value);
336-
polar = php_cartesian_to_polar(x, y, z, eli);
351+
polar = cartesian_to_polar(x, y, z, eli);
337352
add_assoc_double(return_value, "lat", polar.latitude);
338353
add_assoc_double(return_value, "long", polar.longitude);
339354
add_assoc_double(return_value, "height", polar.height);
340355
}
356+
/* }}} */
341357

342358

343-
PHP_FUNCTION(change_datum)
359+
/* {{{ proto transform_datum(double latitude, double longitude, long from_reference_ellipsoid, long to_reference_ellipsoid)
360+
* Unified function to transform projection of geo-cordinates between datums */
361+
PHP_FUNCTION(transform_datum)
344362
{
345363
double latitude, longitude;
346364
long from_reference_ellipsoid, to_reference_ellipsoid;
@@ -351,16 +369,17 @@ PHP_FUNCTION(change_datum)
351369
}
352370
geo_ellipsoid eli_from = get_ellipsoid(from_reference_ellipsoid);
353371
geo_ellipsoid eli_to = get_ellipsoid(to_reference_ellipsoid);
354-
point = php_polar_to_cartesian(latitude, longitude, eli_from);
372+
point = polar_to_cartesian(latitude, longitude, eli_from);
355373
geo_helmert_constants helmert_constants = get_helmert_constants(from_reference_ellipsoid, to_reference_ellipsoid);
356-
converted_point = php_helmert(point.x, point.y, point.z, helmert_constants);
357-
polar = php_cartesian_to_polar(converted_point.x, converted_point.y, converted_point.z, eli_to);
374+
converted_point = helmert(point.x, point.y, point.z, helmert_constants);
375+
polar = cartesian_to_polar(converted_point.x, converted_point.y, converted_point.z, eli_to);
358376

359377
array_init(return_value);
360378
add_assoc_double(return_value, "lat", polar.latitude);
361379
add_assoc_double(return_value, "long", polar.longitude);
362380
add_assoc_double(return_value, "height", polar.height);
363381
}
382+
/* }}} */
364383

365384
/* {{{ proto haversine(double fromLat, double fromLong, double toLat, double toLong [, double radius ])
366385
* Calculates the greater circle distance between the two lattitude/longitude pairs */

php_geospatial.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ PHP_FUNCTION(haversine);
107107
PHP_FUNCTION(helmert);
108108
PHP_FUNCTION(polar_to_cartesian);
109109
PHP_FUNCTION(cartesian_to_polar);
110-
PHP_FUNCTION(change_datum);
111-
PHP_FUNCTION(to_decimal);
112-
PHP_FUNCTION(to_dms);
110+
PHP_FUNCTION(transform_datum);
111+
PHP_FUNCTION(dms_to_decimal);
112+
PHP_FUNCTION(decimal_to_dms);
113113

114114
#endif /* PHP_GEOSPATIAL_H */
115115

tests/JodrellBank.phpt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ WGS84 to OSGB36
33
--FILE--
44
<?php
55

6-
function getDecimal($d, $m, $s) {
7-
$mul = $d < 0 ? -1 : 1;
8-
return $mul * (abs($d)+ $m / 60 + $s /3600);
9-
}
6+
$lat = dms_to_decimal(53, 14, 10.5);
7+
$long = dms_to_decimal(-2, 18, 25.7);
108

11-
$lat = getDecimal(53, 14, 10.5);
12-
$long = getDecimal(-2, 18, 25.7);
13-
14-
$polar = change_datum($lat, $long, GEO_WGS84, GEO_AIRY_1830);
9+
$polar = transform_datum($lat, $long, GEO_WGS84, GEO_AIRY_1830);
1510

1611
echo round($polar['lat'] ,6),PHP_EOL;
1712
echo round($polar['long'] ,6),PHP_EOL;

tests/OSGB36_to_WGS84.phpt

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
--TEST--
2-
WGS84 to OSGB36
2+
OSGB36 to WGS84
33
--FILE--
44
<?php
55

6-
function getDecimal($d, $m, $s) {
7-
$mul = $d < 0 ? -1 : 1;
8-
return $mul * (abs($d)+ $m / 60 + $s /3600);
9-
}
10-
11-
$lat = getDecimal(53, 14, 10.5);
12-
$long = getDecimal(-2, 18, 25.7);
6+
$lat = dms_to_decimal(53, 14, 10.5, 'N');
7+
$long = dms_to_decimal(2, 18, 25.7, 'W');
138

14-
$polar = change_datum($lat, $long, GEO_AIRY_1830, GEO_WGS84);
9+
$polar = transform_datum($lat, $long, GEO_AIRY_1830, GEO_WGS84);
1510

16-
echo round($polar['lat'] ,6),PHP_EOL;
17-
echo round($polar['long'] ,6),PHP_EOL;
11+
var_dump(decimal_to_dms($polar['lat'], 'latitude'));
12+
var_dump(decimal_to_dms($polar['long'] ,'longitude'));
1813
echo round($polar['height'] ,3),PHP_EOL;
1914
--EXPECT--
20-
53.236526
21-
-2.30856
15+
array(4) {
16+
["degrees"]=>
17+
int(53)
18+
["minutes"]=>
19+
int(14)
20+
["seconds"]=>
21+
float(11.493372672732)
22+
["direction"]=>
23+
string(1) "N"
24+
}
25+
array(4) {
26+
["degrees"]=>
27+
int(2)
28+
["minutes"]=>
29+
int(18)
30+
["seconds"]=>
31+
float(30.817794659248)
32+
["direction"]=>
33+
string(1) "W"
34+
}
2235
75.061

tests/WGS84_to_OSGB36.phpt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ WGS84 to OSGB36
33
--FILE--
44
<?php
55

6-
function getDecimal($d, $m, $s) {
7-
$mul = $d < 0 ? -1 : 1;
8-
return $mul * (abs($d)+ $m / 60 + $s /3600);
9-
}
6+
$lat = dms_to_decimal(53, 23, 1);
7+
$long = dms_to_decimal(-1, 28, 1);
108

11-
$lat = getDecimal(53, 23, 1);
12-
$long = getDecimal(-1, 28, 1);
13-
14-
$polar = change_datum($lat, $long, GEO_WGS84, GEO_AIRY_1830);
9+
$polar = transform_datum($lat, $long, GEO_WGS84, GEO_AIRY_1830);
1510

1611
var_dump($polar);
1712
--EXPECT--

tests/to_dms.phpt renamed to tests/decimal_to_dms.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ degrees minutes seconds
33
--FILE--
44
<?php
55

6-
$dms = to_dms(-1.034291666667, 'longitude');
6+
$dms = decimal_to_dms(-1.034291666667, 'longitude');
77
var_dump($dms);
88

9-
$dms = to_dms(-1.034291666667, 'latitude');
9+
$dms = decimal_to_dms(-1.034291666667, 'latitude');
1010
var_dump($dms);
1111

1212
--EXPECT--
@@ -16,7 +16,7 @@ array(4) {
1616
["minutes"]=>
1717
int(2)
1818
["seconds"]=>
19-
float(3.45)
19+
float(3.4500000011994)
2020
["direction"]=>
2121
string(1) "W"
2222
}
@@ -26,7 +26,7 @@ array(4) {
2626
["minutes"]=>
2727
int(2)
2828
["seconds"]=>
29-
float(3.45)
29+
float(3.4500000011994)
3030
["direction"]=>
3131
string(1) "S"
3232
}

tests/dms_to_decimal.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
decimal
3+
--FILE--
4+
<?php
5+
6+
$decimal = dms_to_decimal(1, 2, 3.45, 'W');
7+
var_dump($decimal);
8+
$decimal = dms_to_decimal(2, 18, 25.7, 'W');
9+
var_dump($decimal);
10+
$decimal = dms_to_decimal(-2, 18, 25.7, 'W');
11+
var_dump($decimal);
12+
13+
--EXPECT--
14+
float(-1.0342916666667)
15+
float(-2.3071388888889)
16+
float(-2.3071388888889)

tests/to_decimal.phpt

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)