예제 #1
0
 def create_route(self, start_pt, target_pt):
     """
     Based on points passed through a HTTP request a route is calculated, geometry generated on the basis of it
     and persisted to the database.
     :param start_pt: the start point specified
     :param target_pt: the end point specified
     :return: the id of the generated and persisted route geometry back to the page which requested it
     """
     start_end = grid_utils.GridUtilities.start_end_points_to_cells(start_pt, target_pt)
     start_node = start_end[0]
     end_node = start_end[1]
     reverse_path = self.path_finder.find_path(start_node, end_node, self.gra.graph_dict)
     path = self.path_finder.rebuild_path(reverse_path[0], start_node, end_node)
     route_geoms = _generate_route_geom(path)
     start_pt_record = GridUtilities.find_cell_centre_projected(start_node, self.qs)
     end_pt_record = GridUtilities.find_cell_centre_projected(end_node, self.qs)
     route = TestRoutes(name='Route No {}'.format(random.randint(1, 100000)),
                        distance=round(route_geoms[0].length/1852, 2),
                        geom=route_geoms[0],
                        geom_4326=route_geoms[1],
                        start_geom=start_pt_record,
                        end_geom=end_pt_record)
     route.save()
     return route.gid
예제 #2
0
def _generate_route_geom(path):
    """
    Helper method which looks after the conversion of a list of geometry ids to a line
    which can be persisted in the database and displayed on screen
    :param path:
    :return: a linestring geometry representing the route calculated
    """
    waypoints_proj = []
    waypoints_geo = []
    for item in path:
        cell = PathGrid.objects.get(pk=item)
        centre_point_geo = GridUtilities.convert_to_latlon(cell.geom.centroid)
        centre_point_proj = cell.geom.centroid
        waypoints_proj.append(centre_point_proj)
        waypoints_geo.append(centre_point_geo)
    route_line_proj = LineString(waypoints_proj, srid=32616)
    route_line_geom = LineString(waypoints_geo, srid=4326)
    return route_line_proj, route_line_geom