Beispiel #1
0
 def get(self, request, file_name):
     open_space_c = OpenSpaceConfigController()
     geojson = open_space_c.load_geojson(file_name)
     if geojson:
         return Response(geojson,
                         status=status.HTTP_200_OK,
                         headers={'access-control-allow-origin': '*'})
     return Response(status=status.HTTP_400_BAD_REQUEST,
                     headers={'access-control-allow-origin': '*'})
Beispiel #2
0
 def __init__(self,
              open_space_config_dir=OPEN_SPACE_CONFIG_DIR,
              building_config_dir=BUILDING_CONFIG_DIR):
     self.graph = None
     self.current_osm_id = 0
     self.osp_config_c = OpenSpaceConfigController(open_space_config_dir)
     building_cc = BuildingConfigController(config_dir=building_config_dir)
     self.indoor_map_c = GraphBuildingController(self,
                                                 building_cc=building_cc)
Beispiel #3
0
 def get(self, request):
     open_space_c = OpenSpaceConfigController()
     files = [{
         "file_name": f
     } for f in open_space_c.get_open_spaces_files()]
     files.sort(key=lambda x: x['file_name'])
     results = FileNameSerializer(files, many=True).data
     return Response(results,
                     status=status.HTTP_200_OK,
                     headers={'access-control-allow-origin': '*'})
Beispiel #4
0
 def get(self, request, file_name):
     open_space_cc = OpenSpaceConfigController()
     open_space = open_space_cc.get_open_space(file_name)
     if open_space:
         building_c = BuildingConfigController()
         open_space.set_buildings(building_c.buildings)
         return Response(open_space.get_minimal_dict(),
                         status=status.HTTP_200_OK,
                         headers={'access-control-allow-origin': '*'})
     return Response(status=status.HTTP_400_BAD_REQUEST,
                     headers={'access-control-allow-origin': '*'})
Beispiel #5
0
    def test_open_space_coloring(self):
        config_controller = OpenSpaceConfigController(
            config_dir=f'{TEST_DIR}/config_controller/default')
        colored_geojson = config_controller.get_colored_geojson(
            'feki_wrong_colored.geojson')

        with open(f'{TEST_DIR}/config_controller/default/feki.geojson',
                  'r') as f:
            expected_colored_geojson = json.load(f)
        self.maxDiff = None
        self.assertEqual(colored_geojson['geojson'], expected_colored_geojson)
Beispiel #6
0
 def test_open_space_correct_loaded(self):
     entry_1 = EntryPoint([10.903171598911285, 49.907197803913526])
     entry_2 = EntryPoint([10.903244018554688, 49.90710452333503])
     expected_open_space = OpenSpace(
         'feki.geojson', [[10.90245008468628, 49.908267062844295],
                          [10.902847051620483, 49.90777821234866],
                          [10.90245008468628, 49.908267062844295]], [
                              [[10.903726816177384, 49.90669339567286],
                               [10.903995037078857, 49.906373819229145]],
                              [[10.905743837356567, 49.9067504009781],
                               [10.90479701757431, 49.90643773468725],
                               [10.905743837356567, 49.9067504009781]],
                          ], [[[10.904405415058136, 49.908685086546825],
                               [10.90417206287384, 49.90864190244783],
                               [10.904405415058136, 49.908685086546825]],
                              [[10.90440809726715, 49.90866954027564],
                               [10.90440809726715, 49.90866954027564]]],
         [entry_1, entry_2])
     config_controller = OpenSpaceConfigController(
         config_dir=f'{TEST_DIR}/config_controller/minimal')
     open_space = config_controller.get_open_space('feki.geojson')
     self.assertTrue(open_space)
     self.maxDiff = None
     self.assertEqual(open_space, expected_open_space)
Beispiel #7
0
 def handle(self, *args, **options):
     open_space_cc = OpenSpaceConfigController()
     open_space_cc.set_open_space_colors()
Beispiel #8
0
class OSMController:
    def __init__(self,
                 open_space_config_dir=OPEN_SPACE_CONFIG_DIR,
                 building_config_dir=BUILDING_CONFIG_DIR):
        self.graph = None
        self.current_osm_id = 0
        self.osp_config_c = OpenSpaceConfigController(open_space_config_dir)
        building_cc = BuildingConfigController(config_dir=building_config_dir)
        self.indoor_map_c = GraphBuildingController(self,
                                                    building_cc=building_cc)

    def create_bbox_open_spaces_plot(self, bbox=None):
        open_spaces = self.osp_config_c.get_open_spaces()
        buildings = self.indoor_map_c.building_cc.get_buildings()
        self.graph = self.download_map(bbox) if bbox else self.download_map()

        for open_space in open_spaces:
            if self._is_contained_open_space(open_space, bbox):
                graph_open_space = GraphOpenSpace(open_space, osmm=self)
                self._insert_open_space(buildings, graph_open_space)
        self.plot_graph()

    def create_complete_open_space_plot(self,
                                        open_space,
                                        output_dir="/osm_data",
                                        file_name=None):
        buildings = self.indoor_map_c.building_cc.get_buildings()
        graph_open_space = self._init_open_space_graph(open_space)
        self._insert_open_space(buildings, graph_open_space)
        self.plot_graph(
            output_dir=output_dir,
            file_name=file_name if file_name else graph_open_space.file_name,
            minimized=False)

    def download_map(self, bbox: BBox = BAMBERG_BBOX):
        return ox.graph_from_bbox(*bbox.get_bbox(), simplify=False)

    @staticmethod
    def load_map():
        return ox.graph_from_file(
            f'{OSM_OUTPUT_DIR}/{OSM_OUTPUT_FILENAME}.osm')

    def plot_graph(self, output_dir=None, file_name=None, minimized=True):
        if output_dir and file_name and not minimized:
            ox.plot_graph(self.graph,
                          save=True,
                          file_format='svg',
                          filename=f'{output_dir}/{file_name}',
                          edge_linewidth=0.5,
                          node_size=1)
        else:
            ox.plot_graph(self.graph,
                          save=True,
                          file_format='svg',
                          filename=f'{OSM_OUTPUT_DIR}/network_plot',
                          edge_linewidth=0.025,
                          node_size=0.1)

    def save_graph(self):
        ox.save_graph_osm(
            self.graph,
            filename=f'{OSM_OUTPUT_FILENAME}.osm',
            folder=OSM_OUTPUT_DIR,
            # oneway=False,
        )
        logger.info(
            f'Saved osm xml to {OSM_OUTPUT_DIR}/{OSM_OUTPUT_FILENAME}.osm')

    def add_osm_edge(self,
                     from_id,
                     to_id,
                     name,
                     maxspeed=None,
                     type=EDGE_TYPES.NORMAL):
        if maxspeed:
            self.graph.add_edge(from_id,
                                to_id,
                                highway='pedestrian',
                                lanes='1',
                                name=name,
                                oneway=True,
                                maxspeed=maxspeed,
                                color_label=type)
        else:
            self.graph.add_edge(from_id,
                                to_id,
                                highway='pedestrian',
                                lanes='1',
                                name=name,
                                oneway=True,
                                color_label=type)

    def add_osm_node(self, coords: List[List[float]]):
        node_id = self._get_new_node_id()
        self.graph.add_node(node_id, osmid=node_id, x=coords[0], y=coords[1])
        return node_id

    def _get_new_node_id(self) -> int:
        self.current_osm_id += 1
        return self.current_osm_id

    def get_nearest_edge(self, coord):
        return ox.get_nearest_edge(self.graph, coord[::-1])

    def get_coord_from_id(self, node_id):
        node = self.graph.node[node_id]
        return [node['x'], node['y']]

    def _insert_building_to_graph(self, graph_open_space):
        graph_open_space.add_walkable_edges()
        graph_open_space.add_restricted_area_edges()
        graph_buildings = self.indoor_map_c.get_graph_buildings(
            graph_open_space.buildings)
        logger.warn(graph_buildings)
        self.indoor_map_c.add_buildings_to_graph(graph_buildings)
        graph_open_space.remove_walkable_edges()
        graph_open_space.remove_restricted_area_edges()
        self.plot_graph(
            output_dir="/osm_data",
            file_name=f'{graph_open_space.file_name}_building_without_entry',
            minimized=False)
        for building in graph_buildings:
            for staircase in building.graph_staircases:
                for entry in staircase.get_not_blocked_entries():
                    graph_open_space.add_building_entry_to_open_space(entry)
        self.plot_graph(output_dir="/osm_data",
                        file_name=f'{graph_open_space.file_name}_building',
                        minimized=False)

    def _insert_open_space(self, buildings: List[Building],
                           graph_open_space: GraphOpenSpace):
        self._instert_open_space_buildings(buildings, graph_open_space)
        self._insert_open_space_visibility_graph(graph_open_space)
        self._insert_open_space_entries(graph_open_space)

    def _instert_open_space_buildings(self, buildings: List[Building],
                                      graph_open_space: GraphOpenSpace):
        graph_open_space.set_buildings(buildings)
        self._insert_building_to_graph(graph_open_space)

    def _insert_open_space_walkable(self, graph_open_space: GraphOpenSpace):
        graph_open_space.add_walkable_edges()

    def _insert_open_space_restricted(self, graph_open_space: GraphOpenSpace):
        graph_open_space.add_restricted_area_edges()

    def _insert_open_space_visibility_graph(self,
                                            graph_open_space: GraphOpenSpace):
        graph_open_space.add_visibility_graph_edges()

    def _insert_open_space_entries(self, graph_open_space: GraphOpenSpace):
        graph_open_space.add_graph_entry_points()

    def create_open_space_walkable_plot(self,
                                        open_space,
                                        output_dir="/osm_data",
                                        file_name=None):
        graph_open_space = self._init_open_space_graph(open_space)
        self._insert_open_space_walkable(graph_open_space)
        self.plot_graph(
            output_dir=output_dir,
            file_name=file_name if file_name else graph_open_space.file_name,
            minimized=False)

    def create_open_space_restricted_plot(self,
                                          open_space,
                                          output_dir="/osm_data",
                                          file_name=None):
        graph_open_space = self._init_open_space_graph(open_space)
        self._insert_open_space_restricted(graph_open_space)
        self.plot_graph(
            output_dir=output_dir,
            file_name=file_name if file_name else graph_open_space.file_name,
            minimized=False)

    def create_open_space_visibility_graph_plot(self,
                                                open_space,
                                                output_dir="/osm_data",
                                                file_name=None):
        graph_open_space = self._init_open_space_graph(open_space)
        self._insert_open_space_visibility_graph(graph_open_space)
        self.plot_graph(
            output_dir=output_dir,
            file_name=file_name if file_name else graph_open_space.file_name,
            minimized=False)

    def create_open_space_entries_plot(self,
                                       open_space,
                                       output_dir="/osm_data",
                                       file_name=None):
        graph_open_space = self._init_open_space_graph(open_space)
        self._insert_open_space_entries(graph_open_space)
        self.plot_graph(
            output_dir=output_dir,
            file_name=file_name if file_name else graph_open_space.file_name,
            minimized=False)

    def create_open_space_walkable_restricted_plot(self,
                                                   open_space,
                                                   output_dir="/osm_data",
                                                   file_name=None):
        graph_open_space = self._init_open_space_graph(open_space)
        self._insert_open_space_walkable(graph_open_space)
        self._insert_open_space_restricted(graph_open_space)
        self.plot_graph(
            output_dir=output_dir,
            file_name=file_name if file_name else graph_open_space.file_name,
            minimized=False)

    def create_open_space_walkable_restricted_entries_plot(
            self, open_space, output_dir="/osm_data", file_name=None):
        graph_open_space = self._init_open_space_graph(open_space)
        self._insert_open_space_walkable(graph_open_space)
        self._insert_open_space_restricted(graph_open_space)
        self._insert_open_space_entries(graph_open_space)
        self.plot_graph(
            output_dir=output_dir,
            file_name=file_name if file_name else graph_open_space.file_name,
            minimized=False)

    def create_open_space_buildings_plot(self,
                                         open_space: OpenSpace,
                                         buildings: [],
                                         output_dir="/osm_data",
                                         file_name=None):
        graph_open_space = self._init_open_space_graph(open_space)
        self._instert_open_space_buildings(buildings, graph_open_space)
        self._insert_open_space_walkable(graph_open_space)
        self._insert_open_space_restricted(graph_open_space)
        self._insert_open_space_entries(graph_open_space)
        self.plot_graph(
            output_dir=output_dir,
            file_name=file_name if file_name else graph_open_space.file_name,
            minimized=False)

    def _init_open_space_graph(self, open_space):
        self.graph = self.download_map(
            open_space.get_boundaries(boundary_degree_extension=0.0005))
        graph_open_space = GraphOpenSpace(open_space, osmm=self)
        return graph_open_space

    def _is_contained_open_space(self, open_space: OpenSpace, bbox: BBox):
        open_space_bbox = open_space.get_boundaries()
        return bbox.min_lat < open_space_bbox.min_lat \
               and bbox.min_lon < open_space_bbox.min_lon \
               and bbox.max_lat > open_space_bbox.max_lat \
               and bbox.max_lon > open_space_bbox.max_lon
Beispiel #9
0
 def test_default_config_file(self):
     config_controller = OpenSpaceConfigController(
         config_dir=f'{TEST_DIR}/config_controller/default')
     self.assertEqual(len(config_controller.open_spaces), 2)
Beispiel #10
0
 def test_multiple_walkables_config_file(self):
     config_controller = OpenSpaceConfigController(
         config_dir=f'{TEST_DIR}/config_controller/multiple_walkable')
     self.assertEqual(len(config_controller.open_spaces), 0)
Beispiel #11
0
 def test_empty_config_file(self):
     config_controller = OpenSpaceConfigController(
         config_dir=f'{TEST_DIR}/config_controller/empty')
     self.assertEqual(len(config_controller.open_spaces), 0)