class LabelsLayer(BaseLayer): def __init__(self, data, label_column, color=None, font_name=FONT_NAME, font_size=14, anchor_x='left', anchor_y='top'): """Create a layer with a text label for each sample :param data: data access object :param label_column: column in the data access object where the labels text is stored :param color: color :param font_name: font name :param font_size: font size :param anchor_x: anchor x :param anchor_y: anchor y """ self.data = data self.label_column = label_column self.color = color self.font_name = font_name self.font_size = font_size self.anchor_x = anchor_x self.anchor_y = anchor_y if self.color is None: self.color = [255, 0, 0] def invalidate(self, proj): self.painter = BatchPainter() x, y = proj.lonlat_to_screen(self.data['lon'], self.data['lat']) self.painter.set_color(self.color) self.painter.labels(x, y, self.data[self.label_column], font_name=self.font_name, font_size=self.font_size, anchor_x=self.anchor_x, anchor_y=self.anchor_y) def draw(self, proj, mouse_x, mouse_y, ui_manager): self.painter.batch_draw() def bbox(self): return BoundingBox.from_points(lons=self.data['lon'], lats=self.data['lat'])
class LabelsLayer(BaseLayer): def __init__(self, data, label_column, color=None, font_name=FONT_NAME, font_size=14, anchor_x='left', anchor_y='top'): """Create a layer with a text label for each sample :param data: data access object :param label_column: column in the data access object where the labels text is stored :param color: color :param font_name: font name :param font_size: font size :param anchor_x: anchor x :param anchor_y: anchor y """ self.data = data self.label_column = label_column self.color = color self.font_name = font_name self.font_size = font_size self.anchor_x = anchor_x self.anchor_y = anchor_y if self.color is None: self.color = [255,0,0] def invalidate(self, proj): self.painter = BatchPainter() x, y = proj.lonlat_to_screen(self.data['lon'], self.data['lat']) self.painter.set_color(self.color) self.painter.labels(x, y, self.data[self.label_column], font_name=self.font_name, font_size=self.font_size, anchor_x=self.anchor_x, anchor_y=self.anchor_y) def draw(self, proj, mouse_x, mouse_y, ui_manager): self.painter.batch_draw() def bbox(self): return BoundingBox.from_points(lons=self.data['lon'], lats=self.data['lat'])
class AnimatedProcess(BaseLayer): def __init__(self, network, edgelists, show_addresses=False, save_frames=False, line_width=1.3): # Set network and edge lists. self.network = network self.edgelists = edgelists self.num_frames = len(self.edgelists) # Set flag specifying whether to show addresses. self.show_addresses = show_addresses # Set flag specifying whether to save frames. self.save_frames = save_frames # Set line width. self.line_width = line_width # Initialize state counter. self.count = 0 # Initialize geolocator for retrieving addresses. geolocator = Nominatim(user_agent='test') # Set coordinates of nodes and get addresses. self.node_coordinates = np.empty((2, network.number_of_nodes()), dtype=float) self.node_addresses = [] for idx, node in enumerate(network.nodes()): self.node_coordinates[:, idx] = np.array(self.network.nodes[node]['latlon']) if self.show_addresses: address = geolocator.reverse(self.node_coordinates[:, idx]).address self.node_addresses.append(address[:address.index(',', address.index(',') + 1)]) def invalidate(self, proj): self.x, self.y = proj.lonlat_to_screen(self.node_coordinates[1, :], self.node_coordinates[0, :]) def draw(self, proj, mouse_x, mouse_y, ui_manager): # Prepare edges for next frame. self.edge_src = np.empty((2, len(self.edgelists[self.count])), dtype=float) self.edge_dst = np.empty((2, len(self.edgelists[self.count])), dtype=float) for idx, edge in enumerate(self.edgelists[self.count]): self.edge_src[:, idx] = self.network.nodes[edge[0]]['latlon'] self.edge_dst[:, idx] = self.network.nodes[edge[1]]['latlon'] self.edge_src_trans_x, self.edge_src_trans_y = proj.lonlat_to_screen(self.edge_src[1, :], self.edge_src[0, :]) self.edge_dst_trans_x, self.edge_dst_trans_y = proj.lonlat_to_screen(self.edge_dst[1, :], self.edge_dst[0, :]) # Initialize painter, plot nodes and addresses. self.painter = BatchPainter() self.painter.points(self.x, self.y, point_size=10, rounded=True) if self.show_addresses: self.painter.labels(self.x, self.y, self.node_addresses, font_size=10, anchor_x='left') # Plot edges. self.painter.set_color([255, 0, 0]) self.painter.lines(self.edge_src_trans_x, self.edge_src_trans_y, self.edge_dst_trans_x, self.edge_dst_trans_y, width=self.line_width) # Draw and increment counter. self.painter.batch_draw() if self.count < len(self.edgelists) - 1: self.count += 1 self.count = self.count % self.num_frames print("count: {0}/{1}".format(self.count, self.num_frames)) # If saving frames. if self.save_frames: GeoplotlibApp.screenshot(f'./results/animation_frames/{self.count}.png')