Extending Power BI with Python and R by Luca Zavarella

Extending Power BI with Python and R by Luca Zavarella

Author:Luca Zavarella [Zavarella, Luca]
Language: eng
Format: epub
Tags: Power BI, Python, R, business intelligence, data visualization, dashboards, interactive visuals
Publisher: Packt Publishing
Published: 2021-11-26T00:00:00+00:00


From Figure 9.4, you can see that the resources node can contain more than one subnode, identified with an integer. This is because sometimes the geocoding engine cannot exactly identify the geographic point from the passed address and therefore returns more than one result. The estimatedTotal attribute indicates the number of possible geocodes identified. For simplicity, we have extracted from the JSON the last identified resource, which is the one with the highest identifier.

Besides the answer in JSON format, from the request, you can also extract other values, such as the reason for the state of the answer (useful to understand whether the GET operation is successful or not), the complete content of the answer in text format, and the URL used for the GET request. You will need the requests, urllib, json, pandas, and time modules. The latter is used to measure the geocoding execution time of all addresses in the dataset. Everything can be encapsulated in a handy function called bing_geocode_via_address():

def bing_geocode_via_address(address): # trim the string from leading and trailing spaces using strip full_url = f"{base_url}query={urllib.parse.quote(address.strip(), safe='')}?key={AUTH_KEY}" r = requests.get(full_url) try: data = r.json() # number of resources found, used as index to get # the latest resource num_resources = data['resourceSets'][0]['estimatedTotal'] formattedAddress = data['resourceSets'][0]['resources'][num_resources-1]['address']['formattedAddress'] lat = data['resourceSets'][0]['resources'][num_resources-1]['point']['coordinates'][0] lng = data['resourceSets'][0]['resources'][num_resources-1]['point']['coordinates'][1] except: num_resources = 0 formattedAddress = None lat = None lng = None text = r.text status = r.reason url = r.url return num_resources, formattedAddress, lat, lng, text, status, url



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.