1

My background is in arcpy and Esri so all of the Qgis is new to me.

So I cannot figure out how to duplicate polygon with python. I want to draw one polygon and then be able to use it to make more and make a grid of them like I am showing below in the picture.

What should I do? What resources should I look at? (I have looked at the documentation but doesn't make sense to me)

from qgis.core import *

layer = QgsVectorLayer(r"path/Test_file.shp", "Test_file", "ogr")

feats = [ feat for feat in layer.getFeatures() ]

The_Features = layer.getFeatures()
num=0
for i in The_Features:
    geom = i.geometry()


    Point_1 = geom.vertexAt(0)
    raw1 = Point_1.asWkt()
    Point_1_x = float(raw1[7:29])
    Point_1_y = float(raw1[31:51])
    print(raw1)

    Point_2 = geom.vertexAt(1)
    raw2 = Point_2.asWkt()
    Point_2_x = float(raw2[7:29])
    Point_2_y = float(raw2[31:51])
    print(raw2)

    Point_3 = geom.vertexAt(2)
    raw3 = Point_3.asWkt()
    Point_3_x = float(raw3[7:29])
    Point_3_y = float(raw3[31:51])
    print(raw3)

    Point_4 = geom.vertexAt(3)
    raw4 = Point_4.asWkt()
    Point_4_x = float(raw4[7:29])
    Point_4_y = float(raw4[31:51])
    print(raw4)
    print(Point_4_x)
    print(Point_4_y)
    num+=1
    break

 print(num)
 # 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()

enter image description here

  • please edit your question to correct the code formatting. – ThingumaBob Jun 7 '18 at 19:02
  • Is it right now? – jacob Blankenship Jun 7 '18 at 22:19
  • yep, looks good. you should see your posts preview live at the bottom of the question interface or via the Preview button in the app!? – ThingumaBob Jun 7 '18 at 22:22
1

Here's a solution to create a grid from a single polygon (the left-bottom; the polygons will be created to the right and above it). Make sure your initial layer has only one polygon in the desired 'starting point' and run this code:

layer = QgsVectorLayer(r"path/Test_file.shp", "Test_file", "ogr")

rows=4
columns=4

xfactor=500 #space interval x in meters (assuming projected CRS)
yfactor=200 #space interval y in meters (assuming projected CRS)

feats = [ feat for feat in layer.getFeatures() ]
geom = feats[-1].geometry() 


for i in range(rows):
    for j in range(columns):
        Point_1 = geom.vertexAt(0)
        raw1 = Point_1.asWkt()
        Point_1_x = float(raw1[7:29])+(xfactor*j)
        Point_1_y = float(raw1[31:51])+(yfactor*i)
        print(raw1)

        Point_2 = geom.vertexAt(1)
        raw2 = Point_2.asWkt()
        Point_2_x = float(raw2[7:29])+(xfactor*j)
        Point_2_y = float(raw2[31:51])+(yfactor*i)
        print(raw2)

        Point_3 = geom.vertexAt(2)
        raw3 = Point_3.asWkt()
        Point_3_x = float(raw3[7:29])+(xfactor*j)
        Point_3_y = float(raw3[31:51])+(yfactor*i)
        print(raw3)

        Point_4 = geom.vertexAt(3)
        raw4 = Point_4.asWkt()
        Point_4_x = float(raw4[7:29])+(xfactor*j)
        Point_4_y = float(raw4[31:51])+(yfactor*i)
        print(raw4)

        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)]

        feat = QgsFeature() 
        feat.setGeometry(QgsGeometry.fromPolygonXY( [points])) 
        prov.addFeatures([feat])

layer.updateExtents()

Example Input:

enter image description here

Output

enter image description here

A side note, that if you are to repeat this process on existing layers, consider using a GeoPackage, as overwriting and deleting features from a single shapefile has proved to be erroneous when I tested.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.