How to do it...

Let's take a look at the following steps:

  1. First, let's define a function to get a map tile (a PNG image of size 256 x 256) from OpenStreetMap. We will also define a function to convert geographical coordinate systems to tile indexes, as shown in the following code:
import mathimport requestsdef get_osm_tile(x, y, z):    url = 'http://tile.openstreetmap.org/%d/%d/%d.png' % (z, x, y)    req = requests.get(url)    if not req.ok:        req.raise_for_status()    return reqdef deg_xy(lat, lon, zoom):    lat_rad = math.radians(lat)    n = 2 ** zoom    x = int((lon + 180) / 360 * n)    y = int((1 - math.log(math.tan(lat_rad) + (1 /    math.cos(lat_rad))) / math.pi) / 2 * n)    return x, y

This code is responsible for getting a tile from the OpenStreetMap server, which ...

Get Bioinformatics with Python Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.