Geographic Information Systems Asked on August 12, 2020
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.
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':
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':
Proper solution:
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 luck!
Answered by Miro on August 12, 2020
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')
Answered by BERA on August 12, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP