[Qgis-developer] Re: mismatch between mapCanvas size vs. mapRenderer size

SBL Stefan.Blumentrath at nina.no
Mon Jan 23 05:47:48 EST 2012


Hi

Firstly, I probably should clarify my aim: I would like to export the map
Canvas into a high resolution tif with geographical information (either
geotif or tfw), in order to be able to translate this tif into a Garmin
Custom Map (which can be loaded on a compatible GPS unit). Therefore, I need
a high quality tif and tfw...

Thanks to Martins posting I could exlude that the difference between
mapCanvas and mapRenderer extent caused the offset in my tif with tfw
export. Now, I assume that the problem is, that the Composer map does not
fill the entire paper (although paper size is set according to mapRenderer
extent). It is cleanly aligned in the upper left, but does not reach the
lower right corner (a few lines with empty pixels occure on the bottom and
right border).

My approach is now to count the empty lines at the bottom / right border and
to exclude them from the image export. The following code gave quite good
results (at least they looked good), but is probably unnecessary complicated
and not 100% reliable (!?).

Are there better ways to export the mapCanvas into a high quality tif and
tfw?


target_dpi = 192

canvas = qgis.utils.iface.mapCanvas()
mapRenderer = canvas.mapRenderer()
mapRect = mapRenderer.extent()
width = mapRenderer.width()
height = mapRenderer.height()

# set up composition
c = QgsComposition(mapRenderer)
c.setPlotStyle(QgsComposition.Print)

c.setPrintResolution(target_dpi)
c.setPaperSize(mapRenderer.width()/(mapRenderer.outputDpi()/25.4),
mapRenderer.height()/(mapRenderer.outputDpi()/25.4))

dpi = c.printResolution()
dpmm = dpi / 25.4

# add a map to the composition
x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()

width = int(round(dpmm * c.paperWidth()))
height = int(round(dpmm * c.paperHeight()))

composerMap = QgsComposerMap(c, x,y,w,h)
c.addItem(composerMap)

# create output image and initialize it
image = QImage(QSize(width, height), QImage.Format_ARGB32)
image.setDotsPerMeterX(dpmm * 1000)
image.setDotsPerMeterY(dpmm * 1000)
image.fill(0)

#render the composition
imagePainter = QPainter(image)
#imagePainter.setRenderHint(0x01)
sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight())
targetArea = QRectF(0, 0, width, height)
c.render(imagePainter, targetArea, sourceArea)
imagePainter.end()

#Count empty lines at bottom and right border
lastPixelX = image.width() - 1
lastPixelY = image.height() - 1

xPixelValue = ''
yPixelValue = ''

while yPixelValue != '(0, 0, 0, 255)':
	lastPixelY = lastPixelY - 1
	yPixelValue =
str(QColor(image.pixel(int(image.width()/2),lastPixelY)).getRgb())


while xPixelValue != '(0, 0, 0, 255)':
	lastPixelX = lastPixelX - 1
	xPixelValue =
str(QColor(image.pixel(lastPixelX,int(image.height()/2))).getRgb())

#Make a copy of the image containing only the maps pixels
exportImage = image.copy(0, 0, lastPixelX, lastPixelY)	
exportImage.save("c:\\skript_test.tif", "tif")

#Export tfw-file
xScale = (mapRect.xMaximum() - mapRect.xMinimum()) / ((
(mapRenderer.width()) / mapRenderer.outputDpi()) * target_dpi)
yScale = (mapRect.yMaximum() - mapRect.yMinimum()) / ((
(mapRenderer.height()) / mapRenderer.outputDpi()) * target_dpi)

xDif=(( mapRenderer.width() / mapRenderer.outputDpi()) *
target_dpi)-exportImage.width()
yDif=(( mapRenderer.height() / mapRenderer.outputDpi()) *
target_dpi)-exportImage.height()
			
f = open("c:\\skript_test.tfw", 'w')
f.write(str(xScale) + '\n')
f.write(str(0) + '\n')
f.write(str(0) + '\n')
f.write('-' + str(yScale) + '\n')
f.write(str(mapRect.xMinimum()+(xScale*xDif/2)) + '\n')
f.write(str(mapRect.yMaximum()-(yScale*yDif)))
f.close()


Stefan



--
View this message in context: http://osgeo-org.1560.n6.nabble.com/mismatch-between-mapCanvas-size-vs-mapRenderer-size-tp4099155p4332143.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.


More information about the Qgis-developer mailing list