Re: [QGIS-Developer] Custom python expression question

2017-05-05 Thread Matthias Kuhn 
I hope you don't mind very verbose docs ;)

https://github.com/qgis/QGIS/blob/fd25288a6444c1fd6fb965a408550992d7d1f84b/python/user.py#L69-L94

On 05/05/2017 02:35 PM, Nathan Woodrow wrote:
> Yes please :) 
> 
> On Fri, May 5, 2017 at 10:29 PM, Matthias Kuhn   > wrote:
> 
> On 05/05/2017 02:22 PM, Nyall Dawson wrote:
> > On 5 May 2017 at 22:08, Matthias Kuhn   > wrote:
> >>
> >> I think we should also send this to the @qgsfunction in QGIS 3.
> >>
> >
> > Agreed. Shall I assign a ticket to you or will you file a PR for this?
> >
> > Nyall
> >
> 
> Is backwards compatibility a requirement?
> 
> Matthias
> ___
> QGIS-Developer mailing list
> QGIS-Developer@lists.osgeo.org 
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> 
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> 
> 
> 
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [QGIS-Developer] Custom python expression question

2017-05-05 Thread Matthias Kuhn 
On 05/05/2017 02:22 PM, Nyall Dawson wrote:
> On 5 May 2017 at 22:08, Matthias Kuhn   wrote:
>>
>> I think we should also send this to the @qgsfunction in QGIS 3.
>>
> 
> Agreed. Shall I assign a ticket to you or will you file a PR for this?
> 
> Nyall
> 

Is backwards compatibility a requirement?

Matthias
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [QGIS-Developer] Custom python expression question

2017-05-05 Thread Nyall Dawson
On 5 May 2017 at 22:08, Matthias Kuhn   wrote:
>
> I think we should also send this to the @qgsfunction in QGIS 3.
>

Agreed. Shall I assign a ticket to you or will you file a PR for this?

Nyall
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [QGIS-Developer] Custom python expression question

2017-05-05 Thread Matthias Kuhn 
Actually, there would also be the "context" which gives access to the
@layer variable (a QgsVectorLayer object) and other additional things.

It's just slightly more complex to set up because it's not proxied to
the @qgsfunction.

One will need to subclass QgsExpression.Function as done here:

https://github.com/qgis/QGIS/blob/master/python/core/__init__.py#L74-L102

And then register it manually.

I think we should also send this to the @qgsfunction in QGIS 3.

Matthias


On 05/05/2017 12:42 PM, Nathan Woodrow wrote:
> Hey Andreas,
> 
> Parent means the parent expression itself.  So in a function, you can
> know what the whole expression is e.g it points to the instance of. 
> 
> You can't really know what the layer because expressions are not always
> layer bound and QgsFeatures don't hold a reference to the layer they are
> from.  Best you can do at the moment I think is to pass the layer name
> or id into the custom expressions.
> 
> - Nathan
> 
> 
> ___
> QGIS-Developer mailing list
> QGIS-Developer@lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> 
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [QGIS-Developer] Custom python expression question

2017-05-05 Thread Nathan Woodrow
Hey Andreas,

Parent means the parent expression itself.  So in a function, you can know
what the whole expression is e.g it points to the instance of.

You can't really know what the layer because expressions are not always
layer bound and QgsFeatures don't hold a reference to the layer they are
from.  Best you can do at the moment I think is to pass the layer name or
id into the custom expressions.

- Nathan
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

Re: [QGIS-Developer] Custom python expression question

2017-05-05 Thread Marco Grisolia
Hi Andreas,
I don't know if it is the best way for doing it, but I generally add the
current feature and the current layer as input parameters of the custom
function:

def my_function(layer_name, curr_feat, feature, parent):

They are set as inputs in this way when calling the custom function:

my_function(@layer_name, $currentfeature)

Then, I can simply call the current layer and the current feature inside
the function in this way:

layer = QgsMapLayerRegistry.instance().mapLayersByName(layer_name)[0]

geom = curr_feat.geometry()

For a better understanding, you may also have a look to my recent answer on
this question on GIS Stack Exchange [0]:

[0]
https://gis.stackexchange.com/questions/235014/increasing-marker-size-based-on-number-of-points-at-same-location-in-qgis

I hope that helps.
Best regards,
Marco





2017-05-05 10:45 GMT+02:00 Neumann, Andreas :

> Hi,
>
> If one creates a custom python expression, one gets a reference to the
> "feature" and the "parent".
>
> Question: what is "parent" referring to? What object type is it?
>
> Is it somehow possible in the Python script to get a reference to the
> layer the feature is part of? Would there be some example Python code
> around that shows how one can get access to the layer the feature belongs
> to?
>
> Thank you for any hints,
>
> Andreas
>
>
> ___
> QGIS-Developer mailing list
> QGIS-Developer@lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer

[QGIS-Developer] Custom python expression question

2017-05-05 Thread Neumann, Andreas
Hi, 

If one creates a custom python expression, one gets a reference to the
"feature" and the "parent". 

Question: what is "parent" referring to? What object type is it? 

Is it somehow possible in the Python script to get a reference to the
layer the feature is part of? Would there be some example Python code
around that shows how one can get access to the layer the feature
belongs to? 

Thank you for any hints, 

Andreas___
QGIS-Developer mailing list
QGIS-Developer@lists.osgeo.org
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer