#! /usr/bin/python
# Copyright (C) 2000, 2001 Intevation GmbH <intevation@intevation.de>
# Author: Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the LGPL (>=v2)
# Read the file COPYING coming with MapIt! for details.

__version__ = "$Revision: 1.8 $"

import os, sys
import cgi

sys.path.insert(0, os.path.join(sys.path[0], '../base'))

import makemap, tileinfo, markerinfo, mapconfig, common

def print_map(image, format, **params):
    """Print image to stdout as an image file

    Parameters:
        image - PIL image
        format - The PIL name of the output format
        **params - Additional keyword parameters that are passed through
                   to the image's save method
    """
    apply(image.save, (sys.stdout, format), params)

def main():
    map = tileinfo.MapInfo(mapconfig.tile_dir, mapconfig.tile_ext)
    marker = markerinfo.MarkerInfo(mapconfig.marker_locations,
                                   mapconfig.marker_dir,
                                   mapconfig.marker_ext)
    parameters = common.Parameters(cgi.FieldStorage(), map = map,
                                   marker = marker)
    print 'Content-type: ' + mapconfig.output_mime
    print
    image = makemap.generate_map(parameters)
    print_map(image, mapconfig.output_pil_format)
    makemap.write_log_entry(mapconfig.makemap_counter_file)


main()
