File: symbol_xml2db.py

package info (click to toggle)
qgis 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 374,696 kB
  • ctags: 66,263
  • sloc: cpp: 396,139; ansic: 241,070; python: 130,609; xml: 14,884; perl: 1,290; sh: 1,287; sql: 500; yacc: 268; lex: 242; makefile: 168
file content (105 lines) | stat: -rw-r--r-- 3,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
"""
/***************************************************************************
                               symbol_xml2db.py
                              -------------------
    begin                : 26-5-2012
    copyright            : (C) 2012 by Arunmozhi
    email                : aruntheguy 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.                                   *
 *                                                                         *
 ***************************************************************************/

    The script creates a sqlite3 Db for storing the symbols which will be
    shipped with QGIS by default. It then converts symbols and colorramps
    in the symbology_ng_style.xml to the database table entries.
"""
import sqlite3

from xml.dom.minidom import parse, parseString

xmlfile = "../resources/symbology-ng-style.xml"
dbfile = "../resources/symbology-ng-style.db"

_symbol = "CREATE TABLE symbol("\
        "id INTEGER PRIMARY KEY,"\
        "name TEXT UNIQUE,"\
        "xml TEXT,"\
        "groupid INTEGER)"

_colorramp = "CREATE TABLE colorramp("\
        "id INTEGER PRIMARY KEY,"\
        "name TEXT UNIQUE,"\
        "xml TEXT,"\
        "groupid INTEGER)"

_tag = "CREATE TABLE tag("\
        "id INTEGER PRIMARY KEY,"\
        "name TEXT)"

_tagmap = "CREATE TABLE tagmap("\
        "tag_id INTEGER NOT NULL,"\
        "symbol_id INTEGER)"

_symgroup = "CREATE TABLE symgroup("\
        "id INTEGER PRIMARY KEY,"\
        "name TEXT,"\
        "parent INTEGER)"

_smartgroup = "CREATE TABLE smartgroup("\
        "id INTEGER PRIMARY KEY,"\
        "name TEXT,"\
        "xml TEXT)"

_ctagmap = "CREATE TABLE ctagmap("\
        "tag_id INTEGER NOT NULL,"\
        "colorramp_id INTEGER)"

create_tables = [ _symbol, _colorramp, _tag, _tagmap, _ctagmap, _symgroup, _smartgroup ]

# Create the DB with required Schema
conn = sqlite3.connect( dbfile )
c = conn.cursor()
print "Creating tables in the Database\n"
for table in create_tables:
    try:
        c.execute( table )
        print table
    except sqlite3.OperationalError as e:
        pass
    conn.commit()

# parse the XML file &  write symbol into DB
dom = parse( xmlfile )
symbols = dom.getElementsByTagName( "symbol" )
for symbol in symbols:
    symbol_name = symbol.getAttribute( "name" )
    if '@' in symbol_name:
        parts = symbol_name.split('@')
        parent_name = parts[1]
        layerno = int(parts[2])
        c.execute( "SELECT xml FROM symbol WHERE name=(?)", (parent_name,) )
        symdom = parseString( c.fetchone()[0] ).getElementsByTagName( 'symbol' )[0]
        symdom.getElementsByTagName( "layer" )[ layerno ].appendChild( symbol )
        c.execute( "UPDATE symbol SET xml=? WHERE name=?", ( symdom.toxml(), parent_name ))
    else:
        c.execute( "INSERT INTO symbol VALUES (?,?,?,?)", ( None, symbol_name, symbol.toxml(), 0 ) )
conn.commit()


# ColorRamps
colorramps = dom.getElementsByTagName( "colorramp" )
for ramp in colorramps:
    ramp_name = ramp.getAttribute( "name" )
    c.execute( "INSERT INTO colorramp VALUES (?,?,?,?)", ( None, ramp_name, ramp.toxml(), 0 ) )
conn.commit()

# Finally close the sqlite cursor
c.close()