MaptimeLA presents:
Python +
BeautifulSoup +
QGIS
=Awesome!

#maptimeLA

Thank You
to Arup for the space
and CARTO for the pizza!

Python is a programming language.

It is commonly used in GIS, math, and science.

If you are used to languages like JavaScript, some key differences are:

  • indents instead of brackets
  • variables are local (no var)
  • unicode strings are prefixed by u

Python has a lot of libraries.
These contain code you can use via shortcuts.

Use pip for installing libraries. Recent installs should come with pip. Use pip --version to check in command line.

Use pip (or your preferred method) to install BeautifulSoup4:

pip install beautifulsoup4

BeautifulSoup is a library for extracting data from html and xml.

Use pip (or your preferred method) to install requests:

pip install requests

Requests is a library for extracting data from html and xml.

Let's write some Python to get the gages from this LA County page as a csv.

But first, inspect the page to learn more about the structure of the data we want.

We start by importing the libraries we need:
  import requests
  from bs4 import BeautifulSoup
  import csv

Then we fetch the site using requests:
  url = "http://www.ladpw.org/wrd/precip/alertlist.cfm"
  response = requests.get(url)
  code = response.content

Time to make soup:
  soup = BeautifulSoup(code, "html.parser")
  table = soup.find('table',
    attrs={"summary" : "Table
    contains ALERT raingage list"})

Parse the table together:

Time for QGIS!

QGIS is a free geographic information system software for making maps and analyzing spatial data.

The longitude is missing the negative sign. We will add it in QGIS:

Now we can add the table as points.

Turn on the basemap using OpenLayers and save as a shapefile.

Now let's write some Python to get the rainfall from individual gages as a csv.

But first, inspect the page to learn more about the structure of the data we want.

Python for individual gage:

Now we are going to use relations in QGIS to relate the rain to our gage points:

Now what can you do? As an example we could sum and color the gage by rainfall: