Skip to content

Instantly share code, notes, and snippets.

@maptastik
Last active April 29, 2017 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maptastik/8e0294993b4100bf18b9cddfcd05cf8e to your computer and use it in GitHub Desktop.
Save maptastik/8e0294993b4100bf18b9cddfcd05cf8e to your computer and use it in GitHub Desktop.

Create a hexbin grid with equal-area and equilateral hexagons

Using hexbin grids is a great way to aggregate data. However, with the commonly used Web Mercator projection, it is impossible to balance the technical necessity of uniform area across all of the hexbins with the aesthetics of equilateralness for each hexbin in the grid. You can have one or the other, but not both. You'll need to use a different projection to help generate an appropriate compromise between the area and shape of hexbins. This walk-thru is meant to show how to make a map of Lexington, KY with equal-area hexbins. In particular, it explains how to create hexbins that are not only equal-area, but also equilateral.

Prerequisites

1. Setup QGIS

  1. Open up a new QGIS project. You could adapt an existing .qgs project, but it's perhaps a bit cleaner just to start fresh.
  2. Navigate to Project>Project Properties... and click.
  3. The Project Properties window should open. Click the CRS tab.
  4. Check Enable 'on the fly' CRS transformation (OTF) at the top of the window if it isn't already checked.
  5. Next you need to set the CRS for the project. For Fayette County, a good one for our purposes is NAD_1983_StatePlane_Kentucky_North_FIPS_1601_Feet (SPN). In the Filter box, type 102679. This is a unique identifier for this CRS so it should be presented to you as the only CRS to select. Select NAD_1983_StatePlane_Kentucky_North_FIPS_1601_Feet and click OK.

2. Reproject the Fayette County bouandary shapefile

The Fayette County boundary shapefile you downloaded from the Lexington Open Data portal is unprojected and uses WGS84 as its CRS. You're going to use this file to help create our hexbin grid. However it's geometry isn't based on our project's CRS, this could present some problems. As such, you need to reproject the boundary file from WGS84 to SPN.

  1. Add the county boundary shapefile to you project.
  2. In the table Layers Panel, find the boundary layer. Right-click and select Save As....
  3. The Save vector layer as... window should open. There are several options you can configure here, but you'll keep most of the defaults. There are a few fields you need to check on or specify values for:
  • Format: ESRI Shapefile
  • File name: Specify what you want to call the reprojected boundary file and where to save it
  • CRS: In the dropdown menu, select Project CRS
  • Add saved file to map: Make sure this checked
  1. Click OK.
  2. Your reprojected county boundary should appear on the map. At this point you can go ahead and remove the original WGS84 boundary layer from your map. You won't need it anymore.

3. Add tile layer basemap

In previous versions of QGIS, you had to use a plugin to add tiled basemaps to your project. The popular OpenLayers plugin, while super in many regards, did work well with on the fly projection. As of QGIS 2.18, you can add tiled basemaps like those produced by Mapbox, Carto, and Stamen without a plugin. What's more, they also work well with on the fly projection.

  1. In the Browser panel, scroll down to option Tile Server (XYZ). Right-click and select New connection....
  2. A window will pop open prompting you for the tile URL for the tile layer you want to add. In this case you want to use ESRI's imagery tile layer. Copy the URL, http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}, and paste it into the form. Click OK.
  3. Another window will pop up asking for a name for the layer you just added. This can be whatever you want. We'll use ESRI Imagery for our purposes here. Once you've typed in your layer title, click OK.
  4. Back in the Browser panel, expand Tile Server (XYZ). You should see the title of the layer you just added. Right-click and selct Add Layer.
  5. It's likely that this tile layer will be added to the top of the Layers Panel. In the Layers Panel, click and drag the tile layer so that it is below the boundary layer.

4. Create a hexbin layer

Now that the project is all setup, you can finally create our hexbin grid. You'll use the MMQGIS plugin for this.

  1. If you haven't already, install the MMQGIS plugin
  2. Go to Plugins>Manage and Install Plugins...
  3. Search MMQGIS, select the plugin and click Install.
  4. Navigate to MMQGIS>Create>Create grid layer and click
  5. The Grid box should open. There are a few parameters you'll need to set:
  • Shape Type: Hexagons
  • Units: Project Units
  • Extent: Layer Extent
  • Layer: The reprojected boundary file
  • Output Shapefile: Specify a name and location for you grid shapefile to be saved
  • X & Y Spacing: You will need to play with this a bit to get the right. Fortunately, the X and Y spacing are linked so when you change one, the other changes as well so that the relative dimensions of the hexagons remains the same.
  1. When you're satisfied with your settings, click OK.

Check out your map. You should see a regular grid of hexagons covering the extent of the county boundary layer! Hooray!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment