def setUpClass(cls):

        cls.qgs = QgsApplication([], False)
        cls.qgs.initQgis()
Example #2
0
def main(user='', masterpass='', pkidir=''):
    if not user or not pkidir:
        print 'Missing parameters for user or pkidir'
        print '  user: {0}'.format(user)
        print '  pkidir: {0}'.format(pkidir)
        sys.exit(1)

    # Get user's pre-defined QGIS master password.
    # This can be done in a variety of ways, depending upon user auth
    # systems (queried from LDAP, etc.), using a variety of Python packages.
    # As an example, we could hard-code define it as a standard password that
    # must be changed later by user, OR if we know the user's defined password.
    #masterpass = some_user_query_function(user)

    if not masterpass:
        print 'Master password must be defined'
        sys.exit(1)

    print 'Setting authentication config using:'
    print '  user: {0}'.format(user)
    print '  master pass: {0}'.format(masterpass)
    print '  pkidir: {0}'.format(pkidir)

    # instantiate QGIS
    qgsapp = QgsApplication(sys.argv, True)

    # These are for referencing the correct QSettings for the QGIS app
    QCoreApplication.setOrganizationName('QGIS')
    QCoreApplication.setOrganizationDomain('qgis.org')
    QCoreApplication.setApplicationName('QGIS2')

    # Initialize QGIS
    qgsapp.initQgis()
    print qgsapp.showSettings()

    # Initialize the auth system
    # noinspection PyArgumentList
    authm = QgsAuthManager.instance()
    authm.init()
    # This will use the standard qgis-auth.db location, but the rest of this
    # script will not work if qgis-auth.db already exists and you do NOT know
    # the user's chosen master password already stored in it.

    # If you want to generate individual qgis-auth.db for a list of users, just
    # do:
    #   authdbdir = tempfile.mkdtemp()
    #   authm.init(authdbdir)
    # Note that the saved paths to PKI components in the db will need to be the
    # same absolute paths as when the auth db is copied to the user's machine.
    # This means paths with the current user's name in them will not work when
    # copied to a different user (unless names are the same).

    print authm.authenticationDbPath()

    # Define pool of users and loop through them, or use the current user.
    #users = ["user"]
    #for user in users:

    # Set master password for QGIS and (optionally) store it in qgis-auth.db.
    # This also verifies the set password against by comparing password
    # against its derived hash stored in auth db.
    if not authm.setMasterPassword(masterpass, True):
        print 'Failed to verify or store/verify password'
        sys.exit(1)

    # Now that we have a master password set/stored, we can use it to
    # encrypt and store authentication configurations.
    # There are 3 configurations that can be stored (as of Nov 2014), and
    # examples of their initialization are in the unit tests for
    # QGIS-with-PKI source tree (test_qgsauthsystem_api-sample.py).

    # Add authentication configuration.
    # You can add as many auth configs as needed, but only one can be linked
    # to a given custom server config; although, you can create as many custom
    # server configs as needed. In this example, we are defining only one auth
    # config and linking it to multiple custom server configs, representing
    # different OWS services located at the same domain.

    # NOTE: PKI file components need to *already* exist on the filesystem in a
    # location that doesn't change, as their paths are stored in the auth db.

    # # Basic configuration
    # configname = 'My Basic Config'
    # config = QgsAuthConfigBasic()
    # config.setName(kind)
    # config.setUri('https://localhost:8443')
    # config.setUsername('username')
    # config.setPassword('password')  # will need queried or set per user
    # config.setRealm('Realm')

    # ^^  OR  vv

    # # PKI-Paths (PEM-based) configuration
    # configname = 'My PKI Paths Config'
    # config = QgsAuthConfigPkiPaths()
    # config.setName(configname)
    # config.setUri('https://localhost:8443')
    # config.setCertId(os.path.join(pkidir, '{0}_cert.pem'.format(user)))
    # config.setKeyId(os.path.join(pkidir, '{0}_key.pem'.format(user)))
    # config.setKeyPassphrase('')  # will need queried and set per user
    # config.setIssuerId(os.path.join(pkidir, 'ca.pem'))
    # config.setIssuerSelfSigned(True)

    # ^^  OR  vv

    # PKI-PKCS#12 (*.p12-based) configuration
    configname = 'My PKI PKCS#12 Config'
    config = QgsAuthConfigPkiPkcs12()
    config.setName(configname)
    config.setUri('https://localhost:8443')
    config.setBundlePath(os.path.join(pkidir, '{0}.p12'.format(user)))
    config.setBundlePassphrase(
        'password')  # will need queried and set per user
    config.setIssuerPath(os.path.join(pkidir, 'ca.pem'))
    config.setIssuerSelfSigned(True)

    # Securely store the config in database (encrypted with master password)
    res = authm.storeAuthenticationConfig(config)
    if not res[0]:
        print 'Failed to store {0} config'.format(configname)
        sys.exit(1)

    # The auth config has been given a unique ID from the auth system when it
    # was stored; retrieve it, so it can be linked to a custom server config.
    configid = config.id()

    # If the user does not have the OWS connection(s) that this auth config is
    # meant to connect to, define now.
    # NOTE: this assumes the individual connections do not already exist. If the
    # connection settings do exist, this will OVERWRITE them.

    settings = QSettings()  # get application's settings object

    print 'settings.fileName(): {0}'.format(settings.fileName())
    print 'settings.organizationName(): {0}'.format(
        settings.organizationName())
    print 'settings.applicationName(): {0}'.format(settings.applicationName())

    # WMS
    connkind = 'WMS'
    connname = 'My {0} SSL Server'.format(connkind)
    credskey = '/Qgis/{0}/{1}'.format(connkind, connname)
    connkey = '/Qgis/connections-{0}/{1}'.format(connkind.lower(), connname)

    settings.setValue(credskey + '/authid', configid)  # link to auth config
    settings.setValue(credskey + '/username',
                      '')  # deprecated; use auth config
    settings.setValue(credskey + '/password',
                      '')  # deprecated; use auth config

    settings.setValue(connkey + '/url', 'https://localhost:8443/geoserver/wms')

    # Optional settings for WMS (these are the defaults)
    # dpiMode: 0=Off, 1=QGIS, 2=UMN, 4=GeoServer, 7=All (default)
    settings.setValue(connkey + '/dpiMode', 7)
    settings.setValue(connkey + '/ignoreAxisOrientation', False)
    settings.setValue(connkey + '/ignoreGetFeatureInfoURI', False)
    settings.setValue(connkey + '/ignoreGetMapURI', False)
    settings.setValue(connkey + '/invertAxisOrientation', False)
    settings.setValue(connkey + '/referer', '')
    settings.setValue(connkey + '/smoothPixmapTransform', False)

    # WCS
    connkind = 'WCS'
    connname = 'My {0} SSL Server'.format(connkind)
    credskey = '/Qgis/{0}/{1}'.format(connkind, connname)
    connkey = '/Qgis/connections-{0}/{1}'.format(connkind.lower(), connname)

    settings.setValue(credskey + '/authid', configid)  # link to auth config
    settings.setValue(credskey + '/username',
                      '')  # deprecated; use auth config
    settings.setValue(credskey + '/password',
                      '')  # deprecated; use auth config

    settings.setValue(connkey + '/url', 'https://localhost:8443/geoserver/wcs')

    # Optional settings for WCS (these are the defaults)
    # dpiMode: 0=Off, 1=QGIS, 2=UMN, 4=GeoServer, 7=All (default)
    settings.setValue(connkey + '/dpiMode', 7)
    settings.setValue(connkey + '/ignoreAxisOrientation', False)
    settings.setValue(connkey + '/ignoreGetMapURI', False)
    settings.setValue(connkey + '/invertAxisOrientation', False)
    settings.setValue(connkey + '/referer', '')
    settings.setValue(connkey + '/smoothPixmapTransform', False)

    # WFS
    connkind = 'WFS'
    connname = 'My {0} SSL Server'.format(connkind)
    credskey = '/Qgis/{0}/{1}'.format(connkind, connname)
    connkey = '/Qgis/connections-{0}/{1}'.format(connkind.lower(), connname)

    settings.setValue(credskey + '/authid', configid)  # link to auth config
    settings.setValue(credskey + '/username',
                      '')  # deprecated; use auth config
    settings.setValue(credskey + '/password',
                      '')  # deprecated; use auth config

    settings.setValue(connkey + '/url', 'https://localhost:8443/geoserver/wfs')

    # Optional settings for WFS (these are the defaults)
    settings.setValue(connkey + '/referer', '')
Example #3
0
 def setUpClass(cls):
     cls.app = QgsApplication([], False)
Example #4
0
QGIS_SERVER_PKI_CERTIFICATE = os.environ.get('QGIS_SERVER_PKI_CERTIFICATE')
QGIS_SERVER_PKI_KEY = os.environ.get('QGIS_SERVER_PKI_KEY')
QGIS_SERVER_PKI_AUTHORITY = os.environ.get('QGIS_SERVER_PKI_AUTHORITY')
QGIS_SERVER_PKI_USERNAME = os.environ.get('QGIS_SERVER_PKI_USERNAME')

# Check if PKI - https is enabled
https = (QGIS_SERVER_PKI_CERTIFICATE is not None and
         os.path.isfile(QGIS_SERVER_PKI_CERTIFICATE) and
         QGIS_SERVER_PKI_KEY is not None and
         os.path.isfile(QGIS_SERVER_PKI_KEY) and
         QGIS_SERVER_PKI_AUTHORITY is not None and
         os.path.isfile(QGIS_SERVER_PKI_AUTHORITY) and
         QGIS_SERVER_PKI_USERNAME)


qgs_app = QgsApplication([], False)
qgs_server = QgsServer()


if os.environ.get('QGIS_SERVER_HTTP_BASIC_AUTH') is not None:
    import base64

    class HTTPBasicFilter(QgsServerFilter):

        def requestReady(self):
            handler = self.serverInterface().requestHandler()
            auth = self.serverInterface().requestHandler().requestHeader('HTTP_AUTHORIZATION')
            if auth:
                username, password = base64.b64decode(auth[6:]).split(b':')
                if (username.decode('utf-8') == os.environ.get('QGIS_SERVER_USERNAME', 'username') and
                        password.decode('utf-8') == os.environ.get('QGIS_SERVER_PASSWORD', 'password')):
Example #5
0
    plugins_dir = os.path.dirname(plugin_dir)

    # python path setting
    sys.path.append(plugins_dir)

    # initialize output directory
    initOutputDir()

    plugin_name = os.path.basename(plugin_dir)
    suite = unittest.TestLoader().discover(plugin_name + ".tests")
    unittest.TextTestRunner(verbosity=2).run(suite)


if __name__ == "__main__":
    gui_mode = True
    QGISAPP = QgsApplication(sys.argv, gui_mode)
    QGISAPP.initQgis()
    print "=" * 70
    print QGISAPP.showSettings()
    print "=" * 70

    # set up network disk cache
    manager = QgsNetworkAccessManager.instance()
    cache = QNetworkDiskCache(manager)
    cache.setCacheDirectory(pluginPath(os.path.join("tests", "cache")))
    cache.setMaximumCacheSize(50 * 1024 * 1024)
    manager.setCache(cache)

    # run test!
    runTest()
#########################################################################################
#########################################################################################
# SETUP PREAMBLE FOR RUNNING STANDALONE SCRIPTS.
# NOT NECESSARY IF YOU ARE RUNNING THIS INSIDE THE QGIS GUI.
print('preliminary setup')
import sys
import os

from qgis.core import (QgsApplication)

from qgis.analysis import QgsNativeAlgorithms

# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix
QgsApplication.setPrefixPath('C:/OSGeo4W64/apps/qgis', True)
qgs = QgsApplication([], False)
qgs.initQgis()

# Add the path to Processing framework
sys.path.append('C:/OSGeo4W64/apps/qgis/python/plugins')

# Import and initialize Processing framework
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
#########################################################################################
#########################################################################################

from geoprocess import GeoProcess

# set paths to inputs and outputs
Example #7
0
def qgs_init(pp):
    """ PyQGIS initialization. """
    QgsApplication.setPrefixPath(pp, True)
    qgs = QgsApplication([], False)
    qgs.initQgis()
    return qgs
def start_qgis_application(enable_gui: bool = False,
                           enable_processing: bool = True,
                           verbose: bool = False,
                           cleanup: bool = True) -> 'QgsApplication':
    """ Start qgis application

        :param boolean enable_gui: Enable graphical interface, default to False
        :param boolean enable_processing: Enable processing, default to False
        :param boolean verbose: Output qgis settings, default to False
        :param boolean cleanup: Register atexit hook to close qgisapplication on exit().
            Note that prevents qgis to segfault when exiting. Default to True.
    """

    os.environ['QGIS_NO_OVERRIDE_IMPORT'] = '1'
    os.environ['QGIS_DISABLE_MESSAGE_HOOKS'] = '1'

    setup_qgis_paths()

    from qgis.core import QgsApplication, Qgis

    if QgsApplication.QGIS_APPLICATION_NAME != "QGIS3":
        raise RuntimeError("You need QGIS3 (found %s)" %
                           QgsApplication.QGIS_APPLICATION_NAME)

    if not enable_gui:
        #  We MUST set the QT_QPA_PLATFORM to prevent
        #  Qt trying to connect to display in containers
        if os.environ.get('DISPLAY') is None:
            print("Warning: Setting Qt offscreen mode")
            os.environ['QT_QPA_PLATFORM'] = 'offscreen'

    qgis_prefix = os.environ.get('QGIS_HOME', '/usr')

    global _qgis_application

    if _qgis_application is not None:
        print("Qgis already initialized", file=sys.stderr, flush=True)
        return _qgis_application

    _qgis_application = QgsApplication([], enable_gui)

    # Install logger hook
    install_logger_hook(verbose=verbose)

    _qgis_application.setPrefixPath(qgis_prefix, True)
    _qgis_application.initQgis()

    if cleanup:
        # Closing QgsApplication on exit will
        # prevent our app to segfault on exit()
        import atexit

        @atexit.register
        def exitQgis():
            global _qgis_application
            if _qgis_application:
                _qgis_application.exitQgis()
                del _qgis_application

    if verbose:
        print(_qgis_application.showSettings())

    if enable_processing:
        init_processing(verbose)

    if verbose:
        print("Qgis %s initialized......" % Qgis.QGIS_VERSION)

    return _qgis_application
Example #9
0
def get_qgis_app(cleanup=True):
    """ Start one QGIS application to test against.

    :returns: Handle to QGIS app, canvas, iface and parent. If there are any
        errors the tuple members will be returned as None.
    :rtype: (QgsApplication, CANVAS, IFACE, PARENT)

    If QGIS is already running the handle to that app will be returned.
    """

    global QGIS_APP, PARENT, IFACE, CANVAS  # pylint: disable=W0603

    if iface:
        from qgis.core import QgsApplication
        QGIS_APP = QgsApplication
        CANVAS = iface.mapCanvas()
        PARENT = iface.mainWindow()
        IFACE = iface
        return QGIS_APP, CANVAS, IFACE, PARENT

    from qgis.core import QgsApplication
    from qgis.gui import QgsMapCanvas
    from qgis.PyQt.QtCore import QSize
    from qgis.PyQt.QtWidgets import QWidget
    from .qgis_interface import QgisInterface

    global QGISAPP  # pylint: disable=global-variable-undefined

    try:
        QGISAPP
    except NameError:
        myGuiFlag = True  # All test will run qgis in gui mode

        # In python3 we need to convert to a bytes object (or should
        # QgsApplication accept a QString instead of const char* ?)
        try:
            argvb = list(map(os.fsencode, sys.argv))
        except AttributeError:
            argvb = sys.argv

        # Note: QGIS_PREFIX_PATH is evaluated in QgsApplication -
        # no need to mess with it here.
        QGISAPP = QgsApplication(argvb, myGuiFlag)

        QGISAPP.initQgis()
        s = QGISAPP.showSettings()
        LOGGER.debug(s)

        def debug_log_message(message, tag, level):
            """
            Prints a debug message to a log
            :param message: message to print
            :param tag: log tag
            :param level: log message level (severity)
            :return:
            """
            print('{}({}): {}'.format(tag, level, message))

        QgsApplication.instance().messageLog().messageReceived.connect(
            debug_log_message)

        if cleanup:
            import atexit

            @atexit.register
            def exitQgis():  # pylint: disable=unused-variable
                """
                Gracefully closes the QgsApplication instance
                """
                try:
                    QGISAPP.exitQgis()  # pylint: disable=used-before-assignment
                    QGISAPP = None  # pylint: disable=redefined-outer-name
                except NameError:
                    pass

    if PARENT is None:
        # noinspection PyPep8Naming
        PARENT = QWidget()

    if CANVAS is None:
        # noinspection PyPep8Naming
        CANVAS = QgsMapCanvas(PARENT)
        CANVAS.resize(QSize(400, 400))

    if IFACE is None:
        # QgisInterface is a stub implementation of the QGIS plugin interface
        # noinspection PyPep8Naming
        IFACE = QgisInterface(CANVAS)

    return QGISAPP, CANVAS, IFACE, PARENT
Example #10
0
import sys
import os
from qgis.core import QgsApplication, QgsVectorLayer
from tuflow.integrity_tool.DataCollector import DataCollector

# initialise QGIS data providers
argv = [bytes(x, 'utf-8') for x in sys.argv]
qgis = QgsApplication(argv, False)
qgis.initQgis()

# path to test map layers
path_elwood_pipes = r"C:\Users\Ellis.Symons\Desktop\Ash Wright\temporary\1d_nwk_Elwood_Pipe_L.shp"
path_elwood_pits = r"C:\Users\Ellis.Symons\Desktop\Ash Wright\temporary\1d_nwk_Elwood_pits_P.shp"

# QgsVectorLayers
elwood_pipes = QgsVectorLayer(path_elwood_pipes, "1d_nwk_Elwood_Pipe_L")
elwood_pits = QgsVectorLayer(path_elwood_pits, "1d_nwk_Elwood_pits_P")

if __name__ == '__main__':
    if elwood_pipes.isValid():
        dataCollector = DataCollector()
        dataCollector.collectData([elwood_pipes])
Example #11
0
def get_qgis_app(requested_locale='en_US', qsetting=''):
    """ Start one QGIS application to test against.

    :param locale: The locale we want the qgis to launch with.
    :type locale: str

    :param qsetting: String to specify the QSettings. By default,
        use empty string.
    :type qsetting: str

    :returns: Handle to QGIS app, canvas, iface and parent. If there are any
        errors the tuple members will be returned as None.
    :rtype: (QgsApplication, CANVAS, IFACE, PARENT)

    If QGIS is already running the handle to that app will be returned.
    """
    global QGIS_APP, PARENT, IFACE, CANVAS  # pylint: disable=W0603

    from qgis.PyQt.QtCore import QSettings
    if qsetting:
        settings = QSettings(qsetting)
    else:
        settings = QSettings()

    default_user_directory = setting('defaultUserDirectory')
    current_locale = general_setting('locale/userLocale',
                                     default='en_US',
                                     qsettings=settings)
    locale_match = current_locale == requested_locale

    if iface and locale_match:
        from qgis.core import QgsApplication
        QGIS_APP = QgsApplication
        CANVAS = iface.mapCanvas()
        PARENT = iface.mainWindow()
        IFACE = iface

    try:
        from qgis.core import QgsApplication
        from qgis.gui import QgsMapCanvas  # pylint: disable=no-name-in-module
        # noinspection PyPackageRequirements
        from qgis.PyQt import QtWidgets, QtCore  # pylint: disable=W0621
        # noinspection PyPackageRequirements
        from qgis.PyQt.QtCore import QCoreApplication, QSettings
        from safe.test.qgis_interface import QgisInterface
    except ImportError:
        return None, None, None, None

    if qsetting:
        settings = QSettings(qsetting)
    else:
        settings = QSettings()

    if not QGIS_APP:
        gui_flag = True  # All test will run qgis in gui mode

        # AG: For testing purposes, we use our own configuration file
        # instead of using the QGIS apps conf of the host
        # noinspection PyCallByClass,PyArgumentList
        QCoreApplication.setOrganizationName('QGIS')
        # noinspection PyCallByClass,PyArgumentList
        QCoreApplication.setOrganizationDomain('qgis.org')
        # noinspection PyCallByClass,PyArgumentList
        QCoreApplication.setApplicationName('QGIS2InaSAFETesting')

        # We disabled message bars for now for extent selector as
        # we don't have a main window to show them in TS - version 3.2
        set_setting('show_extent_warnings', False, settings)
        set_setting('showRubberBands', True, settings)
        set_setting('show_extent_confirmations', False, settings)
        set_setting('analysis_extents_mode', HAZARD_EXPOSURE, settings)
        if default_user_directory:
            set_setting('defaultUserDirectory', default_user_directory,
                        settings)

        # noinspection PyPep8Naming
        if 'argv' in dir(sys):
            QGIS_APP = QgsApplication([p.encode('utf-8') for p in sys.argv],
                                      gui_flag)
        else:
            QGIS_APP = QgsApplication([], gui_flag)

        # Make sure QGIS_PREFIX_PATH is set in your env if needed!
        QGIS_APP.initQgis()

        # Initialize processing
        processing.Processing.initialize()

        s = QGIS_APP.showSettings()
        LOGGER.debug(s)

    if not locale_match:
        """Setup internationalisation for the plugin."""

        # Save some settings
        set_general_setting('locale/overrideFlag', True, settings)
        set_general_setting('locale/userLocale', requested_locale, settings)

        locale_name = str(requested_locale).split('_')[0]
        # Also set the system locale to the user overridden local
        # so that the inasafe library functions gettext will work
        # .. see:: :py:func:`common.utilities`
        os.environ['LANG'] = str(locale_name)

        inasafe_translation_path = os.path.join(
            safe_dir('i18n'), 'inasafe_' + str(locale_name) + '.qm')

        if os.path.exists(inasafe_translation_path):
            if isinstance(QGIS_APP, sip.wrappertype):
                translator = QTranslator()
            else:
                translator = QTranslator(QGIS_APP)
            result = translator.load(inasafe_translation_path)
            if not result:
                message = 'Failed to load translation for %s' % locale_name
                raise Exception(message)
            # noinspection PyTypeChecker,PyCallByClass
            QCoreApplication.installTranslator(translator)

        # at the end, reload InaSAFE modules so it will get translated too
        reload_inasafe_modules()

    if PARENT is None:
        # noinspection PyPep8Naming
        PARENT = QtWidgets.QWidget()

    if CANVAS is None:
        # noinspection PyPep8Naming
        CANVAS = QgsMapCanvas(PARENT)
        CANVAS.resize(QtCore.QSize(400, 400))

    if IFACE is None:
        # QgisInterface is a stub implementation of the QGIS plugin interface
        # noinspection PyPep8Naming
        IFACE = QgisInterface(CANVAS)

    return QGIS_APP, CANVAS, IFACE, PARENT
Example #12
0
        
        # make the output file name
        _,ext = os.path.splitext(file)                    # extension of current file
        basefile,_ = os.path.splitext(intersect_name)     # name of the intersection file w/o extension
        newfile = basefile + ext                          # new name + old extension
        
        # copy
        copyfile(catchment_path/file, intersect_path/newfile);
        
        
# --- QGIS analysis
# Initialize QGIS
os.environ["QT_QPA_PLATFORM"] = "offscreen" # disable QT trying to connect to display; needed on HPC infrastructure
qgis_path = which('qgis') # find the QGIS install location
QgsApplication.setPrefixPath(qgis_path, True) # supply path to qgis install location
qgs = QgsApplication([], False) # create a reference to the QgsApplication, GUI = False
qgs.initQgis() # load providers

# Convert Path() to string for QGIS
catchment_file = str(intersect_path/intersect_name) # needs to be the copied file because output is automatically added to this
dem_file = str(dem_path/dem_name)

# Load the shape and raster
layer_polygon = QgsVectorLayer(catchment_file,'merit_hydro_basin','ogr')
layer_raster  = QgsRasterLayer(dem_file,'merit_hydro_dem')

# Check we loaded the layers correctly
if not layer_raster.isValid():
    print('Raster layer failed to load')
    
if not layer_polygon.isValid():
Example #13
0
def convert_qgis_gpkg_to_kml(qgs_file: str, output_kml_path: str, stage_dir: str = None) -> str:

    app = None
    driver_name = "libkml"

    try:
        from qgis.core import QgsApplication, QgsProject

        # Load application to load QApplication dependencies.
        app_directory = stage_dir  # hosts various session type files like qgis.db etc...
        app = QgsApplication([], False, app_directory)
        app.initQgis()

        # Application needs to be initialized before importing.
        from qgis.core import QgsVectorFileWriter

        # Load project from template output.
        project = QgsProject.instance()
        if not project.read(qgs_file):
            raise Exception(f"Could not read from {qgs_file}")

        layers = project.mapLayers()
        symbology_scale = 20000
        if stage_dir:
            output_dir = os.path.join(stage_dir, "kml")
        else:
            output_dir = os.path.join(".", "kml")

        kml_files = []
        for layer_name, layer in layers.items():
            output_filename = f"{layer.source().partition('layername=')[2].split('|')[0]}.kml"
            output_filepath = os.path.join(output_dir, output_filename)
            kml_files.append(output_filename)
            if not os.path.exists(output_dir):
                os.mkdir(output_dir)
            QgsVectorFileWriter.writeAsVectorFormat(
                layer=layer,
                fileName=output_filepath,
                fileEncoding="utf-8",
                driverName=driver_name,
                symbologyExport=QgsVectorFileWriter.SymbolLayerSymbology,
                symbologyScale=symbology_scale,
            )

        out_driver = ogr.GetDriverByName(driver_name)
        if os.path.exists(output_kml_path):
            out_driver.DeleteDataSource(output_kml_path)

        out_driver.CreateDataSource(output_kml_path)

        # Ensure the land_polygons and boundary layers are at the bottom when the files get merged.
        try:
            kml_files.append(kml_files.pop(kml_files.index("land_polygons.kml")))
            kml_files.append(kml_files.pop(kml_files.index("boundary.kml")))
        except ValueError as ve:
            logger.warning(ve)

        # Merge all of the KML files into a single file.
        for kml_file in kml_files:
            kml_path = os.path.join(output_dir, kml_file)
            gdal.VectorTranslate(output_kml_path, kml_path, accessMode="append")

        return output_kml_path

    finally:
        if app:
            app.exit()
Example #14
0
def get_qgis_app():
    """ Start one QGIS application to test against.

    :returns: Handle to QGIS app, canvas, iface and parent. If there are any
        errors the tuple members will be returned as None.
    :rtype: (QgsApplication, CANVAS, IFACE, PARENT)

    If QGIS is already running the handle to that app will be returned.
    """
    global QGIS_APP, PARENT, IFACE, CANVAS  # pylint: disable=W0603

    if iface:
        from qgis.core import QgsApplication
        QGIS_APP = QgsApplication
        CANVAS = iface.mapCanvas()
        PARENT = iface.mainWindow()
        IFACE = iface
        return QGIS_APP, CANVAS, IFACE, PARENT

    try:
        from qgis.core import QgsApplication
        from qgis.gui import QgsMapCanvas  # pylint: disable=no-name-in-module
        # noinspection PyPackageRequirements
        from PyQt4 import QtGui, QtCore  # pylint: disable=W0621
        # noinspection PyPackageRequirements
        from PyQt4.QtCore import QCoreApplication, QSettings
        from safe.test.qgis_interface import QgisInterface
    except ImportError:
        return None, None, None, None

    if QGIS_APP is None:
        gui_flag = True  # All test will run qgis in gui mode

        # AG: For testing purposes, we use our own configuration file instead
        # of using the QGIS apps conf of the host
        # noinspection PyCallByClass,PyArgumentList
        QCoreApplication.setOrganizationName('QGIS')
        # noinspection PyCallByClass,PyArgumentList
        QCoreApplication.setOrganizationDomain('qgis.org')
        # noinspection PyCallByClass,PyArgumentList
        QCoreApplication.setApplicationName('QGIS2InaSAFETesting')

        # noinspection PyPep8Naming
        if 'argv' in dir(sys):
            QGIS_APP = QgsApplication(sys.argv, gui_flag)
        else:
            QGIS_APP = QgsApplication([], gui_flag)

        # Make sure QGIS_PREFIX_PATH is set in your env if needed!
        QGIS_APP.initQgis()
        s = QGIS_APP.showSettings()
        LOGGER.debug(s)

        # Save some settings
        settings = QSettings()
        settings.setValue('locale/overrideFlag', True)
        settings.setValue('locale/userLocale', 'en_US')
        # We disabled message bars for now for extent selector as
        # we don't have a main window to show them in TS - version 3.2
        settings.setValue('inasafe/show_extent_confirmations', False)
        settings.setValue('inasafe/show_extent_warnings', False)
        settings.setValue('inasafe/showRubberBands', True)
        settings.setValue('inasafe/analysis_extents_mode', HAZARD_EXPOSURE)

    if PARENT is None:
        # noinspection PyPep8Naming
        PARENT = QtGui.QWidget()

    if CANVAS is None:
        # noinspection PyPep8Naming
        CANVAS = QgsMapCanvas(PARENT)
        CANVAS.resize(QtCore.QSize(400, 400))

    if IFACE is None:
        # QgisInterface is a stub implementation of the QGIS plugin interface
        # noinspection PyPep8Naming
        IFACE = QgisInterface(CANVAS)

    return QGIS_APP, CANVAS, IFACE, PARENT
 def init_qgis(self):
     self.qgs = QgsApplication([], True)
     self.qgs.initQgis()
Example #16
0
        self.mapCanvas.refresh()

        # self.set_crs()

    def set_crs(self):
        self.moveThread = MoveThread(self)
        self.moveThread.degreeSignal.connect(self.process_signal)
        self.moveThread.start()

    def process_signal(self, d):
        print(d)
        crsDest = QgsCoordinateReferenceSystem(
            f"PROJ:+proj=ortho +lat_0=0 +lon_0={d} +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs"
        )

        self.mapCanvas.setDestinationCrs(crsDest)
        self.mapCanvas.refresh()


if __name__ == '__main__':
    qgsAPP = QgsApplication([], True)
    QgsApplication.setPrefixPath("qgis", True)

    qgsAPP.initQgis()

    myApp = MyApp()
    myApp.show()

    qgsAPP.exec_()
    qgsAPP.exitQgis()
Example #17
0
def start_app(cleanup=True):
    """
    Will start a QgsApplication and call all initialization code like
    registering the providers and other infrastructure. It will not load
    any plugins.

    You can always get the reference to a running app by calling `QgsApplication.instance()`.

    The initialization will only happen once, so it is safe to call this method repeatedly.

        Parameters
        ----------

        cleanup: Do cleanup on exit. Defaults to true.

        Returns
        -------
        QgsApplication

        A QgsApplication singleton
    """
    global QGISAPP

    try:
        QGISAPP
    except NameError:
        myGuiFlag = True  # All test will run qgis in gui mode

        try:
            sys.argv
        except AttributeError:
            sys.argv = ['']

        # In python3 we need to convert to a bytes object (or should
        # QgsApplication accept a QString instead of const char* ?)
        try:
            argvb = list(map(os.fsencode, sys.argv))
        except AttributeError:
            argvb = sys.argv

        # Note: QGIS_PREFIX_PATH is evaluated in QgsApplication -
        # no need to mess with it here.
        QGISAPP = QgsApplication(argvb, myGuiFlag)

        os.environ['QGIS_CUSTOM_CONFIG_PATH'] = tempfile.mkdtemp(
            '', 'QGIS-PythonTestConfigPath')
        QGISAPP.initQgis()
        print(QGISAPP.showSettings())

        def debug_log_message(message, tag, level):
            print('{}({}): {}'.format(tag, level, message))

        QgsApplication.instance().messageLog().messageReceived.connect(
            debug_log_message)

        if cleanup:
            import atexit

            @atexit.register
            def exitQgis():
                QGISAPP.exitQgis()

    return QGISAPP
if "-P" in sys.argv:
    project_path = sys.argv[sys.argv.index("-P") + 1]
if "-L" in sys.argv:
    parcelle_layer = sys.argv[sys.argv.index("-L") + 1]
if "-I" in sys.argv:
    parcelle_id = sys.argv[sys.argv.index("-I") + 1]
if "-T" in sys.argv:
    export_type = sys.argv[sys.argv.index("-T") + 1]
if "-D" in sys.argv:
    target_dir = sys.argv[sys.argv.index("-D") + 1]
if "-O" in sys.argv:
    output_log = sys.argv[sys.argv.index("-O") + 1]

# Instantiate QGIS
QgsApplication.setPrefixPath(qgisPrefixPath, True)
qgs = QgsApplication([], True)
QgsApplication.initQgis()

# Open the project
p = QgsProject()
p.read(project_path)
canvas = QgsMapCanvas()
bridge = QgsLayerTreeMapCanvasBridge(p.layerTreeRoot(), canvas)
bridge.setCanvasLayers()

# Get the layers in the project
layerList = p.mapLayersByName(parcelle_layer)
if not layerList:
    layers = p.mapLayers()
    for lname, layer in layers.items():
        print(lname + ' ' + layer.name() + ' ' + parcelle_layer)
Example #19
0
def start_qgis_application(enable_gui: bool = False,
                           enable_processing: bool = False,
                           verbose: bool = False,
                           cleanup: bool = True,
                           logger: logging.Logger = None,
                           logprefix: str = 'Qgis:') -> 'QgsApplication':
    """ Start qgis application

        :param boolean enable_gui: Enable graphical interface, default to False
        :param boolean enable_processing: Enable processing, default to False
        :param boolean verbose: Output qgis settings, default to False
        :param boolean cleanup: Register atexit hook to close qgisapplication on exit().
            Note that prevents qgis to segfault when exiting. Default to True.
    """
    os.environ['QGIS_NO_OVERRIDE_IMPORT'] = '1'
    os.environ['QGIS_DISABLE_MESSAGE_HOOKS'] = '1'

    logger = logger or logging.getLogger()
    setup_qgis_paths()

    from qgis.core import Qgis, QgsApplication

    logger.info("Starting Qgis application: %s", Qgis.QGIS_VERSION)

    if QgsApplication.QGIS_APPLICATION_NAME != "QGIS3":
        raise RuntimeError("You need QGIS3 (found %s)" %
                           QgsApplication.QGIS_APPLICATION_NAME)

    if not enable_gui:
        #  We MUST set the QT_QPA_PLATFORM to prevent
        #  Qt trying to connect to display in containers
        if os.environ.get('DISPLAY') is None:
            logger.info("Setting offscreen mode")
            os.environ['QT_QPA_PLATFORM'] = 'offscreen'

    qgis_prefix = os.environ.get('QGIS3_HOME', '/usr')

    # XXX Set QGIS_PREFIX_PATH, it seems that setPrefixPath
    # does not do the job correctly
    os.environ['QGIS_PREFIX_PATH'] = qgis_prefix

    global qgis_application

    qgis_application = QgsApplication([], enable_gui)
    qgis_application.setPrefixPath(qgis_prefix, True)
    qgis_application.initQgis()

    if cleanup:
        # Closing QgsApplication on exit will
        # prevent our app to segfault on exit()
        import atexit

        logger.info("%s Installing cleanup hook" % logprefix)

        @atexit.register
        def exitQgis():
            global qgis_application
            if qgis_application:
                qgis_application.exitQgis()
                del qgis_application

    if verbose:
        print(qgis_application.showSettings())

    # Install logger hook
    install_logger_hook(logger, logprefix, verbose=verbose)

    logger.info("%s Qgis application initialized......" % logprefix)

    if enable_processing:
        init_processing()
        logger.info("%s QGis processing initialized" % logprefix)

    return qgis_application
Example #20
0
 def __init__(self):
     QgsApplication.setPrefixPath("/usr", True)
     gui_flag = False
     self.app = QgsApplication([], gui_flag)
def main():
    """reads in a pre-processed groundwater layer, with recharge-discharge (CSV) and returns tif
    
    Args:
        state(str): state for which well elevations must be obtained
        season(str): e.g. rech-96, disc-96
        
    Returns:
        None: well elevations with locations stored in CSV as SHP
    
    """
    state = sys.argv[1]
    season = sys.argv[2]
    dataPath = root.joinpath("data","groundwater")
    metaPath = root.joinpath("outputs","groundwater","csv",state+"_metadata.log")
    outputsPath = root.joinpath("outputs","groundwater")
    
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p',
                        handlers=[logging.FileHandler(str(metaPath))],
                       )
    
    logging.info("get Recharge-Discharge for '%s' dataset",state)

    # Initialize QGIS Application
    QgsApplication.setPrefixPath("G:\\Users\\Craig\\miniconda3\\envs\\geo_env\\Library\\python\\qgis", True)
    qgs = QgsApplication([], False)
    qgs.initQgis()
    
    # Append the path where QGIS processing plugin can be found
    sys.path.append('G:\\Users\\Craig\\miniconda3\\envs\\geo_env\\Library\\python\\plugins')
    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    feedback = QgsProcessingFeedback()
    
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

    # Get file with recharge-discharge values from previous step
    vectorPath = outputsPath.joinpath("shapefiles",state+"_processed_wRD.shp")
    print(vectorPath,vectorPath.exists())
    vLayer = QgsVectorLayer(str(vectorPath), 'well_rech_disc_layer', 'ogr') #.setSubsetString(season + " IS NOT NULL")
    print("islayer valid:", vLayer.isValid())
    
    # subset layer for the chosen season and choose only non null values
    filter = "\"" + season + "\"" + " IS NOT NULL"
    expr = QgsExpression(filter)
    subset = vLayer.getFeatures(QgsFeatureRequest(expr))
    vLayer.selectByIds([k.id() for k in subset])  # why didn't direct selection work? addFeature (false)
    print(vLayer.selectedFeatureCount())
    
    # write subsetted layer to shapefile
    subsetPath = outputsPath.joinpath("shapefiles","noNulls",state+"_"+season+"_noNulls.shp")
    _writer = QgsVectorFileWriter.writeAsVectorFormat(vLayer, str(subsetPath), "utf-8", vLayer.crs(), "ESRI Shapefile", onlySelected=True)
    
    # import subsetted layer
    subLayer = QgsVectorLayer(str(subsetPath), 'well_rech_disc_layer_nonulls', 'ogr') 
    print("is sub layer valid:", subLayer.isValid())
    
    # declare params for grid layer 
    # https://gdal.org/tutorials/gdal_grid_tut.html
    params = {
        'INPUT':subLayer,
        'POWER':2,    # FOR gridinversedistance
#         'RADIUS':0.25,    # FOR gridinversedistancenearestneighbor / gridlinear
#         'RADIUS_1':0.25,    # FOR gridinversedistance / gridnearestneighbor / gridaverage
#         'RADIUS_2':0.25,    # FOR gridinversedistance  /  gridnearestneighbor / gridaverage
        'MAX_POINTS':12,    # FOR gridinversedistance 
        'MIN_POINTS':1,    # FOR gridinversedistance / gridaverage
        'NODATA': -9999,
        'Z_FIELD':season,
        'OUTPUT':str(outputsPath.joinpath("tif","idw",state+"_"+season+"_grid_id_min_1_max_12_nonulls.tif"))
    }
    
    res = processing.run("gdal:gridinversedistance",params,feedback=feedback)
    print(res['OUTPUT'])
Example #22
0
        :returns: Translated version of message.
        :rtype: QString
        """
        # noinspection PyTypeChecker,PyArgumentList,PyCallByClass
        return QtCore.QCoreApplication.translate('QTraffic', message)


if __name__ == '__main__':
    # add current running path in the searching path
    srcpath = os.path.dirname(os.path.realpath(sys.argv[0]))
    sys.path.append(srcpath)

    try:
        # init qgis env and application
        app = QgsApplication(sys.argv, True)
        QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'], True)
        QgsApplication.initQgis()

        # load project
        projectFile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                   'config', 'defaultProject.ini')
        project = QtCore.QSettings(projectFile, QtCore.QSettings.IniFormat)
        project.setIniCodec("UTF-8")

        # run gui
        gui = NewFuelFormulaEditor(iface, project)
        gui.show()

        app.exec_()
    # print(os.environ)

    # Supply path to qgis install location
    # QgsApplication.setPrefixPath(os.environ.get('QGIS_PREFIX_PATH'), True)

    # In python3 we need to convert to a bytes object (or should
    # QgsApplication accept a QString instead of const char* ?)
    try:
        argvb = list(map(os.fsencode, sys.argv))
    except AttributeError:
        argvb = sys.argv

    # Create a reference to the QgsApplication.  Setting the
    # second argument to False disables the GUI.
    qgs = QgsApplication(argvb, True)

    # Load providers
    qgs.initQgis()

    # print(qgs.showSettings())
    # print(qgs.libraryPaths())
    # print(QgsProviderRegistry.instance().pluginList())

    # wrap in dialog
    dlg = QDialog()
    layout = QVBoxLayout(dlg)
    image_lbl = QLabel(dlg)
    layout.addWidget(image_lbl)
    # layout.setMargin(0)
Example #24
0
            logger.error(traceback.format_exc())

    sub_algo = [
        each for each in os.listdir("description")
        if "-" in each and ".xml" in each
    ]
    for key in sub_algo:
        shutil.copy("description/doc/%s" % key.split("-")[0] + ".html",
                    "description/doc/%s" % key.split(".")[0] + ".html")


if __name__ == "__main__":
    # Prepare the environment
    from qgis.core import QgsApplication

    app = QgsApplication([], True)
    QgsApplication.initQgis()

    # Prepare processing framework
    from processing.core.Processing import Processing
    Processing.initialize()

    import OTBSpecific_XMLcreation
    #     try:
    #         import processing
    #     except ImportError, e:
    #         raise Exception("Processing must be installed and available in PYTHONPATH")

    try:
        import otbApplication
    except ImportError as e:
Example #25
0
def start_qgis_application(
        enable_gui: bool = False,
        enable_processing: bool = False,
        verbose: bool = False,
        cleanup: bool = True,
        logger: logging.Logger = None,
        logprefix: str = 'Qgis:',
        settings: Dict = None) -> 'qgis.core.QgsApplication':
    """ Start qgis application

        :param boolean enable_gui: Enable graphical interface, default to False
        :param boolean enable_processing: Enable processing, default to False
        :param boolean verbose: Output qgis settings, default to False
        :param boolean cleanup: Register atexit hook to close qgisapplication on exit().
            Note that prevents qgis to segfault when exiting. Default to True.
    """

    os.environ['QGIS_NO_OVERRIDE_IMPORT'] = '1'
    os.environ['QGIS_DISABLE_MESSAGE_HOOKS'] = '1'

    logger = logger or logging.getLogger()
    qgisPrefixPath = setup_qgis_paths()

    from qgis.PyQt.QtCore import QCoreApplication
    from qgis.core import Qgis, QgsApplication

    logger.info("Starting Qgis application: %s", Qgis.QGIS_VERSION)

    global version_info
    version_info = tuple(
        int(n) for n in Qgis.QGIS_VERSION.split('-')[0].split('.'))

    if QgsApplication.QGIS_APPLICATION_NAME != "QGIS3":
        raise RuntimeError("You need QGIS3 (found %s)" %
                           QgsApplication.QGIS_APPLICATION_NAME)

    if not enable_gui:
        #  We MUST set the QT_QPA_PLATFORM to prevent
        #  Qt trying to connect to display in containers
        if os.environ.get('DISPLAY') is None:
            logger.info("Setting offscreen mode")
            os.environ['QT_QPA_PLATFORM'] = 'offscreen'

    global qgis_application

    qgis_application = QgsApplication([], enable_gui)
    qgis_application.setPrefixPath(qgisPrefixPath, True)

    # From qgis server
    # Will enable us to read qgis setting file
    QCoreApplication.setOrganizationName(QgsApplication.QGIS_ORGANIZATION_NAME)
    QCoreApplication.setOrganizationDomain(
        QgsApplication.QGIS_ORGANIZATION_DOMAIN)
    QCoreApplication.setApplicationName(QgsApplication.QGIS_APPLICATION_NAME)

    qgis_application.initQgis()

    if cleanup:
        # Closing QgsApplication on exit will
        # prevent our app to segfault on exit()
        import atexit

        logger.info("%s Installing cleanup hook" % logprefix)

        @atexit.register
        def exitQgis():
            global qgis_application
            if qgis_application:
                qgis_application.exitQgis()
                del qgis_application

    optpath = os.getenv('QGIS_OPTIONS_PATH')
    if optpath:
        # Log qgis settings
        load_qgis_settings(optpath, logger, verbose)

    if settings:
        # Initialize settings
        from qgis.core import QgsSettings
        qgsettings = QgsSettings()
        for k, v in settings.items():
            qgsettings.setValue(k, v)

    if verbose:
        print(qgis_application.showSettings())

    # Install logger hook
    install_logger_hook(logger, logprefix, verbose=verbose)

    logger.info("%s Qgis application initialized......" % logprefix)

    if enable_processing:
        init_qgis_processing()
        logger.info("%s QGis processing initialized" % logprefix)

    return qgis_application
Example #26
0
 def setUpClass(cls):
     cls.testdatapath = unitTestDataPath('qgis_server_security') + '/'
     cls.db = os.path.join(cls.testdatapath, 'db.sqlite')
     cls.db_clone = os.path.join(cls.testdatapath, 'db_clone.sqlite')
     cls.project = os.path.join(cls.testdatapath, 'project.qgs')
     cls.app = QgsApplication([], False)
Example #27
0
#Calculate NDVI for QGIS
# from QGIS import iface-> symbolic importation of iface object of QGIS
# import qgis

from qgis.analysis import QgsRasterCalculator, QgsRasterCalculatorEntry
import processing, os, ogr, csv, sys
from processing.core.Processing import Processing
from qgis.core import QgsRasterLayer, QgsApplication, QgsVectorFileWriter, QgsVectorLayer
from PyQt4.QtCore import QFileInfo

#import pandas as pd

qgis_prefix = r"C:\Program Files\QGIS 2.18\apps\qgis"  # Indicamos donde esta el QGIS
QgsApplication.setPrefixPath(qgis_prefix,
                             True)  # Configuramos la aplicacion QGIS
app = QgsApplication([], True, None)  # El True inhabilita la interfaz grafica
QgsApplication.initQgis()  # Iniciamos la aplicacion QGIS


# Indice Diferencial de Vegetacion Normalizado
def NDVI(band4, band8, output):
    entries = []
    #define raster 1 (band4)
    raster1 = QgsRasterCalculatorEntry()
    raster1.ref = 'band4@1'
    raster1.raster = band4
    raster1.bandNumber = 1
    entries.append(raster1)
    #define raster 2 (band8)
    raster2 = QgsRasterCalculatorEntry()
    raster2.ref = 'band8@1'