Part 1: Social Media Data

Workshop: Social Media, Data Science, & Cartograpy
Alexander Dunkel, Madalina Gugulica

First step: Enable worker_env in jupyter lab

In [1]:
!cd .. && sh 7: /projects/p_lv_mobicart_2021/workshop_env/bin/python: not found
Well done!

This is the first notebook in a series of four notebooks:

  1. Introduction to Social Media data, jupyter and python spatial visualizations
  2. Introduction to privacy issues with Social Media data and possible solutions for cartographers
  3. Specific visualization techniques example: TagMaps clustering
  4. Specific data analysis: Topic Classification

Open these notebooks through the file explorer on the left side.

If you haven't worked with jupyter, these are some tips:
  • Jupyter Lab allows to interactively execute and write annotated code
  • There are two types of cells: Markdown cells contain only text (annotations), Code cells contain only python code
  • Cells can be executed by SHIFT+Enter
  • The output will appear below
  • States of python will be kept in-between code cells: This means that a value assigned to a variable in one cell remains available afterwards
  • This is accomplished with IPython, an interactive version of python
  • Important: The order in which cells are executed does not have to be linear. It is possible to execute any cell in any order. Any code in the cell will use the current "state" of all other variables. This also allows you to update variables.
Some links
This python environment is prepared for spatial data processing/ cartography.
The following is a list of the most important packages, with references to documentation: We will explore some functionality of these packages in this workshop.
If you want to run these notebooks at home, try the IfK Jupyter Docker Container, which includes the same packages.


We are creating several output graphics and temporary files.

These will be stored in the subfolder notebooks/out/.

In [2]:
from pathlib import Path

OUTPUT = Path.cwd() / "out"
Syntax: pathlib.Path() / "out" ? Python pathlib provides a convenient, OS independend access to local filesystems. These paths work independently of the OS used (e.g. Windows or Linux). Path.cwd() gets the current directory, where the notebook is running. See the docs..

To reduce the code shown in this notebook, some helper methods are made available in a separate file.

Load helper module from ../py/module/

In [3]:
import sys

module_path = str(Path.cwd().parents[0] / "py")
if module_path not in sys.path:
from modules import tools

Activate autoreload of changed python files:

In [4]:
%load_ext autoreload
%autoreload 2

Introduction: VGI and Social Media Data

Broadly speaking, GI and User Generated Content can be classified in the following three categories of data:
  • Authoritative data that follows objective criteria of measurement such as Remote Sensing, Land-Use, Soil data etc.
  • Explicitly volunteered data, such as OpenStreetMap or Wikipedia. This is typically collected by many people, who collaboratively work on a common goal and follow more or less specific contribution guidelines.
  • Subjective information sources
    • Explicit: e.g. Surveys, Opinions etc.
    • Implicit: e.g. Social Media
Social Media data belongs to the third category of subjective information, representing certain views held by groups of people. The difference to Surveys is that there is no interaction needed between those who analyze the data and those who share the data online, e.g. as part of their daily communication.

Social Media data is used in marketing, but it is also increasingly important for understanding people's behaviour, subjective values, and human-environment interaction, e.g. in citizen science and landscape & urban planning. In this notebook, we will explore basic routines how Social Media and VGI can be accessed through APIs and visualized in python.

Social Media APIs

  • Social Media data can be accessed through public APIs.
  • This will typically only include data that is explicitly made public by users.
  • Social Media APIs exist for most networks, e.g. Flickr, Twitter, or Instagram
Privacy? We'll discuss legal, ethical and privacy issues with Social Media data in the second notebook: 02_hll_intro.ipynb

Instagram Example

  • Retrieving data from APIs requires a specific syntax that is different for each service.
  • commonly, there is an endpoint (a url) that returns data in a structured format (e.g. json)
  • most APIs require you to authenticate, but not all (e.g. Instagram,
But the Instagram API was discontinued!
  • Instagram discontinued their official API in October 2018. However, their Web-API is still available, and can be accessed even without authentication.
  • One rationale is that users not signed in to Instagram can have "a peek" at images, which provides significant attraction to join the network.
  • We'll discuss questions of privacy and ethics in the second notebook.

Load Instagram data for a specific Hashtag.

In [68]:
hashtag = "park"
query_url = f'{hashtag}/?__a=1'
Syntax: f'{}' ? This is called an f-string, a convenient python convention to concat strings and variables.
In [69]:
from IPython.core.display import HTML
display(HTML(tools.print_link(query_url, hashtag)))
  • If you're not signed in: Chances are high that you're seeing a "Login" page. Since we are working in a workshop, only very few requests to Instagram non-login API are allowed.
  • otherwise, you'll see a json object with the latest feed content
  • In the following, we will try to retrieve this json object and display it.

First, try to get the json-data without login. This may or may not work:

In [70]:
import requests

json_text = None
response = requests.get(
    url=query_url, headers=tools.HEADER)
In [71]:
if not response.status_code == 429 and not "/login/" in response.url:
    json_text = response.text
    print("Loaded live json")
Loaded live json

Optionally, write to temporary file:

In [72]:
if json_text:
    with open(OUTPUT / f"live_{hashtag}.json", 'w') as f:

If the the url refers to the "login" page (or status_code 429), access is blocked. In this case, get the sample json:

In [73]:
if not json_text:
    # check if manual json exists
    local_json = [json for json in OUTPUT.glob('*.json')]
    if len(local_json) > 0:
        # read local json
        with open(local_json[0], 'r') as f:
            json_text =
        print("Loaded local json")
Syntax: [x for x in y] ? This is called a list comprehension, a convenient python convention to to create lists (from e.g. generators etc.).

If neither live nor local json has been loaded, load sample json:

In [74]:
if not json_text:
    sample_url = tools.get_sample_url()
    sample_json_url = f'{sample_url}/download?path=%2F&files=park.json'
    response = requests.get(url=sample_json_url)
    json_text = response.text
    print("Loaded sample json")

Turn text into json format:

In [75]:
import json
json_data = json.loads(json_text)

Have a peek at the returned data.

In [76]:
print(json.dumps(json_data, indent=2)[0:550])
  "graphql": {
    "hashtag": {
      "id": "17841563668080313",
      "name": "park",
      "allow_following": false,
      "is_following": false,
      "is_top_media_only": false,
      "profile_pic_url": "",
      "edge_hashtag_to_media": {

The json data is nested. Values can be accessed with dictionary keys.

In [77]:
total_cnt = json_data["graphql"]["hashtag"]["edge_hashtag_to_media"].get("count")

    f'''<details><summary>Working with the JSON Format</summary>
    The json data is nested. Values can be accessed with dictionary keys. <br>For example,
    for the hashtag <strong>{hashtag}</strong>, 
    the total count of available images on Instagram is <strong>{total_cnt:,.0f}</strong>.
Working with the JSON Format The json data is nested. Values can be accessed with dictionary keys.
For example, for the hashtag park, the total count of available images on Instagram is 33,143,913.

Another more flexible data analytics interface is available with pandas.DataFrame().

Dataframe ? A pandas dataframe is the typical tabular data format used in python data science. Most data can be directly converted to a DataFrame.
In [78]:
import pandas as pd
pd.set_option("display.max_columns", 4)
df = pd.json_normalize(

0 1 2 3 4 5 6 7 8 9 ... 62 63 64 65 66 67 68 69 70 71
node.comments_disabled False False False False False False False False False False ... False False False False False False False False False False
node.__typename GraphImage GraphSidecar GraphImage GraphSidecar GraphImage GraphImage GraphImage GraphImage GraphImage GraphVideo ... GraphImage GraphImage GraphImage GraphImage GraphImage GraphImage GraphImage GraphSidecar GraphImage GraphImage 2540774574208661591 2540774554570229779 2540774469588120132 2540774404073415997 2540774256188660988 2540774033270344301 2540774016191271118 2540773950893762390 2540773905451753198 2540773845474579301 ... 2540755014767762889 2540747449048563737 2540726769225711143 2540719763220795766 2540649887994549360 2540550480086579626 2539424865805258775 2538900007730711579 2537878748329462561 2536668389280079876
node.edge_media_to_caption.edges [{'node': {'text': 'Mai bahut paresan hu ki am... [{'node': {'text': 'Детские шалости . . . . . ... [{'node': {'text': 'Throwback to before Corona... [{'node': {'text': '2021-3-28 最近サボり気味だったのでボックス... [{'node': {'text': 'Trying to find some color ... [{'node': {'text': 'Smile 😅 #smile #son #myson... [{'node': {'text': 'Spring is in the air 🥰 So ... [{'node': {'text': '#tree#tree_magic#trees#tre... [{'node': {'text': 'Heute ist Mache-einen-Spaz... [{'node': {'text': 'The evening is all about r... ... [{'node': {'text': '💕💕'}}] [{'node': {'text': 'You go to team 1, 2 or 3 ?... [{'node': {'text': 'It’s the last Cherry Bloss... [{'node': {'text': 'Defensive Castling. . . .... [{'node': {'text': 'Cherry blossom . . . . . ... [{'node': {'text': 'Spring is finally here!!!🥺... [{'node': {'text': 'Selfie con el verano. #sel... [{'node': {'text': '《PASSA PRO LADO》Chile é o ... [{'node': {'text': 'Bloom into something SPECI... [{'node': {'text': '#bounty #pitbull #dontbull...
node.shortcode CNCpQN-DThX CNCpP7rgogT CNCpOsiLnJE CNCpNvhM2U9 CNCpLlylRz8 CNCpIWLnYpt CNCpIGRn5DO CNCpHJdlqdW CNCpGfJCJ7u CNCpFnSHZNl ... CNCkzl0hl3J CNCjFfshQAZ CNCeYkHBj4n CNCcynQlxF2 CNCM5y4jRRw CNB2TODgjmq CM92XYEHdAX CM7_Br5Bowb CM4W0Z9MMsh CM0DnXQAGAE
node.edge_media_to_comment.count 0 0 0 0 0 0 0 0 0 0 ... 1 1 1 5 0 4 2 5 54 4
node.taken_at_timestamp 1617103953 1617103951 1617103941 1617103933 1617103915 1617103889 1617103887 1617103879 1617103873 1617103870 ... 1617101621 1617100720 1617098254 1617097419 1617089089 1617077239 1616943055 1616880487 1616758744 1616614458
node.dimensions.height 1350 1080 1080 562 1350 1350 1232 1080 1080 1080 ... 803 719 1350 1080 720 1350 1350 1351 1080 1136
node.dimensions.width 1080 1080 1080 750 1080 1080 1080 1080 1080 1080 ... 1080 1080 1080 1080 1080 1080 1080 1080 1080 1080
node.display_url ...
node.edge_liked_by.count 1 1 0 0 0 0 4 0 2 1 ... 3 4 19 16 16 213 72 128 955 263
node.edge_media_preview_like.count 1 1 0 0 0 0 4 0 2 1 ... 3 4 19 16 16 213 72 128 955 263 5851781099 3119365248 194246328 6945287843 7678988855 3237980116 1799586119 46158799795 10834760559 9407786715 ... 39990270 46646388447 16262526 14780238 29627463 26419246 1026486804 5362882268 2872355804 6830279584
node.thumbnail_src ...
node.thumbnail_resources [{'src': 'https://scontent-frt3-2.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frx5-1.cdninstagram... [{'src': 'https://scontent-frt3-2.cdninstagram... [{'src': 'https://scontent-frt3-2.cdninstagram... [{'src': 'https://scontent-frt3-2.cdninstagram... [{'src': 'https://scontent-frt3-2.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frt3-2.cdninstagram... ... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frx5-1.cdninstagram... [{'src': 'https://scontent-frx5-1.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram... [{'src': 'https://scontent-frx5-1.cdninstagram... [{'src': 'https://scontent-frt3-1.cdninstagram...
node.is_video False False False False False False False False False True ... False False False False False False False False False False
node.accessibility_caption Photo by Sachin Kumar Prajapati in Lucknow, Ut... Photo by 🇷🇺 Alx Che 🇷🇺 on March 30, 2021. May... Photo by Igor Jakovljević in Jet d'eau Genève... None Photo by Sumit Pandey on March 30, 2021. May b... Photo by Piotr Wójcik in Priory Park with @oo... Photo by Daria Zvereva in Villa Eden The Leadi... Photo by Brigita Instructor on March 30, 2021.... Photo by CW Niemeyer Buchverlage on March 30, ... None ... Photo by Kwan Norachid on March 30, 2021. May ... Photo by Kultura Park on March 30, 2021. May b... Photo by Sasha Wallace 💛 in Beechwood Park, Ne... Photo by David L. Merin in Cambodia. May be an... Photo by Paulina Nartowicz in Hall Place. May ... Photo by @lhmann on March 29, 2021. May be an ... Photo by Nadia Arlene Donaire García in Parque... Photo by Daniele Barros on March 27, 2021. May... Photo by fяαωℓα in Planet Earth. May be art. Photo by thelifeofBounty on March 24, 2021. Ma...
node.product_type NaN NaN NaN NaN NaN NaN NaN NaN NaN feed ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
node.video_view_count NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.0 ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

19 rows × 72 columns

View the first few images

First, define a function.

In [79]:
from typing import List
import matplotlib.pyplot as plt

from PIL import Image, ImageFilter
from io import BytesIO

def image_grid_fromurl(url_list: List[str]):
    """Load and show images in a grid from a list of urls"""
    count = len(url_list)
    plt.figure(figsize=(11, 18))
    for ix, url in enumerate(url_list):
        r = requests.get(url=url)
        i =
        resize = (150, 150)
        i = i.resize(resize)
        i = i.filter(ImageFilter.BLUR)
        ax = plt.subplot(1, count, ix + 1)

Use the function to display images from "node.display_url" column.

In [80]:

Creating Maps

  • Frequently, VGI and Social Media data contains references to locations such as places or coordinates.
  • Most often, spatial references will be available as latitude and logitude (decimal degrees and WGS1984 projection).
  • To demonstrate integration of data, we are now going to query another API,, to get a list of places near certain coordinates.
In [18]:
lat = 51.03711
lng = 13.76318

Get list of nearby places using's API:

In [19]:
query_url = f''
params = {
In [20]:
response = requests.get(
    url=query_url, params=params)
if response.status_code == 200:
    print(f"Query successful. Query url: {response.url}")
Query successful. Query url:
In [21]:
json_data = json.loads(response.text)
print(json.dumps(json_data, indent=2)[0:500])
  "batchcomplete": "",
  "query": {
    "geosearch": [
        "pageid": 4712421,
        "ns": 14,
        "title": "Category:Gro\u00dfer Garten, Dresden",
        "lat": 51.0375,
        "lon": 13.7631,
        "dist": 43.7,
        "primary": ""
        "pageid": 4312703,
        "ns": 14,
        "title": "Category:Palais im Gro\u00dfen Garten",
        "lat": 51.0378,
        "lon": 13.7628,
        "dist": 81.2,
        "primary": ""

Get List of places.

In [22]:
location_dict = json_data["query"]["geosearch"]

Turn into DataFrame.

In [23]:
df = pd.DataFrame(location_dict)
pageid ns title lat lon dist primary
0 4712421 14 Category:Großer Garten, Dresden 51.037500 13.763100 43.7
1 4312703 14 Category:Palais im Großen Garten 51.037800 13.762800 81.2
2 82327811 14 Category:Südallee, Dresden 51.035530 13.764342 193.6
3 82325805 14 Category:Querallee, Dresden 51.035881 13.760845 212.9
4 82328367 14 Category:Herkulesallee, Dresden 51.039355 13.763524 250.8
In [24]:
(50, 7)

If we have queried 50 records, we have reached the limit specified in our query. There is likely more available, which would need to be queried using subsequent queries (e.g. by grid/bounding box). However, for the workshop, 50 locations are enough.

Modify data.: Replace "Category:" in column title.

  • Functions can be easily applied to subsets of records in DataFrames.
  • although it is tempting, do not iterate through records
  • dataframe vector-functions are almost always faster and more pythonic
In [25]:
df["title"] = df["title"].str.replace("Category:", "")

Turn DataFrame into a GeoDataFrame

GeoDataframe ? A geopandas GeoDataFrame is the spatial equivalent of a pandas dataframe. It supports all operations of DataFrames, plus spatial operations. A GeoDataFrame can be compared to a Shapefile in (e.g.), QGis.
In [26]:
import geopandas as gp
gdf = gp.GeoDataFrame(
    df, geometry=gp.points_from_xy(df.lon,

Set projection, reproject

Projections in Python
  • Most available spatial packages have more or less agreed on a standard format for handling projections in python.
  • The recommended way is to define projections using their epsg ids, which can be found using
  • Note that, sometimes, the projection-string refers to other providers, e.g. for Mollweide, it is "ESRI:54009"
In [27]:
CRS_PROJ = "epsg:3857" # Web Mercator
CRS_WGS = "epsg:4326" # WGS1984 = CRS_WGS # Set projection
gdf = gdf.to_crs(CRS_PROJ) # Project
In [28]:
pageid ns name lat lon dist primary geometry
0 4712421 14 Großer Garten, Dresden 51.037500 13.763100 43.7 POINT (1532101.284 6627929.721)
1 4312703 14 Palais im Großen Garten 51.037800 13.762800 81.2 POINT (1532067.888 6627982.831)
2 82327811 14 Südallee, Dresden 51.035530 13.764342 193.6 POINT (1532239.543 6627580.976)
3 82325805 14 Querallee, Dresden 51.035881 13.760845 212.9 POINT (1531850.258 6627643.112)
4 82328367 14 Herkulesallee, Dresden 51.039355 13.763524 250.8 POINT (1532148.483 6628258.121)

Display location on a map

  • Maplotlib and contextily provide one way to plot static maps.
  • we're going to show another, interactive map renderer afterwards

Import contextily, which provides static background tiles to be used in matplot-renderer.

In [29]:
import contextily as cx

1. Create a bounding box for the map

In [30]:
x = gdf.loc[0].geometry.x
y = gdf.loc[0].geometry.y

margin = 1000 # meters
bbox_bottomleft = (x - margin, y - margin)
bbox_topright = (x + margin, y + margin)
gdf.loc[0] ?
  • gdf.loc[0] is the loc-indexer from pandas. It means: access the first record of the (Geo)DataFrame.
  • .geometry.x is used to access the (projected) x coordinate geometry (point). This is only available for GeoDataFrame (geopandas)

2. Create point layer, annotate and plot.

  • With matplotlib, it is possible to adjust almost every pixel individual.
  • However, the more fine-tuning is needed, the more complex the plotting code will get.
  • In this case, it is better to define methods and functions, to structure and reuse code.
In [31]:
from matplotlib.patches import ArrowStyle
# create the point-layer
ax = gdf.plot(
    figsize=(10, 15),
# set display x and y limit
    bbox_bottomleft[0], bbox_topright[0])
    bbox_bottomleft[1], bbox_topright[1])
# turn of axes display
# add callouts 
# for the name of the places
for index, row in gdf.iterrows():
    # offset labels by odd/even
    label_offset_x = 30
    if (index % 2) == 0:
        label_offset_x = -100
    label_offset_y = -30
    if (index % 4) == 0:
        label_offset_y = 100
        xy=(row["geometry"].x, row["geometry"].y),
        xytext=(label_offset_x, label_offset_y),
        textcoords="offset points",
                "simple, head_length=2, head_width=2, tail_width=.2"), 
    ax, alpha=0.5,