2013-04-11 10:07:35 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
general.py
|
|
|
|
---------------------
|
|
|
|
Date : April 2013
|
|
|
|
Copyright : (C) 2013 by Victor Olaya
|
|
|
|
Email : volayaf 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-08-04 09:10:08 +02:00
|
|
|
from __future__ import print_function
|
2016-09-21 18:24:26 +02:00
|
|
|
from future import standard_library
|
|
|
|
standard_library.install_aliases()
|
|
|
|
from builtins import str
|
2013-04-11 10:07:35 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'April 2013'
|
|
|
|
__copyright__ = '(C) 2013, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-11 10:07:35 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-11 10:07:35 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-04-01 11:25:00 +03:00
|
|
|
import os
|
2016-08-04 09:10:08 +02:00
|
|
|
try:
|
|
|
|
import configparser
|
|
|
|
except ImportError:
|
2016-09-21 18:24:26 +02:00
|
|
|
import configparser as configparser
|
2016-04-01 11:25:00 +03:00
|
|
|
|
2017-04-25 20:03:39 +10:00
|
|
|
from qgis.core import (QgsApplication)
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.Processing import Processing
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterSelection
|
2016-06-09 14:57:01 +03:00
|
|
|
from processing.gui.Postprocessing import handleAlgorithmResults
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-11 10:07:35 +02:00
|
|
|
|
2017-04-04 11:04:36 +10:00
|
|
|
def algorithmOptions(id):
|
2017-03-22 19:03:36 +02:00
|
|
|
"""Prints all algorithm options with their values.
|
|
|
|
"""
|
2017-04-04 11:04:36 +10:00
|
|
|
alg = QgsApplication.processingRegistry().algorithmById(id)
|
2013-10-01 20:52:22 +03:00
|
|
|
if alg is not None:
|
2017-03-22 16:17:05 +02:00
|
|
|
opts = ''
|
2013-04-11 10:07:35 +02:00
|
|
|
for param in alg.parameters:
|
|
|
|
if isinstance(param, ParameterSelection):
|
2017-03-22 16:17:05 +02:00
|
|
|
opts += '{} ({})\n'.format(param.name, param.description)
|
|
|
|
for option in enumerate(param.options):
|
|
|
|
opts += '\t{} - {}\n'.format(option[0], option[1])
|
|
|
|
print(opts)
|
2013-04-11 10:07:35 +02:00
|
|
|
else:
|
2017-04-04 11:04:36 +10:00
|
|
|
print('Algorithm "{}" not found.'.format(id))
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-11 10:07:35 +02:00
|
|
|
|
2017-04-04 11:04:36 +10:00
|
|
|
def algorithmHelp(id):
|
2017-03-22 19:03:36 +02:00
|
|
|
"""Prints algorithm parameters with their types. Also
|
|
|
|
provides information about options if any.
|
|
|
|
"""
|
2017-04-04 11:04:36 +10:00
|
|
|
alg = QgsApplication.processingRegistry().algorithmById(id)
|
2013-10-01 20:52:22 +03:00
|
|
|
if alg is not None:
|
2015-01-22 13:23:08 +01:00
|
|
|
alg = alg.getCopy()
|
2016-09-21 18:24:26 +02:00
|
|
|
print(str(alg))
|
2017-04-04 11:04:36 +10:00
|
|
|
algorithmOptions(id)
|
2013-04-11 10:07:35 +02:00
|
|
|
else:
|
2017-04-04 11:04:36 +10:00
|
|
|
print('Algorithm "{}" not found.'.format(id))
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-11 10:07:35 +02:00
|
|
|
|
2017-03-22 19:17:12 +02:00
|
|
|
def run(algOrName, *args, **kwargs):
|
2017-03-22 19:03:36 +02:00
|
|
|
"""Executes given algorithm and returns its outputs as dictionary
|
|
|
|
object.
|
|
|
|
"""
|
2015-08-14 14:12:01 +02:00
|
|
|
alg = Processing.runAlgorithm(algOrName, None, *args, **kwargs)
|
2013-04-11 10:07:35 +02:00
|
|
|
if alg is not None:
|
|
|
|
return alg.getOutputValuesAsDictionary()
|
|
|
|
|
|
|
|
|
2017-03-22 19:17:12 +02:00
|
|
|
def runAndLoadResults(name, *args, **kwargs):
|
2017-03-22 19:03:36 +02:00
|
|
|
"""Executes given algorithm and load its results into QGIS project
|
|
|
|
when possible.
|
|
|
|
"""
|
2015-08-14 14:12:01 +02:00
|
|
|
return Processing.runAlgorithm(name, handleAlgorithmResults, *args, **kwargs)
|
2016-04-01 11:25:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def version():
|
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
2016-11-07 10:03:15 +10:00
|
|
|
cfg = configparser.ConfigParser()
|
2016-04-01 11:25:00 +03:00
|
|
|
cfg.read(os.path.join(pluginPath, 'metadata.txt'))
|
|
|
|
ver = cfg.get('general', 'version').split('.')
|
|
|
|
return 10000 * int(ver[0]) + 100 * int(ver[1]) + int(ver[2])
|