2

I want to get features based on their attributes and it works fine when I use this Python script:

canvas = iface.mapCanvas()
curLayer = canvas.currentLayer()
it = curLayer.getFeatures(QgsFeatureRequest().setFilterExpression('"ELAVSNAVN" = \'Fraugde By, Fraugde\' and "MATRNR" = \'15g\''))

for baseFeature in it:
    print baseFeature.id()

But nothing is returned when I use special danish letters as shown in this example:

it = curLayer.getFeatures(QgsFeatureRequest().setFilterExpression('"ELAVSNAVN" = \'Åsum By, Åsum\' and "MATRNR" = \'10a\''))

What can I do solve this problem ?

4

Make sure you are using a unicode string in Python for stuff like that:

expression = u'"ELAVSNAVN" = \'Åsum By, Åsum\' and "MATRNR" = \'10a\''
  • The result is still the same - nothing is returned. I've now made a workaround: I'm stepping through all the features of the layer using – EvanH Jun 19 '14 at 4:42
  • The result is still the same - nothing is returned. I've now made a workaround: I'm stepping through all the features of the layer using: it = curLayer.getFeatures(). For each feature I compare the specific attribute using: for baseFeature in it: if baseFeature["ELAVSNAVN"] == str(valueToCompare).decode(utf-8"): .... This works fine when running in a script but when I use this code in a plugin a unicodeDecodeError is thrown. Please tell me why.. – EvanH Jun 19 '14 at 4:57
  • @EvanH Please edit your question to incorporate the advice offered in this upvoted answer and the precise outcome. That way other potential answerers will be in a better position to see what you have tried and where you are stuck. Comments, that potential answerers may or may not read, are not the place for important information. See meta.gis.stackexchange.com/a/3353 – PolyGeo Jan 30 '15 at 23:38
0

I faced similar problems in my qgis2leaf plugin for QGIS. So I used "import re" and its methods. so I change every layer name like this:

for i in range(len(self.layer_list)): 
            self.layer_list[i] = re.sub('[\W_]+', '', self.layer_list[i].text())

maybe this helps...

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.