Geographic Information Systems Asked on June 7, 2021
I have a have a static URL pointing to a json
file on the web that I load to QGIS. It is updated regularly. In the source, before listing the features, it contains metadata, between else a line "creation_time": "[timestamp]"
.
I want to add the date/time as text to the print layout: how do I get the information? I tried to get it using expressions with layer_property( '[layer_name]', 'attribution')
, but the output is empty.
The first few lines of the .json file look like this:
{
"crs": {
"type":"name",
"properties":{
"name":"[EPSG:code]"}
},
"license": "[license info]",
"mapname": "[name of the map]",
"map_long_name": "[despription of the map]",
"map_short_name": "[short name]",
"map_abstract": "[abstract of the map]",
"creation_time": "[timestamp]",
"type":"FeatureCollection",
"features":[
You could use json_to_map()
to turn your json into a dictionary and then get the value of 'creation_time'
by using map_get()
to search for that key. Here an example, (I made a full json out of your example, copying a random one from the web):
map_get(json_to_map('
{
"crs":{
"type":"name",
"properties":{
"name":"[EPSG:code]"
}
},
"license":"[license info]",
"mapname":"[name of the map]",
"map_long_name":"[despription of the map]",
"map_short_name":"[short name]",
"map_abstract":"[abstract of the map]",
"creation_time":"[timestamp]",
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"properties":{
"name":"codecentric AG",
"address":"Hochstr. 11",
"marker-color":"#008800",
"marker-symbol":"commercial"
},
"geometry":{
"type":"Point",
"coordinates":[
7.0069,
51.1623
]
}
}
]
}
'),'creation_time')
This example will return '[timestamp]'
. Depending on how you access this data, you need to hand it over as string to this function.
I could not find a possibility to load data from external urls via expressions. So I created this custom function:
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
It basically accesses the json content from a given url. My test url is http://api.plos.org/search?q=title:DNA
. In case your url does not return a json by default, you may need adjust the second line to request = urllib.request.Request(url,header)
. In case you want to receive your json as string, uncomment the second last line and change the last one to return txt
.
Example usage: map_get(jsonfromurl('http://api.plos.org/search?q=title:DNA'),'response')
, so in your case maybe map_get(jsonfromurl('yoururls'),'creation_time')
.
Correct answer by MrXsquared on June 7, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP