cara memuat openlayers_plugin QGIS dalam skrip python mandiri (bukan konsol python tertanam QGIS)


9

jika saya mencoba untuk memuat plugin openlayers dalam aplikasi mandiri itu selalu gagal dengan False, meskipun jalur ditetapkan, dan plugin menunjukkan tersedia.

(diuji pada Ubuntu 14.04, QGIS 2.4, python-2.7)

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, os

import qgis
import qgis.gui, qgis.utils
from qgis.core import *
from PyQt4 import QtGui


# open an app
app = QtGui.QApplication(sys.argv)
# supply path to where is your qgis installed
QgsApplication.setPrefixPath(u'/usr', True)
# load providers
QgsApplication.initQgis()

# set up the plugins
qgis.utils.plugin_paths = \
    ['/usr/share/qgis/python/plugins',
    os.path.expanduser('~/.qgis2/python/plugins'),]
qgis.utils.updateAvailablePlugins()
print qgis.utils.available_plugins
print "...load:", qgis.utils.loadPlugin(u'openlayers_plugin')
print "...start:", qgis.utils.startPlugin(u'openlayers_plugin')
print "active:\n",qgis.utils.active_plugins

canvas = qgis.gui.QgsMapCanvas()
canvas.show()

apa yang terjadi disini ? memposting Bagaimana cara mengambil lapisan openlayers dari pyqgis? merujuk ke konsol python tertanam, di mana plugin itu tersedia secara ajaib sebelumnya.

Terima kasih!


Mengapa Anda memerlukan plugin lapisan terbuka di luar dalam skrip?
Nathan W

Solusi lain untuk masalah ini diposting di sini
Henhuy

Jawaban:


3

Ini tidak pernah, well mungkin dengan beberapa peretasan, akan bekerja dengan baik. Plugin biasanya menggunakan QgisInterfaceobjek yang memberikan akses ke antarmuka dan metode QGIS. Anda tidak memiliki objek ini di skrip mandiri Anda. Sebagian besar plugin, jika tidak semua, tidak dirancang untuk berjalan di luar QGIS seperti ini.


2
Ini penting untuk diketahui, tetapi sayangnya patut disesalkan. Sebagai seorang pemula yang berpengalaman dalam QGis, saya pikir sangat bagus untuk dapat mengembangkan langkah-langkah pemrosesan data tertentu dengan semua alat QGis di lingkungan GUI penuh, sementara kemudian dapat memiliki skrip just-the-job yang dilucuti Versi: kapan. Sesuatu seperti DummyQgisInterfaceharus mungkin ...
qbit

7

Menjadi sangat tertarik pada paradigma TDD, saya telah menghabiskan beberapa waktu menyusun dummy-interface (berdasarkan pada beberapa fragmen kode yang saya temukan di internet) yang memungkinkan memanggil QGIS dan QGIS-plugins standalone.

Antarmuka yang saya buat terlihat seperti berikut dan saya menggunakannya untuk semua unit-test saya sejak:

# coding=utf-8
"""QGIS plugin implementation.

.. note:: 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.

.. note:: The basis of this source code was copied from the 'postgis viewer' application
     with original authors:
     Copyright (c) 2010 by Ivan Mincik, ivan.mincik@gista.sk
     Copyright (c) 2011 German Carrillo, geotux_tuxman@linuxmail.org
     Copyright (c) 2014 Tim Sutton, tim@linfiniti.com

"""
from qgis._core import QgsVectorLayer
from PyQt4.QtCore import QObject, pyqtSlot, pyqtSignal, QCoreApplication, QSize
from PyQt4.QtGui import QWidget
from qgis.core import QgsMapLayerRegistry, QgsApplication, QgsVectorLayer
from qgis.gui import QgsMapCanvasLayer, QgsMapCanvas

import logging
import sys

from mole import config

LOGGER = logging.getLogger('QGIS')


def set_up_interface():
    """
    Sets up a QGIS pseudo-application which enables calling methods as if when calling them from QGIS-console.

    :return qgis_app: Pseudo QGIS-instance
    :rtype: QgsApplication
    :return canvas: The map canvas
    :rtype: QgsMapCanvas
    :return iface: A dummy interface, giving access to needed method-calls
    :rtype: QgisInterface
    """
    gui_flag = True  # All test will run qgis in gui mode
    qgis_app = QgsApplication(sys.argv, gui_flag)
    prefix_path = config.qgis_prefix_path()
    qgis_app.setPrefixPath(prefix_path, True)
    qgis_app.initQgis()
    QCoreApplication.setOrganizationName('QGIS')
    QCoreApplication.setApplicationName('QGIS2')

    # parent = QWidget()
    # canvas = QgsMapCanvas(parent)
    # canvas.resize(QSize(400, 400))
    canvas = MyMapCanvas()

    iface = QgisInterface(canvas)

    return qgis_app, canvas, iface

#noinspection PyMethodMayBeStatic,PyPep8Naming
class QgisInterface(QObject):
    """Class to expose QGIS objects and functions to plugins.

    This class is here for enabling us to run unit tests only,
    so most methods are simply stubs.
    """
    currentLayerChanged = pyqtSignal(QgsMapCanvasLayer)

    def __init__(self, canvas):
        """Constructor
        :param canvas:
        """
        QObject.__init__(self)
        self.canvas = canvas
        self.legend_interface = MyLegendInterface()
        self.active_layer = None
        # Set up slots so we can mimic the behaviour of QGIS when layers
        # are added.
        LOGGER.debug('Initialising canvas...')
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().layersAdded.connect(self.addLayer)
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().layerWasAdded.connect(self.addLayer)
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().removeAll.connect(self.removeAllLayers)

        # For processing module
        self.destCrs = None


    @pyqtSlot('QgsMapLayer')
    def addLayer(self, layer):
        """Handle a layer being added to the registry so it shows up in canvas.

        :param layer: list<QgsMapLayer> list of map layers that were added

        .. note: The QgsInterface api does not include this method, it is added
                 here as a helper to facilitate testing.

        .. note: The addLayer method was deprecated in QGIS 1.8 so you should
                 not need this method much.
        """
        # set the recently added layer as active
        # LOGGER.debug('Layer Count Before: %s' % len(self.canvas.layers()))
        current_layers = self.canvas.layers()
        final_layers = [] + current_layers
        final_layers.append(QgsMapCanvasLayer(layer))
        self.canvas.setLayerSet(final_layers)
        self.active_layer = layer

    @pyqtSlot()
    def removeAllLayers(self):
        """Remove layers from the canvas before they get deleted."""
        self.canvas.setLayerSet([])

    def newProject(self):
        """Create new project."""
        # noinspection PyArgumentList
        QgsMapLayerRegistry.instance().removeAllMapLayers()

    def legendInterface(self):
        """Get the legend."""
        return self.legend_interface

    def activeLayer(self):
        """Get pointer to the active layer (layer selected in the legend)."""
        return self.active_layer

    def setActiveLayer(self, layer):
        """Set the given layer as active.
        :param layer: Layer that shall be set active
        :type layer: QgsMapLayer
        """
        self.active_layer = layer

    class actionAddFeature(object):

        def __init__(self):
            pass

        def trigger(self):
            pass

    class actionZoomToLayer(object):

        def __init__(self):
            pass

        def trigger(self):
            pass

    # ---------------- API Mock for QgsInterface follows -------------------
    def zoomFull(self):
        """Zoom to the map full extent."""
        pass

    def zoomToPrevious(self):
        """Zoom to previous view extent."""
        pass

    def zoomToNext(self):
        """Zoom to next view extent."""
        pass

    def zoomToActiveLayer(self):
        """Zoom to extent of active layer."""
        pass

    def addVectorLayer(self, path, base_name, provider_key):
        """Add a vector layer.

        :param path: Path to layer.
        :type path: str

        :param base_name: Base name for layer.
        :type base_name: str

        :param provider_key: Provider key e.g. 'ogr'
        :type provider_key: str
        """
        pass

    def addRasterLayer(self, path, base_name):
        """Add a raster layer given a raster layer file name

        :param path: Path to layer.
        :type path: str

        :param base_name: Base name for layer.
        :type base_name: str
        """
        pass

    def addToolBarIcon(self, action):
        """Add an icon to the plugins toolbar.

        :param action: Action to add to the toolbar.
        :type action: QAction
        """
        pass

    def removeToolBarIcon(self, action):
        """Remove an action (icon) from the plugin toolbar.

        :param action: Action to add to the toolbar.
        :type action: QAction
        """
        pass

    def addToolBar(self, name):
        """Add toolbar with specified name.

        :param name: Name for the toolbar.
        :type name: str
        """
        pass

    def mapCanvas(self):
        """Return a pointer to the map canvas."""
        return self.canvas

    def mainWindow(self):
        """Return a pointer to the main window.

        In case of QGIS it returns an instance of QgisApp.
        """
        pass

    def addDockWidget(self, area, dock_widget):
        """Add a dock widget to the main window.

        :param area: Where in the ui the dock should be placed.
        :type area:

        :param dock_widget: A dock widget to add to the UI.
        :type dock_widget: QDockWidget
        """
        pass


class MyLegendInterface(object):

    def __init__(self):
        self.layer_visibility = {}

    def setLayerVisible(self, layer, yes_no):
        self.layer_visibility[layer.name()] = yes_no

    def isLayerVisible(self, layer):
        try:
            return self.layer_visibility[layer.name()]
        except KeyError:
            print('Layer {} has not been set (in)visible yet.'.format(layer.name()))
            return False


class MyMapCanvas(object):

    def __init__(self):
        self.layer_set = []

    def layers(self):
        return self.layer_set

    def layer(self, index):
        layer = self.layer_set[index].layer()
        return layer

    def setLayerSet(self, layer_set):
        self.layer_set = layer_set

    def layerCount(self):
        return len(self.layer_set)

Jika Anda ingin menguji / menggunakan QGIS plus interaksi dengan plugin yang terinstal sekarang, lakukan hal berikut (mis. Dalam setUp yang belum dipatenkan misalnya):

qgis_app, canvas, iface = set_up_interface()
plugin_name = 'openlayers_plugin'
utils.plugin_paths = [os.path.expanduser('~/.qgis2/python/plugins')]
utils.updateAvailablePlugins()
utils.loadPlugin(plugin_name)
utils.iface = self.iface
utils.startPlugin(plugin_name) 

Untuk beberapa contoh penggunaan lainnya dan beberapa contoh nyata pada aplikasi pengujian QGIS unit, Anda dapat memeriksa halaman github kami ( https://github.com/UdK-VPT/Open_eQuarter/tree/master/mole ). Tes folder berisi semua unit-tes (yang sebagian besar menguji modul dalam paket interaksi qgis, yang berisi modul, yang berinteraksi dengan plugin point_sampling_tool).


Saya pikir saya punya ini untuk bekerja tetapi harus melakukan utils.iface = ifacedua baris sebelumnya dan harus mengejek QgsMessageBar di iface ...
Spacedman
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.