You are here

function farm_area_calculate_area in farmOS 7

Calculate the area of a farm area.

Parameters

$area_id: The area id to load.

$format: Boolean indicating whether or not the returned value should be formatted based on the unit of measurement

Return value

string Returns the calculated are of the area as a string, in meters squared by default. If $format is TRUE it will be converted and formatted using farm_area_format_calculated_area().

See also

farm_area_format_calculated_area()

3 calls to farm_area_calculate_area()
farm_area_calculate_area_multiple in modules/farm/farm_area/farm_area.module
Calculate the area of multiple farm areas.
farm_soil_amendment_form in modules/farm/farm_soil/farm_soil.farm_quick.amendment.inc
Soil amendment quick form.
farm_soil_disturbance_form in modules/farm/farm_soil/farm_soil.farm_quick.disturbance.inc
Soil disturbance quick form.

File

modules/farm/farm_area/farm_area.module, line 316

Code

function farm_area_calculate_area($area_id, $format = FALSE) {

  // Load the area.
  $area = taxonomy_term_load($area_id);

  // If the area doesn't exist, bail.
  if (empty($area)) {
    return '';
  }

  // Get WKT from the field. If empty, bail.
  if (!empty($area->field_farm_geofield[LANGUAGE_NONE][0]['geom'])) {
    $geom = $area->field_farm_geofield[LANGUAGE_NONE][0]['geom'];
  }
  else {
    return '';
  }

  // Load the WKT into a GeoPHP Geometry object and reduce it.
  geophp_load();
  $polygon = geoPHP::load($geom, 'wkt');

  // If the geometry is empty, bail.
  if ($polygon
    ->isEmpty()) {
    return '';
  }

  // Reduce the geometry. If this returns FALSE, bail.
  $polygon = geoPHP::geometryReduce($polygon);
  if (empty($polygon)) {
    return '';
  }

  // Ensure that it is a simple polygon.
  if ($polygon
    ->geometryType() != 'Polygon') {
    return '';
  }

  // Calculate the area in square meters.
  $measurement = farm_map_polygon_area($polygon);

  // Return the area as a string (optionally formatted using the default
  // system of measure).
  if (!empty($format)) {
    return farm_area_format_calculated_area($measurement);
  }
  return $measurement;
}