caumarshal.py
changeset 3 89d76549ba6e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/caumarshal.py	Thu Dec 30 14:22:05 2010 +0100
     1.3 @@ -0,0 +1,37 @@
     1.4 +import ConfigParser
     1.5 +from caugrid import Grid
     1.6 +
     1.7 +meta_section = "meta"
     1.8 +data_section = "cells"
     1.9 +
    1.10 +def dumpGrid(grid, filename):
    1.11 +	config = ConfigParser.RawConfigParser()
    1.12 +
    1.13 +	config.add_section(meta_section)
    1.14 +	config.set(meta_section, 'min x', grid.minx)
    1.15 +	config.set(meta_section, 'max x', grid.maxx)
    1.16 +	config.set(meta_section, 'min y', grid.miny)
    1.17 +	config.set(meta_section, 'max y', grid.maxy)
    1.18 +	config.set(meta_section, 'cell no', len(grid))	
    1.19 +	
    1.20 +	config.add_section(data_section)
    1.21 +	config.set(data_section, "cells", ";".join([str(pos) for pos in grid.cells.iterkeys()]))
    1.22 +
    1.23 +	with open(filename, 'wb') as configfile:
    1.24 +		config.write(configfile)
    1.25 +
    1.26 +def loadGrid(filename):
    1.27 +	config = ConfigParser.RawConfigParser()
    1.28 +	config.read(filename)
    1.29 +
    1.30 +	grid = Grid()
    1.31 +	grid.minx = config.getint(meta_section, "min x")
    1.32 +	grid.maxx = config.getint(meta_section, "max x")
    1.33 +	grid.miny = config.getint(meta_section, "min y")
    1.34 +	grid.maxy = config.getint(meta_section, "max y")
    1.35 +
    1.36 +	cells = config.get(data_section, "cells")
    1.37 +	for cell in cells.split(";"):
    1.38 +		cell = [int(cell.strip("(),")) for cell in cell.split()]
    1.39 +		grid.cells[(cell[0], cell[1])] = 1
    1.40 +	return grid