1
|
|
2
|
"""QGIS Unit tests for QgsSpatialiteProvider
|
3
|
|
4
|
.. note:: This program is free software; you can redistribute it and/or modify
|
5
|
it under the terms of the GNU General Public License as published by
|
6
|
the Free Software Foundation; either version 2 of the License, or
|
7
|
(at your option) any later version.
|
8
|
"""
|
9
|
__author__ = 'Vincent Mora'
|
10
|
__date__ = '09/07/2013'
|
11
|
__copyright__ = 'Copyright 2013, The QGIS Project'
|
12
|
|
13
|
__revision__ = '$Format:%H$'
|
14
|
|
15
|
import os
|
16
|
import random
|
17
|
|
18
|
from qgis.core import *
|
19
|
from qgis.gui import *
|
20
|
|
21
|
from utilities import (getQgisTestApp,
|
22
|
TestCase,
|
23
|
unittest
|
24
|
)
|
25
|
|
26
|
from pyspatialite import dbapi2 as sqlite3
|
27
|
|
28
|
|
29
|
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
|
30
|
|
31
|
|
32
|
def die(error_message):
|
33
|
raise Exception(error_message)
|
34
|
|
35
|
class TestQgsSpatialiteProvider(TestCase):
|
36
|
|
37
|
@classmethod
|
38
|
def setUpClass(cls):
|
39
|
"""Run before all tests"""
|
40
|
|
41
|
if os.path.exists("test.sqlite") :
|
42
|
os.remove("test.sqlite")
|
43
|
con = sqlite3.connect("test.sqlite")
|
44
|
cur = con.cursor()
|
45
|
sql = "SELECT InitSpatialMetadata()"
|
46
|
cur.execute(sql)
|
47
|
|
48
|
|
49
|
sql = "CREATE TABLE test_mpg (id SERIAL PRIMARY KEY, name STRING NOT NULL)"
|
50
|
cur.execute(sql)
|
51
|
sql = "SELECT AddGeometryColumn('test_mpg', 'geometry', 4326, 'MULTIPOLYGON', 'XY')"
|
52
|
cur.execute(sql)
|
53
|
sql = "INSERT INTO test_mpg (name, geometry) "
|
54
|
sql += "VALUES ('multipolygon with 8 squares', GeomFromText('MULTIPOLYGON("
|
55
|
for i in range(0,4,2):
|
56
|
for j in range (0,4,2):
|
57
|
sql += "(("
|
58
|
sql += str(i) + " " + str(j) + ","
|
59
|
sql += str(i+1) + " " + str(j) + ","
|
60
|
sql += str(i+1) + " " + str(j+1) + ","
|
61
|
sql += str(i) + " " + str(j+1) + ","
|
62
|
sql += str(i) + " " + str(j)
|
63
|
sql += ")),"
|
64
|
sql = sql[:-1]
|
65
|
sql += ")', 4326))"
|
66
|
cur.execute(sql)
|
67
|
|
68
|
con.commit()
|
69
|
con.close()
|
70
|
|
71
|
@classmethod
|
72
|
def tearDownClass(cls):
|
73
|
"""Run after all tests"""
|
74
|
|
75
|
|
76
|
|
77
|
pass
|
78
|
|
79
|
def setUp(self):
|
80
|
"""Run before each test."""
|
81
|
pass
|
82
|
|
83
|
def tearDown(self):
|
84
|
"""Run after each test."""
|
85
|
pass
|
86
|
|
87
|
def test_SplitMultipolygon(self):
|
88
|
"""Create spatialite database"""
|
89
|
layer = QgsVectorLayer("dbname=test.sqlite table=test_mpg (geometry)", "test_mpg", "spatialite")
|
90
|
assert(layer.isValid())
|
91
|
assert(layer.hasGeometryType())
|
92
|
layer.featureCount() == 1 or die("we should have 1 features")
|
93
|
layer.startEditing()
|
94
|
layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0)==0 or die("error in split of one polygon of multipolygon")
|
95
|
layer.splitFeatures([QgsPoint(2.5, -0.5), QgsPoint(2.5, 4)], 0)==0 or die("error in split of two polygons of multipolygon at a time")
|
96
|
layer.commitChanges() or die("this commit should work")
|
97
|
|
98
|
|
99
|
|
100
|
|
101
|
|
102
|
|
103
|
if __name__ == '__main__':
|
104
|
unittest.main()
|
105
|
|
106
|
|