2012-06-08 08:25:42 +05:30
|
|
|
#!/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("\
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
"id INTEGER PRIMARY KEY,"\
|
|
|
|
"name TEXT UNIQUE,"\
|
|
|
|
"xml TEXT,"\
|
|
|
|
"groupid INTEGER)"
|
2012-06-08 08:25:42 +05:30
|
|
|
|
|
|
|
_colorramp = "CREATE TABLE colorramp("\
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
"id INTEGER PRIMARY KEY,"\
|
|
|
|
"name TEXT UNIQUE,"\
|
|
|
|
"xml TEXT,"\
|
|
|
|
"groupid INTEGER)"
|
2012-06-08 08:25:42 +05:30
|
|
|
|
|
|
|
_tag = "CREATE TABLE tag("\
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
"id INTEGER PRIMARY KEY,"\
|
|
|
|
"name TEXT)"
|
2012-06-08 08:25:42 +05:30
|
|
|
|
|
|
|
_tagmap = "CREATE TABLE tagmap("\
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
"tag_id INTEGER NOT NULL,"\
|
|
|
|
"symbol_id INTEGER)"
|
2012-06-08 08:25:42 +05:30
|
|
|
|
|
|
|
_symgroup = "CREATE TABLE symgroup("\
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
"id INTEGER PRIMARY KEY,"\
|
|
|
|
"name TEXT,"\
|
|
|
|
"parent INTEGER)"
|
2012-06-08 08:25:42 +05:30
|
|
|
|
2012-07-23 15:34:21 +05:30
|
|
|
_smartgroup = "CREATE TABLE smartgroup("\
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
"id INTEGER PRIMARY KEY,"\
|
|
|
|
"name TEXT,"\
|
|
|
|
"xml TEXT)"
|
2012-07-23 15:34:21 +05:30
|
|
|
|
2012-07-26 08:19:34 +05:30
|
|
|
_ctagmap = "CREATE TABLE ctagmap("\
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
"tag_id INTEGER NOT NULL,"\
|
|
|
|
"colorramp_id INTEGER)"
|
2012-07-26 08:19:34 +05:30
|
|
|
|
|
|
|
create_tables = [ _symbol, _colorramp, _tag, _tagmap, _ctagmap, _symgroup, _smartgroup ]
|
2012-06-08 08:25:42 +05:30
|
|
|
|
|
|
|
# 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:
|
2012-12-24 14:38:00 +05:30
|
|
|
c.execute( "INSERT INTO symbol VALUES (?,?,?,?)", ( None, symbol_name, symbol.toxml(), 0 ) )
|
2012-06-08 08:25:42 +05:30
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
|
|
|
# ColorRamps
|
|
|
|
colorramps = dom.getElementsByTagName( "colorramp" )
|
|
|
|
for ramp in colorramps:
|
|
|
|
ramp_name = ramp.getAttribute( "name" )
|
2012-12-24 14:38:00 +05:30
|
|
|
c.execute( "INSERT INTO colorramp VALUES (?,?,?,?)", ( None, ramp_name, ramp.toxml(), 0 ) )
|
2012-06-08 08:25:42 +05:30
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
# Finally close the sqlite cursor
|
|
|
|
c.close()
|