[GIS] Using polygons to create heatmaps in QGIS

heat mapqgis

I've been fiddling around with heatmaps on QGIS for a few days, but came across a small obstacle on my way. I have a dataset comprised of point data, corresponding to objects inside a 1 sq meter grid. Each of those objects has several data associated, namely their weight (in grams). So, I have already created a simple heatmap based on the number of points and another one using the grams value of each point as the weight parameter of the heatmap. This resulted in two different heatmaps, both of them interesting to my analysis.

Nevertheless, I have a different dataset that comes from the same grid – this dataset corresponds to objects that I know came from a specific square on the grid, but to which I don't have a specific point location (as I said, I only know they came from a specific square, but I don't have XY coordinates for them inside that square). I have all the other data for these objects, namely their weight and I wanted to use them to create a new heatmap, where I would attempt to represent the distribution of objects through the grid, based upon their weight. I was thinking of creating a new attribute on my grid shapefile, where I would insert the total object weight (both the ones to which I have XY coordinates and the ones to which I don't have that information) for each square.

I've been looking at the heatmap plugin on QGIS, but it only allows me to use point shapefiles as the input layer… Is there a way of creating a heatmap using polygons as the input layer? Remember, the polygons do not overlap, so I cannot use the approaches sugested elsewhere, regarding overlapping polygons.

I have already created a "points per polygon" shapefile and I guess I could also create a "total weight por polygon" shapefile, but I'd rather have a heatmap, since it would give out a less "boxy" visualization.

Best Answer

You might want to create centroids for your grid of polygons, then and attach the weight value of the data with no exact location (located in a grid cell) to the centroids. This can be accomplished by:

  1. Vector->Geometry Tools->Polygon centroids
  2. Vector->Database management->Join attributes by location

That should give you the weights (from each polygon in the grid) into a point layer of centroids. Now you can merge these points together with your actual point locations to create one point vector with weights, and do any further heat maps etc.

If you want to total the weight per polygon, that's an additional process. I do these kinds of things in a database. Import both the polygon grid and the points into Spatialite, for example then try these SQL commands in Spatialite-gui:

ALTER TABLE grid ADD COLUMN total_weights REAL;
UPDATE grid SET total_weights=(SELECT SUM(p.weight) FROM points AS p WHERE ST_Contains(grid.geometry, p.geometry));

(Of course, you'll have to adjust the column names and table names)

Related Question