# Convert Geometry.boundingbox() coordinates to geometry PyQGIS

I have a shapefile of 50 line segments and I want to create a separate shapefile containing a bounding box (envelope) for each segment.

``````for feature in roads.getFeatures():
#we need to get an envelope around each feature
inAttr = feature.attributes() # Input attributes
inGeom = feature.geometry() # Input geometry
bf_inGeom = inGeom.buffer(25,1).boundingBox()
xmax = bf_inGeom.xMaximum()
xmin = bf_inGeom.xMinimum()
ymax = bf_inGeom.yMaximum()
ymin = bf_inGeom.yMinimum()
cordlist = [QgsPoint(xmax, ymin), QgsPoint(xmax, ymax),
QgsPoint(xmin, ymax), QgsPoint(xmin,ymin)]
gPolygon = QgsGeometry.fromPolygon(cordlist)
``````

I am just learning the API and while I've successfully loaded the shapefiles, I can't seem to make and display a bounding box polygon.

I'm intermediate on Python. I'm pouring over the docs, but any tips about learning this API would help answer this question too.

How can I convert `QgsRectangle` to `QgsGeometry`?

I'm on QGIS 2.14.11 using Debian 9 Stretch.

I found that I was using the wrong version for the API definitions and the cookbook.

Todays solution is twofold.

1. Check the version numbers of the manuals match your QGIS version.

2. In 2.14 convert a `QgsRectangle` to `QgsGeometry` with the following.

`QgsGeometry.fromRect(const QgsRectangle)`

``````bf_inGeom = inGeom.buffer(25,1).boundingBox() # -> returns QgsRectangle
gPolygon  = QgsGeometry.fromRect(bf_inGeom)   # -> QgsGeometry
``````