-1

I am writing a script that converts dxf drawing to a shapefile. Using my script now I can create a shapefile. Attribute table of that shapefile is initially empty and it has only one row. In that attribute table, there is a column called "Req_No". I make Req_No column value in to 23 and try the following code and it works.

selection=vl.getFeatures(QgsFeatureRequest().setFilterExpression(' "Req_No"=23'))
for feat in selection:
  vl.changeAttributeValue(feat.id(),1, '45')
  vl.changeAttributeValue(feat.id(),2, 'H.M.P.jagath')
  vl.commitChanges()

Now I want to do this automatically. If I further explain, I want attribute table to update while the shapefile is forming. But initially attribute table columns are all empty.

How should I change the above mentioned code when Req_No column is Null?

I am using QGIS version 2.18.9.

  • 2
    Please do not write your titles in ALL CAPS. – PolyGeo Jun 18 '18 at 10:21
  • 1
    Hi, it's hard to tell what exactly you want to do. Do you want to update an attribute value for all features where Req_No is 23? Or for all features which are there, regardless the Req_No attribute? I would recommend reading the pyqgis developer cookbook – Matthias Kuhn Jun 19 '18 at 14:58
0

The keyword for a value you refer to as 'empty' is NULL. Therefore the below should work to select all features where the Req_No field is empty

vl.getFeatures(QgsFeatureRequest().setFilterExpression(' "Req_No" IS NULL'))

  • This answer was flagged as very low quality, probably because it is a one-liner. I suggest formatting code as code (use the {} button) and perhaps provide a brief explanation about how this code works. Tks. – Andre Silva Jun 19 '18 at 16:32
  • Glad I could help. If I answered your question satisfactorily, please give it a tick to mark your question as answered – firefly-orange Jun 21 '18 at 14:41
  • Actually i am new to this site.I was not aware of that before.I have given a tick to the answer.I am not allow to put a tick to the question.Therefore I am not sure Whether, this is what you expected.Thank you. – Shanaka Herath Jun 22 '18 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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