1

As of v3.10, there is an asPolygon method for qgis.core.QgsRectangle.

This runs:

QgsRectangle(1804512.8556, 5453390.58088, 1813512.8556, 5461390.58088).asPolygon()

enter image description here

However, no vector data (i.e. an actual rectangle) appears on-screen in the View.

Maybe all I've done is transpose the inputs and haven't actually created a feature, yes?

What do I need to add/specify to write features to either a temp or shp file? (my end goal is to iterate through records in the csv to create multiple rectangles in a single shp)

2
  • What is data coordinate system? Commented May 13, 2021 at 4:17
  • CRS = EPSG:2193
    – CreekGeek
    Commented May 13, 2021 at 4:20

2 Answers 2

6

You can use the following structure. For other rectangles, add the highlighted lines (between ###) to a for loop.

layer = QgsVectorLayer("Polygon?crs=EPSG:2193", "Polygons", "memory")
QgsProject.instance().addMapLayer(layer)

#### Coordinates coming from CSV ###
xMin = 1804512.8556
yMin = 5453390.58088
xMax = 1813512.8556
yMax = 5461390.58088

rect = QgsRectangle(xMin, yMin, xMax, yMax)
polygon = QgsGeometry.fromRect(rect)

feature = QgsFeature()
feature.setGeometry(polygon)

layer.dataProvider().addFeatures([feature])
#####################################
4
  • This one is a bit easier to set up the loop. Which I have running....Thanks! Should I post a separate question for passing other attributes?
    – CreekGeek
    Commented May 13, 2021 at 6:14
  • @CreekGeek you can use feature["attribute_name"] = value pattern for attributes. Commented May 13, 2021 at 7:27
  • I'm getting "KeyError: 'attribute_name'" . Do I need to create the key as part of the feature first? maybe .setFields() ?
    – CreekGeek
    Commented May 13, 2021 at 8:17
  • I started a new question for the attributes gis.stackexchange.com/q/396529/17482
    – CreekGeek
    Commented May 13, 2021 at 9:44
3

Try the code snippet below. This will create a temporary layer, add a polygon feature with geometry created from a QgsRectangle and add the memory layer to your project.

You also need to know what CRS your csv coordinates are in and tell QGIS about it. I have commented the code to show you where to specify the CRS.

project = QgsProject().instance()

rect = QgsRectangle(1804512.8556, 5453390.58088, 1813512.8556, 5461390.58088)
geom = QgsGeometry().fromRect(rect)

ftr = QgsFeature()
ftr.setGeometry(geom)

#Define your Coordinate Reference System here
crs = 'crs=epsg:2193'

lyr = QgsVectorLayer('Polygon?{}'.format(crs), 'Test_polygon','memory')

with edit(lyr):
    lyr.addFeature(ftr)

project.addMapLayer(lyr)
3
  • 1
    Cheers! .fromRect() is what I was missing while tinkering with QgsGeometry
    – CreekGeek
    Commented May 13, 2021 at 4:19
  • No worries. Following the comment under your question, I have edited my answer to use crs=epsg:2193
    – Ben W
    Commented May 13, 2021 at 4:26
  • 1
    cheers. Your answer was very clear and I had it running
    – CreekGeek
    Commented May 13, 2021 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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