Hi all,
Let’s continue with another small and quick Python use case on data.
Here is a use case to plot gmaps heatmap with Python. The data is coming from a live REST service (New York city bike, because it is live, free and easy) and a simple JSON parsing is done to catch the location data.
I’m using Jupyter Notebook, which is THE Python platform for data manipulation, exploration, testing, science etc …
Here is the code, fairly simple. Of course you need a gmaps api key.
I decided to render a heatmap because it is eye candy but you can render anything you want (marks…). Next time, I will show you how to embed data into the map and add some real time features (read the previous article about real time plotting : http://open-bi.blogspot.ch/2018/04/simple-and-compact-python-bitcoin-ticker.html).
import gmaps
import requests
import gmaps.datasets
import numpy as np
gmaps.configure(api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") # Your Google API key
url = 'https://gbfs.citibikenyc.com/gbfs/en/station_information.json'
print('Processing url request ....')
r = requests.get(url)
r.json()
data = r.json()
coords = []
locations = []
for i in data['data']['stations']:
lat = float(i['lat'])
lon = float(i['lon'])
coords = [lat, lon]
locations.append(coords)
#hard coded coordinates for New York below, but you can do something better …
fig = gmaps.figure(center=(40.71448, -74.00598), zoom_level=12, layout={
'width': '700px',
'height': '800px',
'padding': '3px',
'border': '1px solid black'
})
fig.add_layer(gmaps.heatmap_layer(locations))
fig
No comments:
Post a Comment