#!/usr/bin/env python # coding: utf-8 # ## Visualization of Airport Data on Map. # # The idea and the airport's data for this demo was found in the "Towards Data Science" article [How to Visualize Data on top of a Map in Python using the Geoviews library](https://towardsdatascience.com/how-to-visualize-data-on-top-of-a-map-in-python-using-the-geoviews-library-c4f444ca2929) by [Christos Zeglis](https://medium.com/@christoszeglis). # # The paper demonstrates how to make a plot to visualize the passengers volume for the busiest airports in Greece, and the neighbor country, Turkey, for comparison reasons. # # The author uses Geoviews and Bokeh libraries to achieve such visualisation. We are going to use the [Lets-Plot](https://github.com/JetBrains/lets-plot/blob/master/README.md) library solely to do the same job and, on top of it, fill both countries boundaries with semi-transparent colors. # # The tasks completed in this tutorial: # # - Configuring map-tiles for the interactive base-map layer. # - Obtaining boundaries of Greece and Turkey using Lets-Plot geo-coding module. # - Creating a proportional symbols map by combining base-map, polygons and point layers. # - Castomizing the tooltip contents. # In[1]: import numpy as np import pandas as pd from lets_plot import * # In[2]: LetsPlot.setup_html() # ### Configuring the basemap. # # For the purposes of this tutorial, we are going to use "CityLights 2012" map-tiles [© NASA Global Imagery Browse Services (GIBS)](https://earthdata.nasa.gov/eosdis/science-system-description/eosdis-components/gibs). # In[3]: LetsPlot.set(maptiles_zxy( url='https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/VIIRS_CityLights_2012/default//GoogleMapsCompatible_Level8/{z}/{y}/{x}.jpg', attribution='© NASA Global Imagery Browse Services (GIBS)', max_zoom=8 )) # ### Loading the "airports" dataset. # # The "airports" dataset is already cleaned and only contains data on Greece and Turkey airports. # In[4]: airports = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/airports.csv") airports.head(3) # ### Obtaining boundaries (or polygons) of Greece and Turkey. # # On this step we are using built-in geo-coding capabilities of the Lets-Plot library. # In[5]: from lets_plot.geo_data import * countries_gcoder = geocode_countries(['GR', 'TR']) countries_gcoder.get_geocodes() # ### Showing the data on map. # # - Add an interactive base-map layer with custom initial location and zoom level. # - Add polygons layer to fill the country boundaries with semi-transparent colors. # - Add points layer marking the airports location with the point size proportional to the airport's passengers volume. # - Customize tooltip on the points layer to show all the airport data. # - Use the 'scale_fill_manual()' function in order to fill polygons and points with blue and red colors. # - Setup the desired marker's size using the 'scale_size()' function. # In[6]: (ggplot() + geom_livemap(location=[26.65, 38.61], zoom=6) + geom_polygon(aes(fill='country'), data=countries_gcoder.inc_res().get_boundaries(), size=0, alpha=.2) + geom_point(aes('longitude', 'latitude', fill='country', size='passengers'), data=airports, shape=21, alpha=.7, color='white', tooltips=layer_tooltips() .format('passengers', '{.1f} m' ) .format('^x', '.2f').format('^y', '.2f') .line('@|@IATA') .line('Passengers|@passengers') .line('City|@city') .line('Country|@country') .line('Longitude|^x') .line('Latitude|^y')) + scale_fill_manual(values=['#30a2da', '#fc4f30']) + scale_size(range=[10, 40], trans='sqrt') + theme(legend_position='none') + ggsize(900, 520) )