Graphics Reference
In-Depth Information
Working with custom map layers
Instead of using a standard map layer with a data provider, features, symbols, and so on,
you can implement your own custom map layer entirely in Python. Custom map layers are
generally used to draw specific data that is too complicated to represent as vector format
data, or to draw special visual features such as a grid or a watermark onto the map.
Custom map layers are implemented by subclassing the QgsPluginLayer class. The
process is actually very simple, though you will need to translate between map and device
coordinates so that the items you draw in your Python layer match up with the features
drawn in the other layers within your canvas.
Note
Don't get confused by the name; you don't have to write a QGIS plugin to create your own
QgsPluginLayer subclass.
Let's see how we can create our own subclass of QgsPluginLayer . We're going to cre-
ate a simple grid that can appear as a layer within the map. Let's start by defining the Qg-
sPluginLayer subclass itself:
class GridLayer(QgsPluginLayer):
def __init__(self):
QgsPluginLayer.__init__(self, "GridLayer", "Grid
Layer")
self.setValid(True)
In our __init__() method, we give the plugin layer a unique name ( "GridLayer" )
and a user-visible name ( "Grid Layer" ), and then tell QGIS that the layer is valid.
Next, we need to set up the coordinate reference system and extent of our layer. Since we're
creating a grid that covers the entire Earth, we'll use the standard EPSG 4236 coordinate
system (that is, latitude/longitude coordinates), and set the extent of the layer to cover the
entire surface of the Earth:
self.setCrs(QgsCoordinateReferenceSystem(4326))
self.setExtent(QgsRectangle(-180, 90, 180, 90))
Search WWH ::




Custom Search