Hi,
I'm trying to figure out how to make display coordinates of each hexagon in a hex array. The numbers are a combination of the row and column of the hexagon. The following is an example of what I'm trying to do:
Thanks!
Hi,
I'm trying to figure out how to make display coordinates of each hexagon in a hex array. The numbers are a combination of the row and column of the hexagon. The following is an example of what I'm trying to do:
Thanks!
Xander there are two representations of hexagons...pointy-headed and flat-headed (and they aren't my terms). I document both on my blog:
Numpy Snippets # 3 ... Phish_Nyet ... creating sampling grids using numpy and arcpy and
Hexagons, Rectangles and Triangles... Sampling Frameworks .
One of them has some code for numbering but it is different than the method you used, to give some options.
So pick your type and number accordingly.
Hi Dan Patterson , thanks for reminding me of what numpy is capable. Great links!
Okay, a second test looks like this:
... and applying the label format from your example:
The code I used for this was:
import math import arcpy import os def generate_hexagons(startx, starty, endx, endy, radius): hex_h = math.sin(math.radians(60)) * radius * 2 hex_w = radius + math.cos(math.radians(60)) * radius cols = int(math.ceil((endx - startx) / hex_w)) rows = int(math.ceil((endy - starty) / hex_h)) dct = {} cnt = 0 for col in range(cols): for row in range(rows): cnt += 1 x = startx + (col * hex_w) y = endy - (row * hex_h) m = col % 2 == 1 polygon = create_hexagon(x, y, radius, m) dct[cnt] = [polygon, col+1, row+1] return dct def create_hexagon(x, y, radius, shift): lst = [] shift_y = -1 * (math.sin(math.radians(60)) * radius) if shift else 0 for a in range(7): angle = a * 60 lst.append(arcpy.Point(x + math.cos(math.radians(angle)) * radius, y + math.sin(math.radians(angle)) * radius + shift_y)) return arcpy.Polygon(arcpy.Array(lst)) # create empty fc arcpy.env.overwriteOutput = True out_fc = r"C:\Forum\Hexagon\hexa2v05.shp" path, name = os.path.split(out_fc) arcpy.CreateFeatureclass_management(path, name, "POLYGON") # generate the hexagons from 0,0 to 10,10 with a radius of 1 dct = generate_hexagons(0, 0, 10, 10, 1) # write the hexagons to a fc fld_lbl = "Label" arcpy.AddField_management(out_fc, fld_lbl, "TEXT", None, None, 10) flds = ("SHAPE@", fld_lbl) with arcpy.da.InsertCursor(out_fc, flds) as curs: for cnt, lst in dct.items(): polygon = lst[0] col = lst[1] row = lst[2] # lbl = "C{0} R{1}".format("%02d" % (col,), "%02d" % (row,)) lbl = "{0}{1}".format("%02d" % (col,), "%02d" % (row,)) row = (polygon, lbl,) curs.insertRow(row)
If you have any questions let me know.
Kind regards, Xander
... and if you want to use the cloud: Creating thematic maps with hexagons in ArcGIS Online | ArcGIS Blog
Oh dear, mmm, I guess I did something wrong here... I used some code to create hexagons and analyzed the rows and columns to create the labels, but my hexagons are oriented differently... Oops. This is my result (I added the R and C to show what value represents the row and which one the column):
The code I used is:
I'm gonna dive into this again and generate the hexagons like you show in the image.