2015-01-12 18:14:30 +10:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import glob
|
|
|
|
|
2015-01-22 20:53:45 +01:00
|
|
|
from qgis.core import QgsApplication
|
2015-01-12 18:14:30 +10:00
|
|
|
|
|
|
|
def load_user_expressions(path):
|
|
|
|
"""
|
|
|
|
Load all user expressions from the given paths
|
|
|
|
"""
|
|
|
|
#Loop all py files and import them
|
|
|
|
modules = glob.glob(path + "/*.py")
|
|
|
|
names = [os.path.basename(f)[:-3] for f in modules]
|
|
|
|
for name in names:
|
|
|
|
if name == "__init__":
|
|
|
|
continue
|
|
|
|
# As user expression functions should be registed with qgsfunction
|
|
|
|
# just importing the file is enough to get it to load the functions into QGIS
|
|
|
|
__import__("expressions.{0}".format(name), locals(), globals())
|
|
|
|
|
|
|
|
|
|
|
|
userpythonhome = os.path.join(QgsApplication.qgisSettingsDirPath(), "python")
|
|
|
|
expressionspath = os.path.join(userpythonhome, "expressions")
|
|
|
|
startuppy = os.path.join(userpythonhome, "startup.py")
|
|
|
|
|
2015-01-20 09:54:30 +10:00
|
|
|
sys.path.append(userpythonhome)
|
|
|
|
|
2015-01-12 18:14:30 +10:00
|
|
|
# exec startup script
|
|
|
|
if os.path.exists(startuppy):
|
|
|
|
execfile(startuppy, locals(), globals())
|
|
|
|
|
|
|
|
if not os.path.exists(expressionspath):
|
|
|
|
os.makedirs(expressionspath)
|
|
|
|
|
|
|
|
initfile = os.path.join(expressionspath, "__init__.py")
|
|
|
|
if not os.path.exists(initfile):
|
|
|
|
open(initfile, "w").close()
|
|
|
|
|
|
|
|
import expressions
|
|
|
|
|
|
|
|
expressions.load = load_user_expressions
|
|
|
|
expressions.load(expressionspath)
|
2015-01-22 16:11:52 +10:00
|
|
|
expressions.template = """\"\"\"
|
2015-05-27 22:37:50 +10:00
|
|
|
Define new functions using @qgsfunction. feature and parent must always be the
|
|
|
|
last args. Use args=-1 to pass a list of values as arguments
|
2015-01-22 16:11:52 +10:00
|
|
|
\"\"\"
|
|
|
|
|
|
|
|
from qgis.core import *
|
|
|
|
from qgis.gui import *
|
|
|
|
|
2015-05-27 22:37:50 +10:00
|
|
|
@qgsfunction(group='Custom')
|
2015-01-22 16:11:52 +10:00
|
|
|
def func(value1, feature, parent):
|
2015-05-27 22:37:50 +10:00
|
|
|
return value1
|
2015-01-22 16:11:52 +10:00
|
|
|
"""
|