You are here

function farm_map_line_length in farmOS 7

Calculate the length of a LineString in meters.

Parameters

LineString $line: The line to measure.

Return value

string Returns the length of the line as a string, in meters.

File

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

Code

function farm_map_line_length($line) {

  // Load GeoPHP.
  geophp_load();

  // Set BCMath scale.
  farm_map_set_bcscale();

  // Start with a length of zero.
  $length = 0;

  // Iterate through the points.
  foreach ($line
    ->getPoints() as $delta => $point) {

    // Attempt to load the previous point.
    $previous_point = $line
      ->geometryN($delta);

    // If a previous point is available
    if ($previous_point) {

      // If BCMath is available, use that. Otherwise, use normal PHP float
      // operations.
      if (geoPHP::bcmathInstalled()) {
        $length = bcadd($length, farm_map_distance($previous_point, $point));
      }
      else {
        $length += farm_map_distance($previous_point, $point);
      }
    }
  }

  // Reset BCMath scale.
  farm_map_reset_bcscale();

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