Graphics Reference
In-Depth Information
del writer
As you can see, we check to see whether the SpatiaLite database file exists in our
data
subdirectory, and create a new database if necessary. We define the various fields that will
hold the various track attributes, and use a
QgsVectorFileWriter
object to create
the database.
You will also need to modify the
main()
function to call the
setupDatabase()
method. Add the following line to this function after the call to
window.raise_()
:
window.setupDatabase()
Now that we've set up our database for the track layer, we can define our various map lay-
ers. We'll create a new method called
setupMapLayers()
to do this. Let's start by de-
fining a
layers
variable to hold the various map layers, and initialize our base map lay-
er:
def setupMapLayers(self):
cur_dir =
os.path.dirname(os.path.realpath(__file__))
layers = []
filename = os.path.join(cur_dir, "data",
"basemap.tif")
self.baseLayer = QgsRasterLayer(filename, "basemap")
QgsMapLayerRegistry.instance().addMapLayer(self.baseLayer)
layers.append(QgsMapCanvasLayer(self.baseLayer))
Next, we want to set up our
tracks
layer. Since this is stored in a SpatiaLite database, we
have to use a
QgsDataSourceURI
object to connect the database to the map layer. The
following code shows how this is done:
uri = QgsDataSourceURI()
uri.setDatabase(os.path.join(cur_dir, "data",
"tracks.sqlite"))
uri.setDataSource('', 'tracks', 'GEOMETRY')