Re: [Qgis-developer] Creating a spatialite layer in a python plugin

2013-07-15 Thread Daniel
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
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


Re: [Qgis-developer] Creating a spatialite layer in a python plugin

2013-07-15 Thread Daniel
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 daniel...@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
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer


[Qgis-developer] Creating a spatialite layer in a python plugin

2013-07-12 Thread Daniel
Is it possible to create a spatialite layer using python bindings?

I compiled qgis version 1.8 from source on Ubuntu 12.04

GDAL 1.9.2
Spatialite 3.0.0~beta20110817-3

I enable PYSPATIALITE from ccmake menu.

From plugin if i call the method:

QgsVectorFileWriter.supportedFiltersAndFormats()

I have the following output, where there is not SpatiaLite format available.

GeoJSON [OGR] (*.geojson *.GEOJSON)
GeoRSS [OGR] (*.xml *.XML)
Generic Mapping Tools [GMT] [OGR] (*.gmt *.GMT)
SQLite [OGR] (*.sqlite *.SQLITE)
ESRI Shapefile [OGR] (*.shp *.SHP)
INTERLIS 1 [OGR] (*.itf *.xml *.ili *.ITF *.XML *.ILI)
Geography Markup Language [GML] [OGR] (*.gml *.GML)
Geoconcept [OGR] (*.gxt *.txt *.GXT *.TXT)
AutoCAD DXF [OGR] (*.dxf *.DXF)
INTERLIS 2 [OGR] (*.itf *.xml *.ili *.ITF *.XML *.ILI)
Microstation DGN [OGR] (*.dgn *.DGN)
Comma Separated Value [OGR] (*.csv *.CSV)
Atlas BNA [OGR] (*.bna *.BNA)
GPS eXchange Format [GPX] [OGR] (*.gpx *.GPX)
S-57 Base file [OGR] (*.000 *.000)
Keyhole Markup Language [KML] [OGR] (*.kml *.KML)
Mapinfo File [OGR] (*.mif *.tab *.MIF *.TAB)


What I am missing?

Thanks in advance.

-- 
Daniel Vaz
___
Qgis-developer mailing list
Qgis-developer@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer