I would like to make a biodiversity heatmap in QGIS out of polygons and not points. I have a shape file made up of many overlapping polygons. I know how to make a heatmap with a set of points by using centroids, or by using a QGIS plugin called the Tombio FSC Biological Records Tool, but how do I do this with polygons? An example shape file of multiple sub-polygons can be downloaded from the IUCN (iucnredlist.org/resources/spatial-data-download), an animal conservation group, by creating a free account with them and downloading any of their shape files (e.g. marine mammals, mammalia, amphibia, reptiles). I want my map to look like this one, for example.
1that map looks like the "number of species" polygons have a graduated style, and there's a country boundary layer overlaid on top– cskSep 11, 2019 at 2:35
I thought about that but that didn't work. I'm working with a single shape file that has a number of subclasses. When setting style to graduated it just changes each of the polygons to a different color. It doesn't do addition of all the overlapping polygons– MapDeathSep 11, 2019 at 2:44
4It sounds like you want to count the number of polygon each overlapping area. There's a good answer for that question here (for your purposes you can probably skip the final step of rasterizing): gis.stackexchange.com/a/297334/81764– cskSep 12, 2019 at 18:43
1iucnredlist.org/resources/spatial-data-download - I suppose this is the download website link which took me a while to find.– MiroJul 29, 2020 at 4:22
1And I believe this question gives you complete guide on how to sum up values to visualize the layer the way you want: gis.stackexchange.com/questions/210959/…– MiroJul 29, 2020 at 4:38
Union the polygons with themselves:
Then adjust and execute code below. It will find the duplicate geometries produced by Union and store their count in a field:
from collections import defaultdict lyr = iface.activeLayer() #Click union layer feats = [f for f in lyr.getFeatures()] opcount = defaultdict(list) processed =  for i in range(0,len(feats)): f1 = feats.pop() for f2 in feats: if f1.geometry().equals(f2.geometry()): #Union will produce identical overlapping geometries if f2.id() not in processed: opcount[f1.id()].append(f2.id()) processed.append(f2.id()) field_to_update = 'overlaps' #Add integer field before executing code with edit(lyr): for f in lyr.getFeatures(): for k,v in opcount.items(): if f.id() in [k]+v: newval = 1+len(v) if 'newval' not in locals(): newval = 1 f[field_to_update] = newval del(newval) lyr.updateFeature(f) print('Done')
While this seems like it would be a sensible and logical solution, for a dataset greater than around 300 polygons, I couldn't get past 30% completion of just the Unioning after letting it run for two days straight. Unfortunately the answer I'm looking for must must be able to accomodate large sets of polygons and do it quickly like ArcGIS can. Thanks for the response though, I'm sure it may help people with smaller datasets Aug 12, 2020 at 4:39
Ok! If i get the time I will try to make it faster.
.equalsmethod is probably slow to perform on all combinations of polygons. It would be possible to first Group by similar area and only
equalcompare those.– BERAAug 12, 2020 at 6:23
Let me know if you ever make it faster and/or make a post about it Nov 19, 2020 at 3:08
I have downloaded the 1.5 GB shape file called MAMMALS for testing. There was one invalid polygon, which was easy to validate. Now the issue with this layer is that for Union, and most other analysis alike, it has way too complicated polygons which will be hard to process in reasonable time on local desktop.
This leads me to suggest quick and dirty solution - 'rasterize':
- Load your polygon layer into QGIS, set stroke style to 'No Pen', and Fill color to white with 1% opacity
- Export it to image with desired extent and resolution - Project / Import/Export / Export Map to Image...
- Load your exported image back to QGIS and color it based on band any way you like
Clearly there is a limitation of 100 classes, and everything above 100 is 100.
Slower solution but likely achievable on pc with reasonable processing time - 'point sampling':
- Load your polygon layer
- Generate regular points at reasonable interval (Vector / Research Tools / Regular Points...)
- For every point get it as many times as it intersects polygon (Vector / Data Management Tools / Join attributes by location - select attributes you need (just 'presence' will do), and make sure the join type is Create separate feature for each matching feature (one-to-many)
- Style joined point layer with Heatmap styling
Use e.g. PostgreSQL / PostGIS database system, build spatial index, fix potential invalid geometries, and run SQL query to create new layer with all intersections/differences geometries with their appropriate count - question like Separate polygons based on intersection using PostGIS might help to build a SQL with desired results and make sure you run it on beefy PC, ideally on supercomputer.
Good try but this needs to be able to be accomplished with any number of shape files (mine personally I downloaded and subsetted from the IUCN has over 300), and not created with a grainy raster grid. The ArcGIS solution does a perfect 1:1 overlapping of all polygons without having to rasterize. It's ok if my question can't be accomplished in QGIS, in which case it should be added as a desired feature to the QGIS development team GitHub page Jul 29, 2020 at 18:52
@MapDeath It can be accomplished. See 'Proper Solution', and that is just one of approaches possible. It is just not straightforward with your data. All processing tools in QGIS can be automated via model builder. So you can build model to validate geometry, create spatial indexes to speed up process on your files and then run union and aggregate to do all in batch. I do not think with the data I have seen this is simple task in any software. As I have ArcGIS Pro availible I will give it a shot later today to see how well it goes there. Jul 30, 2020 at 0:13
@MapDeath Just so you know in ArcGIS Pro with Mammals dataset on a relatively good machine after 9 hours of processing the tool ended up with ERROR 160196: Invalid Topology - Failed to execute (CountOverlappingFeatures). Jul 30, 2020 at 10:20
I sent my colleague my 300 polygon IUCN shape file and he had it back to me within 15 minutes however he did it in ArcGIS Jul 31, 2020 at 2:11
@MapDeath that is surely possible. It all depends on dataset itself. I was testing Mammals dataset which has over 1300 multipolygons, when converted to single polygons over 130 thousands, lot of them still very complex as boundaries often follow completely whole continents. Aug 1, 2020 at 8:39