function farm_area_generate_bbox_geometries in farmOS 7
Generates a set of rectangle geometries running horizontally to fill a bounding box.
Parameters
array $bbox: An array containing bounding box information (in the same format that GeoPHP generates).
int $count: The number of rectangles to fit into the box.
Return value
array Returns an array of rectangle geometries that fit into the bounding box.
1 call to farm_area_generate_bbox_geometries()
- 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.
File
- modules/
farm/ farm_area/ farm_area_generate/ farm_area_generate.module, line 446 - Farm area generate module.
Code
function farm_area_generate_bbox_geometries($bbox, $count) {
// Load GeoPHP.
geophp_load();
// Set BCMath scale.
farm_map_set_bcscale();
// Calculate how wide each rectangle needs to be.
if (geoPHP::bcmathInstalled()) {
$total_width = bcsub($bbox['maxy'], $bbox['miny']);
$geom_width = bcdiv($total_width, $count);
}
else {
$total_width = $bbox['maxy'] - $bbox['miny'];
$geom_width = $total_width / $count;
}
// Fill the bounding box with rectangles.
$geometries = array();
$starting_point = new Point($bbox['minx'], $bbox['maxy']);
for ($i = 1; $i <= $count; $i++) {
$points = array();
$points[] = $starting_point;
if (geoPHP::bcmathInstalled()) {
$points[] = new Point($bbox['maxx'], bcsub($bbox['maxy'], bcmul($geom_width, $i - 1)));
$points[] = new Point($bbox['maxx'], bcsub($bbox['maxy'], bcmul($geom_width, $i)));
$points[] = new Point($bbox['minx'], bcsub($bbox['maxy'], bcmul($geom_width, $i)));
}
else {
$points[] = new Point($bbox['maxx'], $bbox['maxy'] - $geom_width * ($i - 1));
$points[] = new Point($bbox['maxx'], $bbox['maxy'] - $geom_width * $i);
$points[] = new Point($bbox['minx'], $bbox['maxy'] - $geom_width * $i);
}
$points[] = $starting_point;
$geometries[] = new Polygon(array(
new LineString($points),
));
$starting_point = $points[3];
}
// Reset BCMath scale.
farm_map_reset_bcscale();
// Return the geometries.
return $geometries;
}