I struggled with setting up the V and H spacing for hexagons. The dialogue box in mmqgis is less than helpful. What exactly does V spacing mean? What if I want to specify an area for each hexbin? What if I want to specify a side length? What does a V spacing of 1000 units mean in area?
I made a simple Python script (and an accompanying graphic) to help users with this problem. The issue is that the parameters are used to create the bounding of the hexagons, rather than any one hexagon itself.
Copy and paste this into a text file called get_params.py
, then open console/command line and change the directory to where the file is and run » python get_params.py
.
from math import sqrt
# Passes a desired area (in acres) and returns a dictionary of mmqgis v- and h-spacing parameters
def getParamsFromArea(target_area_ac):
target_area_ft = 43560 * target_area_ac
target_side_length = sqrt( target_area_ft / (3 * sqrt(3) / 2) )
bounding_side_length = target_side_length * sqrt(3)
bounding_incircle_radius = 3 * target_side_length / 2
params = {'h-spacing': bounding_incircle_radius, 'v-spacing': bounding_side_length}
print("""A target hexbin of {} acres yields a side
length of {:.4f} feet and is {} sq.ft. in area""".format(target_area_ac, target_side_length, target_area_ft))
for k, v in params.iteritems():
print("{}: {:10.8f} feet".format(k, v))
for k, v in params.iteritems():
print("{}: {:10.8f} meters".format(k, v * 0.3048))
return params
# Passes a desired side length (in feet) and returns a dictionary of mmqgis v- and h-spacing parameters
def getParamsFromLength(target_side_length):
target_area_ft = ( target_side_length * target_side_length ) * ( (3 * sqrt(3) ) / 2 )
target_area_ac = target_area_ft * 2.29568e-5
bounding_side_length = target_side_length * sqrt(3)
bounding_incircle_radius = 3 * target_side_length / 2
params = {'h-spacing': bounding_incircle_radius, 'v-spacing': bounding_side_length}
print("A target hexbin with side length {} feet yields an area of {:.2f} acres and {} sq.ft. in area".format(target_side_length, target_area_ac, target_area_ft))
for k, v in params.iteritems():
print("{}: {:10.8f} feet".format(k, v))
for k, v in params.iteritems():
print("{}: {:10.8f} meters".format(k, v * 0.3048))
return params
# Passes the mmqgis dialogue parameter 'V spacing' and prints the resulting side length and area
def translateVsp(v_spacing):
bounding_side_length = v_spacing
target_side_length = bounding_side_length / sqrt(3)
target_area_ft = ( target_side_length * target_side_length ) * ( (3 * sqrt(3) ) / 2 )
target_area_ac = target_area_ft * 2.29568e-5
print("A V spacing of {} units yields a side length of {} units and an area of {} units squared.".format(v_spacing, target_side_length, target_area_ft))
# Get user input
mode = raw_input("Do you want to get parameters or translate V spacing? ('p' or 't'): ")
if mode == 't':
v = input("Enter the V spacing from mmqgis input dialogue: ")
translateVsp(v)
elif mode == 'p':
calc_type = raw_input("Do you want to get parameters from a side length or an area? ('l' or 'a'): ")
if calc_type == 'a':
a = input("Enter the number of acres for the hexbin: ")
getParamsFromArea(a)
elif calc_type == 'l':
l = input("Enter the side length for the hexbin: ")
getParamsFromLength(l)
else:
print("Error: enter 'l' or 'a' (without quotes) when prompted.")
else:
print("Error: enter 'p' or 't' (without quotes) when prompted to select a calculation mode.")