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 :-)):
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
\nGDAL provider
\nECW
ERDAS Compressed Wavelets (SDK 5.0)Datensatzbeschreibung
\nD:/Daten/QGIStmp/Nordost/dop20_400_5826.ecw
\n\nCOLORSPACE=RGB
\n\nCOMPRESSION_RATE_TARGET=20
\n\nVERSION=2
\nKanal 1
\nKanal 2
\nKanal 3
\nDimensionen
\nX: 10000 Y: 10000 Kan\xe4le: 3
\nX : 5000,Y 5000
X : 2500,Y 2500
X : 1250,Y 1250
X : 625,Y 625
X : 312,Y 312
X : 156,Y 156
Ursprung
\n400000,5.828e+06
\nPixelgr\xf6\xdfe
\n0.2,-0.2
\nLeerwert
\n*Leerwert nicht gesetzt*
\n\nDatentyp
\nByte - Acht Bit vorzeichenlose Ganzzahl
\nPyramiden\xfcbersichten
\nR\xe4umliches Bezugssystem des Layers
\n+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
\nLayerausdehnung (in ursp\xfcnglicher Projektion des Layers)
\n400000.0000000000000000,5826000.0000000000000000 : 402000.0000000000000000,5828000.0000000000000000
\n\nKanal
\nKanal 1
\nKanal Nr
\n\n1
\nKeine Statistik
\n\nNoch keine Statistik gesammelt
\n\nKanal
\nKanal 2
\nKanal Nr
\n\n2
\nKeine Statistik
\n\nNoch keine Statistik gesammelt
\n\nKanal
\nKanal 3
\nKanal Nr
\n\n3
\nKeine 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
):
I get a result without any problem (I don't know if it is the desired result).
No comments:
Post a Comment