2

I am simply trying to run a command in QGIS to extract the coordinates of a point layer, but I get a syntax error when trying to do so (see code below). I can not see where the issue is - any suggestions?

for f in layer.getFeatures():
  geom = f.geometry()
  print geom.asPoint()

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 3
    print geom.asPoint()
             ^
SyntaxError: invalid syntax
1
  • 9
    For Python 3.x your syntax needs to be print(geom.asPoint()).
    – PolyGeo
    Jul 14 '19 at 10:50
1

As suggested by PolyGeo in his comment, print is now a true function, so in Python 3 you have to call it with its arguments given in parenthesis otherwise such error will be raised:

print "Hello world"

Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 64, in runsource
    code = self.compile(source, filename, symbol)
  File "/usr/lib/python3.6/codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "/usr/lib/python3.6/codeop.py", line 99, in _maybe_compile
    raise err1
  File "/usr/lib/python3.6/codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "/usr/lib/python3.6/codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    print "Hello world"
                      ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello world")?

Hence, your code should be:

layer = QgsProject.instance().mapLayersByName('my_layer')[0] 
# [0] because the result is a list, so I have to take its first element.
for f in layer.getFeatures():
    geom = f.geometry()
    print(geom.asPoint())

Which results in:

<QgsPointXY: POINT(852.5430 508.0729)>
<QgsPointXY: POINT(091.4880 580.0480)>
(...)

If you don't actually call the .asPoint() method, e.g. by forgetting the parentheses, here's what happens:

layer = QgsProject.instance().mapLayersByName('my_layer')[0] 
# [0] because the result is a list, so I have to take its first element.
for f in layer.getFeatures():
    geom = f.geometry()
    print(geom.asPoint) # parentheses forgotten here!

Which doesn't raise an error but which results in this:

<built-in method asPoint of QgsGeometry object at 0x7fd1231a5558>
<built-in method asPoint of QgsGeometry object at 0x7fd1231a5d38>
(...)

I'm working on QGIS 3.12.1 and Python 3.6.9 at the time I wrote this answer.

Check for Python version inside QGIS

This is how I know my Python version from the Python console inside QGIS itself:

import sys
sys.version_info
sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)

And here's how you can get a layer by its name:
Getting layer by name in PyQGIS?

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