Tutorial to Georeference Google Searches with Python and QGIS

GoogleQGIS.PNG

To carry out a series of studies and investigations we have to know both name and location of certain places. This may be available through some web pages, however the use of data is limited and advance geoprocessing tools on softwares as QGIS can not be used. There are several services or APIs that can bring you the spatial location of Google searches. In this tutorial we will use the Google Maps API for the location of Hospitals in a radius of 5 kilometers from a point in Miraflores, Lima. Only open source software tools have been used for this tutorial.

This tutorial is done by the Python 3 interpreter named Jupyter that can be downloaded from this link: https://www.continuum.io/downloads

QGIS is our well-known and promoted Geographic Information Systems software that can be downloaded from: www.qgis.org

To get a Google API please go to the link: 

https://developers.google.com/maps/documentation/embed/get-api-key

 

Tutorial

 

Python Code

import urllib
import urllib.request
import json

googleGeocodeUrl = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query='
termino = "hospitales"
ubicacion = "&location=-12.135,-77.023&radius=5000"
APIKEY = '&key='+'aqui-va-tu-API-key'

url = googleGeocodeUrl + termino + ubicacion + APIKEY
print(url)

url = googleGeocodeUrl + termino + ubicacion + APIKEY
json_response = urllib.request.urlopen(url)
busqueda = json_response.read().decode('utf-8')
busquedajson = json.loads(busqueda)

archivolugares = open('ubicacionhospitales.csv','w')
for lugar in busquedajson['results']:
print(lugar['name'])
print(lugar['geometry']['location'])
archivolugares.write(lugar['name']+','+str(lugar['geometry']['location']['lng'])\
 +','+str(lugar['geometry']['location']['lat'])+'\n')
archivolugares.close()

Saul Montoya

Saul Montoya es Ingeniero Civil graduado de la Pontificia Universidad Católica del Perú en Lima con estudios de postgrado en Manejo e Ingeniería de Recursos Hídricos (Programa WAREM) de la Universidad de Stuttgart con mención en Ingeniería de Aguas Subterráneas y Hidroinformática.

 

Suscribe to our online newsletter

Subscribe for free newsletter, receive news, interesting facts and dates of our courses in water resources.