Graphics Reference
In-Depth Information
Creating the map canvas
Our
Ui_ExplorerWindow
class defines an instance variable named
centralWid-
get
, which acts as a placeholder for our window's contents. Since we want to place a
QGIS map canvas into our window, let's implement the code to create our map canvas and
place it into this central widget. Add the following to the end of your
MapExplorer
win-
dow's
__init__()
method (in
lex.py
):
self.mapCanvas = QgsMapCanvas()
self.mapCanvas.useImageToRender(False)
self.mapCanvas.setCanvasColor(Qt.white)
self.mapCanvas.show()
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.mapCanvas)
self.centralWidget.setLayout(layout)
Next, we want to fill our map canvas with the basemap and landmark map layers. To do
this, we'll define a new method called
loadMap()
, and call this at the appropriate time.
Add the following method to your
MapExplorer
class:
def loadMap(self):
cur_dir = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(cur_dir, "data",
"NE1_HR_LC_SR_W_DR",
"NE1_HR_LC_SR_W_DR.tif")
self.basemap_layer = QgsRasterLayer(filename,
"basemap")
QgsMapLayerRegistry.instance().addMapLayer(
self.basemap_layer)
filename = os.path.join(cur_dir, "data",
"ne_10m_populated_places",
"ne_10m_populated_places.shp")
self.landmark_layer = QgsVectorLayer(filename,
"landmarks",