How To Create Hex Binning For Data Analysis

Introduction

Hex Binning is a method to visualize density of data using location information and aggregating data over Hex Polygon. For example, if you have location data of number of uber/any taxi rides, then to access the density based on geo-location hex binning is used. It is a technique to group the data under one hex and aggregate the count within that hex. The result is the density map using which pricing can be altered and more taxis can be sent to reduce the demand and maintain the prize etc.

Today let us see how we can create Hex Bin maps for certain geo-data, mainly will focus on global scale and entire world map. This same method can be scaled down and used for a country, state, city to perform location data analysis, if required in real-time also.
 

The first step is to create a hex grid on world map and clip it with country boundaries, for this I will use Open source QGIS (https://www.qgis.org). A detailed explanation on how to create hex vector dataset can be found in my git repository https://github.com/santoshkaranam/maps .

Steps to Create Hex Grid Using QGIS

  1. Select "create grid" from research tools in QGIS as shown 

    How to Create Hex Binning for Data Analysis
     
  2. For 100km hex grid select 1 degree overlay. (approx. 1 degrees = 111 Kms)
  3. Click "Run" button to create the hex grid with map extent

Clip Hex Grid to World Extent

  1. Load a simple world datasets.
  2. select intersection from QGIS 

    How to Create Hex Binning for Data Analysis
     
  3. Click "Run" to get clipped hex layer.
  4. If you need a complete Hex even at the edges, then save the clicked Hex layer as a CSV file without geometries and open the CSV file to copy all the IDs.
  5. Now Filter the original hex layer before intersection with the IDs from CSV. Save the filtered hex to new dataset file.

All the above steps were run to create the hex-grid-100km-worldextent_clipped.geojson file in the GIT repository https://github.com/santoshkaranam/maps

1. hex-grid-100km-worldextent.geojson

This geojson consists of hex grid with hex size of 100km with world extent. It is in EPSG 4326 WGS84 project system. This grid can be used as base layer for analysis based on requirement. Eg to represent heat maps, aggregation maps, hexbinning, etc. The location can be anywhere on land or in the sea.

2. hex-grid-100km-worldextent_clipped.geojson

This geojson is same as above but clipped to country boundaries. This layer can serve as base layer for locations inside country boundaries on land.

Next Step is to load this dataset into Postgis and aggregate data under the hex polygons.

As new data is loaded into the database we need to aggregate it under the hex polygon and serve it to website as need to display on the map as shown here - https://hexbinning.vercel.app/

The NextJS website code is also present in the above git hub repository along with the data here - https://github.com/santoshkaranam/maps/blob/main/src/public/data.json