#!/usr/bin/python

# Script to create smaller map tiles for use in the GRAMPS MapView.
# (c) Martin Hawlisch, martin.hawlisch@gmx.de, 2006.
# I hereby put this script into the public domain.


# Images are generated by using the NASA Blue Marble next generation images.
#    http://visibleearth.nasa.gov/view_set.php?categoryID=2355
# July, Blue Marble Next Generation w/ Topography and Bathymetry:
#    http://visibleearth.nasa.gov/view_rec.php?id=7106
# 5400 pixel jpeg file:
#    http://veimages.gsfc.nasa.gov/7106/world.topo.bathy.200407.3x5400x2700.jpg
# This images are public domain.

import os
import sys

tile_defines = []

def process_file( imagefile, image_width, image_height, map_x, map_y, map_width, outfilepattern, tile_size):
	y = 0
	while (y*tile_size) < image_height:
		x = 0
		while (x*tile_size) < image_width:
			outfile = outfilepattern % (y,x)
			if not os.path.isfile( outfile):
				cmd = "jpegtran -optimize -crop %dx%d+%d+%d -outfile %s %s" %\
					(tile_size,tile_size,x*tile_size,y*tile_size, outfile,imagefile)
				print cmd
				if os.system( cmd):
					print("WARNING: lossless crop using jpegtran (part of libjpeg-progs) failed. Trying lossy fallback")
					cmd = "convert %s -crop %dx%d+%d+%d %s" %\
						(imagefile,tile_size,tile_size,x*tile_size,y*tile_size, outfile)
					print cmd
					if os.system( cmd):
						sys.exit("ERROR, image crop using jpegtran (part of libjpeg-progs) and imagemagick failed.")
			else:
				print "tile %s already exists" % outfile
			tile_width = map_width/(image_width/tile_size)
			tile_x = map_x + tile_width * x
			tile_y = map_y - tile_width * y
			print "location: %dx%d" % (tile_x,tile_y)
			tile_defines.append("self.zoom_map.add_map_source('%s', %d,%d, %d,%d, %d,%d)" %\
				(outfile,tile_x,tile_y,tile_width,tile_width,tile_size,tile_size))
			x = x + 1
		y = y + 1
	pass

def scale_down( infile, outfile, new_width):
	if not os.path.isfile( outfile):
		cmd = "convert %s -resize %d %s" % (infile, new_width, outfile)
		print cmd
		if os.system( cmd):
			sys.exit("ERROR. Scaling down using imagemagick failed.")
	else:
		print "scaled down image %s already exists" % outfile

def fetch_image( server, filename):
	if not os.path.isfile( sourcemap_midres):
		print "downloading map file %s" % filename
		cmd = "wget -v %s/%s" % (server, sourcemap_midres)
		print cmd
		if os.system( cmd):
			sys.exit("ERROR, image download using wget failed.")
	else:
		print "mapfile %s already downloaded" % filename
	
server = "http://veimages.gsfc.nasa.gov/7130"

# Step 1: Fetch mid-res image
sourcemap_midres = "world.topo.200407.3x5400x2700.jpg"
fetch_image( server, sourcemap_midres)


# Step 2: Scale that down a bit (to a multiple of 16 pixels)
sourcemap_lowres = "world.topo.200407.3x3200x1600.jpg"
scale_down(sourcemap_midres,sourcemap_lowres,3200)

# Step 3: Create tiles
process_file( sourcemap_lowres, 3200, 1600, -180, 90, 360, "world.topo.200407.3x3200x1600_tile_%d_%d.jpg",400)

# Step 4: Scale down mid-res image to 2048x1024
sourcemap_lowres = "world.topo.200407.3x1600x800.jpg"
scale_down(sourcemap_midres,sourcemap_lowres,1600)

# Step 4: Tile low-res image
process_file( sourcemap_lowres, 1600, 800, -180, 90, 360, "world.topo.200407.3x1600x800_tile_%d_%d.jpg",400)

# Step 5: Scale down once more
sourcemap_thumb = "world.topo.200407.3x800x400.jpg"
scale_down(sourcemap_lowres,sourcemap_thumb,800)

# Step 5: Scale down thumb-scale image
sourcemap_thumb = "world.topo.200407.3x400x200.jpg"
scale_down(sourcemap_lowres,sourcemap_thumb,400)

# Step 5: Scale down thumb-scale image
sourcemap_thumb = "world.topo.200407.3x128x60.jpg"
scale_down(sourcemap_lowres,sourcemap_thumb,128)


for d in tile_defines:
	print d

