4

I have list of points and I want to create a polygon.

First I need to sort points for that I tried:

  1. Find the center point of all points
  2. Calculated angle from the center to each point,
  3. Based on angle sorted the points

but in some cases it's given an irregular polygon:

def createrIntersectionPoly(self, geom):
        bbx = geom.boundingBox()
        features = [x for x in self.roadedgelayer.getFeatures(
            QgsFeatureRequest().setFilterRect(bbx).setFlags(QgsFeatureRequest.ExactIntersect))]
        vertices = [j for i in features for j in i.geometry().vertices() if
                    QgsGeometry().fromPointXY(QgsPointXY(j)).within(geom)]
        if len(vertices) > 3:
            
            centerpoint = self.getCenterPoint(vertices)          
            
            pointsorder = {self.getLineAngele(i, centerpoint): i for i in vertices}
            # print(pointsorder)
            polygonpoints = [QgsPointXY(pointsorder[i]) for i in sorted(pointsorder.keys())]
            self.intpolylayer.startEditing()
            polygoem = QgsGeometry().fromPolygonXY([polygonpoints])
            feat = QgsFeature(self.intpolylayer.fields())
            feat.setGeometry(polygoem)
            self.intpolylayer.addFeature(feat)
            self.intpolylayer.commitChanges()

    @staticmethod
    def getCenterPoint(vertices):
        totalpoints = len(vertices)
        centerpointx = sum([i.x() for i in vertices]) / totalpoints
        centerpointy = sum([i.y() for i in vertices]) / totalpoints
        return QgsPoint(QgsPointXY(centerpointx, centerpointy))

    @staticmethod
    def getLineAngele(point1, point2):
        rad = math.atan2(point1.x() - point2.x(), point1.y() - point2.y())
        return rad * (180 / math.pi) if rad > 0 else 360 + (rad * (180 / math.pi))

but my points are like enter image description here

I need polygon like enter image description here

But when point in u shape polygon creates zigzag

How can I sort points to create a polygon?

3
  • 1
    What if you try the "Alpha shape", in QGIS it is a "Concave hull".
    – Taras
    May 20 at 6:43
  • concave or convex shapes skips the some of points but i need to maintain the all points May 20 at 7:06
  • 3
    With your points, I think I will create clusters (like this PostGIS function postgis.net/docs/ST_ClusterDBSCAN.html) and then, create lines with points for U shapes using distances between points, then get the extremity points of each line, then link the extremity points between them by shortest distance. May 20 at 7:16

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.