2

I am creating a shapefile with adding 20 polgons to it.

        poly = ogr.Geometry(ogr.wkbPolygon)
        for p in range(20):#20 should be replaced by pix_no
            ridx = rind1[p]
            cidx = rind2[p]
            value = array[ridx][cidx]
            Xcoord, Ycoord = self.pixelOffset2coord(ds,cidx,ridx)
            outFeature = ogr.Feature(featureDefn)
            poly.AddGeometry(doPolygon(Xcoord-15, Ycoord-15, paddingX=150, paddingY=150, angle=0, polygonType = 'square') )
            outFeature.SetGeometry(poly)
            outFeature.SetField("VALUE0", int(Xcoord))
            outFeature.SetField("VALUE1", int(Ycoord))
            outLayer.CreateFeature(outFeature)
            outFeature.Destroy()

Now, I want to display the individual polygons in a loop. But I am getting only one polygon in first iteration, two in second and so on. How can I display one at a time?

 for feat in features:
            var = len(QgsProject.instance().mapLayersByName('Hlayer')) != 0
            if var:
                QgsProject.instance().removeMapLayer(Vlout)
                del Vlout

            Vlout = QgsVectorLayer("polygon?crs=epsg:32643", "Hlayer", "memory")
            pr = Vlout.dataProvider()
            pr.addFeatures([feat])

            #val0 = feat["VALUE0"]
            #val1 = feat["VALUE1"]



            #set the polygon border color and make it transparent

            single_symbol_renderer = Vlout.renderer()
            symbol = QgsFillSymbol.createSimple({'color':'255,0,0,0','color_border':'#DC143C','width_border':'0.8'})
            single_symbol_renderer.setSymbol(symbol)


            Vlout.triggerRepaint()                
            Vlout.commitChanges()
            Vlout.updateExtents()

            #Add the layer to map registry
            QgsProject.instance().addMapLayer(Vlout)


            layers =  QgsProject.instance().mapLayers()
            map_canvas_layer_list = [l for l in layers.values()]
            self.canvas.setLayers(map_canvas_layer_list)
            self.canvas.setExtent(Vlout.extent())

            self.canvas.refresh()
            self.canvas.show()

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.