Geography

How to draw South-America-focused map using matplotlib basemap

Note that matplotlib basemap is deprecated in favour of cartopy !

Also see:

This code allows you to draw an map that is focused on South America using matplotlib basemap:

my_map = Basemap(projection='ortho', lat_0=-13, lon_0=-55, resolution='l')

Complete example code

This code reproduces the image shown above:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
my_map = Basemap(projection='ortho', lat_0=-13, lon_0=-55, resolution='l')
my_map.drawcoastlines(linewidth=1)
my_map.drawcountries(linewidth=0.5)

# Make plot larger
plt.gcf().set_size_inches(20, 10)
# Save to file
plt.savefig("South America.svg")

 

Posted by Uli Köhler in Geography, Python

How to draw North-America-focused map using matplotlib basemap

Note that matplotlib basemap is deprecated in favour of cartopy !

Also see:

This code allows you to draw an map that is focused on North America using matplotlib basemap:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
my_map = Basemap(projection='ortho', lat_0=45 ,lon_0=-100, resolution='l')
my_map.drawcoastlines(linewidth=1)
my_map.drawcountries(linewidth=0.5)

# Make plot larger
plt.gcf().set_size_inches(20, 10)
# Save to file
plt.savefig("North America.svg")

 

Posted by Uli Köhler in Geography, Python

How to fix Python ModuleNotFoundError: No module named ‘osmium’

Problem:

When you running your python script, you see an error message like

Traceback (most recent call last):
  File "run.py", line 2, in <module>
    import osmium as osm
ModuleNotFoundError: No module named 'osmium

Solution:

Install pyosmium using

pip install osmium

or

pip3 install osmium

 

 

Posted by Uli Köhler in OpenStreetMap, Python

Minimal example how to read .osm.pbf file using Python & osmium

This minimal example uses the osmium python binding to read an .osm.pbf file and count the number of nodes, ways and relations.

First, we need to download a suitable dataset. For this example, we’ll be using kenya-latest.osm.pbf which you can download from Geofabrik:

wget https://download.geofabrik.de/africa/kenya-latest.osm.pbf

Now we can run the script:

#!/usr/bin/env python3
import osmium as osm

class OSMHandler(osm.SimpleHandler):
    def __init__(self):
        osm.SimpleHandler.__init__(self)
        self.node_count = 0
        self.way_count = 0
        self.relation_count = 0

    def node(self, n):
        self.node_count += 1

    def way(self, w):
        self.way_count += 1

    def relation(self, r):
        self.relation_count += 1

osmhandler = OSMHandler()
osmhandler.apply_file("kenya-latest.osm.pbf")
print(f'Number of nodes: {osmhandler.node_count}')
print(f'Number of way: {osmhandler.way_count}')
print(f'Number of relations: {osmhandler.relation_count}')

Example output:

Number of nodes: 29442019
Number of way: 3188550
Number of relations: 2225

 

Posted by Uli Köhler in OpenStreetMap, Python