PythonScripts: gen_terrain_image_script.py

File gen_terrain_image_script.py, 4.7 KB (added by Uriel, 7 years ago)

Use Maperitive to generate the entire world (or a portion) as geo-referenced images.

Line 
1'''
2First step in generating a terrain script of the entire world
3Assumes you have the SRTM tiles of the entire world
4
5Instead of loading all tiles and making a map,
6writes a geo-referenced image per slice.
7
8Use gen_tiles_from_images.py to convert these images to web tiles.
9Use convert_georef.py to convert Maperitive's georef format to GDAL's
10
11'''
12import os
13import argparse
14
15#defaults which can be modified via command-line params
16images_dir = "G:\\Images"
17maperitive_path = "G:\\Maperitive\\"
18script_file = "gen_images.mscript"
19
20#These are the full bounds of the SRTM data
21map_bounds = {
22    'W':-180,
23    'E':180,
24    'S':-56,
25    'N':59,
26}
27
28script_head = '''
29// SCRIPT TO GENERATE WORLD TERRAIN SHADING GEOREFERENCED IMAGES
30// Expects {src} data in Maperitive/Cache/Rasters/{src}
31// Will output images to {idir}
32
33set-dem-source name={src}
34'''
35
36#To create tiles from these images, uncomment these lines and comment
37#the clear-map call in script_gen.
38#NOTE: This will only work if the images are small enough to not fill up
39#   your RAM! Try a large sample rate, such as 4 or 6.
40script_foot = '''
41//set-geo-bounds -180,-56,179.999999,60
42//generate-tiles exclude-partial=true minzoom=1 maxzoom=7 tilesdir=g:\lowtiles
43'''
44
45script_gen = '''
46//Tile {tnum} of {ttot}
47clear-map
48set-geo-bounds {W},{S},{E},{N}
49generate-relief-igor color="black" intensity=3 sample-rate={rate}
50//generate-contours interval=10
51save-source index=1 file={fname}
52'''
53
54def gen_bounds(args):
55    #generate the maperitive script metadata
56    print "Generating script starting at slice", args.index
57
58    #Break the map into chunks, stepping an input number of degrees
59    EW_step = args.step #longitude
60    NS_step = args.step #latitude
61
62    EW_range = range(map_bounds['W'], map_bounds['E'], EW_step)
63    NS_range = range(map_bounds['S'], map_bounds['N'], NS_step)
64
65    if args.srtm3:
66        mem = EW_step * NS_step * (1201*1201*2)
67    else:
68        mem = EW_step * NS_step * (3601*3601*2)
69    total = len(EW_range) * len(NS_range)
70    print "Divided world into {} slices ({:,} bytes each)".format(total,mem)
71
72    metadata = []
73    num = 1
74    for EW in EW_range:
75        for NS in NS_range:
76            bounds = {
77                'tnum': num,
78                'ttot': total,
79                'W':max(map_bounds['W'], EW),
80                'E':min(map_bounds['E'], EW + EW_step),
81                'S':max(map_bounds['S'], NS),
82                'N':min(map_bounds['N'], NS + NS_step),               
83            }
84
85            if num == args.index:
86                print "Start tile #{tnum}: {W},{S},{E},{N}".format(**bounds)
87            if num >= args.index:
88                metadata.append(bounds)
89
90            num += 1
91    return metadata
92
93def gen_script(data, args):
94    # Generate the maperitive script
95    source = "SRTM3" if args.srtm3 else "SRTM1"
96    with open(os.path.join(args.mdir, "Scripts", args.script), "w") as F:
97        F.write(script_head.format(src=source, idir=args.idir))
98
99        for elem in data:
100            Fname = "tile_%05d.%s"%(elem['tnum'], args.format)
101            F.write(script_gen.format(fname=os.path.join(args.idir, Fname), rate=args.rate, **elem))
102
103        F.write(script_foot)
104
105def main(args):
106    if not os.path.exists(args.idir):
107        os.mkdir(args.idir)
108
109    data = gen_bounds(args)
110    gen_script(data, args)
111
112if __name__ == '__main__':
113    P = argparse.ArgumentParser(
114        description="Generates a maperitive script to create a set of georeferenced images of the world's terrain",
115        epilog="To create a good set of world data, run with: --srtm3 -s 15 -r 6")
116    P.add_argument("-i", "--index", type=int, default=1,
117         help="Index to start at (helpful to continue a failed run where you left off)")
118    P.add_argument("-s", "--step", type=int, default=15,
119         help="Degrees to step per tile")   
120    P.add_argument("-r", "--rate", type=int, default=6,
121         help="Sample rate of hillshading (higher makes a smaller image)")
122    P.add_argument("-f", "--format", choices=["png", "tif", "jpg"], default="png",
123        help="Image format to write")
124    P.add_argument("--srtm3", action="store_true",
125        help="Use the lower-resolution SRTMGL3 data instead of GL1")
126    P.add_argument("--mdir", default=maperitive_path,
127        help="Path to the Maperitive folder to write the script to. Default: %s"%maperitive_path)
128    P.add_argument("--idir", default=images_dir,
129        help="Path to output the generated tiles. Default: %s"%images_dir)
130    P.add_argument("--script", default=script_file,
131        help="File name of the script to generate. Default: %s"%script_file)
132    args = P.parse_args()
133
134    main(args)