Convert Shapefiles into Hexagrams Automatically

Convert Shapefiles into Hexagrams Automatically

Mr. Bailey’s geogrid R-library is pretty neat. You will need to install the “R” language along with geogrid and sf libraries, which you can do with install.packages(‘geogrid’). After installing R and the library, the code to create a hexagram is pretty simple, although you will want to tweak the output a bit in QGIS after creating the computer-generated hexagram.

# required libraries
library(sf)
library(geogrid)

# load geojson shapefile 
df <- st_read( "/tmp/nyco.geojson")

# iterate through seed values 1 to 10 to compare which
# is the best output of hex file (optional, but various seeds 
# can improve the output)

for (i in seq(1,10)) {
# calculates hexgrid as a foreign object
  tmp <- calculate_grid(shape = df, grid_type = "hexagonal", seed = i)

# Convert foreign object to an spatial dataframe
  df_hex <- assign_polygons(df, tmp) 
  df_hex = st_as_sf(df_hex)

# save spatial dataframe as geojson
  st_write(df_hex, paste("/tmp/nyco_hex_",i,".geojson", sep = "") )
}

Here is a sample hexagram I created using the Census TIGER/Line for New York counties.

Median Household Income in New York State, a Hexagram

Leave a Reply

Your email address will not be published. Required fields are marked *