2

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[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 using QGIS 3.4.9-Madeira and my OS is MacOS 10.14.6 Mojave.

5
  • You should remove the outer enclosing parentheses from this line: if (feature.geometry().contains(point.geometry())):
    – Ben W
    Commented Jan 19, 2020 at 23:38
  • I don't think the problem is related to syntax. It works on Windows 10, QGIS 3.8 correctly. Commented Jan 20, 2020 at 8:38
  • Thanks for clarifying @Kadir Şahbaz. Interesting that it works with that syntax. I didn't test, though- it just struck me as a possible cause of problem.
    – Ben W
    Commented Jan 20, 2020 at 9:48
  • How exactly are you running this code? Entering it line-by-line into the QGIS Python console, as a Processing algorithm, or some other way? The context would be useful to know.
    – LRitzdorf
    Commented Jan 25, 2020 at 4:06
  • The description is like "all works fine but the points coordinates are not contained by the geometry coordinates of the other features". Make sure that the layers are defined in the same CRS and the features geometries coordinates overlaps. Commented Feb 2, 2020 at 19:50

2 Answers 2

1

It looks like your issue lies in the way you've tried to format your output and write it to your file.

First, instead of opening the file in binary writing ('wb') mode, try standard writing mode ('w'). Once you've done that, there's no need to encode your output string - just use output_file.write(msgout).

Note that this doesn't explicitly specify an encoding format. If that's important for you, you can specify encoding='utf-8' in the open() call.

Additionally, make sure that the only layers present in the project are the other vector layers that you want to search in. Anything else, especially a raster layer like OpenStreetMap, will cause an error when your getFeatures() call is made.

Hope this helps! If not, as always, feel free to comment below.

4
  • Hi, I tried what you suggested but unfortunately got the same result as last time. Do you think it could be due to the way the code is written? For example, is there anything suggesting that vector geometry isn't accounted for but point data are?
    – Henry
    Commented Jan 21, 2020 at 12:32
  • @Henry Interesting. In that case, it looks like your problem is with the actual code, rather than the output. After you run the code, is there anything illuminating in the error logs? Also, is there a possibility that you could attach your data files here? If not, I'll make my own to test this myself.
    – LRitzdorf
    Commented Jan 25, 2020 at 4:02
  • Nothing comes up in the error log. And the data files are pretty large so I am not sure I'll be able to attach. The data files are just simple vector layers, the majority of them overlap each other while the test points are on top of the load order.
    – Henry
    Commented Jan 26, 2020 at 12:13
  • Okay, I just tested this with a very small dataset I made for myself. It seems to work, with the modification proposed in this answer (i.e. not using binary mode). Note that the output is only visible in the file after calling close(). (Also see my edit to the answer.) To clarify, you are running this from the Python Console that appears as a panel at the bottom of the QGIS window, right?
    – LRitzdorf
    Commented Feb 2, 2020 at 17:52
0

In response to comments on my previous answer, I've created a test dataset (available at https://www.dropbox.com/sh/wf2xdw2gzgci0yo/AACD2809I876ijEdpy_zjUgMa?dl=0) to make sure everything works as expected. Typing the code from the question line-by-line using the Python Console (Ctrl+Alt+P on Windows), and with normal file writing as in my previous answer, works for me. At the moment, it's unclear why the OP's problem occurred in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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