Creating utility functions to generate maps
Now we will create the first function that will compose our application.
Still in the
mapnik_experiments
folder, create a new file:map_functions.py
.Insert the code as follows into that file:
# coding=utf-8 import mapnik def create_map(style_file, output_image, size=(800, 600)): """Creates a map from a XML file and writes it to an image. :param style_file: Mapnik XML file. :param output_image: Name of the output image file. :param size: Size of the map in pixels. """ map = mapnik.Map(*size) mapnik.load_map(map, style_file) map.zoom_all() mapnik.render_to_file(map, output_image) if __name__ == '__main__': create_map('map_style.xml', '../output/world3.png', size=(400, 400))
What we did here is pack the map generation code into a function that we can reuse in the future. It takes two required arguments: the XML style file and the name of the image file that Mapnik will write the results to...