Saturday, 9 September 2017

raster - Rendering a custom bounding box to an image file using python and QGIS?


Edit: the problem was merely a typo in the coordinates (see comment on definition of p). Otherwise the code below should work for other situations, too, if adapted properly. The weird pixelated/grey/colorful images arise, when the rendered coordinates do not lie on the layer extend.




I have tried to adopt some code from the PyQGISDeveloperCookbook (p.37) to render a QGIS raster layer to a png image file (see bottom for layer's metadata). One thing that I have tried to change is to modify the rendered extent to a custom bounding box bbox centered in a given point p:


    from PyQt4.QtGui import QImage, QPainter
from PyQt4.QtCore import QSize

import os

# raster layer in CRS UTM 33N (epsg 25833)
rlayer = iface.activeLayer()
# radius for image in meters
radius = 50
# center point
p = [33401477,5826362] # <- should be 334014.77 for x-coordinate
# bounding box
bbox = QgsRectangle(p[0]-radius, p[1]-radius, p[0]+radius, p[1]+radius)

# image
img = QImage(QSize(200,200), QImage.Format_ARGB32_Premultiplied)
## create painter
painter = QPainter()
painter.begin(img)
render = QgsMapRenderer()
## set layer set
lst = [rlayer.id()]
render.setLayerSet(lst)
## set extent

render.setExtent(bbox)
## set output size
render.setOutputSize(img.size(), img.logicalDpiX())
## do the rendering
render.render(painter)
painter.end()
## save image
img.save("D:\\Daten\\QGIStmp\\bbox1.png", "png")

The resulting image does not really look the way I expected (though interesting :-)):



bbox1.png


I tried hard (generating more interesting images), but I couldn't figure out what the problem could be.




Querying Layer Metadata:


>>> rlayer.metadata()


Response:


    u'

Treiber

\n

GDAL provider

\nECW
ERDAS Compressed Wavelets (SDK 5.0)

Datensatzbeschreibung

\n

D:/Daten/QGIStmp/Nordost/dop20_400_5826.ecw

\n

\nCOLORSPACE=RGB

\n

\nCOMPRESSION_RATE_TARGET=20

\n

\nVERSION=2

\n

Kanal 1

\n

Kanal 2

\n

Kanal 3

\n

Dimensionen

\n

X: 10000 Y: 10000 Kan\xe4le: 3

\n

X : 5000,Y 5000

X : 2500,Y 2500

X : 1250,Y 1250

X : 625,Y 625

X : 312,Y 312

X : 156,Y 156

Ursprung

\n

400000,5.828e+06

\n

Pixelgr\xf6\xdfe

\n

0.2,-0.2

\n

Leerwert

\n

*Leerwert nicht gesetzt*

\n

\n

Datentyp

\n

Byte - Acht Bit vorzeichenlose Ganzzahl

\n

Pyramiden\xfcbersichten

\n

R\xe4umliches Bezugssystem des Layers

\n

+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs

\n

Layerausdehnung (in ursp\xfcnglicher Projektion des Layers)

\n

400000.0000000000000000,5826000.0000000000000000 : 402000.0000000000000000,5828000.0000000000000000

\n

\nKanal

\n

Kanal 1

\n

Kanal Nr

\n

\n1

\n

Keine Statistik

\n

\nNoch keine Statistik gesammelt

\n

\nKanal

\n

Kanal 2

\n

Kanal Nr

\n

\n2

\n

Keine Statistik

\n

\nNoch keine Statistik gesammelt

\n

\nKanal

\n

Kanal 3

\n

Kanal Nr

\n

\n3

\n

Keine Statistik

\n

\nNoch keine Statistik gesammelt

\n'

Answer



Your code is ok, but there is probably a typo in the p list. The first item value seems too big for the location you are working on (I generally work using CRS UTM 32N): probably the last digits of 33401477 are decimals.


If I use other values for p:



p = [318333,4993348]

on this sample raster (CRS UTM 32N, EPSG:32632):


enter image description here


I get a result without any problem (I don't know if it is the desired result).


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...