Quantcast
Channel: Question and Answer » gdal
Viewing all articles
Browse latest Browse all 397

How to sort features in a layer using OGR/Python?

$
0
0

How can I sort the features of an ogr layer by the values of a field?
I tried naive variations of

def sortlayer(ds, layer, field):
    lname = layer.GetName()
    lsort = ds.ExecuteSQL(b'select * from "{}" order by {}'.format(lname, field))
    ds.DeleteLayer(lname)
    ds.CopyLayer(lsort, lname)

but this either crashed or freezed.

Bonus points for a solution that doesn’t change the order of layers in the datasource and/or that doesn’t rely on unique layer names.

Actually the datasources are in-memory (memory-driver), and I may have up to 10 layers with up to 10.000 features over all.

Maybe it would be better not to use SQL but create an index as list, and then swap features around in the layer with Layer.GetFeature()/.SetFeature()?

Thanks, Redoute


Viewing all articles
Browse latest Browse all 397

Trending Articles