Help rendering shapefiles (.shp) to TIF with labels

المشرف العام

Administrator
طاقم الإدارة
I'm running into some problems here. I've basically copied the script below with minor tweaks from a few other posts here on stackexchange. When I run my code and all I get for output is a TIF that's white with a legend. For the life of me I can't figure out what's wrong.

My goal is to take a shapefile (shp) and convert it to a tif with the contour lines labeled in elevation. I'm able to open the shapefile up in qgis, and overlay it on the dem file from which I made the contours. I'm even able to overlay the two in a window (canvas), using another script I wrote, but I can't for the life of me get it to save to a .tif file with the contours and labels.

Here is my code as it now stands:

from qgis.core import * from qgis.gui import * from qgis.utils import *

import PyQt4from PyQt4.QtCore import *from PyQt4.QtGui import *print '\n'#initialize qgisapp = QgsApplication([], True)app.setPrefixPath('c:/OSGeo4W64/apps/qgis', True)app.initQgis()# Creat a map instancemapinstance = QgsMapLayerRegistry.instance()#add layer to instancevlayer = QgsVectorLayer('c:/Data/Clairmont/quarterContour.shp', 'contour', 'ogr')rlayer = QgsRasterLayer('c:/Data/Clairmont/smoothQuarter.tif', 'smoothQuarter')# mapinstance.addMapLayer(vlayer)if vlayer.isValid(): print 'Vector layer is valid' mapinstance.addMapLayer(vlayer)else: print 'Vector layer is not valid'if rlayer.isValid(): print 'Raster layer is valid'palyr = QgsPalLayerSettings()palyr.enabled = Truepalyr.fieldName = 'elev'palyr.setDataDefinedProperty(QgsPalLayerSettings.Size,True,True,'8', '')palyr.placement = QgsPalLayerSettings.Linepalyr.writeToLayer(vlayer)# vlayer.setCustomProperty("labeling", "pal")# vlayer.setCustomProperty("labeling/enabled", "true")# vlayer.setCustomProperty("labeling/fontFamily", "Arial")# vlayer.setCustomProperty("labeling/fontSize", "14")# vlayer.setCustomProperty("labeling/fieldName", "ELEV")# vlayer.setCustomProperty("labeling/placement", "2")mapRenderer = QgsMapRenderer()lst = [vlayer.id(), rlayer.id()]print type(lst)mapRenderer.setLayerSet(lst)# Set Extent# rect = rlayer.extent()rect = vlayer.extent()goodExtent = mapRenderer.setExtent(rect)print 'mapExtent is valid: %s' %(goodExtent)print 'mapRenderer.extent X:%d, Y:%d' %(mapRenderer.extent().width(), mapRenderer.extent().height())# enum: 0 for mm, 1 for pixels# mapRenderer.setOutputUnits(1)print 'Output DPI: %d' %(mapRenderer.outputDpi())print 'Output Units: %s' %(mapRenderer.outputUnits())# print 'Output Size X:%i Y:%i' %(mapRenderer.outputSize().width(), mapRenderer.outputSize.height())# print 'Map Units: %s' %(mapRenderer.mapUnits())# print 'Map units per pixel: %s' %(mapRenderer.mapUnitsPerPixel())print 'vlayer.id(): %s' %(vlayer.id())mapRenderer.setLabelingEngine(QgsPalLabeling())# mapRenderer.setLayerSet([QgsMapCanvasLayer(vlayer)])# canvas = QgsMapCanvas()# canvas.setCanvasColor(Qt.white)# canvas.enableAntiAliasing(True)# canvas.show()# canvas.setExtent(vlayer.extent())# canvas.setLayerSet([QgsMapCanvasLayer(vlayer)])c = QgsComposition(mapRenderer)c.setPlotStyle(QgsComposition.Print)# c.setPlotStyle(QgsComposition.Preview)print 'Paperwidth: %d' %(c.paperWidth())print 'Paperheight: %d' %(c.paperHeight())x, y = 0, 0w, h = c.paperWidth(), c.paperHeight()composerMap = QgsComposerMap(c,x,y,w,h)c.addItem(composerMap)composerLabel = QgsComposerLabel(c)# This seems to just be a bunch of legend stuff, but it doesn't do anything# composerLabel.adjustSizeToText()# c.addItem(composerLabel)# //c.addComposerMap(composerMap)# composerLabel.setItemPosition(20,10)# composerLabel.setItemPosition(20,10, 100, 30)legend = QgsComposerLegend(c)legend.model().setLayerSet(mapRenderer.layerSet())c.addItem(legend)# Set Image Sizingprint 'printResolution and dpi: %d' %(c.printResolution())dpi = c.printResolution()dpmm = dpi/25.4# dpm = dpi/0.0254width = int(dpmm * c.paperWidth())height = int(dpmm * c.paperHeight())print 'Width: %i Height: %i' %(width, height)# img = QImage(QSize(width, height), QImage.Format_RGB32)# img = QImage(5000, 5000, QImage.Format_RGB32)img = QImage(width, height, QImage.Format_RGB32)img.setDotsPerMeterX(dpmm * 1000)img.setDotsPerMeterY(dpmm * 1000)print 'Dots per meter X: %d Dots per meter Y: %d' %(img.dotsPerMeterX(), img.dotsPerMeterY())img.fill(0)imagePainter = QPainter(img)sourceArea = QRectF(0, 0, c.paperWidth(), c.paperHeight())targetArea = QRectF(0, 0, width, height)# Render Imagec.render(imagePainter, targetArea, sourceArea)imagePainter.end()img.save("out.tif", "tif")# app.exec_()# app.exitQgis()print '\n'print '*********************************************************************'print '*************************Script Complete*****************************'print '*********************************************************************'print '\n'All I get is a tif of the wrong size (pixelsx X pixelsy) that is entirely white with a legend in the top left corner. I know for a fact that my shapefile is fine in the sense that I'm able to make it overlay in QGIS Desktop programatically. I just can't figure out what I'm doing wrong in my script.

Another hint that my shapefile and dem raster are ok is that I've got some other code to make these appear as an overlay in a QT window and they come up just fine... including the elevation labels.

Any help would be greatly appreciated.-Marc



أكثر...
 
أعلى