1

I'm pretty new to using the console and still have a lot to learn so this might come across as a really stupid question.

I imputed this code into the console:

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[0].getFeatures():
   counter += 1
   print(counter)

   for layer in layers:
       if layer.name() != iter_layer[0].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 trying to run a few thousand points through each of the layers I have created and get an output on a text file that I can use to analyze further. And added a counter to see how long it takes as when I first ran this it took around a week. However, when I run it this is what I get:

Traceback (most recent call last):  
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/code.py", line 91, in runcode    
exec(code, self.locals)  
File "input", line 1, in 'module'  
File "string", line 31, in 'module'
KeyError: 'pcds'

This is probably very simple to fix but I have searched online for a while and couldn't find anything to help.

I am using QGIS 3.4.9-Madeira and my OS is MacOS 10.14.6 Mojave.

3

This line is causing the error msgout = '%s, %s \n' % (point["pcds"], layer.name())

Without seeing the data, I'm assuming your point file doesn't have a column named 'pcds'

Renaming it to the correct column should solve the error.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.