[Qgis-developer] Creating a spatialite layer in a python plugin
Daniel
danielvaz at gmail.com
Mon Jul 15 10:43:23 PDT 2013
Leaving source code for future reference:
Exporting Shapefile or Vector Layer to Spatialite using QgsVectorFileWriter
# -*- coding: utf-8 -*-
import sys
from qgis.core import *
app = QgsApplication(sys.argv, False)
# need to locate srs.db
app.setPkgDataPath( '/usr/local/share/qgis' )
# need to locate qgis libraries
app.setPrefixPath( '/usr/local' )
# need to locate c++ plugins (*.so)
app.setPluginPath( '/usr/local/lib/qgis/plugins' )
app.initQgis()
# show the environment variables
print 'lib path:', app.libraryPath()
print 'plugin path:', app.pluginPath()
print 'srs.db:', app.srsDbFilePath()
#
vlayer = QgsVectorLayer('track.shp', 'layer_name_you_like', 'ogr')
#
crs = vlayer.crs()
#
if not crs.isValid():
# choose a 4326 - WGS84 CRS
crs = QgsCoordinateReferenceSystem( 4326,
QgsCoordinateReferenceSystem.EpsgCrsId )
error = QgsVectorFileWriter.writeAsVectorFormat( vlayer,
'test.sqlite',
'System',
crs,
'SQLite',
False,
None,
["SPATIALITE=YES",] )
if error != QgsVectorFileWriter.NoError:
print 'Error number:', error
On Mon, Jul 15, 2013 at 2:19 PM, Daniel <danielvaz at gmail.com> wrote:
> Answer:
>
> vlayer = QgsVectorLayer('track.shp', 'layer_name_you_like', 'ogr')
>
> error_msg = ''
>
> error = QgsVectorFileWriter.writeAsVectorFormat( vlayer, 'test.sqlite',
> 'System', vlayer.crs(), 'SQLite', False, error_msg, [ "SPATIALITE=YES" , ] )
>
> where:
>
> vlayer => vector layer which will be exported
> to spatialite
> 'test.sqlite' => absolute path to new spatialite
> database file
> 'System' => string that represents the file
> encoding
> vlayer.crs() => vector layer crs
> 'SQLite' => provider name
> False => export onlySelected?
> error_msg => if there is any error message will be
> put in that variable (if user don't care about error handling, set to None)
> [ "SPATIALITE=YES" , ] => database options. (here is the trick)
>
>
>
> Also, there is a class
> http://www.qgis.org/api/classQgsVectorLayerImport.html
>
> But, I didn't have success in create an empty database using the
> QgsVectorLayerImport. I think that class (QgsVectorLayerImport) expects a
> created spatialite database.
>
> --
> Daniel Vaz
>
--
Daniel Vaz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130715/39c62c76/attachment-0001.html>
More information about the Qgis-developer
mailing list