Overall Question: How can I get the rendered tif image to keep the modifications seen in the image produced by map canvas?
I am writing a standalone Python script. I have simplified my code below for debugging purposes. Its purpose is to load a shapefile vector layer, in my case SOUNDG.shp, of the geometry type 'point', and then serve a tif image of the vector layer SOUNDG.
I have experimented with QgsMapCanvas thoroughly and can confirm that this specific vector layer is correctly displayed in the canvas; thus, paths to my file and the creation of a vector layer object are correct. Now, I am abandoning canvas entirely and am transitioning to solely relying on serving up a tif image through my script. I am keeping my modifications of the vector layer in my script as well (the depth values).
Canvas Image of SOUNDG point data
As you can see I have modified the vector layer corresponding to depth values.
Tif image
In contrast to canvas, when rendering to tif in my script, as seen below, my SOUNDG.tif image is a blank white image. There is nothing displayed. In addition, I receive no error messages.
I have tried rendering multiple layers of other geometries (lines and polygons) to tif and they are rendered with no issues at all. All layers are visible. My only issue lies with vector layers that have point geometries - they aren't visible when rendered to tif.
How can I fix this? (Using only script)
Here is my code based off of PyQgis' Cookbook:
# supply path to qgis install location
QgsApplication.setPrefixPath('/usr', True)
# create a reference to the QgsApplication, setting the
# second argument to False disables the GUI
app = QgsApplication([], True)
# load providers
app.initQgis()
# create image
img = QImage(QSize(800, 600), QImage.Format_ARGB32_Premultiplied)
# set image's background color
color = QColor(255, 255, 255)
img.fill(color.rgb())
# create painter
p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)
renderer = QgsMapRenderer()
# keep track of all layer IDs
mapLayers = []
# create vector layer object
layer = QgsVectorLayer('/ENC_ROOT/US6SP10M/shapefiles/SOUNDG.shp',\
'SOUNDG', 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(layer)
mapLayers.append(layer.id())
renderer.setLayerSet(mapLayers)
# set extent
rect = QgsRectangle(renderer.fullExtent())
rect.scale(1.1)
renderer.setExtent(rect)
# set output size
renderer.setOutputSize(img.size(), img.logicalDpiX())
# do the rendering
renderer.render(p)
p.end()
# save image'
img.save("/ENC_ROOT/US6SP10M/shapefiles/SOUNDG.tif", "tif")
app.exitQgis()
I noticed this occurs when also rendering to 'png', or rather, any method of rasterization.
UPDATE: I've begun to experiment with the gdal_rasterize route:
def rasterize(layer, source_dir, output_dir):
# this renders the point to tif correctly but no modifications are considered
call(['gdal_rasterize', '-a', 'DEPTH', '-ts', '800', '600', '-l', layer, source_dir, output_dir])
This method now gives me a .tif image that displays points in a black and white format. When I create modifications to the point layer, such as replacing the dots with ints representing water depths, is there a way such that those modifications would still hold in the rasterized .tif version?
Tif image using gdal_rasterize
I've spent numerous edits trying to make this a smooth read and very understandable. If you aren't up voting it then that is telling me that something isn't clear for you. Please make that clear so that I can edit the post as needed; this will hopefully bring me to someone who does have an answer.