2

I'm blocked with creation of an iterator in my script. I want coordinates of selected points. If I do that, it works, it print all points coordinates

##point=vector
layer = processing.getObject(point)
it = layer.getFeatures()
for i in it:
    x = i.geometry().asPoint().x()
    y = i.geometry().asPoint().y()
    print "X Coord :  %f" %x
    print "Y Coord :  %f" %y
    print

So after I add my selection. But it crash.

##point=vector
layer = processing.getObject(point)
nbr = QgsExpression( "ID LIKE '%-1'" )
itnbr = layer.getFeatures( QgsFeatureRequest( nbr ) )
for i in itnbr:
    x = i.geometry().asPoint().x()
    y = i.geometry().asPoint().y()
    print "X Coord :  %f" %x
    print "Y Coord :  %f" %y
    print

I have an OGR error message : invalid data source

What causes that?

8
  • Your code works fine for me. How are you defining layer?
    – Joseph
    Sep 15, 2017 at 14:08
  • Yes sorry I forgot to put it in my post layer = processing.getObject(point)
    – Tim C.
    Sep 15, 2017 at 14:42
  • And point is a point vector layer in qgis
    – Tim C.
    Sep 15, 2017 at 14:49
  • What does it say if you use print layer?
    – Joseph
    Sep 15, 2017 at 14:57
  • <qgis._core.QgsVectorLayer object at 0x123d49cc8>
    – Tim C.
    Sep 15, 2017 at 15:03

1 Answer 1

2

Issue is probably related to type field of your shapefile. Generally, ID values are numerical and you are using a Wildcard Character (%) with a minus sign (-) for your search. It's only logical if shapefile has a string field in attributes table. To test my approach I used a shapefile point layer (25 features) with ID field and following version of your code:

import processing

point = '/home/zeito/pyqgis_data/random_points.shp'

layer = processing.getObject(point)
nbr = QgsExpression( u"ID LIKE '%1'" )
#nbr2 = QgsExpression( u"MARKER_COL LIKE '%-ue'" )

itnbr = layer.getFeatures( QgsFeatureRequest( nbr ) )

for i in itnbr:
    x = i.geometry().asPoint().x()
    y = i.geometry().asPoint().y()
    print "X Coord :  %f" %x
    print "Y Coord :  %f" %y
    print

After running the code at Python Console of QGIS I got:

X Coord :  390945.280000
Y Coord :  4451149.380000

X Coord :  439639.970000
Y Coord :  4455290.050000

X Coord :  366311.250000
Y Coord :  4433366.270000

for IDs 1, 11 and 21; as expected.

Afterward, I modified only one record in attributes table (MARKER_COL field); as it can be observed at following image:

enter image description here

for running following version of your code:

import processing

point = '/home/zeito/pyqgis_data/random_points.shp'

layer = processing.getObject(point)
#nbr = QgsExpression( u"ID LIKE '%1'" )
nbr2 = QgsExpression( u"MARKER_COL LIKE '%-ue'" )

itnbr = layer.getFeatures( QgsFeatureRequest( nbr2 ) )

for i in itnbr:
    x = i.geometry().asPoint().x()
    y = i.geometry().asPoint().y()
    print "X Coord :  %f" %x
    print "Y Coord :  %f" %y
    print

Result was:

X Coord :  390945.280000
Y Coord :  4451149.380000

for 'bl-ue' record (MARKER_COL field); again as expected.

1
  • Indeed, my ID field is a string one like 12-34. My data provider gives me it like that. It contains row number and number in row of vines. I found a solution too : I create a list with itnbr and make my loop on it, weird... but it works. What is u just before expression ?
    – Tim C.
    Sep 18, 2017 at 7:42

Your Answer

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

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