Lets say I have created a custom QGIS function, like for example
from qgis.core import *
from qgis.gui import *
import urllib.request
import urllib
import json
@qgsfunction(args='auto', group='Custom')
def jsonfromurl(url, feature, parent):
header = {"accept":"application/json"}
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
response_data = response.read()
encoding = response.info().get_content_charset('utf-8')
data = json.loads(response_data.decode(encoding))
#txt = json.dumps(data) #uncomment this line to receive data as string. also change next line to: return txt
return data
This function only works when evaluated against a feature. Otherwise it returns the preview No feature was found on this layer to evaluate the expression.
and just nothing when applying it.
How can I change that function, so it also works if no feature is given. For example (used as expression) in a print layout text box or an empty layer?
Like here: Adding a textbox to a QGIS layout where I want to display the result from the request https://nominatim.openstreetmap.org/reverse?format=json&lat=51.6895477895927&lon=-0.43032246110111033&zoom=18&addressdetails=1
.
Simply removing feature
and/or parent
seems not to work and as stated on https://howtoinqgis.wordpress.com/2017/05/20/how-to-create-custom-functions-in-qgis-using-the-function-editor/ these arguments are always needed.
qgsfunction
always takes a feature - how are you using this, may be it should be a plugin not an expression