Using 3.10.1-A Coruña on Windows 10
I am looping over some (10) national shapefiles and finding intersection and areas but the process is very slow.
I have two loops - one loop for each postcode and one loop for each feature in the Parent polygon layer.
It takes an hour to test 31 Parent polygons against 2,670 postcodes.
It takes 2.25 hours to test 43 Parent polygons against the same number of postcodes.
I have some shapefiles with hundreds and thousands of polygons and I fear this will literally take weeks of computation.
I'm using Ben W's code suggestion from my post PyQGIS layer intersection area seems really inaccurate. Ben W has been a great help to me on this site. I do not want to give the impression that I disrespect Ben W skills and helpful experience at all.
Are there optimization strategies for these kind big loop QGIS tasks?
I'm trying Ben W's suggestion of a spatial index. I've put together this code, but am stuck on the next step:
POA_layer = QgsProject().instance().mapLayersByName('POA_2016_AUST')[0]
parent_region_layer =
QgsProject().instance().mapLayersByName('PHN_boundaries_AUS_May2017_V7')[0]
d = QgsDistanceArea()
d.setEllipsoid('WGS84')
#POA_features = [f for f in POA_layer.getFeatures()]
#parent_region_features = [f for f in parent_region_layer.getFeatures()]
POA_features = {feature.id(): feature for (feature) in
POA_layer.getFeatures()}
parent_region_features = {feature.id(): feature for (feature) in
parent_region_layer.getFeatures()}
POA_index = QgsSpatialIndex()
map(POA_index.insertFeature, POA_features.values())
Parent_index = QgsSpatialIndex()
map(Parent_index.insertFeature, parent_region_features.values())