Graphics Reference
In-Depth Information
Using memory-based layers
While a map layer would normally display geospatial data taken from an external data
source such as a shapefile, a raster DEM file, or a database, it is also possible to create geo-
spatial features directly from your Python code. For example, imagine that you write a pro-
gram to display the halfway point along a road. This halfway point could be represented as
a QgsPoint geometry, which would be displayed on the map using an appropriate marker
symbol. Since you are calculating the point, this isn't a feature you would want to store in a
shapefile or database. Rather, the feature is calculated and displayed when your program is
run.
This is an ideal application for a memory-based layer. This type of layer stores geospatial
features in memory, allowing you to create new features on the fly and display them within
a map layer.
To create a memory-based map layer, instantiate a new QgsVectorLayer object, just
like normal. The initializer for this class looks like the following:
layer = QgsVectorLayer(path, baseName, providerLib)
Note
This is slightly simplified—there is another parameter, loadDefaultStyleFlag ,
which doesn't apply to memory-based layers. Fortunately, there's a default value for this
parameter, so we can ignore it.
Let's take a look at the three parameters needed to create a memory-based map layer:
path : This string provides information that is needed to create the memory-based
layer, including the type of information that the layer will store. We will look at
this parameter in more detail shortly.
baseName : This is the name used for the memory-based layer. The name can be
anything you like, though the user will see it in the QGIS layer legend.
providerLib : This should be set to "memory" for memory-based layers.
To create a simple memory-based layer, you can do the following:
layer = QgsVectorLayer("Polygon", "My Layer", "memory")
Search WWH ::




Custom Search