Last active
February 28, 2022 06:56
-
-
Save mfbenitezp/578dcc52f95c0b840ac173fcf0b161a8 to your computer and use it in GitHub Desktop.
GoogleMaps Places JSON to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# In[46]: | |
import csv | |
import json | |
import requests | |
# In[55]: | |
# Get the data. | |
#NearbyBySearch, radius of 50km. around a particular location in Nakapiripirit, Uganda | |
# places = requests.get(f'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=1.6908759,34.6405647&radius=50000&type=hospital+in+Nakapiripirit&key=YourGoogleAPIKey') | |
#Text Search requests (Hospital in Nakapiripirit, Uganda) | |
places = requests.get(f'https://maps.googleapis.com/maps/api/place/textsearch/json?query=hospital+in+Nakapiripirit&key=YourGoogleAPIKey') | |
# In[56]: | |
# Convert the response to a JSON object. | |
places = json.loads(places.text)['results'] | |
places | |
# In[61]: | |
filename = 'UG_Nakapiripirit_hospitals' | |
# In[64]: | |
#Creating the CSV file to later import it into ArcGIS o QGIS and create a Spatial layer. | |
columns = ['name', 'Lat','Long', 'types', 'icon'] | |
with open(f'C:/Users/benit/Desktop/{filename}.csv', 'w') as out_file: | |
writer = csv.writer(out_file, delimiter=',') | |
writer.writerow(columns) | |
for place in places: | |
name = place['name'] | |
icon = place['icon'] | |
types = place['types'] | |
lat, lng = place['geometry']['location']['lat'], place['geometry']['location']['lng'] | |
data = [name, lat, lng, icon,types] | |
print(f'{filename} -> {data}') | |
writer.writerow(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment