PythonScripts: gen_tiles_from_images_script.py

File gen_tiles_from_images_script.py, 2.3 KB (added by Uriel, 7 years ago)

Converts a set of geo-referenced images to a set of web tiles.

Line 
1'''
2Generates web tiles from a set of geo-reference images.
3Intended to be used as a step following a run of gen_terrain_image_script.py
4'''
5
6import os
7import argparse
8
9images_dir = 'G:\\Images'
10tiles_dir = 'G:\\TilesOut'
11maperitive_path = "G:\\Maperitive\\"
12script_file = "gen_image_tiles.mscript"
13
14def gen(args):
15    sfile = os.path.join(args.mdir, "Scripts", args.script)
16    out = 'use-ruleset alias=default\nclear-map\n'
17
18    files = os.listdir(args.idir)
19    files = [f for f in files if f.lower().endswith(args.format.lower())]
20
21    if len(files) == 0:
22        print "No files found. Did you set the correct format with -f {png|tif|jpg}?"
23    else:
24        print "Found %d images to load."%(len(files))       
25
26    for f in files:
27        out += "load-source \"%s\"\n"%(os.path.join(args.idir, f))
28    out += 'set-geo-bounds -180,-56,179.999999,60\n'
29    out += 'generate-tiles minzoom=%d maxzoom=%d tilesdir=%s'%(args.minzoom, args.maxzoom, args.tdir)
30
31    with open(sfile, "w") as F:
32        F.write(out)
33
34    print "Wrote script file:", sfile
35
36def main(args):
37    if not os.path.exists(args.tdir):
38        os.mkdir(args.tdir)
39
40    gen(args)
41
42if __name__ == '__main__':
43    P = argparse.ArgumentParser(
44        description="Generates a maperitive script to load a folder of georeferenced images and create web tiles from them")
45    P.add_argument("idir", default=images_dir,
46        help="Path of the input images. Default: %s"%images_dir)
47    P.add_argument("tdir", default=tiles_dir,
48        help="Path of the output tiles. Default: %s"%tiles_dir)   
49    P.add_argument("-f", "--format", choices=["png", "tif", "jpg"], default="png",
50        help="Image format to write")
51    P.add_argument("-n", "--minzoom", type=int, default=1,
52        help="Minimum zoom level to generate (default 1)")
53    P.add_argument("-x", "--maxzoom", type=int, default=7,
54        help="Maximum zoom level to generate (default 7)")   
55    P.add_argument("--mdir", default=maperitive_path,
56        help="Path to the Maperitive folder to write the script to. Default: %s"%maperitive_path)
57    P.add_argument("--script", default=script_file,
58        help="File name of the script to generate. Default: %s"%script_file)
59    args = P.parse_args()
60
61    main(args)