function farm_map_distance in farmOS 7
Calculate the distance between two latitude/longitude points in meters.
Parameters
Point $p1: The first point.
Point $p2: The second point.
Return value
string Returns the distance as a string, in meters.
1 call to farm_map_distance()
- farm_map_line_length in modules/
farm/ farm_map/ farm_map.geo.inc - Calculate the length of a LineString in meters.
File
- modules/
farm/ farm_map/ farm_map.geo.inc, line 267 - Farm map geometry functions.
Code
function farm_map_distance($p1, $p2) {
// Load GeoPHP.
geophp_load();
// Set BCMath scale.
farm_map_set_bcscale();
// Build a LineString and calculate the center point.
$line = new LineString(array(
$p1,
$p2,
));
$centroid = $line
->centroid();
// Calculate the length of latitude and longitude degrees at the centroid.
$lon_deg_len = farm_map_lon_deg_len($centroid
->getY());
$lat_deg_len = farm_map_lat_deg_len($centroid
->getY());
// If BCMath is available, use that. Otherwise, use normal PHP float
// operations.
if (geoPHP::bcmathInstalled()) {
$length = bcsqrt(bcadd(bcpow(bcmul(bcsub($p1
->getX(), $p2
->getX()), $lon_deg_len), '2'), bcpow(bcmul(bcsub($p1
->getY(), $p2
->getY()), $lat_deg_len), '2')));
}
else {
$length = sqrt(pow(($p1
->getX() - $p2
->getX()) * $lon_deg_len, 2) + pow(($p1
->getY() - $p2
->getY()) * $lat_deg_len, 2));
}
// Reset BCMath scale.
farm_map_reset_bcscale();
// Return the length as a string.
return (string) $length;
}