3

I got number of QgsRectangles and i want to save images bounded by them.

Code written in python:

    for box in self.boxes:
        xs = [box[0][0],box[1][0],box[2][0],box[3][0]]
        ys = [box[0][1],box[1][1],box[2][1],box[3][1]]
        min_x = min(xs)
        min_y = min(ys)
        max_x = max(xs)
        max_y = max(ys)
        box = QgsRectangle(min_x,min_y,max_x,max_y)
        self.iface.mapCanvas().setExtent(box)
        self.iface.mapCanvas().refresh()
        time.sleep(1)
        self.utils.iface.mapCanvas().saveAsImage('%s.tiff' %(str(min_x)))

The problem is script walks over all my boxes, but extents are set only for the last one. I thought qgis will set extents, show location of my box and save an image associated with it.

2
  • Point (xmin, ymax) for first box and point (xmax, ymin) for last box are only points that you need for calculating complete extent. Script doesn't need to walk over all boxes to do that.
    – xunilk
    Sep 4 '17 at 16:53
  • You need to give some time to the canvas to be refreshed. See: gis.stackexchange.com/questions/194867/… Sep 5 '17 at 3:38
1

Following code gets each QgsRectangle for each feature (simulating an individual box) in a 10 x 10 grid (w-e pixel resolution = n-s pixel resolution = 30):

enter image description here

and, afterward, it calculates (xmin, ymax) for first box and (xmax, ymin) for last box. They are only points that you need for calculating complete extent; printed as WKT format in last instruction.

layer = iface.activeLayer()

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

boxes = [ feat.geometry().boundingBox() for feat in feats ]

pmax = QgsPoint(boxes[0].xMinimum(), boxes[0].yMaximum())
pmin = QgsPoint(boxes[-1].xMaximum(), boxes[-1].yMinimum())

rect_ext = QgsRectangle(pmax, pmin).asWktPolygon()

print rect_ext

After running above code at Python code, printed bounding box as WKT it can be visualized with help of QuickWKT plugin of QGIS (see next image). It works.

enter image description here

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.