From c698c1d6c60db039a43a0d74c0f34746831f4467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Mon, 27 Nov 2017 13:21:53 +0100 Subject: [PATCH] save and load router --- src/c3nav/routing/router.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/c3nav/routing/router.py b/src/c3nav/routing/router.py index 9471928f..8c1cfd30 100644 --- a/src/c3nav/routing/router.py +++ b/src/c3nav/routing/router.py @@ -1,6 +1,9 @@ +import os +import pickle from collections import deque import numpy as np +from django.conf import settings from django.utils.functional import cached_property from shapely import prepared from shapely.geometry import Point @@ -10,10 +13,15 @@ from c3nav.mapdata.models import AltitudeArea, GraphEdge, Level, WayType class Router: - def __init__(self): - self.nodes = None - self.node_coords = None - self.node_lookup = None + filename = os.path.join(settings.CACHE_ROOT, 'router') + + def __init__(self, levels, spaces, areas, nodes, waytypes, graph): + self.levels = levels + self.spaces = spaces + self.areas = areas + self.nodes = nodes + self.waytypes = waytypes + self.graph = graph @staticmethod def get_altitude_in_areas(areas, point): @@ -21,8 +29,6 @@ class Router: @classmethod def build(cls): - graph = cls() - levels_query = Level.objects.prefetch_related('buildings', 'spaces', 'altitudeareas', 'spaces__holes', 'spaces__columns', 'spaces__obstacles', 'spaces__lineobstacles', @@ -123,6 +129,14 @@ class Router: waytype.upwards_indices = np.array(waytype.upwards_indices, dtype=np.uint32).reshape((-1, 2)) waytype.nonupwards_indices = np.array(waytype.nonupwards_indices, dtype=np.uint32).reshape((-1, 2)) + router = cls(levels, spaces, areas, nodes, waytypes, graph) + pickle.dump(router, open(cls.filename, 'wb')) + return router + + @classmethod + def load(cls): + return pickle.load(open(cls.filename, 'rb')) + class BaseRouterProxy: def __init__(self, src):