Re: [Qgis-developer] iteration over a layer's features in python plugins

2013-06-17 Thread Nathan Woodrow
You don't need to use a while loop.  I'm really not sure people keep using
that pattern when you don't need to and makes you code more complicated.
list and iterator have the same next() method so they act the same
https://github.com/qgis/Quantum-GIS/blob/master/python/core/qgsfeatureiterator.sip#L12

You can just do:

for f in layer.getFeatures():

So now if you want to just get the selected or all you can do:

if getselection:
   features = layer.selectedFeatures()
else:
  features = layer.getFeatures()

So I hate/gald to say that your FeatureIterator is not neeeded because it
is built into QgsFeatureIterator.

Regards,
Nathan


On Mon, Jun 17, 2013 at 11:47 PM, Ricardo Filipe Soares Garcia da <
ricardo.garcia.si...@gmail.com> wrote:

> Hi list
>
> When building plugins that operate on vector layers, it is a common
> pattern to include a checkbox in the plugin's GUI where the user can choose
> to perform the plugin analysis on the whole layer or just using the
> currently selected features (see for example most fTools dialogs).
>
> However, I think the QGIS API does not make the implementation of this
> pattern as easy as it could be. The QgsVectorLayer.getFeatures() method
> returns an iterator but the QgsVectorLayer.selectedFeatures() returns a
> list. It would be nice if they both returned an iterator.
>
> When working with all the features in a layer, I  use:
>
> feat = QgsFeature()
> feat_iterator = v_layer.getFeatures()
> while feat_iterator.nextFeature(feat):
> # do stuff with the feature
>
> When working with only the currently selected features the code is like:
>
> selected_feats = v_layer.selectedFeatures()
> for feat in selected_feats:
> # do stuff with the feature
>
> Since QgsVectorLayer.getFeatures can take a QgsFeatureRequest as argument,
> I think a nice solution could be to add one more filter to
> QgsFeatureRequest to allow iterating over a list of feature ids. It
> currently already allows to pass in a single feature id, it would just be a
> matter of expanding that.
>
> In the meantime I am using a custom iterator class[1] in Python that wraps
> this different behaviour and lets me use the same logic for processing the
> whole layer or just the selected features. I think it would be cleaner if
> this could be done on the API side.
>
> Maybe I am asking for too much too late because of the feature freeze for
> 2.0?
>
>
> [1] - (see the FeatureIterator class)
> https://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py
>
>
> --
> ___ ___ __
> Ricardo Garcia Silva
>
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>
>
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [Qgis-developer] iteration over a layer's features in python

2013-06-17 Thread Ricardo Filipe Soares Garcia da
Thanks for the tip Nathan!

I thought the while loop was necessary because we had to call
QgsFeatureIterator.nextFeature() and just now realized that the for loop
will do it implicitly. I thought that would work only for python
iterators... I guess QgsFeatureIterator is one of them ;)



On Mon, Jun 17, 2013 at 6:33 PM, Nathan Woodrow  wrote:

>  plugins
> MIME-Version: 1.0
> Content-Type: multipart/mixed;
> boundary="===3430773931903739203=="
>
> --===3430773931903739203==
> Content-Type: multipart/alternative; boundary=089e0102eaa65b15f904df59d6c1
>
> --089e0102eaa65b15f904df59d6c1
> Content-Type: text/plain; charset="utf-8"
> Content-Transfer-Encoding: 7bit
>
> You don't need go use a while, I don't know why people keep using that
> pattern when a simple for will do and is cleaner.
>
> For f in layer.getFeatures:
>
> No it just comes down to
>
> If selected:
> features = layers.selectedFeatures()[]
> Else:
> features = layer.getFeatures()
> From: Ricardo Filipe Soares Garcia da
> Sent: 17/06/2013 11:47 PM
> To: qgis-developer@lists.osgeo.org
> Subject: [Qgis-developer] iteration over a layer's features in python
> plugins
> Hi list
>
> When building plugins that operate on vector layers, it is a common pattern
> to include a checkbox in the plugin's GUI where the user can choose to
> perform the plugin analysis on the whole layer or just using the currently
> selected features (see for example most fTools dialogs).
>
> However, I think the QGIS API does not make the implementation of this
> pattern as easy as it could be. The QgsVectorLayer.getFeatures() method
> returns an iterator but the QgsVectorLayer.selectedFeatures() returns a
> list. It would be nice if they both returned an iterator.
>
> When working with all the features in a layer, I  use:
>
> feat = QgsFeature()
> feat_iterator = v_layer.getFeatures()
> while feat_iterator.nextFeature(feat):
> # do stuff with the feature
>
> When working with only the currently selected features the code is like:
>
> selected_feats = v_layer.selectedFeatures()
> for feat in selected_feats:
> # do stuff with the feature
>
> Since QgsVectorLayer.getFeatures can take a QgsFeatureRequest as argument,
> I think a nice solution could be to add one more filter to
> QgsFeatureRequest to allow iterating over a list of feature ids. It
> currently already allows to pass in a single feature id, it would just be a
> matter of expanding that.
>
> In the meantime I am using a custom iterator class[1] in Python that wraps
> this different behaviour and lets me use the same logic for processing the
> whole layer or just the selected features. I think it would be cleaner if
> this could be done on the API side.
>
> Maybe I am asking for too much too late because of the feature freeze for
> 2.0?
>
>
> [1] - (see the FeatureIterator class)
> https://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py
>
>
> --
> ___ ___ __
> Ricardo Garcia Silva
>
> --089e0102eaa65b15f904df59d6c1
> Content-Type: text/html; charset="utf-8"
> Content-Transfer-Encoding: quoted-printable
>
>  http-equiv=3D"Cont=
> ent-Type"> =
> font-size: 11pt;">You don't need go use a while, I don't know why people
> ke=
> ep using that pattern when a simple for will do and is cleaner.For
> =
> f in layer.getFeatures:No it just comes down toIf
> selected:=
>     features =3D
> layers.selectedFeatures()[]Else: >    features =3D
> layer.getFeatures() an style=3D"font-family: Tahoma,sans-serif; font-size: 10pt; font-weight:
> b=
> old;">From:  font-size=
> : 10pt;">Ricardo Filipe Soares Garcia da style=3D"font-fami=
> ly: Tahoma,sans-serif; font-size: 10pt; font-weight: bold;">Sent:
>  pan style=3D"font-family: Tahoma,sans-serif; font-size: 10pt;">17/06/2013
> 1=
> 1:47 PM font-size:=
>  10pt; font-weight: bold;">To:  Tahoma,sa=
> ns-serif; font-size: 10pt;">qgis-developer@lists.osgeo.org =
> style=3D"font-family: Tahoma,sans-serif; font-size: 10pt; font-weight:
> bold=
> ;">Subject:  font-size=
> : 10pt;">[Qgis-developer] iteration over a layer's features in python
> plugi=
> nsHi
> list=
> When building plugins that operate on vector layers, it is a common
> pattern=
>  to include a checkbox in the plugin's GUI where the user can choose
> to=
>  perform the plugin analysis on the whole layer or just using the
> currently=
>  selected features (see for example most fTools dialogs).
>
> However, I think the QGIS API does not ma

Re: [Qgis-developer] iteration over a layer's features in python

2013-06-17 Thread Nathan Woodrow
 plugins
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===3430773931903739203=="

--===3430773931903739203==
Content-Type: multipart/alternative; boundary=089e0102eaa65b15f904df59d6c1

--089e0102eaa65b15f904df59d6c1
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

You don't need go use a while, I don't know why people keep using that
pattern when a simple for will do and is cleaner.

For f in layer.getFeatures:

No it just comes down to

If selected:
features = layers.selectedFeatures()[]
Else:
features = layer.getFeatures()
From: Ricardo Filipe Soares Garcia da
Sent: 17/06/2013 11:47 PM
To: qgis-developer@lists.osgeo.org
Subject: [Qgis-developer] iteration over a layer's features in python
plugins
Hi list

When building plugins that operate on vector layers, it is a common pattern
to include a checkbox in the plugin's GUI where the user can choose to
perform the plugin analysis on the whole layer or just using the currently
selected features (see for example most fTools dialogs).

However, I think the QGIS API does not make the implementation of this
pattern as easy as it could be. The QgsVectorLayer.getFeatures() method
returns an iterator but the QgsVectorLayer.selectedFeatures() returns a
list. It would be nice if they both returned an iterator.

When working with all the features in a layer, I  use:

feat = QgsFeature()
feat_iterator = v_layer.getFeatures()
while feat_iterator.nextFeature(feat):
# do stuff with the feature

When working with only the currently selected features the code is like:

selected_feats = v_layer.selectedFeatures()
for feat in selected_feats:
# do stuff with the feature

Since QgsVectorLayer.getFeatures can take a QgsFeatureRequest as argument,
I think a nice solution could be to add one more filter to
QgsFeatureRequest to allow iterating over a list of feature ids. It
currently already allows to pass in a single feature id, it would just be a
matter of expanding that.

In the meantime I am using a custom iterator class[1] in Python that wraps
this different behaviour and lets me use the same logic for processing the
whole layer or just the selected features. I think it would be cleaner if
this could be done on the API side.

Maybe I am asking for too much too late because of the feature freeze for
2.0?


[1] - (see the FeatureIterator class)
https://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py


-- 
___ ___ __
Ricardo Garcia Silva

--089e0102eaa65b15f904df59d6c1
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

You don't need go use a while, I don't know why people ke=
ep using that pattern when a simple for will do and is cleaner.For =
f in layer.getFeatures:No it just comes down toIf selected:=
    features =3D layers.selectedFeatures()[]Else:    features =3D layer.getFeatures()From: Ricardo Filipe Soares Garcia daSent: 17/06/2013 1=
1:47 PMTo: qgis-developer@lists.osgeo.orgSubject: [Qgis-developer] iteration over a layer's features in python plugi=
nsHi list=
When building plugins that operate on vector layers, it is a common pattern=
 to include a checkbox in the plugin's GUI where the user can choose to=
 perform the plugin analysis on the whole layer or just using the currently=
 selected features (see for example most fTools dialogs).

However, I think the QGIS API does not make the impleme=
ntation of this pattern as easy as it could be. The QgsVectorLayer.getFeatu=
res() method returns an iterator but the QgsVectorLayer.selectedFeatures() =
returns a list. It would be nice if they both returned an iterator.=C2=A0

When working with all the features in a layer, I =C2=A0=
use:feat =3D QgsFeature()feat_it=
erator =3D v_layer.getFeatures()while feat_iterator.nextFe=
ature(feat):

=C2=A0 =C2=A0 # do stuff with the featureWhen working with only the currently selected features the code is=
 like:selected_feats =3D v_layer.sele=
ctedFeatures()

for feat in selected_feats:=C2=A0 =C2=A0 # do s=
tuff with the featureSince QgsVectorLa=
yer.getFeatures can take a QgsFeatureRequest as argument, I think a nice so=
lution could be to add one more filter to QgsFeatureRequest to allow iterat=
ing over a list of feature ids. It currently already allows to pass in a si=
ngle feature id, it would just be a matter of expanding that.

In the meantime I am using a custom iterato=
r class[1] in Python that wraps this different behaviour and lets me use th=
e same logic for processing the whole layer or just the selected features. =
I think it would be cleaner if this could be done on the API side.

Maybe I am asking for too much too late bec=
ause of the feature freeze for 2.0?[1] - (see the FeatureIterator class) https=
://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py<=
/div>

-- __=
_ ___ __Ricardo Garcia Silva


--08

Re: [Qgis-developer] iteration over a layer's features in python plugins

2013-06-17 Thread Ricardo Filipe Soares Garcia da
> I've got that functionality ready in a separate branch but it will have
> to wait for 2.1 due to the feature freeze as you mentioned
>
>
Great! Will be looking forward to it then :)


-- 
___ ___ __
Ricardo Garcia Silva
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [Qgis-developer] iteration over a layer's features in python plugins

2013-06-17 Thread Matthias Kuhn
Hi Ricardo,

On Mon 17 Jun 2013 15:47:08 CEST, Ricardo Filipe Soares Garcia da wrote:
> Hi list
>
> When building plugins that operate on vector layers, it is a common
> pattern to include a checkbox in the plugin's GUI where the user can
> choose to perform the plugin analysis on the whole layer or just using
> the currently selected features (see for example most fTools dialogs).
>
> However, I think the QGIS API does not make the implementation of this
> pattern as easy as it could be. The QgsVectorLayer.getFeatures()
> method returns an iterator but the QgsVectorLayer.selectedFeatures()
> returns a list. It would be nice if they both returned an iterator.
>
> When working with all the features in a layer, I  use:
>
> feat = QgsFeature()
> feat_iterator = v_layer.getFeatures()
> while feat_iterator.nextFeature(feat):
> # do stuff with the feature
>
> When working with only the currently selected features the code is like:
>
> selected_feats = v_layer.selectedFeatures()
> for feat in selected_feats:
> # do stuff with the feature
>
> Since QgsVectorLayer.getFeatures can take a QgsFeatureRequest as
> argument, I think a nice solution could be to add one more filter to
> QgsFeatureRequest to allow iterating over a list of feature ids. It
> currently already allows to pass in a single feature id, it would just
> be a matter of expanding that.

I've got that functionality ready in a separate branch but it will have 
to wait for 2.1 due to the feature freeze as you mentioned

>
> In the meantime I am using a custom iterator class[1] in Python that
> wraps this different behaviour and lets me use the same logic for
> processing the whole layer or just the selected features. I think it
> would be cleaner if this could be done on the API side.
>
> Maybe I am asking for too much too late because of the feature freeze
> for 2.0?
>
>
> [1] - (see the FeatureIterator class)
> https://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py
>
>
> --
> ___ ___ __
> Ricardo Garcia Silva
>
>
> ___
> Qgis-developer mailing list
> Qgis-developer@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer

Best
Matthias
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


[Qgis-developer] iteration over a layer's features in python plugins

2013-06-17 Thread Ricardo Filipe Soares Garcia da
Hi list

When building plugins that operate on vector layers, it is a common pattern
to include a checkbox in the plugin's GUI where the user can choose to
perform the plugin analysis on the whole layer or just using the currently
selected features (see for example most fTools dialogs).

However, I think the QGIS API does not make the implementation of this
pattern as easy as it could be. The QgsVectorLayer.getFeatures() method
returns an iterator but the QgsVectorLayer.selectedFeatures() returns a
list. It would be nice if they both returned an iterator.

When working with all the features in a layer, I  use:

feat = QgsFeature()
feat_iterator = v_layer.getFeatures()
while feat_iterator.nextFeature(feat):
# do stuff with the feature

When working with only the currently selected features the code is like:

selected_feats = v_layer.selectedFeatures()
for feat in selected_feats:
# do stuff with the feature

Since QgsVectorLayer.getFeatures can take a QgsFeatureRequest as argument,
I think a nice solution could be to add one more filter to
QgsFeatureRequest to allow iterating over a list of feature ids. It
currently already allows to pass in a single feature id, it would just be a
matter of expanding that.

In the meantime I am using a custom iterator class[1] in Python that wraps
this different behaviour and lets me use the same logic for processing the
whole layer or just the selected features. I think it would be cleaner if
this could be done on the API side.

Maybe I am asking for too much too late because of the feature freeze for
2.0?


[1] - (see the FeatureIterator class)
https://github.com/ricardogsilva/coneforinputs/blob/master/coneforinputs.py


-- 
___ ___ __
Ricardo Garcia Silva
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer