1

I actually found several related questions but it seems my specific needs are not answered.

I have a set of polygons that I need to hit all of them with some path (no roads, just as the crow flies). For my purposes just using the centroid of the polygons will be fine, so I created a layer that is nothing but the centroids.

How can I use this layer plus a starting point to get a path that hits all of the points?

The path should not loop around, it should find its own endpoint. Here is my code to create this layer. Hopefully, I can pick it up from here and use this layer to get a path.

def make_tsp(self):
    self.tsp_points = QgsVectorLayer("Point", "tsp_points", "memory")
    feats = []
    used = []
    self.poly_features = [feat for feat in self.poly_layer.getFeatures()]
    for feature in self.dense_layer.getFeatures():
        feat = QgsFeature()
        feat.setGeometry(feature.geometry().centroid())
        feats.append(feat)
        for item in self.poly_features:
            if item.geometry().intersects(feature.geometry()):
                used.append(item)


    for feature in self.poly_features:
        if feature not in used:
            feat = QgsFeature()
            feat.setGeometry(feature.geometry().centroid())
            feats.append(feat)

    self.tsp_points.dataProvider().addFeatures(feats)
    QgsProject.instance().addMapLayer(self.tsp_points)

    start = [feature for feature in self.tsp_points.getFeatures()][0].geometry().asPoint()

I tried looking through this documentation but it is unclear to me what pieces I actually need to give me a path.

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.