I created a graphic model which simply creates multiple vector attributes. All attributes are empty to be filled manually. Only one attribute column (let's call it KEYS
), needs a different process and I'm kind of stuck at that point. I was able to come up with a short Python script that requests random numbers from random.org API. Script works, and I'm able to print out random numbers as a list.
What I couldn't achieve is to get those numbers to be written to KEYS
column for every feature. I am aware of the graphical modeler tool called Advanced Python Field Calculator. What would be the way to achieve this?
I previously used
lyr = iface.activeLayer()
fts = lyr.getFeatures()
lyr.startEditing()
for f in fts:
f['KEYS']='API LIST SHOULD ITERATE HERE'
lyr.updateFeature(f)
lyr.commitChanges()
For something that did not need to iterate over features (it just writes whatever string in the 'API LIST SHOULD ITERATE HERE'
part to KEYS
column.
Finally I am pasting the full script I was able to come up with (doesn't work).
import requests
lyr = iface.activeLayer()
fts = lyr.getFeatures()
reqData = {
"jsonrpc":"2.0",
"method":"generateStrings",
"params":{"apiKey":"myapikeygoeshere",
"n":10,
"length":20,
"characters":"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"replacement":True
},
"id":539
}
output_file = open
response = requests.post('https://api.random.org/json-rpc/2/invoke', json = reqData)
response.raise_for_status()
json = response.json()
data = json['result']['random']['data']
print(data)
print(sorted(data))
lyr.startEditing()
for f in fts:
f['KEYS']='API LIST SHOULD ITERATE HERE'
lyr.updateFeature(f)
lyr.commitChanges()
How do I get that lists content to be written to features in a specific column?