2

I am trying to get an image from inside a polygon.

TLDR;

  • What I am trying to do: save what's inside a polygon as an image.
  • What is happening now: I am only able to save the canvas as an image. Therefore, my end image is dependent on my qgis window size and is not consistent.

This is what my code looks like:

def ExtractTemplate(self, point):
    # PARAMETERS
    mainPath = '~/.qgis/'
    filename = 'template'
    imageType = "png"
    dpi = 150
    lon = RectangleMapTool.lon
    lat = RectangleMapTool.lat
    lonMax = RectangleMapTool.lonmax
    latMax = RectangleMapTool.latmax
    h = RectangleMapTool.yres
    w = RectangleMapTool.xres

    print("Width : " + str(canvasSize.width()) + " / Height : " + str(canvasSize.height()))

    imageWidth_mm = w
    imageHeight_mm = h

    hPoint1 = QgsPoint(lonMax, latMax)
    hPoint2 = QgsPoint(lonMax, lat)
    wPoint1 = QgsPoint(lonMax, lat)
    wPoint2 = QgsPoint(lon, lat)

    print("template: \n lon: "+str(lon)+", lat: "+str(lat) +", lonMax: "+str(lonMax)+",latMax: "+str(latMax))

    iface.mapCanvas().setExtent(RectangleMapTool.extent)
    map_settings = iface.mapCanvas().mapSettings()
    c = QgsComposition(map_settings)
    c.setPaperSize(imageWidth_mm, imageHeight_mm)
    c.setPaperSize(w, h)
    c.setPrintResolution(dpi)

    #set page background to transparent
    transparent_fill =QgsFillSymbolV2.createSimple({ 'outline_style': 'no', 'style': 'no'})
    c.setPageStyleSymbol( transparent_fill )

    x, y = 0, 0
    w, h = c.paperWidth(), c.paperHeight()
    composerMap = QgsComposerMap(c, x, y ,w, h)
    composerMap.setBackgroundEnabled(False)
    c.addItem(composerMap)

    dpmm = dpi / 25
    width = int(dpmm * c.paperWidth()) #dpmm
    height = int(dpmm * c.paperHeight()) #dpmm

    # create output image and initialize it
    #image = QImage(QSize(width, height), QImage.Format_ARGB32)
    image = QImage(QSize(width, height), QImage.Format_ARGB32)


    image.setDotsPerMeterX(dpmm * 1000)
    image.setDotsPerMeterY(dpmm * 1000)
    image.fill(Qt.transparent)

    imagePainter = QPainter(image)
    print(imagePainter)
    c.setPlotStyle(QgsComposition.Print)
    c.renderPage( imagePainter, 0 )
    imagePainter.end()
    print(image)
    imageFilename =  mainPath + filename + '.' + imageType
    image.save(imageFilename, imageType)

    self.temp_img = cv2.imread( '{}'.format(imageFilename))
    print(self.temp_img)

    # resetting canvas zoom
    iface.mapCanvas().setExtent(self.canvas_extent)

    print('Done generating template')

What this does is zooming in on the polygon I created through my plugin, then save the canvas as whole as an image. The problem is that it's not consistent as it depends on what size qgis window is in and template.png is changing when i change the window size.

image without changing extent()

Here is how it looks after zooming in and saving the image: image with changing the extent()

Is there a way where I can extract only what's inside the polygon?

The part where I create polygon:

    # drawing the polygon
        self.layer_polygon =  QgsVectorLayer('Polygon?crs=epsg:32650', 'template_poly', "memory")
        pr = self.layer_polygon.dataProvider()


        poly = QgsFeature()
        point_one=QgsPoint(r.xMinimum(), r.yMinimum())
        point_two=QgsPoint(r.xMaximum(), r.yMaximum())

        # creating these vars to use outside of class
        RectangleMapTool.lon = r.xMinimum()
        RectangleMapTool.lat = r.yMinimum()
        RectangleMapTool.lonmax = r.xMaximum()
        RectangleMapTool.latmax = r.yMaximum()

        points = [point_one,QgsPoint(r.xMaximum(),r.yMinimum()),point_two,QgsPoint(r.xMinimum(),r.yMaximum())]
        poly.setGeometry(QgsGeometry.fromPolygon([points]))
        pr.addFeatures([poly])
        self.layer_polygon.updateExtents()
        QgsMapLayerRegistry.instance().addMapLayer(self.layer_polygon)

        RectangleMapTool.extent = self.layer_polygon.dataProvider().extent()
        RectangleMapTool.xres = RectangleMapTool.extent.width()
        RectangleMapTool.yres = RectangleMapTool.extent.height()
        RectangleMapTool.layer_polygon = self.layer_polygon


        # set properties (polygon styling)
        properties = {"style": "no", "joinstyle": "bevel", "line_color": "228,26,28,255", "line_style": "solid", "line_width": "0.96"}
        s = QgsFillSymbolV2.createSimple(properties)
        self.layer_polygon.setRendererV2( QgsSingleSymbolRendererV2(s))
        self.canvas.refresh() 

        # resetting the mouse to normal
        iface.mapCanvas().unsetMapTool(self)
TomazicM
  • 25,601
  • 22
  • 29
  • 39

0 Answers0