1

Am a used to ArcPy and using that for my coding, however, I now have to use QGIS. So I want to be able to write a script that will take a polygon that I made and then duplicate said polygon two meters over and then do that however many times I want.

Here is an example of an end product would be

enter image description here

So the problem is that this code creates thousand of empty feature but also makes the polygons that I want it to make. How would you solve this?

from qgis.core import *

layer = QgsVectorLayer(r"C:\Users\JBlankenship\Documents\Wilbur-Ellis\Drone Pics 2018\Iowa\7004-1824\June_5\Shapefile\7004_1824.shp", "7004_1824.shp", "ogr")


The_Features = layer.getFeatures()
times_x=0
times_y=0
num1=0
num2=0
num3=0




while num1 <= 7:
    print("Big loop",num1)
    num2=0
    num3=0
    x1=0
    x2=15.4+times_x

    y1=4.6
    y2=0


    for i in layer.getFeatures():
        geom = i.geometry()
        Polygon_cor = geom.asMultiPolygon()
        Point_1_x = Polygon_cor[0][0][0][0]
        Point_1_y = Polygon_cor[0][0][0][1]

        Point_2_x = Polygon_cor[0][0][1][0]
        Point_2_y = Polygon_cor[0][0][1][1]

        Point_3_x = Polygon_cor[0][0][2][0]
        Point_3_y = Polygon_cor[0][0][2][1]

        Point_4_x = Polygon_cor[0][0][3][0]
        Point_4_y = Polygon_cor[0][0][3][1]
        print("Reset")

        break
    print(num1) 
    if num1 > 0:
        while num3 < 1:
            num3+=1
            Point_1_x = Point_1_x + x2
            Point_1_y = Point_1_y + y2
            Point_2_x = Point_2_x + x2
            Point_2_y = Point_2_y + y2
            Point_3_x = Point_3_x + x2
            Point_3_y = Point_3_y + y2
            Point_4_x = Point_4_x + x2
            Point_4_y = Point_4_y + y2
            print("Done R",num3)
            times_x += 15.4


            # Set the provider to accept the data source
            prov = layer.dataProvider()
            points = [QgsPointXY(Point_1_x,Point_1_y), QgsPointXY(Point_2_x,Point_2_y), QgsPointXY(Point_3_x,Point_3_y), QgsPointXY(Point_4_x,Point_4_y)]

            # Add a new feature and assign the geometry
            feat = QgsFeature()
            feat.setGeometry(QgsGeometry.fromPolygonXY( [points]))
            prov.addFeatures([feat])

            # Update extent of the layer
            layer.updateExtents()





    while num2 < 7:
        num2+=1
        Point_1_x = Point_1_x + x1
        Point_1_y = Point_1_y + y1
        Point_2_x = Point_2_x + x1
        Point_2_y = Point_2_y + y1
        Point_3_x = Point_3_x + x1
        Point_3_y = Point_3_y + y1
        Point_4_x = Point_4_x + x1
        Point_4_y = Point_4_y + y1
        print("Done C",num2)

        times_y += 1

        # Set the provider to accept the data source
        prov = layer.dataProvider()
        points = [QgsPointXY(Point_1_x,Point_1_y), QgsPointXY(Point_2_x,Point_2_y), QgsPointXY(Point_3_x,Point_3_y), QgsPointXY(Point_4_x,Point_4_y)]

        # Add a new feature and assign the geometry
        feat = QgsFeature()
        feat.setGeometry(QgsGeometry.fromPolygonXY( [points]))
        prov.addFeatures([feat])

        # Update extent of the layer
        layer.updateExtents()



    num1+=1

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.