Beispiel #1
0
    def clustering(self):
        gc = GeometryCollection(self.lines)

        try:
            intersections = gc.intersection(gc)
            if isinstance(intersections, LineString):
                points = [Point(*intersections.xy)]
            else:
                points = [Point(*point.xy) for point in intersections]

            if points:
                points_ = [(p.x_point, p.y_point) for p in points]
                min_samples = int(round(len(points_) * 0.1, 0))

                clustering = DBSCAN(eps=20,
                                    min_samples=min_samples).fit(points_)

                clusters = Clusters()

                for idx, kl in enumerate(clustering.labels_):
                    # All points marked with -1 is noise
                    if kl == -1:
                        continue
                    points[idx].set_cluster(clusters.get_cluster(kl))

                if not clusters.is_empty():
                    self._latest_clusters = clusters.as_list()

                    self.newest_center = clusters.best_cluster_as_point()
                    self.filtering.add(self.newest_center)
            else:
                self.newest_center = None
            return
        except TypeError as e:
            log.error(str(e))
            return