Jupyter and OSGeoLive

_images/jupyter_logo.png

OSGeo-Live is preconfigured with Jupyter and a set of geospatial Python notebooks:

OSGeo-Live provides a set of digital notebooks to explore several open source solution for geospatial data analysis, with the aim of bridging together the several software libraries already installed on the OSGeo-live to perform complex geo-data-science workflows. The notebooks are developed in the Jupyter environments which is heavily based on the IPython notebook project, are written in different languages (bash, python, R) and are organized in a series of “topic-oriented” geospatial notebooks.

https://wiki.osgeo.org/wiki/Live_GIS_Add_Project#Notebooks

A static HTML rendering of the notebooks shipped within the OSGeo-Live is available at: http://nbviewer.jupyter.org/github/OSGeo/OSGeoLive-Notebooks/blob/master/index.ipynb

Notes:

  • Python2 only at present
  • Many notebooks created as part of the Google Summer of Code (GSoC) project in 2015.
  • Some may require further data to be added to the OSGeoLive virtual machine

Setup

  1. Start the server using: Start > Geospatial > Spatial Tools > Start Jupyter Notebook
_images/jupyter1.png

Otherwise get the following error Firefox can't establish a connection to the server at localhost:8883.

  1. Then open the notebook homepage using: Start > Geospatial > Spatial Tools > Jupyter Notebook

Introduction to Jupyter

  • An interactive tour of the notebook UI can be started by selecting Help -> User Interface Tour from the notebook menu bar.

  • The index page is at http://localhost:8883/notebooks/index.ipynb

  • The dashboard is at http://localhost:8883/tree/projects

  • Notebooks are saved in disk in /home/user/jupyter/notebooks

    _images/jupyter_notebooks.png
  • A notebook file’s (.ipynb) contents is saved in JSON. To look at one in a text editor use:

    leafpad /home/user/jupyter/notebooks/projects/FIONA/fiona-shapely-pyplot.ipynb
    

Example Notebook

During the session we will briefly step through the noteook at: http://localhost:8883/notebooks/projects/FIONA/fiona-ipynb-interop.ipynb This uses the following 3 libraries:

Creating New Notebooks

Simply select File > New Notebook when in an existing notebook. We can paste in the sample scripts below and run them.

Shapely

We can paste in one of the code snippets used in the Shapely documentation for intersections. Code can be downloaded from the docs page directly, or accessed on Github.

from matplotlib import pyplot
from shapely.geometry import Point
from descartes import PolygonPatch
from math import sqrt

GM = (sqrt(5)-1.0)/2.0
W = 8.0
H = W*GM
SIZE = (W, H)

BLUE = '#6699cc'
GRAY = '#999999'

def set_limits(ax, x0, xN, y0, yN):
    ax.set_xlim(x0, xN)
    ax.set_xticks(range(x0, xN+1))
    ax.set_ylim(y0, yN)
    ax.set_yticks(range(y0, yN+1))
    ax.set_aspect("equal")
    
fig = pyplot.figure(1, figsize=SIZE, dpi=90)

a = Point(1, 1).buffer(1.5)
b = Point(2, 1).buffer(1.5)

# 1
ax = fig.add_subplot(121)

patch1 = PolygonPatch(a, fc=GRAY, ec=GRAY, alpha=0.2, zorder=1)
ax.add_patch(patch1)
patch2 = PolygonPatch(b, fc=GRAY, ec=GRAY, alpha=0.2, zorder=1)
ax.add_patch(patch2)
c = a.intersection(b)
patchc = PolygonPatch(c, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2)
ax.add_patch(patchc)

ax.set_title('a.intersection(b)')

set_limits(ax, -1, 4, -1, 3)

#2
ax = fig.add_subplot(122)

patch1 = PolygonPatch(a, fc=GRAY, ec=GRAY, alpha=0.2, zorder=1)
ax.add_patch(patch1)
patch2 = PolygonPatch(b, fc=GRAY, ec=GRAY, alpha=0.2, zorder=1)
ax.add_patch(patch2)
c = a.symmetric_difference(b)

if c.geom_type == 'Polygon':
    patchc = PolygonPatch(c, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2)
    ax.add_patch(patchc)
elif c.geom_type == 'MultiPolygon':
    for p in c:
        patchp = PolygonPatch(p, fc=BLUE, ec=BLUE, alpha=0.5, zorder=2)
        ax.add_patch(patchp)

ax.set_title('a.symmetric_difference(b)')

set_limits(ax, -1, 4, -1, 3)

pyplot.show()

Matplotlib

The following sample matplotlib code can be pasted into a cell in a workbook (matplotlib and numpy are already installed in the OSGeoLive setup). More matplotlib examples and code can be found at https://matplotlib.org/gallery/index.html

import numpy as np
import matplotlib.pyplot as plt


# Fixing random state for reproducibility
np.random.seed(19680801)

# Compute pie slices
N = 20
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)

ax = plt.subplot(111, projection='polar')
bars = ax.bar(theta, radii, width=width, bottom=0.0)

# Use custom colors and opacity
for r, bar in zip(radii, bars):
    bar.set_facecolor(plt.cm.viridis(r / 10.))
    bar.set_alpha(0.5)

plt.show()
_images/polar_chart.png