TransWikia.com

Interrogating and displaying polygon created from points in PyQGIS

Geographic Information Systems Asked on April 8, 2021

I’ve been working on this problem for several questions now – the most recent one here. I believe the code below had created the polygon from the points, and added this polygon to a layer as a polygon in a feature object. The layer is also added to the table of contents. However, for some reason I don’t understand the polygon does not display on the canvas?

# Attach libraries
from qgis.core import * # attach main QGIS library
from qgis.utils import * # attach main python library
import os # attach operating system library

# Set a object for the current project
Prj = QgsProject.instance() # Object for current project

# Create an array [] object with the polygon vertices
vrtcs = []
vrtcs.append(QgsPointXY(396100,8969000))
vrtcs.append(QgsPointXY(396100,8973900))
vrtcs.append(QgsPointXY(397900,8973900))
vrtcs.append(QgsPointXY(397900,8969000))

# Create a polygon from the coordinates
ply_01 = QgsGeometry.fromPolygonXY([vrtcs])

# Create a feature object then put the polygon into the feature
ftr = QgsFeature()
ftr.setGeometry(ply_01)
print(ftr.geometry())

# Create a scratch layer for the feature, in the desired CRS
lyr = QgsVectorLayer('Polygon?crs=epsg:29194', '200909_Bdy',"memory")
Prj.addMapLayers([lyr])

# Set an object for the data provider for the layer (not sure why I need this?)
prv = lyr.dataProvider()

# Make the layer editable
lyr.startEditing()

# Add the feature with the polygon to the layer (this fails)
lyr.addFeatures(ftr)

# Save the layer changes
lyr.commitChanges()

# refresh the canvas to show the polygon ?
iface.mapCanvas().refreshAllLayers()

I also expect there is method to interrogate each object to determine what objects have been appended as I go like the line print(ftr.geometry()) which reports the that the object contains a polygon with listed coordinates.

Is there a similar function, which reports what is in the layer?

One Answer

You are calling lyr.addFeatures() but passing a single layer object instead of wrapping it in a list.

So do either:

lyr.addFeatures([ftr])

Or:

lyr.addFeature(ftr)

Your code can really be simplified to the following minimal snippet which is working fine for me in the Python console in QGIS 3.14

project = QgsProject().instance()

ply_01 = QgsGeometry.fromPolygonXY(
        [[QgsPointXY(396100,8969000),
        QgsPointXY(396100,8973900),
        QgsPointXY(397900,8973900),
        QgsPointXY(397900,8969000)]]
        )

lyr = QgsVectorLayer('Polygon?crs=epsg:29194', '200909_Bdy', "memory")
ftr = QgsFeature()
ftr.setGeometry(ply_01)
with edit(lyr):
    lyr.addFeature(ftr)
project.addMapLayer(lyr)

For the last part of your question, you can use the getFeatures() method to access the features in your layer e.g.

print([f for f in lyr.getFeatures()])

Or to just get the number of features:

print(lyr.featureCount())

Answered by Ben W on April 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP