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_()
No comments:
Post a Comment