def add_street(self, street_name, coor): contained = False for i in self.__vertex_list: if (street_name == i.get_street()): contained = True break if (not contained): length = len(coor) / 2 templen = len(self.__vertex_list) for i in range(0, length): new_ver = Vertex() new_ver.create_ID() new_ver.set_street(street_name) new_ver.set_xcoor(coor[2 * i]) new_ver.set_ycoor(coor[2 * i + 1]) self.__vertex_list.append(new_ver) for i in range(0, length - 1): new_edge = Edge() new_edge.set_street(street_name) j = i + templen new_edge.set_vertex1(self.__vertex_list[j]) new_edge.set_vertex2(self.__vertex_list[j + 1]) self.__edges_list.append(new_edge) else: sys.stderr.write("Error: street has already been added \n") return
def __format_edges(self): formatted_edge_list = [] for i in range(0, len(self.__updated_edges_list)): for j in range(0, len(self.__updated_vertex_list)): if (self.__updated_edges_list[i].get_vertex1().get_ID() == self.__updated_vertex_list[j].get_ID()): new_vertex1 = Vertex() new_vertex1.set_ID(j) break for j in range(0, len(self.__updated_vertex_list)): if (self.__updated_edges_list[i].get_vertex2().get_ID() == self.__updated_vertex_list[j].get_ID()): new_vertex2 = Vertex() new_vertex2.set_ID(j) break formatted_edge = Edge() formatted_edge.set_vertex1(new_vertex1) formatted_edge.set_vertex2(new_vertex2) formatted_edge_list.append(formatted_edge) return formatted_edge_list
def __generate_graph(self): self.__updated_vertex_list[:] = [] self.__updated_edges_list[:] = [] for item in self.__vertex_list: self.__updated_vertex_list.append(item) i = 0 while i < len(self.__edges_list) - 1: j = i + 1 while j < len(self.__edges_list): if (self.__edges_list[i].get_street() != self.__edges_list[j].get_street()): intersection = self.__intersect(self.__edges_list[i], self.__edges_list[j]) if (intersection != "No intersection"): self.__updated_vertex_list.append(intersection) self.intersections.append(intersection) if (len(self.__updated_vertex_list) > 20): break edge1 = Edge() edge1.set_street(self.__edges_list[i].get_street()) edge1.set_vertex1(self.__edges_list[i].get_vertex1()) edge1.set_vertex2(intersection) edge2 = Edge() edge2.set_street(self.__edges_list[i].get_street()) edge2.set_vertex1(intersection) edge2.set_vertex2(self.__edges_list[i].get_vertex2()) edge3 = Edge() edge3.set_street(self.__edges_list[j].get_street()) edge3.set_vertex1(self.__edges_list[j].get_vertex1()) edge3.set_vertex2(intersection) edge4 = Edge() edge4.set_street(self.__edges_list[j].get_street()) edge4.set_vertex1(intersection) edge4.set_vertex2(self.__edges_list[j].get_vertex2()) self.__updated_edges_list.append(edge1) self.__updated_edges_list.append(edge2) self.__updated_edges_list.append(edge3) self.__updated_edges_list.append(edge4) j += 1 if (len(self.__updated_vertex_list) > 20): break i += 1 if (len(self.__updated_vertex_list) > 20): self.__updated_vertex_list[:] = [] return self.__rem_edge_duplicates() self.__rem_vertex_duplicates() self.__rem_unneeded_vertex() self.__fix_edges() self.__rem_joined_edges() return
def __fix_edges(self): to_pop = [] temp_edges = [] i = 0 while i < len(self.__updated_edges_list) - 1: j = i + 1 while j < len(self.__updated_edges_list): if (not self.__overlapping(self.__updated_edges_list[i], self.__updated_edges_list[j])): temp_edges.append(self.__updated_edges_list[i]) temp_edges.append(self.__updated_edges_list[j]) else: x3 = float( self.__updated_edges_list[j].get_vertex1().get_xcoor()) x4 = float( self.__updated_edges_list[j].get_vertex2().get_xcoor()) y3 = float( self.__updated_edges_list[j].get_vertex1().get_ycoor()) y4 = float( self.__updated_edges_list[j].get_vertex2().get_ycoor()) x1 = float( self.__updated_edges_list[i].get_vertex1().get_xcoor()) x2 = float( self.__updated_edges_list[i].get_vertex2().get_xcoor()) y1 = float( self.__updated_edges_list[i].get_vertex1().get_ycoor()) y2 = float( self.__updated_edges_list[i].get_vertex2().get_ycoor()) if (min(x3, x4) <= x1 <= max(x3, x4) and not (min(y3, y4) <= y1 <= max(y3, y4)) and min(x3, x4) <= x2 <= max(x3, x4) and min(y3, y4) < y2 < max(y3, y4)): edge = Edge() edge.set_street( self.__updated_edges_list[i].get_street()) edge.set_vertex1( self.__updated_edges_list[i].get_vertex2()) edge.set_vertex2( self.__updated_edges_list[j].get_vertex1()) temp_edges.append(edge) edge1_to_pop = Edge() edge1_to_pop.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge1_to_pop.set_vertex2( self.__updated_edges_list[i].get_vertex2()) to_pop.append(edge1_to_pop) edge2_to_pop = Edge() edge2_to_pop.set_vertex1( self.__updated_edges_list[j].get_vertex1()) edge2_to_pop.set_vertex2( self.__updated_edges_list[j].get_vertex2()) to_pop.append(edge2_to_pop) elif (min(x1, x2) <= x3 <= max(x1, x2) and not (min(y1, y2) <= y3 <= max(y1, y2)) and min(x1, x2) <= x4 <= max(x1, x2) and min(y1, y2) < y4 < max(y1, y2)): edge = Edge() edge.set_street( self.__updated_edges_list[i].get_street()) edge.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge.set_vertex2( self.__updated_edges_list[j].get_vertex2()) temp_edges.append(edge) edge1_to_pop = Edge() edge1_to_pop.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge1_to_pop.set_vertex2( self.__updated_edges_list[i].get_vertex2()) to_pop.append(edge1_to_pop) edge2_to_pop = Edge() edge2_to_pop.set_vertex1( self.__updated_edges_list[j].get_vertex1()) edge2_to_pop.set_vertex2( self.__updated_edges_list[j].get_vertex2()) to_pop.append(edge2_to_pop) elif (min(x1, x2) < x3 < max(x1, x2) and min(y1, y2) <= y3 <= max(y1, y2) and not (min(x1, x2) <= x4 <= max(x1, x2)) and min(y1, y2) <= y4 <= max(y1, y2)): edge = Edge() edge.set_street( self.__updated_edges_list[i].get_street()) edge.set_vertex1( self.__updated_edges_list[j].get_vertex1()) edge.set_vertex2( self.__updated_edges_list[i].get_vertex2()) temp_edges.append(edge) edge1_to_pop = Edge() edge1_to_pop.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge1_to_pop.set_vertex2( self.__updated_edges_list[i].get_vertex2()) to_pop.append(edge1_to_pop) edge2_to_pop = Edge() edge2_to_pop.set_vertex1( self.__updated_edges_list[j].get_vertex1()) edge2_to_pop.set_vertex2( self.__updated_edges_list[j].get_vertex2()) to_pop.append(edge2_to_pop) elif (min(x3, x4) < x1 < max(x3, x4) and min(y3, y4) <= y1 <= max(y3, y4) and not (min(x3, x4) <= x2 <= max(x3, x4)) and min(y3, y4) <= y2 <= max(y3, y4)): edge = Edge() edge.set_street( self.__updated_edges_list[i].get_street()) edge.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge.set_vertex2( self.__updated_edges_list[j].get_vertex2()) temp_edges.append(edge) edge1_to_pop = Edge() edge1_to_pop.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge1_to_pop.set_vertex2( self.__updated_edges_list[i].get_vertex2()) to_pop.append(edge1_to_pop) edge2_to_pop = Edge() edge2_to_pop.set_vertex1( self.__updated_edges_list[j].get_vertex1()) edge2_to_pop.set_vertex2( self.__updated_edges_list[j].get_vertex2()) to_pop.append(edge2_to_pop) elif (min(x1, x2) < x3 < max(x1, x2) and min(y1, y2) < y3 < max(y1, y2) and not (min(x1, x2) <= x4 <= max(x1, x2)) and not (min(y1, y2) <= y4 <= max(y1, y2))): edge = Edge() edge.set_street( self.__updated_edges_list[i].get_street()) edge.set_vertex1( self.__updated_edges_list[j].get_vertex1()) edge.set_vertex2( self.__updated_edges_list[i].get_vertex2()) temp_edges.append(edge) edge1_to_pop = Edge() edge1_to_pop.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge1_to_pop.set_vertex2( self.__updated_edges_list[i].get_vertex2()) to_pop.append(edge1_to_pop) edge2_to_pop = Edge() edge2_to_pop.set_vertex1( self.__updated_edges_list[j].get_vertex1()) edge2_to_pop.set_vertex2( self.__updated_edges_list[j].get_vertex2()) to_pop.append(edge2_to_pop) elif (min(x3, x4) < x1 < max(x3, x4) and min(y3, y4) < y1 < max(y3, y4) and not (min(x3, x4) <= x2 <= max(x3, x4)) and not (min(y3, y4) <= y2 <= max(y3, y4))): edge = Edge() edge.set_street( self.__updated_edges_list[i].get_street()) edge.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge.set_vertex2( self.__updated_edges_list[j].get_vertex2()) temp_edges.append(edge) edge1_to_pop = Edge() edge1_to_pop.set_vertex1( self.__updated_edges_list[i].get_vertex1()) edge1_to_pop.set_vertex2( self.__updated_edges_list[i].get_vertex2()) to_pop.append(edge1_to_pop) edge2_to_pop = Edge() edge2_to_pop.set_vertex1( self.__updated_edges_list[j].get_vertex1()) edge2_to_pop.set_vertex2( self.__updated_edges_list[j].get_vertex2()) to_pop.append(edge2_to_pop) j += 1 i += 1 self.__updated_edges_list[:] = [] for item in temp_edges: self.__updated_edges_list.append(item) self.__rem_edge_duplicates() k = 0 while k < len(to_pop): l = 0 while l < len(self.__updated_edges_list): if (to_pop[k].get_vertex1().get_ID() == self.__updated_edges_list[l].get_vertex1().get_ID() and to_pop[k].get_vertex2().get_ID() == self.__updated_edges_list[l].get_vertex2().get_ID()): self.__updated_edges_list.pop(l) l += 1 k += 1 return