O'Reilly logo

Geospatial Development By Example with Python by Pablo Carreira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating utility functions to generate maps

Now we will create the first function that will compose our application.

  1. Still in the mapnik_experiments folder, create a new file: map_functions.py.
  2. 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required