Skip to main content
corrected spelling
Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

If you type in the Python Console these help commands:

help(processing.getObject)
Help on function getObject in module processing.tools.dataobjects:

getObject(uriorname)

help(QgsVectorLayer.select)
Help on built-in function select:

select(...)
    QgsVectorLayer.select(QgsRectangle, bool)
    QgsVectorLayer.select(int)
    QgsVectorLayer.select(unknown-type)

help(QgsVectorLayer.selectedFeatures)
Help on built-in function selectedFeatures:

selectedFeatures(...)
    QgsVectorLayer.selectedFeatures() -> list-of-QgsFeature

You can observe that if your 'uiorname' is correct you should first select the features with 'select'.

This code works well in my system:

import processing
from qgis.core import * 
vlayer = processing.getObject('/home/zeito/pyqgis_data/world_borders.shp')
            
list=[501, 967, 3171]
            
for item in list:
    vlayer.select(item)
        
selected_objects = vlayer.selectedFeatures()

print selected_objects

for this shapefile with selected features by index 501, 967 and 3171:

enter image description here

where printed references to selected features objects were:

execfile(u'/home/zeito/scriptspyqgis/selected.py'.encode('UTF-8'))
[<qgis._core.QgsFeature object at 0xc6f2b6c>, <qgis._core.QgsFeature object at 0xc6f2a94>, 
<qgis._core.QgsFeature object at 0xc6f2a4c>] 

If you type in the Python Console these commands:

help(processing.getObject)
Help on function getObject in module processing.tools.dataobjects:

getObject(uriorname)

help(QgsVectorLayer.select)
Help on built-in function select:

select(...)
    QgsVectorLayer.select(QgsRectangle, bool)
    QgsVectorLayer.select(int)
    QgsVectorLayer.select(unknown-type)

help(QgsVectorLayer.selectedFeatures)
Help on built-in function selectedFeatures:

selectedFeatures(...)
    QgsVectorLayer.selectedFeatures() -> list-of-QgsFeature

You can observe that if your 'uiorname' is correct you should first select the features with 'select'.

This code works well in my system:

import processing
from qgis.core import * 
vlayer = processing.getObject('/home/zeito/pyqgis_data/world_borders.shp')
            
list=[501, 967, 3171]
            
for item in list:
    vlayer.select(item)
        
selected_objects = vlayer.selectedFeatures()

print selected_objects

for this shapefile with selected features by index 501, 967 and 3171:

enter image description here

where printed references to selected features objects were:

execfile(u'/home/zeito/scriptspyqgis/selected.py'.encode('UTF-8'))
[<qgis._core.QgsFeature object at 0xc6f2b6c>, <qgis._core.QgsFeature object at 0xc6f2a94>, 
<qgis._core.QgsFeature object at 0xc6f2a4c>] 

If you type in the Python Console these help commands:

help(processing.getObject)
Help on function getObject in module processing.tools.dataobjects:

getObject(uriorname)

help(QgsVectorLayer.select)
Help on built-in function select:

select(...)
    QgsVectorLayer.select(QgsRectangle, bool)
    QgsVectorLayer.select(int)
    QgsVectorLayer.select(unknown-type)

help(QgsVectorLayer.selectedFeatures)
Help on built-in function selectedFeatures:

selectedFeatures(...)
    QgsVectorLayer.selectedFeatures() -> list-of-QgsFeature

You can observe that if your 'uiorname' is correct you should first select the features with 'select'.

This code works well in my system:

import processing
from qgis.core import * 
vlayer = processing.getObject('/home/zeito/pyqgis_data/world_borders.shp')
            
list=[501, 967, 3171]
            
for item in list:
    vlayer.select(item)
        
selected_objects = vlayer.selectedFeatures()

print selected_objects

for this shapefile with selected features by index 501, 967 and 3171:

enter image description here

where printed references to selected features objects were:

execfile(u'/home/zeito/scriptspyqgis/selected.py'.encode('UTF-8'))
[<qgis._core.QgsFeature object at 0xc6f2b6c>, <qgis._core.QgsFeature object at 0xc6f2a94>, 
<qgis._core.QgsFeature object at 0xc6f2a4c>] 
Source Link
xunilk
  • 30.2k
  • 4
  • 43
  • 82

If you type in the Python Console these commands:

help(processing.getObject)
Help on function getObject in module processing.tools.dataobjects:

getObject(uriorname)

help(QgsVectorLayer.select)
Help on built-in function select:

select(...)
    QgsVectorLayer.select(QgsRectangle, bool)
    QgsVectorLayer.select(int)
    QgsVectorLayer.select(unknown-type)

help(QgsVectorLayer.selectedFeatures)
Help on built-in function selectedFeatures:

selectedFeatures(...)
    QgsVectorLayer.selectedFeatures() -> list-of-QgsFeature

You can observe that if your 'uiorname' is correct you should first select the features with 'select'.

This code works well in my system:

import processing
from qgis.core import * 
vlayer = processing.getObject('/home/zeito/pyqgis_data/world_borders.shp')
            
list=[501, 967, 3171]
            
for item in list:
    vlayer.select(item)
        
selected_objects = vlayer.selectedFeatures()

print selected_objects

for this shapefile with selected features by index 501, 967 and 3171:

enter image description here

where printed references to selected features objects were:

execfile(u'/home/zeito/scriptspyqgis/selected.py'.encode('UTF-8'))
[<qgis._core.QgsFeature object at 0xc6f2b6c>, <qgis._core.QgsFeature object at 0xc6f2a94>, 
<qgis._core.QgsFeature object at 0xc6f2a4c>]