New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[processing] allow output directly on Spatialite tables #2423
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
python/plugins/processing/algs/qgis/spatialite_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
spatialite_utils.py | ||
--------------------- | ||
Date : November 2015 | ||
Copyright : (C) 2015 by René-Luc Dhont | ||
Email : volayaf at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
*************************************************************************** | ||
""" | ||
|
||
__author__ = 'René-Luc Dhont' | ||
__date__ = 'November 2015' | ||
__copyright__ = '(C) 2015, René-Luc Dhont' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
from pyspatialite import dbapi2 as sqlite | ||
|
||
class DbError(Exception): | ||
|
||
def __init__(self, message, query=None): | ||
# Save error. funny that the variables are in utf-8 | ||
self.message = unicode(message, 'utf-8') | ||
self.query = (unicode(query, 'utf-8') if query is not None else None) | ||
|
||
def __str__(self): | ||
return 'MESSAGE: %s\nQUERY: %s' % (self.message, self.query) | ||
|
||
class GeoDB: | ||
|
||
def __init__(self, uri=None): | ||
self.uri = uri | ||
self.dbname = uri.database() | ||
|
||
try: | ||
self.con = sqlite.connect(self.con_info()) | ||
|
||
except (sqlite.InterfaceError, sqlite.OperationalError) as e: | ||
raise DbError(e.message) | ||
|
||
self.has_spatialite = self.check_spatialite() | ||
if not self.has_spatialite: | ||
self.has_spatialite = self.init_spatialite() | ||
|
||
def con_info(self): | ||
return unicode(self.dbname) | ||
|
||
def init_spatialite(self): | ||
# Get spatialite version | ||
c = self.con.cursor() | ||
try: | ||
self._exec_sql(c, u'SELECT spatialite_version()') | ||
rep = c.fetchall() | ||
v = [int(a) for a in rep[0][0].split('.')] | ||
vv = v[0] * 100000 + v[1] * 1000 + v[2] * 10 | ||
|
||
# Add spatialite support | ||
if vv >= 401000: | ||
# 4.1 and above | ||
sql = "SELECT initspatialmetadata(1)" | ||
else: | ||
# Under 4.1 | ||
sql = "SELECT initspatialmetadata()" | ||
self._exec_sql_and_commit(sql) | ||
except: | ||
return False | ||
finally: | ||
self.con.close() | ||
|
||
try: | ||
self.con = sqlite.connect(self.con_info()) | ||
|
||
except (sqlite.InterfaceError, sqlite.OperationalError) as e: | ||
raise DbError(e.message) | ||
|
||
return self.check_spatialite() | ||
|
||
def check_spatialite(self): | ||
try: | ||
c = self.con.cursor() | ||
self._exec_sql(c, u"SELECT CheckSpatialMetaData()") | ||
v = c.fetchone()[0] | ||
self.has_geometry_columns = v == 1 or v == 3 | ||
self.has_spatialite4 = v == 3 | ||
except Exception as e: | ||
self.has_geometry_columns = False | ||
self.has_spatialite4 = False | ||
|
||
self.has_geometry_columns_access = self.has_geometry_columns | ||
return self.has_geometry_columns | ||
|
||
def _exec_sql(self, cursor, sql): | ||
try: | ||
cursor.execute(sql) | ||
except (sqlite.Error, sqlite.ProgrammingError, sqlite.Warning, sqlite.InterfaceError, sqlite.OperationalError) as e: | ||
raise DbError(e.message, sql) | ||
|
||
def _exec_sql_and_commit(self, sql): | ||
"""Tries to execute and commit some action, on error it rolls | ||
back the change. | ||
""" | ||
|
||
try: | ||
c = self.con.cursor() | ||
self._exec_sql(c, sql) | ||
self.con.commit() | ||
except DbError as e: | ||
self.con.rollback() | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IS this really like that?? or is it a typo? I am no expert in spatialite...so just asking, since it looks a bit confusing to me at first :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boolean does not exist in SQLite.
In the documentation http://www.sqlite.org/datatype3.html you'll find that Boolean values are stored as integers 0 (false) and 1 (true).