class KMeansLayer(BaseLayer): def __init__(self, data): self.data = data self.k = 2 def invalidate(self, proj): self.painter = BatchPainter() x, y = proj.lonlat_to_screen(self.data['lon'], self.data['lat']) k_means = KMeans(n_clusters=self.k) k_means.fit(np.vstack([x, y]).T) labels = k_means.labels_ self.cmap = create_set_cmap(set(labels), 'hsv') for l in set(labels): self.painter.set_color(self.cmap[l]) self.painter.convexhull(x[labels == l], y[labels == l]) self.painter.points(x[labels == l], y[labels == l], 2) def draw(self, proj, mouse_x, mouse_y, ui_manager): ui_manager.info( 'Use left and right to increase/decrease the number of clusters. k = %d' % self.k) self.painter.batch_draw() def on_key_release(self, key, modifiers): if key == pyglet.window.key.LEFT: self.k = max(2, self.k - 1) return True elif key == pyglet.window.key.RIGHT: self.k = self.k + 1 return True return False
class ConvexHullLayer(BaseLayer): def __init__(self, data, col, fill=True, point_size=4): """ Convex hull for a set of points :param data: points :param col: color :param fill: whether to fill the convexhull polygon or not :param point_size: size of the points on the convexhull. Points are not rendered if None """ self.data = data self.col = col self.fill = fill self.point_size = point_size def invalidate(self, proj): self.painter = BatchPainter() self.painter.set_color(self.col) x, y = proj.lonlat_to_screen(self.data['lon'], self.data['lat']) if len(x) >= 3: self.painter.convexhull(x, y, self.fill) else: self.painter.linestrip(x, y) if self.point_size > 0: self.painter.points(x, y, self.point_size) def draw(self, proj, mouse_x, mouse_y, ui_manager): self.painter.batch_draw()
class KMeansLayer(BaseLayer): def __init__(self, data): self.data = data def invalidate(self, proj): self.painter = BatchPainter() x, y = proj.lonlat_to_screen(self.data['lon'], self.data['lat']) k_means = KMeans() k_means.fit(np.vstack([x,y]).T) labels = k_means.labels_ self.cmap = create_set_cmap(set(labels), 'hsv') for l in set(labels): try: self.painter.set_color(self.cmap[l]) self.painter.convexhull(x[labels == l], y[labels == l]) self.painter.points(x[labels == l], y[labels == l], 2) except Exception: print '=============',l,'==============' def draw(self, proj, mouse_x, mouse_y, ui_manager): self.painter.batch_draw() def on_key_release(self, key, modifiers): return False
class ConvexHullLayer(BaseLayer): def __init__(self, data, col, fill=True, point_size=4): """ Convex hull for a set of points :param data: points :param col: color :param fill: whether to fill the convexhull polygon or not :param point_size: size of the points on the convexhull. Points are not rendered if None """ self.data = data self.col = col self.fill = fill self.point_size=point_size def invalidate(self, proj): self.painter = BatchPainter() self.painter.set_color(self.col) x, y = proj.lonlat_to_screen(self.data['lon'], self.data['lat']) if len(x) >= 3: self.painter.convexhull(x, y, self.fill) else: self.painter.linestrip(x, y) if self.point_size > 0: self.painter.points(x, y, self.point_size) def draw(self, proj, mouse_x, mouse_y, ui_manager): self.painter.batch_draw()
class KMeansLayer(BaseLayer): def __init__(self, data): self.data = data self.k = 2 def invalidate(self, proj): self.painter = BatchPainter() x, y = proj.lonlat_to_screen(self.data['lon'], self.data['lat']) k_means = KMeans(n_clusters=self.k) k_means.fit(np.vstack([x,y]).T) labels = k_means.labels_ self.cmap = create_set_cmap(set(labels), 'hsv') for l in set(labels): self.painter.set_color(self.cmap[l]) self.painter.convexhull(x[labels == l], y[labels == l]) self.painter.points(x[labels == l], y[labels == l], 2) def draw(self, proj, mouse_x, mouse_y, ui_manager): ui_manager.info('Use left and right to increase/decrease the number of clusters. k = %d' % self.k) self.painter.batch_draw() def on_key_release(self, key, modifiers): if key == pyglet.window.key.LEFT: self.k = max(2,self.k - 1) return True elif key == pyglet.window.key.RIGHT: self.k = self.k + 1 return True return False