Book Image

Geospatial Development By Example with Python

By : Pablo Carreira
5 (1)
Book Image

Geospatial Development By Example with Python

5 (1)
By: Pablo Carreira

Overview of this book

From Python programming good practices to the advanced use of analysis packages, this book teaches you how to write applications that will perform complex geoprocessing tasks that can be replicated and reused. Much more than simple scripts, you will write functions to import data, create Python classes that represent your features, and learn how to combine and filter them. With pluggable mechanisms, you will learn how to visualize data and the results of analysis in beautiful maps that can be batch-generated and embedded into documents or web pages. Finally, you will learn how to consume and process an enormous amount of data very efficiently by using advanced tools and modern computers’ parallel processing capabilities.
Table of Contents (17 chapters)
Geospatial Development By Example with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 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...