Create module#

It is not very practical to start Jupyter every time and go through all the cells of the request notebook just to be able to use the functions. Instead, we should store our functions in a separate module, like in nominatim.py:

  1. For this I have created a new text file in Jupyter in the same place as these notebooks, and named it nominatim.py.

  2. Then I copied the imports, the method nominatim_search and its decorator lru_cache and saved the file.

  3. Now we can go back to our notebook and import the code from this file and do our searches:

[1]:
from nominatim import nominatim_search
[2]:
nominatim_search("Alexanderplatz, Berlin, Germany")
[2]:
[{'place_id': 261767431,
  'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
  'osm_type': 'way',
  'osm_id': 783052052,
  'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801'],
  'lat': '52.5219814',
  'lon': '13.413635717448294',
  'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',
  'class': 'place',
  'type': 'square',
  'importance': 0.6914982526373583}]
[3]:
nominatim_search(
    address=None,
    street="8, Marienburger Straße",
    city="Berlin",
    country="Germany",
)
[3]:
[{'place_id': 194632110,
  'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
  'osm_type': 'way',
  'osm_id': 368946992,
  'boundingbox': ['52.5341829', '52.534317', '13.4237697', '13.4240606'],
  'lat': '52.53425085',
  'lon': '13.423921421750569',
  'display_name': '8, Marienburger Straße, Winsviertel, Prenzlauer Berg, Pankow, Berlin, 10405, Deutschland',
  'class': 'building',
  'type': 'apartments',
  'importance': 0.4200099999999999}]

The outsourcing of the notebook’s code to modules makes it easier to reuse it, and also makes the notebooks more readable.

However, for the code to work, geocode.py needs to be in the same folder as a Jupyter notebook. If you want to call this module from another location, the path specification in the import would have to be changed. In this case it is better to create your own package, as described in Packing.