I'm completely new to PYQGIS. I'm pretty handy with MapBasic in MapInfo and was wondering how I would 'translate' this command from SQL select into Python (hope that makes sense)

Essentially, I have a layer with multiple objects - but I only want to select and extract the objects which have an id like %ABC% into a separate layer.

In MapBasic I would:

Select * from *examplelayer* where id Like "%ABC%" into *ABC*

where 'id' is a field I want to search in the table/layer 'examplelayer', id Like "%ABC%" meaning that any id's containing ABC will be selected into a new table/map layer named "ABC"

  • Welcome to GIS SE! As a new user be sure to take the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely.
    – PolyGeo
    Apr 5 '18 at 23:54
  • Can you show more of your python code so we can see where you're up to. Do you have a list of IDs? If so layer.setSelectedFeatures([ID,ID,ID...]) then export the selected, this post gis.stackexchange.com/questions/153309/… might help point you in the right direction. Apr 6 '18 at 0:31
  • 1
    As referred to Creating new Virtual Layer programmatically in QGIS?, this might help you: ABC = QgsVectorLayer( "?query=SELECT * FROM examplelayer WHERE id LIKE '%ABC%'", "ABC", "virtual" ) QgsMapLayerRegistry.instance().addMapLayer(ABC) Apr 6 '18 at 0:59

Browse other questions tagged or ask your own question.