Bug report #19764
Features in memory layers are not accessible by pyQGIS scripts
Status: | Closed | ||
---|---|---|---|
Priority: | Normal | ||
Assignee: | Nyall Dawson | ||
Category: | PyQGIS Console | ||
Affected QGIS version: | 3.2.2 | Regression?: | No |
Operating System: | Win 7 or Win 10 | Easy fix?: | No |
Pull Request or Patch supplied: | No | Resolution: | |
Crashes QGIS or corrupts data: | No | Copied to github as #: | 27589 |
Description
Features in memory layers are not accessible.
QgsFeature.attributes() is a emty list []
QgsFeature.geometry() is a emty geometry
If I save the memory layer in a shp or something else, it works very well.
History
#1 Updated by Giovanni Manghi about 6 years ago
- Priority changed from High to Normal
- Pull Request or Patch supplied changed from Yes to No
#2 Updated by Nyall Dawson about 6 years ago
- Status changed from Open to Feedback
More information is required here.
#3 Updated by Michael Kuerbs about 6 years ago
- Assignee set to Nyall Dawson
I create a memory layer with one feature included attributes.
Now I would access this Feature directly with
feat = layer.getFeature(0)
print(feat.attributes(), feat.geometry().asWkt()) => [] ""
if i work with real storage Layers the first feature is getting with layer.getFeature(0)
if it's a memory layer, the first feature is getting with layer.getFeature(1)
if i get features by iterating, it works very well
why is the index using different in case of menory layers?
#4 Updated by Nyall Dawson about 6 years ago
Feature IDs aren't consistent across providers - they are stable within a particular layer, but won't be transferred if that layer is saved to a different format or provider.
#5 Updated by Jürgen Fischer about 6 years ago
feat = layer.getFeature(0)
getFeature takes a feature id and not an index. And the feature id is provider/data source specific. You shouldn't make assumptions about it other than you can address a feature with the id you got from the layer - as it might not even be safe to assume that features from the same datasource, but on different layers have the same id).
#6 Updated by Michael Kuerbs about 6 years ago
I would like just the first feature a layer.
#7 Updated by Nyall Dawson about 6 years ago
- Status changed from Feedback to Closed
In that case:
feature = next(layer.getFeatures())
(or better)
feature = next(layer.getFeatures(QgsFeatureRequest().setLimit(1))