You are here

function farm_map_set_bcscale in farmOS 7

Helper function for setting BCMath scale. We use this (along with farm_map_reset_bcscale()) in geometry functions below to consistently call bcscale() with a high precision, so that small latitude/longitude geometries can have their length/area calculated with maximum precision.

See also

https://github.com/phayes/geoPHP/issues/114#issuecomment-160463523

11 calls to farm_map_set_bcscale()
farm_area_calculate_area_multiple in modules/farm/farm_area/farm_area.module
Calculate the area of multiple farm areas.
farm_area_convert_area_units in modules/farm/farm_area/farm_area.module
Convert an area from square meters to another unit.
farm_area_generate_bbox_geometries in modules/farm/farm_area/farm_area_generate/farm_area_generate.module
Generates a set of rectangle geometries running horizontally to fill a bounding box.
farm_area_generate_geometries in modules/farm/farm_area/farm_area_generate/farm_area_generate.module
Generate geometries within a polygon at a given orientation.
farm_area_generate_rotate_point in modules/farm/farm_area/farm_area_generate/farm_area_generate.module
Rotate a point around an origin.

... See full list

File

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

Code

function farm_map_set_bcscale($reset = FALSE) {

  // If BCMath is not installed, bail.
  if (!function_exists('bcscale')) {
    return;
  }

  // The first time this function is called, we will save the current bcscale()
  // value in a static variable, so that it can be reset in the future.
  $scale =& drupal_static(__FUNCTION__ . 'scale');

  // We will also keep track of how deep we are in nested functions, to ensure
  // that the scale is not reset before it should be.
  $depth =& drupal_static(__FUNCTION__ . 'depth');
  if (!isset($depth)) {
    $depth = 0;
  }

  // Reset the scale, if desired.
  if (!empty($reset)) {

    // If the scale has not been set, bail.
    // This prevents farm_map_reset_bcscale() being called before this function.
    if (!isset($scale)) {
      return;
    }

    // Decrement the depth, and if the depth is still greater than 0, return so
    // that we do not reset yet.
    $depth--;
    if ($depth > 0) {
      return;
    }

    // Otherwise, reset.
    // bcscale() sets the scale globally back to whatever it was originally when
    // farm_map_set_bcscale() was first run, which we saved in the static $scale
    // variable. We also unset the static $scale variable so that it can be set
    // again later in the request, if necessary.
    bcscale($scale);
    drupal_static_reset(__FUNCTION__ . 'scale');
    return;
  }

  // Set the scale, if it has not been already.
  // bcscale() sets the scale globally, and returns the previous scale, which we
  // save in the static $scale variable, so that we can reset later.
  if (!isset($scale)) {
    $scale = bcscale(24);
  }

  // Remember how deep we are in nested functions.
  $depth++;
}