avatarPropTech@ecyY

Summary

The article provides a tutorial on how to map GIS data on various basemaps using Geopandas and Contextily in Google Colab, with examples of mapping housing estates and natural hazard zones in Hong Kong and New Zealand.

Abstract

The article "Mapping GIS Data on a Basemap by Contextily in Colab" outlines the process of visualizing Geographic Information System (GIS) data on different types of basemaps within Google Colab. It begins by emphasizing the importance of map-based applications in modern technology, such as Uber, and the necessity of layering GIS data with basemaps to effectively display spatial information. The author previously discussed mapping with geopandas but now focuses on the flexibility of choosing basemaps by using Contextily. The tutorial includes steps to install the required software, download GIS data from public sources, and plot this data on basemaps provided by Open Street Map and Stamen Terrain. The article also provides code snippets, screenshots of the resulting maps, and references to additional resources for those interested in exploring different basemaps and advanced mapping techniques.

Opinions

  • The author suggests that the choice of basemap depends on the purpose of the map, with Open Street Map being suitable for displaying spatial relationships by roads and streets, and Stamen Terrain Maps being better for showing contours and land use patterns.
  • The author demonstrates a preference for Contextily due to its ability to provide various basemaps, enhancing the visualization of GIS data.
  • By providing examples of mapping housing estates in Hong Kong and natural hazard zones in the Wellington region, the author implies that these tools are versatile and can be applied to different types of GIS data and geographic areas.
  • The author encourages readers to explore further by referring to a guide on Contextily and an advanced example of mapping global earthquakes, indicating a belief in the value of continuous learning and exploration in the field of GIS visualization.

Mapping GIS Data on a Basemap by Contextily in Colab

Nowadays many online apps are map-based applications, such as Uber, how to map GIS data on a basemap is an important knowledge in developing apps. Very often, data providers would only provide GIS data with coordinates or polygons’ coordinates, and a basemap is required to be layered (matched) to show a map. Depending on different purposes, various types of basemap are used. For example, Open Street Maps are more commonly used to show spatial relationships by roads and streets, Stamen Terrain Maps are better for showing contours and land use patterns. This article shows how to map GIS data on different basemaps by using Contextily in Colab.

We have already discussed how to map by geopandas in colab before (Yiu, 2021), but it is based on the maps provided by geopandas’ datasets and cannot be changed. Here we show how to download GIS data from a public website and then map them on a basemap.

  1. The first step is always installing software, here we install geopandas and contextily.
# plot GIS data on different basemaps by geopandas and contextily
# see https://colab.research.google.com/github/bwsi-hadr/03-Intro-to-GIS/blob/master/03_Intro_to_GIS.ipynb#scrollTo=aOtbuNGUTB7k
!pip install geopandas
!pip install contextily
import geopandas as gpd
import contextily as ctx # for basemaps

2. Then we collect a shape file (.shp) of the locations of HOS courts in Hong Kong provided by ESRI. The website is called “Open Geo-Spatial Data in Hong Kong” which provides 14 categories of GIS data. Here I explore the category of “Housing” where includes many different datasets. HOS Courts in Hong Kong is just one of them as shown in Figure 1.

Here I save the shp file to my google.colab drive first, and define the filepath as the filename and use geopandas to read the file.

#get a shape file 
#Hong Kong shp file from https://opendata.esrichina.hk/
from google.colab import drive
drive.mount('/content/drive/')
#filepath = "Home_Ownership_Scheme_Courts_in_Hong_Kong.shp"
data = gpd.read_file("drive/MyDrive/Colab Notebooks/Home_Ownership_Scheme_Courts_in_Hong_Kong.shp")
data.head()
Figure 1 An Extract from Explore Data Categories — Housing. source: ESRI at https://opendata.esrichina.hk/search?tags=housing

The table is quite big to show here, the following just shows the first 5 rows and the leftmost 10 columns (Figure 2). For the purpose of this article, the most important two columns are The_map_la and The_map_lo which indicate the latitude and longitude (i.e. coordinates) of the court.

Figure 2 The first 5 rows and the leftmost 10 columns of the GIS data from the downloaded file. Source: ESRI

3. If we just plot the GIS data by their coordinates as follows, it will show the dots only and not a map (Figure 3). You are free to set the x, y limits.

#plot the GIS data by coordinates
ax = data.plot(figsize=(10,5))
ax.set_xlim([113.8,114.3])
ax.set_ylim([22.20,22.55])
Figure 3 results of data.plot(figsize=(10,5))

4. Since it is about the locations of housing estates, I try using Open Street Map as the basemap, and the result is as follows (Figure 4):

#use a basemap of OpenStreetMap.Mapnik
ax = data.plot(figsize=(10,10), legend=True)
ctx.add_basemap(ax, crs=data.crs, source=ctx.providers.OpenStreetMap.Mapnik)
Figure 4 Map the GIS data of HOS courts on an OpenStreetMap basemap

5. I also try mapping them on a Stamen Terrain map, see which one is better?

#use another basemap Stamen.Terrain
ax = data.plot(figsize=(10,10), legend=True)
ctx.add_basemap(ax, crs=data.crs, source=ctx.providers.Stamen.Terrain)
Figure 5 Map the GIS data of HOS courts on a Stamen Terrain basemap

There are many other basemaps that you can choose. For more information on different basemaps in Contextily, you may refer to Hamed (2020). For an introduction guide to contextily, you may browse through https://contextily.readthedocs.io/en/latest/intro_guide.html

The coding of this article is saved in my github-GreatLab at https://github.com/Chung-collab/GREAT-LAB/blob/main/GIS_data_on_basemap.ipynb, and a youtube is available at Yiu (2021b)

Here I show one more example of mapping shaded areas rather than dots by using Wellington Ground Shaking Hazard Zones data available at https://data-gwrc.opendata.arcgis.com/datasets/GWRC::wellington-region-ground-shaking-hazard-zones/about

Simply change the data file downloading routine to the following:

#get a shape file 
#Wellington Quake Risk Zones shp file from https://data-gwrc.opendata.arcgis.com/datasets/GWRC::wellington-region-ground-shaking-hazard-zones/about
from google.colab import drive
drive.mount('/content/drive')
data = gpd.read_file("drive/MyDrive/Colab Notebooks/Wellington_Region_Ground_Shaking_Hazard_Zones.shp")
data.head()

The result is a table of geometry polygons for different quake risk severity. Let’s map the severity level (ZONE) by various colours on a terrain basemap.

The code is almost the same as above, simply adding a column=”ZONE” and cmap=’OrRd’ for orange colour, adding legend=True and including a map title with a vertical colour bar to show 1–5 severity level by adding legend_kwds={‘label’: “Wellington Region Ground Shaking Hazard Zones”, ‘orientation’: “vertical”}

ax=data.plot(column="ZONE", figsize=(25,12.5), cmap='OrRd', legend=True, legend_kwds={'label': "Wellington Region Ground Shaking Hazard Zones", 'orientation': "vertical"})
ctx.add_basemap(ax, crs=data.crs, source=ctx.providers.Stamen.Terrain)

The 3rd example is plotting Wellington’s flood zones as follows:

#get a shape file 
#Wellington Flood Zones shp file from #Wellington Flood Zone shp file from https://data-wcc.opendata.arcgis.com/datasets/WCC::flood-zones-3/about
from google.colab import drive
drive.mount('/content/drive/')
#filepath = "Flood_Zones.shp"
data = gpd.read_file("drive/MyDrive/Colab Notebooks/Flood_Zones.shp")
#plot without basemap
ax = data.plot(figsize=(15,10))
#plot with a basemap
ax = data.plot(figsize=(55,35))
ctx.add_basemap(ax, crs=data.crs, cmap='Blues', source=ctx.providers.Stamen.Terrain)

see github: https://github.com/Chung-collab/GREAT-LAB/blob/main/GIS_data_on_basemap.ipynb

If you want to have a more advanced example of mapping datasets by contextily, here is the website showing how to plot the Earthquakes>4.5 in the past 30 days in the world. https://www.stat.berkeley.edu/~nelle/teaching/2017-visualization/README.html

References

Hamed, W. (2020) Free base-maps for static maps using Geopandas and Contextily, Medium, June 6. https://towardsdatascience.com/free-base-maps-for-static-maps-using-geopandas-and-contextily-cd4844ff82e1

Yiu, C.Y. (2021a) Mapping by Geopandas in Colab, Medium, June 12. https://ecyy.medium.com/mapping-by-geopandas-in-colab-fe4b63b9ac00

Yiu, C.Y. (2021b) Mapping GIS Data on a Basemap by Contextily in Colab, Youtube, June 30. https://youtu.be/fZ12UJ9G6js

Contextily
Geopandas
Colab
Esri
Mapping
Recommended from ReadMedium