I am fairly new to using the python console in QGIS so I am not entirely sure if there is an issue with a line of code I am using or human error.
I am using this code to cycle through a point layer of 3000 points situated over around 14 vector layers that I have created. The aim of the code is to efficiently show what layers each point resides on. As each vector layer is assigned a rank I cannot do a select by location as that would take months.
When running a test of around 10 points, the code runs with no issues, but when I go to open the text file I have created for the output, it is empty.
However, interestingly when I ran the code with both the test data points and with the original dataset of 3000 still active in QGIS, an output was produced. So I am assuming the code works and produces an output when a point overlays another point but not when a point overlays a vector layer. Is this an mistake within the code or might it be another factor? For example, the text file has to be formatted in a certain way? The code is displayed below.
counter = 0 # Set the path for the output file output_file = open(r'/Users/admin/Desktop/TestFlood.txt', 'wb') iter_layer = QgsProject.instance().mapLayersByName('test_flood') layers = QgsProject.instance().mapLayers().values() for point in iter_layer.getFeatures(): counter += 1 print(counter) for layer in layers: if layer.name() != iter_layer.name(): for feature in layer.getFeatures(): if (feature.geometry().contains(point.geometry())): msgout = '%s, %s \n' % (point["pcds"], layer.name()) unicode_message = msgout.encode('utf-8') output_file.write(unicode_message) print('Finished') output_file.close()
I am using QGIS 3.4.9-Madeira and my OS is MacOS 10.14.6 Mojave.