[Qgis-developer] DB Manager, spatialite 4 and Python

Ivan Minčík ivan.mincik at gmail.com
Thu Apr 3 13:12:48 PDT 2014


On Sat, Mar 29, 2014 at 8:35 PM, Ivan Minčík <ivan.mincik at gmail.com> wrote:

>
> Hi all,
> I was thinking how to improve situation with pyspatialite and spatialite 4
> incompatibility. I have found that loading dynamic libspatialite module
> instead pyspatialite is easier than I thought, at least in Ubuntu 12.04.
>
> Here is working code example (python-pysqlite2 required):
>
> from pysqlite2 import dbapi2 as sqlite3
> conn = sqlite3.connect(":memory:")
> conn.enable_load_extension(True)
> cur = conn.cursor()
> cur.execute("SELECT load_extension('libspatialite.so.5')")
> cur.execute("SELECT ST_Length(ST_GeometryFromText('LINESTRING(30 10, 10 30, 40 40)'))")
> print cur.fetchone()[0]
> conn.close()
>
>
> Has anybody tried to replace pyspatialite with python-pysqlite2 and
> dynamic libspatialite loading ?
>


OK, I have tried following trivial replacement of pyspatialite with
pysqlite2 and it works great. Can we implement it?

---
/usr/share/qgis/python/plugins/db_manager/db_plugins/spatialite/connector.py
2014-04-01 19:48:56.000000000 +0200
+++ connector.py    2014-04-03 22:09:10.044065426 +0200
@@ -26,7 +26,7 @@
 from ..connector import DBConnector
 from ..plugin import ConnectionError, DbError, Table

-from pyspatialite import dbapi2 as sqlite
+from pysqlite2 import dbapi2 as sqlite

 def classFactory():
     return SpatiaLiteDBConnector
@@ -41,6 +41,10 @@

         try:
             self.connection = sqlite.connect( self._connectionInfo() )
+            self.connection.enable_load_extension(True)
+            self.cursor = self.connection.cursor()
+            self.cursor.execute("SELECT
load_extension('libspatialite.so.5')")
+            self.cursor.close()

         except self.connection_error_types(), e:
             raise ConnectionError(e)

-- 
Ivan Minčík
ivan.mincik at gmail.com  GPG: 0x79529A1E
http://imincik.github.io/0x79529A1E.key
ivan.mincik at gista.sk     GPG: 0xD714B02C
http://imincik.github.io/0xD714B02C.key
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20140403/f3736cb8/attachment.html>


More information about the Qgis-developer mailing list