4

My project goal is to write a standalone app for Unbuntu and QGIS 2.14. It should get a layer of simple points and their name from a Postgres database and show them in a layer. In addition there should be a (google) satellite picture or geotif in the background. I want to get a map including scale bar and north arrow for point x and scale y with an appropriate extent of the map so I can see if there are any other points nearby (for example radius of 10km). Problem: area of interest is the Antarctica.

I think I got the first steps to create such a python script right. I got a layer of points from the database which is valid (vlayer). I got another example layer as a simple csv-file which is also valid (layer1).

My problem is, that the PNG image I get is a white (or if I change the colour of the background image: different coloured) image. There are no layers visible. I have not taken the different projection I want into account yet (epsg_ 3031) which is why I tried only to use the csv-file I created (points in Europe). Even then it does not render the layers.

I already googled around a lot and did benefit from all the threads and so on but now I don't know any further.

# the_app.py
import os
import sys

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

from PyQt4.QtGui import *   
from PyQt4.QtCore import *

def main():
    QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'], True)
    app = QgsApplication([], False)
    app.initQgis()

    # app specific code (console)
    print 'starting test'

##############################################################################
    # adding a layer http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/loadlayer.html
    # Bsp: province = QgsVectorLayer('/filepath/Province_studyUTM36S.shp' , 'layer', 'ogr')    
    uri = "file:///xx/xxx/01/TestKoo.csv?delimiter=%s&xField=%s&yField=%s&elevField=%s" % (";","x","y","elev")
    layer1 = QgsVectorLayer(uri, "test", "delimitedtext")
    if not layer1.isValid():
        print "layer1 failed to load!"

    feature = layer1.getFeatures().next()    
    print feature[2]

############Layer from GoogleMaps satellite####################################
    #???
############Layer from Postgres###########################################


    #PostGIS database — data source is a string with all information needed:
    uri = QgsDataSourceURI()

    # set host name, port, database name, username and password
    uri.setConnection("xx", "5432", "xx", "xx", "xx")
    print "DB connection set"  

    # set database schema, table name, geometry column, optionally subset (WHERE clause) and primary key
    uri.setDataSource("public", "view", "geom", "", "station_id")
    print "DB Data source set"

    vlayer = QgsVectorLayer(uri.uri(), "punkte", "postgres")
    if not vlayer.isValid():
        print "vlayer failed to load!"

    # copy vlayer and only work with copy
    vlayer1 = vlayer

##############################################################################

    # adding all layers to the registry
    QgsMapLayerRegistry.instance().addMapLayer(layer1, False)
    print 'layer1 added to map layer registry'
    QgsMapLayerRegistry.instance().addMapLayer(vlayer1, False)
    print 'vlayer1 added to map layer registry'

    # here I tried to specify the simbols used if default wouldn't work. Still 
    # there is no symbol visible
    symbol = QgsMarkerSymbolV2.createSimple({'name': 'square', 'color': 'red'})
    layer1.rendererV2().setSymbol(symbol)
    layer1.refesh()

    symbol = QgsMarkerSymbolV2.createSimple({'name': 'square', 'color': 'blue'})
    vlayer1.rendererV2().setSymbol(symbol)
    vlayer1.refresh()

##############################################################################

    # Simple rendering
    # create image
    img = QImage(QSize(800, 600), QImage.Format_ARGB32_Premultiplied)

    # set image's background color
    color = QColor(255, 255, 255)  # Parameter für weiß: 255, 255, 255
    img.fill(color.rgb())

    # create painter
    p = QPainter()
    p.begin(img)
    p.setRenderHint(QPainter.Antialiasing)

##############################################################################
    # set layer set
    ID = [layer1.id(), vlayer1.id()]  # add ID of every layer

    render = QgsMapRenderer()
    render.setLayerSet(ID)
    print "Set rendered"

    rect = QgsRectangle(render.fullExtent())
    rect.scale(1.200000)
    render.setExtent(rect)

    # set output size
    render.setOutputSize(img.size(), img.logicalDpiX())

    # do the rendering
    render.render(p)
    print "rendered"

    p.end()
    print "painter ended"

##############################################################################    
    # save image
    img.save("the_app.png","png")

##############################################################################
    QgsApplication.exitQgis()
    #sys.exit(result)

if __name__ == '__main__':
    main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.