8.7 Google Maps Data

In this text, the Google Geocoding API is accessed through an HTTP interface using a Python script.

Geocoding is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude). Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

To use the Geocoding API, you must register your app project on the Google API Console and get a Google API key which you can add to your app or website. So, the first thing to do is navigate to the page Get API Key and follow the instructions given on this page.

The following Python script requests a response for the address ‘Igreja de Nossa Senhora da Candelária – Praça Pio X’, shows the results and extracts the coordinates from the results. The API Key is read from GoogleMapsKey.txt file.

# Google Maps Platform - Web Services - Geocoding API
# https://developers.google.com/maps/documentation/geocoding/start?hl=en
#
# This Python script shows how to geolocalize the address
# Igreja de Nossa Senhora da Candelária - Praça Pio X - Centro,
# Rio de Janeiro - RJ (Church of Our Lady of Candelaria)
# accessing Google Maps Geocoding API through an HTTP interface.
#
# -*- coding: utf-8 -*-

import http.client, urllib.parse
import json

# Read the Google Maps Key from 'GoogleMapsKey.txt' file
GoogleMapsKey = open('GoogleMapsKey.txt', 'r').read().rstrip("\n")

host = 'maps.googleapis.com'
path = '/maps/api/geocode/json'
address = 'Igreja de Nossa Senhora da Candelária - Praça Pio X'

params = '?address=' + urllib.parse.quote (address) + \
         '&key=' + GoogleMapsKey

def get_response():
    conn = http.client.HTTPSConnection(host)
    conn.request("GET", path + params)
    response = conn.getresponse()
    return response.read()

results = json.loads(get_response())
print(json.dumps(results, indent=4))
loc = "Location: " + \
      json.dumps(results['results'][0]['geometry']['location'])
print(loc)

Below is the output produced by this script

$ python google_maps_candelaria.py
{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "s/n",
                    "short_name": "s/n",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Pra\u00e7a Pio X",
                    "short_name": "Pra\u00e7a Pio X",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Centro",
                    "short_name": "Centro",
                    "types": [
                        "political",
                        "sublocality",
                        "sublocality_level_1"
                    ]
                },
                {
                    "long_name": "Rio de Janeiro",
                    "short_name": "Rio de Janeiro",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "Rio de Janeiro",
                    "short_name": "RJ",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "Brazil",
                    "short_name": "BR",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "20040-020",
                    "short_name": "20040-020",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "Pra\u00e7a Pio X, s/n - Centro, Rio de Janeiro - RJ, 20040-020, Brazil",
            "geometry": {
                "location": {
                    "lat": -22.9008826,
                    "lng": -43.1778419
                },
                "location_type": "ROOFTOP",
                "viewport": {
                    "northeast": {
                        "lat": -22.8995336197085,
                        "lng": -43.1764929197085
                    },
                    "southwest": {
                        "lat": -22.9022315802915,
                        "lng": -43.1791908802915
                    }
                }
            },
            "place_id": "ChIJIe1CNFl_mQARG8VNRU8zMXY",
            "types": [
                "church",
                "establishment",
                "place_of_worship",
                "point_of_interest"
            ]
        }
    ],
    "status": "OK"
}
Location: {"lat": -22.9008826, "lng": -43.1778419}
$

Sources

The featured image shows the debugging of the script in Visual Studio Code IDE on Windows 10.

Map

To see the API response on the map was created the google_maps_candelaria.py script in Python that generates the GPX (GPS eXchange Format) file google_maps_candelaria.gpx.

8. External data