Get Started with GeoViews

Introduction

GeoViews is a Python library that makes it easy to explore and visualize geographical

GeoViews is built on the HoloViews library for building flexible visualizations of multidimensional data

You can install GeoViews and its dependencies using conda:

conda install -c pyviz geoviews
In [1]:
import geoviews as gv
import geoviews.feature as gf
import xarray as xr
from cartopy import crs

gv.extension('bokeh', 'matplotlib')

We can visualize the global as following

In [2]:
gf.ocean
Out[2]:

and we can improve it as follows

In [3]:
gf.ocean.options(
    'Feature', projection=crs.Geostationary(), global_extent=True, height=525,width=525,
)
Out[3]:

We can show the lands only too

In [4]:
gf.land.options(
    'Feature', projection=crs.Geostationary(), global_extent=True, height=525,width=525,
)
Out[4]:

with the borders

In [5]:
(gf.ocean * gf.land * gf.coastline * gf.borders).options(
    'Feature', projection=crs.Geostationary(), global_extent=True, height=525,width=525,
)
Out[5]:

World Population Map

Map 1

We will consider first some datasets already uploaded in geoviews Library

In [6]:
import geopandas as gpd
In [7]:
d1=gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
In [8]:
d1.head(n=10)
Out[8]:
pop_est continent name iso_a3 gdp_md_est geometry
0 28400000.0 Asia Afghanistan AFG 22270.0 POLYGON ((61.21081709172574 35.65007233330923,...
1 12799293.0 Africa Angola AGO 110300.0 (POLYGON ((16.32652835456705 -5.87747039146621...
2 3639453.0 Europe Albania ALB 21810.0 POLYGON ((20.59024743010491 41.85540416113361,...
3 4798491.0 Asia United Arab Emirates ARE 184300.0 POLYGON ((51.57951867046327 24.24549713795111,...
4 40913584.0 South America Argentina ARG 573900.0 (POLYGON ((-65.50000000000003 -55.199999999999...
5 2967004.0 Asia Armenia ARM 18770.0 POLYGON ((43.58274580259273 41.09214325618257,...
6 3802.0 Antarctica Antarctica ATA 760.4 (POLYGON ((-59.57209469261153 -80.040178725096...
7 140.0 Seven seas (open ocean) Fr. S. Antarctic Lands ATF 16.0 POLYGON ((68.935 -48.62500000000001, 69.58 -48...
8 21262641.0 Oceania Australia AUS 800200.0 (POLYGON ((145.3979781434948 -40.7925485166058...
9 8210281.0 Europe Austria AUT 329500.0 POLYGON ((16.97966678230404 48.12349701597631,...

World population Map

In [9]:
gv.Polygons(d1, vdims=[('pop_est','Population'), ('name', 'Country')]).options(
    tools=['hover'], width=800, height=500, projection=crs.Robinson()
)
Out[9]:

Saving the map into an html file

In [10]:
g1=gv.Polygons(d1, vdims=[('pop_est','Population'), ('name', 'Country')]).options(
    tools=['hover'], width=800, height=500, projection=crs.Robinson()
)
In [11]:
renderer = gv.renderer('bokeh')
In [12]:
renderer.save(g1, 'population')

Map 2

In [13]:
import pandas as pd
import geoviews as gv

gv.extension('bokeh')
In [14]:
cities = pd.read_csv('cities.csv', encoding="ISO-8859-1")
In [15]:
population = gv.Dataset(cities, kdims=['City', 'Country', 'Year'])
In [16]:
population.columns
Out[16]:
<bound method Dataset.columns of :Dataset   [City,Country,Year]   (Latitude,Longitude,Population)>
In [17]:
points = population.to(gv.Points, ['Longitude', 'Latitude'], ['Population', 'City', 'Country'])

tiles = gv.tile_sources.StamenWatercolor
In [18]:
%%opts Points (size=0.003 cmap='viridis') [tools=['hover'] size_index=2 color_index=2 width=600] 
tiles * points
Out[18]:
In [19]:
%%opts Points (size=0.003 cmap='viridis') [tools=['hover']   size_index=2 color_index=2 width=800 height=600 xaxis=None yaxis=None toolbar=None]
g2=tiles * points
In [20]:
renderer = gv.renderer('bokeh')
In [21]:
renderer.save(g2, 'population2')
In [22]:
g2
Out[22]:

World GDP Map

Loading Libraries

In [23]:
import pandas as pd
import geoviews as gv

gv.extension('bokeh')

Importing the data: The data that I will be using here is available here

In [24]:
df = pd.read_csv('gdp_gps_years.csv', encoding="ISO-8859-1")
In [25]:
df.columns
Out[25]:
Index(['iso2c', 'Country', 'GDP(current$)', 'Year', 'Longitude', 'Latitude'], dtype='object')
In [26]:
gdp = gv.Dataset(df, kdims=['Country', 'Year'])
In [27]:
gdp.columns
Out[27]:
<bound method Dataset.columns of :Dataset   [Country,Year]   (iso2c,GDP(current$),Longitude,Latitude)>
In [28]:
points = gdp.to(gv.Points, ['Longitude', 'Latitude'], ['GDP(current$)', 'Country'])

tiles = gv.tile_sources.StamenWatercolor
In [29]:
%%opts Points (size=0.000015 cmap='viridis') [tools=['hover'] size_index=2 color_index=2 width=800 height=600 xaxis=None yaxis=None toolbar=None] 
tiles * points
Out[29]:
In [30]:
renderer = gv.renderer('bokeh')
In [31]:
gdp1=tiles * points
In [32]:
renderer.save(gdp1, 'gdp1')