Python is very handy to perform quick (and useful) tasks such as parsing a text file.
In my case, I have a problem with the QGIS project files, since they *hardcode the absolute paths of RasterLite databases*.
You can read here the description of my problem:
Since I found no better solution, and I need to move my project to other computers, I decided to write a script that replaces the path of the database on the project file (in XML) by the path of the database on the target computer.
It is a simple script, and you only need to pay attention to two things:
– the script must be located under the same folder as the QGIS project file.
– the database must be located in the parent directory (the program will perform a check to see if this is true)
Initially I passed my ‘hardcoded path’ as a variable, but then, to make it useful for other people I made the script accept the hardcoded path as an argument (if you don’t set it, it will default to my hardcoded path: ‘/home/joana/git/GISforEAF/data
So you can access the script help like this:
>python fix_path.py -h
Usage: fix_path.py [options]
-h, --help show this help message and exit
-p PATH, --path=PATH path to be replaced
And you can pass a path as an argument, like this:
>python fix_path.py -p /home/joana/tmp
This means that the database path, that was pointing to ‘/home/joana/tmp’ in the original path, will now point to the parent directory in the current computer (whatever that is)
The python code is bellow, in the hope that it will be useful to someone:
from optparse import OptionParser
#****** Script to amend the rasterlite paths on QGIS project files *******
# Copyright 2013 Joana Simoes
# 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 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# For this script to work, the project files must have been saved on *
# '/home/joana/git/GISforEAF/data' (or amend variable savedDir) *
# Also the rasterlite database must be one directory up (..)
s = open(fname).read()
old = s
s = s.replace(savedDir, rasterDir)
if (old != s):
print ' Found!'
f = open(fname, 'w')
print ' Replaced!'
parser = OptionParser()
parser.add_option("-p", "--path", dest="pathname",
help="path to be replaced", metavar="PATH")
(options, args) = parser.parse_args()
with open(upDir + '/tutorials_raster.sqlite'): pass
print 'Error: the rasterlite database was not found in the parent directory!.'
print 'Rasterlite database found on ' + upDir + '/tutorials_raster.sqlite'
print 'String to be replaced: ' + savedDir
print 'Replace string: ' + upDir
for fname in dirList:
if fnmatch.fnmatch(fname, '*.qgs'):
print ' parsing ' + fname + '...'