Skip to content

Commit 49b3e9c

Browse files
author
Nathaniel McHugh
committed
add 2 new functions to convert co-ord formats
1 parent 1340ae8 commit 49b3e9c

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

geospatial.c

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ ZEND_BEGIN_ARG_INFO_EX(change_datum_args, 0, 0, 4)
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)
65+
ZEND_ARG_INFO(0, degrees)
66+
ZEND_ARG_INFO(0, minutes)
67+
ZEND_ARG_INFO(0, seconds)
68+
ZEND_ARG_INFO(0, direction)
69+
ZEND_END_ARG_INFO()
70+
71+
ZEND_BEGIN_ARG_INFO_EX(to_dms_args, 0, 0, 2)
72+
ZEND_ARG_INFO(0, decimal)
73+
ZEND_ARG_INFO(0, coordinate)
74+
ZEND_END_ARG_INFO()
75+
6476
/* {{{ geospatial_functions[]
6577
*
6678
* Every user visible function must have an entry in geospatial_functions[].
@@ -71,6 +83,8 @@ const zend_function_entry geospatial_functions[] = {
7183
PHP_FE(polar_to_cartesian, polar_to_cartesian_args)
7284
PHP_FE(cartesian_to_polar, cartesian_to_polar_args)
7385
PHP_FE(change_datum, change_datum_args)
86+
PHP_FE(to_decimal, to_decimal_args)
87+
PHP_FE(to_dms, to_dms_args)
7488
/* End of functions */
7589
{ NULL, NULL, NULL }
7690
};
@@ -188,7 +202,7 @@ PHP_FUNCTION(helmert)
188202
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd|ll", &x, &y, &z, &from_reference_ellipsoid, &to_reference_ellipsoid) == FAILURE) {
189203
return;
190204
}
191-
array_init(return_value);\
205+
array_init(return_value);
192206
geo_helmert_constants helmert_constants = get_helmert_constants(from_reference_ellipsoid, to_reference_ellipsoid);
193207
point = php_helmert(x, y, z, helmert_constants);
194208
add_assoc_double(return_value, "x", point.x);
@@ -261,6 +275,53 @@ geo_lat_long php_cartesian_to_polar(double x, double y, double z, geo_ellipsoid
261275
return polar;
262276
}
263277

278+
PHP_FUNCTION(to_decimal)
279+
{
280+
double degrees, minutes, sign;
281+
double seconds, decimal;
282+
char *direction;
283+
int direction_len;
284+
285+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd|s", &degrees, &minutes, &seconds, &direction, &direction_len) == FAILURE) {
286+
return;
287+
}
288+
289+
sign = strcmp(direction, "S") == 0 || strcmp(direction, "W") == 0 ? -1 : 1;
290+
decimal = degrees + minutes / 60 + seconds / 3600;
291+
decimal *= sign;
292+
RETURN_DOUBLE(decimal);
293+
}
294+
295+
296+
PHP_FUNCTION(to_dms)
297+
{
298+
double decimal, seconds;
299+
int degrees, minutes;
300+
char *direction;
301+
char *coordinate;
302+
int coordinate_len;
303+
304+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ds", &decimal, &coordinate, &coordinate_len) == FAILURE) {
305+
return;
306+
}
307+
if (strcmp(coordinate, "longitude") == 0) {
308+
direction = decimal < 1 ? "W" : "E";
309+
} else {
310+
direction = decimal < 1 ? "S" : "N";
311+
}
312+
313+
array_init(return_value);
314+
decimal = fabs(decimal);
315+
degrees = (int) decimal;
316+
minutes = decimal * 60 - degrees * 60;
317+
seconds = decimal * 3600 - degrees * 3600 - minutes * 60;
318+
seconds = (round(seconds *100)) / 100;
319+
add_assoc_long(return_value, "degrees", degrees);
320+
add_assoc_long(return_value, "minutes", minutes);
321+
add_assoc_double(return_value, "seconds", seconds);
322+
add_assoc_string(return_value, "direction", direction, 1);
323+
}
324+
264325
PHP_FUNCTION(cartesian_to_polar)
265326
{
266327
double x, y, z;

php_geospatial.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ PHP_FUNCTION(helmert);
108108
PHP_FUNCTION(polar_to_cartesian);
109109
PHP_FUNCTION(cartesian_to_polar);
110110
PHP_FUNCTION(change_datum);
111+
PHP_FUNCTION(to_decimal);
112+
PHP_FUNCTION(to_dms);
111113

112114
#endif /* PHP_GEOSPATIAL_H */
113115

tests/to_decimal.phpt

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

tests/to_dms.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
degrees minutes seconds
3+
--FILE--
4+
<?php
5+
6+
$dms = to_dms(-1.034291666667, 'longitude');
7+
var_dump($dms);
8+
9+
$dms = to_dms(-1.034291666667, 'latitude');
10+
var_dump($dms);
11+
12+
--EXPECT--
13+
array(4) {
14+
["degrees"]=>
15+
int(1)
16+
["minutes"]=>
17+
int(2)
18+
["seconds"]=>
19+
float(3.45)
20+
["direction"]=>
21+
string(1) "W"
22+
}
23+
array(4) {
24+
["degrees"]=>
25+
int(1)
26+
["minutes"]=>
27+
int(2)
28+
["seconds"]=>
29+
float(3.45)
30+
["direction"]=>
31+
string(1) "S"
32+
}

0 commit comments

Comments
 (0)