Example #1
0
    def build_indexes(self, index_nodes=True, iterate=False):
        next_node_id = 1
        coords_node = {}
        if self._src is not None:
            request = QgsFeatureRequest()
            request.FetchAttributes = False
            self._edge_map = dict([(f.id(), f)
                                   for f in self._src.getFeatures(request)])
        total = len(self._edge_map)

        for (i, (fid, feature)) in enumerate(self._edge_map.items(), start=1):
            fid = feature.id()
            ls = feature.geometry().constGet()
            endpoints = []
            for point in [ls.startPoint(), ls.endPoint()]:
                coords = (point.x(), point.y())
                node_id = coords_node.get(coords, None)

                if node_id is None:
                    node_id = next_node_id
                    next_node_id += 1
                    coords_node[coords] = node_id
                    node_feature = self._make_node_feature(node_id, point)
                    self._node_map[node_id] = node_feature
                    if index_nodes:
                        self._node_index.insertFeature(node_feature)

                endpoints.append(node_id)
                self._node_edges[node_id].add(fid)

            self._edge_index.insertFeature(feature)
            self._edge_nodes[fid] = endpoints

            if iterate:
                yield float(i) / total