0

I believe I now have a usable program again

This commit is contained in:
Andrew Brown
2010-09-14 23:51:05 -04:00
parent 230e6ad480
commit 2d4f0cc082
4 changed files with 31 additions and 396 deletions

62
gmap.py
View File

@@ -5,17 +5,22 @@ import sys
import os.path
from optparse import OptionParser
import re
import multiprocessing
import world
import quadtree
helptext = """
%prog [-p PROCS] <Path to World> <tiles dest dir>
"""
def main():
try:
cpus = multiprocessing.cpu_count()
except NotImplementedError:
cpus = 1
parser = OptionParser(usage=helptext)
parser.add_option("-p", "--processes", dest="procs", help="How many chunks to render in parallel. A good number for this is 1 more than the number of cores in your computer. Default 2", default=2, action="store", type="int")
parser.add_option("-c", "--cachelife", dest="cachelife", help="How many minutes a tile will be considered valid by the web browser before it fetches a new copy. Used if you have a crontab or similar running this every once in a while. Default is no expiration.", default=0, action="store", type="int")
parser.add_option("-p", "--processes", dest="procs", help="How many chunks to render in parallel. A good number for this is the number of cores in your computer. Default %s" % cpus, default=cpus, action="store", type="int")
options, args = parser.parse_args()
@@ -29,54 +34,13 @@ def main():
parser.error("Where do you want to save the tiles?")
destdir = args[1]
print "Scanning chunks"
all_chunks = world.find_chunkfiles(worlddir)
# First generate the world's chunk images
w = world.WorldRenderer(worlddir)
w.go(options.procs)
# Translate chunks from diagonal coordinate system
mincol, maxcol, minrow, maxrow, chunks = world.convert_coords(all_chunks)
print "Rendering chunks"
results = world.render_chunks_async(chunks, False, options.procs)
if options.procs > 1:
for i, (col, row, filename) in enumerate(chunks):
if i > 0:
if 1000 % i == 0 or i % 1000 == 0:
print "{0}/{1} chunks rendered".format(i, len(chunks))
results['pool'].join()
print "Done"
# Compat-change for windows (which can't pass result objects to
# subprocesses)
chunkmap = {}
for col, row, filename in chunks:
chunkmap[col, row] = results[col, row].get()
del results
print "Writing out html file"
if not os.path.exists(destdir):
os.mkdir(destdir)
zoom = world.get_quadtree_depth(mincol, maxcol, minrow, maxrow)
write_html(destdir, zoom+1, options.cachelife)
print "Your map will have {0} zoom levels".format(zoom+1)
print "Generating quad tree. This may take a while and has no progress bar right now, so sit tight."
tiledir = os.path.join(destdir, "tiles")
if not os.path.exists(tiledir):
os.mkdir(tiledir)
world.generate_quadtree(chunkmap, mincol, maxcol, minrow, maxrow, tiledir, options.procs)
print "DONE"
def write_html(path, zoomlevel, cachelife):
templatepath = os.path.join(os.path.split(__file__)[0], "template.html")
html = open(templatepath, 'r').read()
html = html.replace(
"{maxzoom}", str(zoomlevel))
html = html.replace(
"{cachelife}", str(cachelife))
with open(os.path.join(path, "index.html"), 'w') as output:
output.write(html)
# Now generate the tiles
q = quadtree.QuadtreeGen(w, destdir)
q.go(options.procs)
if __name__ == "__main__":
main()