QGIS2.8 How are the features ordered when using layer.selectedFeatures()

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

QGIS2.8 How are the features ordered when using layer.selectedFeatures()

Zhang Qun
Hi,
Does anyone know how the features are ordered when you extract by selectedFeatures()? Is it in the same order as the input delimited text file? I'm asking because sometimes I found the looping does not give me the same order as in my data source.

selected_features = layer.selectedFeatures()
for f in selected_features:

Thanks.
Ethan

_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QGIS2.8 How are the features ordered when using layer.selectedFeatures()

Nyall Dawson
On Tue, 4 Jun 2019 at 13:38, Zhang Qun <[hidden email]> wrote:
>
> Hi,
> Does anyone know how the features are ordered when you extract by selectedFeatures()? Is it in the same order as the input delimited text file? I'm asking because sometimes I found the looping does not give me the same order as in my data source.

It's an unordered set - i.e. effectively random.

Nyall
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QGIS2.8 How are the features ordered when using layer.selectedFeatures()

Zhang Qun
Thanks Nyall. Then is there anyway i can get the original order? My data is road lane points and the sequence order represent the driving direction.

On Tue, Jun 4, 2019, 11:58 AM Nyall Dawson <[hidden email]> wrote:
On Tue, 4 Jun 2019 at 13:38, Zhang Qun <[hidden email]> wrote:
>
> Hi,
> Does anyone know how the features are ordered when you extract by selectedFeatures()? Is it in the same order as the input delimited text file? I'm asking because sometimes I found the looping does not give me the same order as in my data source.

It's an unordered set - i.e. effectively random.

Nyall

_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
Reply | Threaded
Open this post in threaded view
|

Re: QGIS2.8 How are the features ordered when using layer.selectedFeatures()

Raymond Nijssen
Hi,

I found two ways of doing that, I think the second is faster but not
sure. (And I think they will both be slower than the random order code
you were using before, but the order will remain.)

Good luck,
Raymond


---

layer = iface.activeLayer()

ids = layer.selectedFeatureIds()
print(ids)

# first option, loop all features and test if the id is in
# the selected ids:

for feat in layer.getFeatures():
     if feat.id() in ids:
         print(feat)


# second option, loop selected ids and get their feature:

ids.sort()
print(ids)

for id in ids:
     feat = layer.getFeature(id)
     print(feat)





On 04-06-19 06:01, Zhang Qun wrote:

> Thanks Nyall. Then is there anyway i can get the original order? My data
> is road lane points and the sequence order represent the driving direction.
>
> On Tue, Jun 4, 2019, 11:58 AM Nyall Dawson <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     On Tue, 4 Jun 2019 at 13:38, Zhang Qun <[hidden email]
>     <mailto:[hidden email]>> wrote:
>      >
>      > Hi,
>      > Does anyone know how the features are ordered when you extract by
>     selectedFeatures()? Is it in the same order as the input delimited
>     text file? I'm asking because sometimes I found the looping does not
>     give me the same order as in my data source.
>
>     It's an unordered set - i.e. effectively random.
>
>     Nyall
>
>
> _______________________________________________
> Qgis-user mailing list
> [hidden email]
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
_______________________________________________
Qgis-user mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user