[Qgis-developer] Writing layer w/ custom CRS to Spatialite Database

Weier, Mitchell S. mweier at nd.gov
Fri Jun 5 06:32:33 PDT 2015


Hi All,

Is it possible to write a layer w/ a custom CRS to a spatialite database with pyQgis?  Here’s what I’ve tried:

1) create an empty spatialite database

2) add custom QGIS crs to spatial_ref_sys table in spatialite database using the following sql command:

sql = """INSERT INTO spatial_ref_sys(srid, auth_name, auth_srid, ref_sys_name, proj4text, srtext) VALUES({0}, '', '', '{3}', '{1}', '{2}')""".format(layer.crs().authid()[5:], layer.crs().toProj4(), layer.crs().toWkt(), layer.crs().description())

3) write layer to database using the following:
def loadVectorsIntoDB(layers, dbase):
#database import options
options = {}
options['overwrite'] = True
options['forceSinglePartGeometryType'] = True
uri = QgsDataSourceURI()
uri.setDatabase(dbase)
for layer in layers:
uri.setDataSource('',layer.name(),'the_geom')
ret, errMsg = QgsVectorLayerImport.importLayer(layer, uri.uri(), 'spatialite', layer.crs(), False, False, options)
del uri

The layer will be written to the database, but it is imported with an srid of 0 which is an Undefined - Geographic Long/Lat coordinate system, not the custom CRS.

I have also tried to write a layer using DB manager instead of step 3 and got the same result.  Is it possible to add the CRS to the spatial_ref_sys table in such a way it will match the  layer.crs() argument in the QgsVectorLayerImport.importLayer() function?

Any hints would be great!

Thanks,

Mitch

Mitchell Weier, P.E.
Water Resource Engineer
North Dakota State Water Commission
900 E Boulevard Avenue, Dept.770
Bismarck, ND 58505-0850
(701) 328 1402 phone
(701) 328 3696 fax
mweier at nd.gov<mailto:mweier at nd.gov>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20150605/2895d059/attachment.html>


More information about the Qgis-developer mailing list