3

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"

closed as off-topic by PolyGeo Apr 5 '18 at 23:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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. – Michael Stimson 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) – Kadir Şahbaz Apr 6 '18 at 0:59

Browse other questions tagged or ask your own question.