buildgraph: add options whether to draw the graph

This commit is contained in:
Laura Klünder 2016-12-04 14:03:11 +01:00
parent 01e6674cd0
commit 59143db553
3 changed files with 30 additions and 14 deletions

View file

@ -8,20 +8,21 @@ from c3nav.routing.graph.level import GraphLevel
class Graph():
def __init__(self):
self.levels = {}
for level in Level.objects.all():
self.levels[level.name] = GraphLevel(self, level)
self.connections = []
self.levelconnector_points = {}
def build(self):
for level in Level.objects.all():
self.levels[level.name] = GraphLevel(self, level)
for level in self.levels.values():
level.build()
self.connect_levelconnectors()
def draw_pngs(self, points=True, lines=True):
for level in self.levels.values():
level.draw_png()
level.draw_png(points=points, lines=lines)
def add_levelconnector_point(self, levelconnector, point):
self.levelconnector_points.setdefault(levelconnector.name, []).append(point)

View file

@ -74,7 +74,7 @@ class GraphLevel():
for room in self.rooms:
room.connect_points()
def draw_png(self):
def draw_png(self, points=True, lines=True):
filename = os.path.join(settings.RENDER_ROOT, 'level-%s.base.png' % self.level.name)
graph_filename = os.path.join(settings.RENDER_ROOT, 'level-%s.graph.png' % self.level.name)
@ -83,14 +83,17 @@ class GraphLevel():
draw = ImageDraw.Draw(im)
i = 0
for room in self.rooms:
for point in room.points:
for otherpoint, connection in point.connections.items():
draw.line(_line_coords(point, otherpoint, height), fill=(255, 100, 100))
if lines:
for point in room.points:
for otherpoint, connection in point.connections.items():
draw.line(_line_coords(point, otherpoint, height), fill=(255, 100, 100))
for point in room.points:
i += 1
draw.ellipse(_ellipse_bbox(point.x, point.y, height), (200, 0, 0))
if points:
for point in room.points:
i += 1
draw.ellipse(_ellipse_bbox(point.x, point.y, height), (200, 0, 0))
print(i, 'points')
if points:
print('level %s: %d points' % (self.level.name, i))
im.save(graph_filename)

View file

@ -6,6 +6,18 @@ from c3nav.routing.graph import Graph
class Command(BaseCommand):
help = 'build the routing graph'
def add_arguments(self, parser):
parser.add_argument('--draw-graph', action='store_const', const=True, default=False,
help='render a graph image')
parser.add_argument('--dont-draw-graph-points', action='store_const', const=True, default=False,
help='dont draw points on the graph image')
parser.add_argument('--dont-draw-graph-lines', action='store_const', const=True, default=False,
help='dont draw lines on the graph image')
def handle(self, *args, **options):
graphbuilder = Graph()
graphbuilder.build()
graph = Graph()
graph.build()
if options['draw_graph']:
graph.draw_pngs(points=not options['dont_draw_graph_points'], lines=not options['dont_draw_graph_lines'])