You are here

function farm_map_lat_deg_len in farmOS 7

Calculate latitude degree length at a given latitude. Equations are taken from https://en.wikipedia.org/wiki/Geographic_coordinate_system#Expressing_la...

Parameters

$lat: The latitude to calculate degree length at, in degrees.

Return value

string Returns the length of a degree of latitude at the given latitude as a string, in meters.

2 calls to farm_map_lat_deg_len()
farm_map_distance in modules/farm/farm_map/farm_map.geo.inc
Calculate the distance between two latitude/longitude points in meters.
farm_map_polygon_area in modules/farm/farm_map/farm_map.geo.inc
Calculate the area of a Polygon in square meters.

File

modules/farm/farm_map/farm_map.geo.inc, line 177
Farm map geometry functions.

Code

function farm_map_lat_deg_len($lat) {

  // Load GeoPHP.
  geophp_load();

  // Set BCMath scale.
  farm_map_set_bcscale();

  // Convert degrees to radians.
  $lat = deg2rad($lat);

  // Define coefficients. These are copied from
  // http://gis.stackexchange.com/questions/75528/length-of-a-degree-where-do-the-terms-in-this-formula-come-from
  $m1 = 111132.95255;
  $m2 = 559.84957;
  $m3 = 1.17514;
  $m4 = 0.0023;

  // If BCMath is available, use that. Otherwise, use normal PHP float
  // operations.
  if (geoPHP::bcmathInstalled()) {
    $length = bcsub($m1, bcadd(bcmul($m2, cos(bcmul(2, $lat))), bcsub(bcmul($m3, cos(bcmul(4, $lat))), bcmul($m4, cos(bcmul(6, $lat))))));
  }
  else {
    $length = $m1 - $m2 * cos(2 * $lat) + $m3 * cos(4 * $lat) - $m4 * cos(6 * $lat);
  }

  // Reset BCMath scale.
  farm_map_reset_bcscale();

  // Return the length.
  return (string) $length;
}