1

I have created a standalone PyQGIS app (within Python itself, not a plugin) with PyQt and PyQGIS API. It generates my map really well. I would like to overlay a Google Map layer as base map just like what can be done within QGIS Desktop itself. Is this possible? If so, how should I go about doing it? Should I use QWebView or something?

Following is a snippet of my code:

#  Importing necessary classes and libraries like PyQt4 (for making GUI), qgis for Python-Qgis connection
#  Some classes are useful for overlaying OpenLayers.
#  Refer https://github.com/joystor/BaseMap2Image/blob/master/baseMap2Image.py
import sys
import signal
from PyQt4 import QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.gui import *
from qgis.core import *
from qgis.utils import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
# QtWebKit is used for application that interacts with the web
from PyQt4.QtWebKit import *
from functools import partial


# Specify App
app = QtGui.QApplication([])
# Specify which folder is QGIS installed
QgsApplication.setPrefixPath("/usr", True)
# Opening QGIS
QgsApplication.initQgis()
# Show setting of parameters to check if it matches that on QGIS (log messages at the bottom right -> general tab)
#print('QGIS App Settings')
#print QgsApplication.showSettings()
# Call main window of Qt GUI
main_win = QtGui.QMainWindow()
# Set up Qt GUI frame
frame = QtGui.QFrame(main_win)
main_win.setCentralWidget(frame)
# Specify Qt GUI layout (grid)
grid_layout = QtGui.QGridLayout(frame)
# Call up a shapefile to a vector layer
# Load vector layer
data_source = "/home/victorzhiyulee/Desktop/PJU Only Yeah/PJU Only Yeah Split 2_2.shp"
layer_name = "dun"
provider_name = "ogr"
layer = QgsVectorLayer(data_source, layer_name, provider_name)
# Quick check if layer is valid
if layer.isValid():
    print('Layer is valid')
else:
    print('Layer is invalid')
# Display window
main_win.show()
#lst = layer.id()
# 0.5 - Defining QGis Map Canvas
map_canvas = QgsMapCanvas()
grid_layout.addWidget(map_canvas)
map_canvas.setCanvasColor(QtGui.QColor(255, 255, 255))
#list_of_layers.append(lst)
# Add the big layer into registry
QgsMapLayerRegistry.instance().addMapLayer(layer)
print('Count of Layers: ', QgsMapLayerRegistry.instance().count())
print('Layers included are')
for lapis in QgsMapLayerRegistry.instance().mapLayers():
    print(lapis)
Senarai_layer = QgsMapLayerRegistry.instance().mapLayers().values()
print 'Or in QGIS language: ', Senarai_layer
for lapisan in Senarai_layer:
    print(lapisan.id())

# Setting up map canvas
canvas_layer = QgsMapCanvasLayer(layer)
map_canvas.setLayerSet([canvas_layer])
map_canvas.zoomToFullExtent()

# Printing
img = QImage(QSize(800, 600), QImage.Format_ARGB32_Premultiplied)
color = QColor(255, 255, 255)
img.fill(color.rgb())

p = QPainter()
p.begin(img)
p.setRenderHint(QPainter.Antialiasing)
render = QgsMapRenderer()
render.setLayerSet([layer.id()])


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

render.setOutputSize(img.size(), img.logicalDpiX())

render.render(p)
p.end()

# Show the window
main_win.show()
# Running the script
app.exec_()

closed as too broad by PolyGeo Nov 28 '17 at 1:24

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    did you read the code of the qgis plugin called OpenLayer? – Luigi Pirelli Nov 5 '14 at 10:54
  • @LuigiPirelli I did but to be honest, I don't quite understand it. I guess the main part of the plugin is this .Py file. link I am not sure how they get the map to show though. – Victor Zhiyu Lee Nov 5 '14 at 12:50