Example #1
0
def initialize_processing():
    """
    Initializes processing, if it's not already been done
    """

    need_initialize = False

    needed_algorithms = [
        'native:clip', 'gdal:cliprasterbyextent', 'native:union',
        'native:intersection'
    ]

    if not QgsApplication.processingRegistry().algorithms():
        need_initialize = True

    if not need_initialize:
        for needed_algorithm in needed_algorithms:
            if not QgsApplication.processingRegistry().algorithmById(
                    needed_algorithm):
                need_initialize = True
                break

    if need_initialize:
        QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
        processing.Processing.initialize()
Example #2
0
    def initialize():
        if "model" in [p.id() for p in QgsApplication.processingRegistry().providers()]:
            return

        with QgsRuntimeProfiler.profile('Initialize'):

            # add native provider if not already added
            if "native" not in [p.id() for p in QgsApplication.processingRegistry().providers()]:
                QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms(QgsApplication.processingRegistry()))

            # add 3d provider if available and not already added
            if "3d" not in [p.id() for p in QgsApplication.processingRegistry().providers()]:
                try:
                    from qgis._3d import Qgs3DAlgorithms
                    QgsApplication.processingRegistry().addProvider(Qgs3DAlgorithms(QgsApplication.processingRegistry()))
                except ImportError:
                    # no 3d library available
                    pass

            # Add the basic providers
            for c in [
                QgisAlgorithmProvider,
                GdalAlgorithmProvider,
                ScriptAlgorithmProvider,
                ModelerAlgorithmProvider,
                ProjectProvider
            ]:
                p = c()
                if QgsApplication.processingRegistry().addProvider(p):
                    Processing.BASIC_PROVIDERS.append(p)
            # And initialize
            ProcessingConfig.initialize()
            ProcessingConfig.readSettings()
            RenderingStyles.loadStyles()
Example #3
0
def import_processing():
    if not "processing" in qgis.utils.plugins:
        sys.path.append("/usr/share/qgis/python/plugins/")
        import processing
        from processing.core.Processing import Processing
        Processing.initialize()
        QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
Example #4
0
def run():
    os.environ["QT_QPA_PLATFORM"] = "offscreen"
    QgsApplication.setPrefixPath("/usr", True)
    qgs = QgsApplication([], False)
    qgs.initQgis()

    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

    project = QgsProject()
    project.read(pathqgisproject)

    layer_roi = QgsVectorLayer(path_roi, "temporary_roi", "ogr")
    # print (layer_roi.isValid())
    if layer_roi.isValid():
        for lay_name in layernames:
            layer = project.mapLayersByName(lay_name)
            if layer[0]:
                alg_params_dissolve = {
                    'INPUT': layer[0],
                    'OVERLAY': layer_roi,
                    'OUTPUT': 'memory:'
                }

                result = processing.run('native:clip', alg_params_dissolve)
                layer_filtrer = result['OUTPUT']
                QgsVectorFileWriter.writeAsVectorFormat(
                    layer_filtrer, path_to_save + lay_name + '.gpkg', "utf-8",
                    layer_filtrer.crs(), "GPKG")
                # print (result['OUTPUT'],layer_filtrer.featureCount())
                print(layer_filtrer.featureCount())
    else:
        print('KO')
Example #5
0
def initQgis():
    """
    Method initializing QGIS application with all necessary to work with processing tools

    Note: if initialization is called more than once in a single Django/Dramatiq handler, the application may
    crash with python's fatal error: Segmentation fault, so make sure to reuse the QGis app between your classes.

    :returns: (qgs, processing, postgis)
    """
    os.environ["QT_QPA_PLATFORM"] = "offscreen"
    QgsApplication.setPrefixPath(settings.QGIS_PATH, True)
    """
    provider_registry = QgsProviderRegistry.instance()
    res = True
    if provider_registry.providerMetadata('postgres') is None:
        metadata = QgsProviderMetadata("postgres", "postgres")
        res = provider_registry.registerProvider(metadata)
    print("QgsProviderMetadata(postgres), loaded => " + str(res))
    """
    qgs = QgsApplication([], False)
    qgs.initQgis()
    isWindows = os.name == 'nt'
    if isWindows:
        sys.path.append('C:\\OSGeo4W64\\apps\\qgis\\python\\plugins')

    import processing
    from processing.core.Processing import Processing
    from processing.algs.gdal.GdalUtils import GdalUtils
    #from processing.tools.system import isWindows

    Processing.initialize()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
    processing = processing

    return qgs, processing, GdalUtils, isWindows
    def __init__(self, network=None, constrains=None, res_folder=None, project=None, use='plugin'):
        """ Constrictor
         :param network to find intersections
         :param constrains the optional sight lines
         :param res_folder storing the results
         :param project loading the layers into
         :param use to identify who call that class -  plugin or standalone """

        # general attributes
        self.use = use
        # Initiate QgsApplication in case of standalone app
        if self.use == "standalone":
            self.app = QGuiApplication([])
            QgsApplication.setPrefixPath(r'C:\Program Files\QGIS 3.0\apps\qgis', True)
            QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
            QgsApplication.initQgis()
        # These attributes are input from the user
        self.network = self.upload_new_layer(network, "_network")
        self.constrain = self.upload_new_layer(constrains, "_constrain")
        self.feedback = QgsProcessingFeedback()
        self.res_folder = res_folder

        # These will be used latter
        self.res = []

        # layers[0] = intersections
        # layers[1] =  edges
        self.layers = []

        # attributes to create QgsVectorLayer in memory
        self.vl = QgsVectorLayer()
        # QgsVectorDataProvider
        self.lines = None
        # QgsProject.instance()
        self.project = project
Example #7
0
 def setUpClass(cls):
     start_app()
     from processing.core.Processing import Processing
     Processing.initialize()
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     cls.cleanup_paths = []
     cls.in_place_layers = {}
     cls.vector_layer_params = {}
def init_processing(verbose: bool = False):
    from processing.core.Processing import Processing
    from qgis.analysis import QgsNativeAlgorithms
    from qgis.core import QgsApplication
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
    Processing.initialize()
    if verbose:
        print("QGis processing initialized")
Example #9
0
def init_qgis_processing() -> None:
    """ Initialize processing 
    """
    from processing.core.Processing import Processing
    from qgis.analysis import QgsNativeAlgorithms
    from qgis.core import QgsApplication

    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
    Processing.initialize()
Example #10
0
 def setupProject(self, proj, isBatch, isHUC=False, logFile=None):
     """Set up the project."""
     self._odlg.mainBox.setVisible(True)
     self._odlg.mainBox.setEnabled(False)
     self._odlg.setCursor(Qt.WaitCursor)
     self._odlg.projPath.setText('Restarting project ...')
     title = QFileInfo(proj.fileName()).baseName()
     proj.setTitle(title)
     isHUCFromProjfile, found = proj.readBoolEntry(title, 'delin/isHUC', False)
     if not found:
         # isHUC not previously set.  Use parameter above and record
         proj.writeEntryBool(title, 'delin/isHUC', isHUC)
     else:
         isHUC = isHUCFromProjfile
     # now have project so initiate global vars
     # if we do this earlier we cannot for example find the project database
     self._gv = GlobalVars(self._iface, QSWATPlus.__version__, self.plugin_dir, isBatch, isHUC, logFile)
     if self._gv.SWATPlusDir == '':
         # failed to find SWATPlus directory
         return
     self._odlg.projPath.repaint()
     self.checkReports()
     self.setLegendGroups()
     Processing.initialize()
     if 'native' not in [p.id() for p in QgsApplication.processingRegistry().providers()]:
         QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     # enable edit button if converted from Arc with 'No GIS' option
     title = proj.title()
     choice, found = proj.readNumEntry(title, 'fromArc', -1)
     if found:
         self._gv.fromArcChoice = choice
         if choice == 2:  # NB value from convertFromArc.py
             self._odlg.editLabel.setEnabled(True)
             self._odlg.editButton.setEnabled(True)
     # also assume editor can be run if there are stream and hrus shapefiles in the results directory
     if os.path.isfile(os.path.join(self._gv.resultsDir, Parameters._RIVS + '.shp')) and \
         os.path.isfile(os.path.join(self._gv.resultsDir, Parameters._SUBS + '.shp')):
             self._odlg.editLabel.setEnabled(True)
             self._odlg.editButton.setEnabled(True)
     if self.demProcessed():
         self._demIsProcessed = True
         self.allowCreateHRU()
         self.hrus = HRUs(self._gv, self._odlg.reportsBox)
         #result = hrus.tryRun()
         #if result == 1:
         if self.hrus.HRUsAreCreated():
             # QSWATUtils.progress('Done', self._odlg.hrusLabel)
             self.showReports()
             self._odlg.editLabel.setEnabled(True)
             self._odlg.editButton.setEnabled(True)
     if os.path.exists(QSWATUtils.join(self._gv.resultsDir, Parameters._OUTPUTDB)):
         self._odlg.visualiseLabel.setVisible(True)
         self._odlg.visualiseButton.setVisible(True)
     self._odlg.projPath.setText(self._gv.projDir)
     self._odlg.mainBox.setEnabled(True)
     self._odlg.exportButton.setVisible(True)
     self._odlg.setCursor(Qt.ArrowCursor)
 def setUpClass(cls):
     start_app()
     from processing.core.Processing import Processing
     Processing.initialize()
     ProcessingConfig.setSettingValue(ModelerUtils.MODELS_FOLDER, os.path.join(os.path.dirname(__file__), 'models'))
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     cls.cleanup_paths = []
     cls.in_place_layers = {}
     cls.vector_layer_params = {}
     cls._original_models_folder = ProcessingConfig.getSetting(ModelerUtils.MODELS_FOLDER)
Example #12
0
 def setUpClass(cls):
     """Run before all tests"""
     QCoreApplication.setOrganizationName("QGIS_Test")
     QCoreApplication.setOrganizationDomain(
         "QGIS_TestPyQgsProcessingInPlace.com")
     QCoreApplication.setApplicationName("QGIS_TestPyQgsProcessingInPlace")
     QgsSettings().clear()
     Processing.initialize()
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     cls.registry = QgsApplication.instance().processingRegistry()
Example #13
0
def initialise_processing(app):
    from qgis.analysis import QgsNativeAlgorithms
    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    app.processingRegistry().addProvider(QgsNativeAlgorithms())
    return (
        app,
        processing,
    )
Example #14
0
def importQgis():
    ## QGIS
    from qgis.core import QgsApplication, QgsProcessingRegistry
    from qgis.testing import start_app
    from qgis.analysis import QgsNativeAlgorithms
    import processing
    app = start_app()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

    return QgsApplication, QgsProcessingRegistry, start_app, QgsNativeAlgorithms, processing, app
Example #15
0
def import_processing():
    global iface
    plugin_found = "processing" in qgis.utils.plugins
    if not plugin_found:
        processing_plugin = processing.classFactory(iface)
        qgis.utils.plugins["processing"] = processing_plugin
        qgis.utils.active_plugins.append("processing")

        from processing.core.Processing import Processing
        Processing.initialize()
        QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
def run_qgis_algorithm(algorithm_id, algorithm_parameters):
    import sys
    import qgis.utils

    from qgis.core import (
        QgsApplication,
        QgsProcessingFeedback,
        QgsVectorLayer,
        QgsProcessingProvider,
        QgsProcessingRegistry,
    )
    from qgis.analysis import QgsNativeAlgorithms

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

    #  # Append the path where processing plugin can be found
    sys.path.append('/usr/share/qgis/python/plugins/')

    import processing
    from processing.core.Processing import Processing
    Processing.initialize()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

    print('You are using QGIS version: %s ', qgis.utils.Qgis.QGIS_VERSION)
    print('You are running:  %s ', algorithm_id)

    provider = NDVIProvider()
    provider.loadAlgorithms()

    QgsApplication.processingRegistry().addProvider(provider)

    # Checking if the algorithm is added
    # last_alg = QgsApplication.processingRegistry().algorithms()[-1]
    # print(last_alg.name())
    # print(last_alg.id())
    # last_alg = QgsApplication.processingRegistry().algorithms()[-2]
    # print(last_alg.name())
    # print(last_alg.id())

    # Show help for the algorithm
    processing.algorithmHelp(algorithm_id)
    print('Running algorithm')
    result = processing.run(algorithm_id, algorithm_parameters)
    print('### Result:')
    print(result)

    qgs.exitQgis()
    return result
 def setUpClass(self):
     Processing.initialize()
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     print("\nINFO: Setting up copy CSV points to DB validation...")
     self.qgis_utils = QGISUtils()
     self.db_connection = get_dbconn('test_ladm_col')
     self.db_connection_3d = get_dbconn('test_ladm_col_3d')
     result = self.db_connection.test_connection()
     print('test_connection', result)
     if not result[1]:
         print('The test connection is not working')
         return
     restore_schema('test_ladm_col')
     restore_schema('test_ladm_col_3d')
Example #18
0
 def convertRasterToPointVector(self, inp, outp, bandNumber):
     sys.path.append('/usr/share/qgis/python/plugins/')
     import processing
     from processing.core.Processing import Processing
     Processing.initialize()
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     processing.run(
         "native:pixelstopoints", {
             'INPUT_RASTER': inp,
             'RASTER_BAND': bandNumber,
             'FIELD_NAME': 'VALUE',
             'OUTPUT': outp
         })
     print('contour')
    def setUpClass(cls):
        """Run before all tests"""
        QCoreApplication.setOrganizationName("QGIS_Test")
        QCoreApplication.setOrganizationDomain(
            "QGIS_TestPyQgsProcessingInPlace.com")
        QCoreApplication.setApplicationName("QGIS_TestPyQgsProcessingInPlace")
        QgsSettings().clear()
        Processing.initialize()
        QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
        cls.registry = QgsApplication.instance().processingRegistry()
        fields = QgsFields()
        fields.append(QgsField('int_f', QVariant.Int))
        cls.vl = QgsMemoryProviderUtils.createMemoryLayer(
            'mylayer', fields, QgsWkbTypes.Point,
            QgsCoordinateReferenceSystem(4326))

        f1 = QgsFeature(cls.vl.fields())
        f1['int_f'] = 1
        f1.setGeometry(QgsGeometry.fromWkt('Point(9 45)'))
        f2 = QgsFeature(cls.vl.fields())
        f2['int_f'] = 2
        f2.setGeometry(QgsGeometry.fromWkt('Point(9.5 45.6)'))
        cls.vl.dataProvider().addFeatures([f1, f2])

        assert cls.vl.isValid()
        assert cls.vl.featureCount() == 2

        # Multipolygon layer

        cls.multipoly_vl = QgsMemoryProviderUtils.createMemoryLayer(
            'mymultiplayer', fields, QgsWkbTypes.MultiPolygon,
            QgsCoordinateReferenceSystem(4326))

        f3 = QgsFeature(cls.multipoly_vl.fields())
        f3.setGeometry(
            QgsGeometry.fromWkt(
                'MultiPolygon (((2.81856297539240419 41.98170998812887689, 2.81874467773035464 41.98167537995160359, 2.81879535908157752 41.98154066615795443, 2.81866433873670452 41.98144056064155905, 2.81848263699778379 41.98147516865246587, 2.81843195500470811 41.98160988234612034, 2.81856297539240419 41.98170998812887689)),((2.81898589063455907 41.9815711567298635, 2.81892080450418803 41.9816030048432367, 2.81884192631866437 41.98143737613141724, 2.8190679469505846 41.98142270931093378, 2.81898589063455907 41.9815711567298635)))'
            ))
        f4 = QgsFeature(cls.multipoly_vl.fields())
        f4.setGeometry(
            QgsGeometry.fromWkt(
                'MultiPolygon (((2.81823679385631332 41.98133290154246566, 2.81830770255185703 41.98123540208609228, 2.81825871989355159 41.98112524362621656, 2.81813882853970243 41.98111258462271422, 2.81806791984415872 41.98121008407908761, 2.81811690250246416 41.98132024253896333, 2.81823679385631332 41.98133290154246566)),((2.81835835162010895 41.98123286963267731, 2.8183127674586852 41.98108725356146209, 2.8184520523963692 41.98115436357689134, 2.81835835162010895 41.98123286963267731)))'
            ))
        cls.multipoly_vl.dataProvider().addFeatures([f3, f4])

        assert cls.multipoly_vl.isValid()
        assert cls.multipoly_vl.featureCount() == 2

        QgsProject.instance().addMapLayers([cls.vl, cls.multipoly_vl])
Example #20
0
    def setUpClass(cls):
        """Run before all tests"""
        QCoreApplication.setOrganizationName("QGIS_Test")
        QCoreApplication.setOrganizationDomain(
            "QGIS_TestPyQgsExportToPostgis.com")
        QCoreApplication.setApplicationName("QGIS_TestPyQgsExportToPostgis")
        QgsSettings().clear()
        Processing.initialize()
        QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
        cls.registry = QgsApplication.instance().processingRegistry()

        # Create DB connection in the settings
        settings = QgsSettings()
        settings.beginGroup('/PostgreSQL/connections/qgis_test')
        settings.setValue('service', 'qgis_test')
        settings.setValue('database', 'qgis_test')
Example #21
0
    def wrapped_func(*args,**kwargs):
        fold = wr_parse_arguments()

        # Initiate QGIS in Python without an interface
        ## ESTABLISH QGIS paths based on operating system
        if (fold.OS[0] == 1):
            ## Windows
            QGIS_PREF_PATH = fold.QGISINST[0]+u'/apps/qgis'
            QGIS_PLUG_PATH = fold.QGISINST[0]+u'/apps/qgis/python/plugins'
        else if (fold.OS[1] == 2):
            QGIS_PREF_PATH = fold.QGISINST[0]+u'/MacOS'
            QGIS_PLUG_PATH = fold.QGISINST[0]+u'/Plugins'


        QgsApplication.setPrefixPath(QGIS_PREF_PATH,True)
        qgs = QgsApplication([],False)
        qgs.initQgis()
        print(">> QGIS initialized.")

        # Import `processing` modules
        sys.path.append(QGIS_PLUG_PATH)
        import processing
        from processing.core.Processing import Processing
        ## from processing.tools.general import runAndLoadresults
        Processing.initialize()
        print(">> Processing initialized.")

        # Add Native QGIS algorithms - Need to be after initializing Processing
        ## This loads: `qgis:interesection`, an algorithm needed later
        qgs.processingRegistry().addProvider(QgsNativeAlgorithms())
        print(">> Native algorithms added.")

        # Start with a blank project
        project = QgsProject.instance()
        project.clear()

        # RUN THE MAIN FUNCTION!
        exitFlag = function(fold, qgs, project)
        print(">> Finished with main function.")

        # Close QGIS in Python
        qgs.exitQgis()
        print(">> Exited from QGIS.")
        # Wait before exiting
        print("5 seconds before closing this window.")
        time.sleep(5)
        return exitFlag
def initialize_qgis_processor():
    """
    Initialize the QGIS processor environment (to call and run QGIS algorithms).

    Returns:
        The initialized qgis processor object.
    """

    pr = Processing.Processing()
    pr.initialize()

    # Allows the GeoProcessor to use the native algorithms
    # REF: https://gis.stackexchange.com/questions/279874/
    #   using-qgis3-processing-algorithms-from-standalone-pyqgis-scripts-outside-of-gui/279937
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

    return pr
Example #23
0
 def generateContour(self, inp, outp, bandNumber, interval):
     sys.path.append('/usr/share/qgis/python/plugins/')
     import processing
     from processing.core.Processing import Processing
     Processing.initialize()
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     processing.run(
         "gdal:contour", {
             'INPUT': inp,
             'BAND': bandNumber,
             'INTERVAL': interval,
             'FIELD_NAME': 'ELEV',
             'CREATE_3D': False,
             'CREATE_3D': False,
             'IGNORE_NODATA': False,
             'NODATA': None,
             'OFFSET': 0,
             'OPTIONS': '',
             'OUTPUT': outp
         })
Example #24
0
def main():

    QtCore.qInstallMessageHandler(handler)
    QgsApplication.setPrefixPath("C:\\OSGeo4W64\\apps\\qgis", True)
    app = QgsApplication([], False)
    app.initQgis()

    Processing.initialize()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

    firstWindow = Welcome()
    firstWindow.show()
    QtTest.QTest.qWait(5000)
    firstWindow.close()

    secondWindow = SWAMain()
    secondWindow.show()

    app.exec_()
    app.deleteLater()
    QgsApplication.exitQgis()
Example #25
0
 def generateHillShade(self, inp, outp):
     sys.path.append('/usr/share/qgis/python/plugins/')
     import processing
     from processing.core.Processing import Processing
     Processing.initialize()
     QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
     processing.run(
         "gdal:hillshade", {
             'INPUT': inp,
             'BAND': 1,
             'Z_FACTOR': 1.571e-05,
             'SCALE': 1,
             'AZIMUTH': 315,
             'ALTITUDE': 40,
             'COMPUTE_EDGES': False,
             'ZEVENBERGEN': False,
             'COMBINED': False,
             'MULTIDIRECTIONAL': False,
             'OPTIONS': '',
             'OUTPUT': outp
         })
def distance_to_nearest_hub():
    app = QGuiApplication([])
    QgsApplication.setPrefixPath(r'C:\Program Files\QGIS 3.0\apps\qgis', True)
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
    QgsApplication.initQgis()
    feedback = QgsProcessingFeedback()
    """Upload input data"""

    input_path = r'C:\Users\achituv\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\visibilitysyntax' \
                 r'\test\POI\results_file/cliped.shp'
    input = upload_new_layer(input_path, "test_input")
    hubs_path = r'C:\Users\achituv\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\visibilitysyntax' \
                r'\test\POI\results_file/points_along.shp'
    hubs = upload_new_layer(hubs_path, "test_hubs_")
    output = r'C:\Users\achituv\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\visibilitysyntax' \
             r'\test\POI\results_file/line_to_points.shp'

    alg = HubDistanceLines()
    alg.initAlgorithm()
    # Some preprocessing for context
    project = QgsProject.instance()

    target_crs = QgsCoordinateReferenceSystem()
    target_crs.createFromOgcWmsCrs(input.crs().authid())
    project.setCrs(target_crs)
    context = QgsProcessingContext()
    context.setProject(project)
    params = {
        'INPUT': input,
        'HUBS': hubs,
        'FIELD': 'vis_id',
        'UNIT': 4,
        'OUTPUT': output
    }
    alg.processAlgorithm(params, context, feedback=feedback)
    """For standalone application"""
    # Exit applications
    QgsApplication.exitQgis()
    app.exit()
Example #27
0
    def __init__(self, use, path_shp, name):
        # Initiate QgsApplication in case of standalone app
        if use == "standalone":
            self.app = QGuiApplication([])
            QgsApplication.setPrefixPath(
                r'C:\Program Files\QGIS 3.0\apps\qgis', True)
            QgsApplication.processingRegistry().addProvider(
                QgsNativeAlgorithms())
            QgsApplication.initQgis()
        self.feedback = QgsProcessingFeedback()
        self.shp = self.upload_new_layer(path_shp, name)
        # This variable is to run built tools
        self.shp_path = path_shp

        # Build Qtree object
        my_tree = QTree(10)
        my_tree = self.from_qgis_to_Qtree_list(my_tree)

        # Dimension for Qtree
        x0 = min(my_tree.points, key=lambda x: x.x).x
        y0 = min(my_tree.points, key=lambda x: x.y).y
        x1 = max(my_tree.points, key=lambda x: x.x).x
        y1 = max(my_tree.points, key=lambda x: x.y).y
        w = x1 - x0
        h = y1 - y0
        my_tree.add_root(x0, y0, w, h)
        # print(str(x0) + "," + str(y0) + "," + str(x1) + "," + str(y1))
        # Build the graph
        my_tree.subdivide()
        # Add new field to the network based on which it would be dissolved
        self.add_new_field_and_papulate_it(my_tree.line, "group",
                                           QVariant.LongLong)
        # Dissolve the network
        self.dissolve(
            "group",
            os.path.dirname(__file__) + r'/processing/dissolve_0.shp')
Example #28
0
File: GuiTest.py Project: a17r/QGIS
# This will get replaced with a git SHA1 when you do a git archive

__revision__ = 'fb5caa7a0f944788119b3429efb8f017964c5443'

from qgis.testing import start_app, unittest
from qgis.core import QgsApplication
from qgis.analysis import QgsNativeAlgorithms

from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.gui.BatchAlgorithmDialog import BatchAlgorithmDialog
from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
from processing.gui.wrappers import *

start_app()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())


class AlgorithmDialogTest(unittest.TestCase):

    def testCreation(self):
        alg = QgsApplication.processingRegistry().algorithmById('native:centroids')
        a = AlgorithmDialog(alg)
        self.assertEqual(a.mainWidget().alg, alg)


class WrappersTest(unittest.TestCase):

    def checkConstructWrapper(self, param, expected_wrapper_class):
        alg = QgsApplication.processingRegistry().algorithmById('native:centroids')
    def calculate(self, process_path):

        qgs = QgsApplication([], False)

        qgs.initQgis()
        Processing.initialize()
        QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

        gdal.AllRegister()

        inputLayer = self.input_file
        process_path = process_path
        outPath = self.output_file
        cellSize = self.cellSize
        Elevation = self.elevation
        lista_table = self.rattings

        for file in glob.glob(os.path.dirname(inputLayer) + "/a.*"):
            copyfile(file, process_path + os.path.basename(file))

        inputLayer = process_path + os.path.basename(inputLayer)

        layer = QgsVectorLayer(inputLayer, inputLayer, "ogr")
        vectorlayer_vector = layer.dataProvider()
        # extent
        extent_rect = vectorlayer_vector.extent()
        xmin = extent_rect.xMinimum()
        xmax = extent_rect.xMaximum()
        ymin = extent_rect.yMinimum()
        ymax = extent_rect.yMaximum()
        extent = str(xmin) + "," + str(xmax) + "," + str(ymin) + "," + str(
            ymax)

        # read fields and add a new column with the indexes
        fields = layer.fields()
        new_field = QgsField("Indexes", QVariant.Double)
        layer_new = vectorlayer_vector.addAttributes([new_field])
        layer.updateFields()
        newFieldIndex = vectorlayer_vector.fieldNameIndex(new_field.name())
        allAttrs = vectorlayer_vector.attributeIndexes()
        # editing the new column
        #numberRows = int(self.tableWidget.rowCount())
        #numberColumns = int(self.tableWidget.columnCount())
        #classes = ''
        #lista = []
        #for i in range(0,numberRows):
        #    for j in range(0,numberColumns):
        #        self.line = self.tableWidget.item(i,j)
        #        lista = lista + [str(self.line.text())]

        # list of description on tool table
        #lista_table = lista

        field_names = [field.name() for field in fields]
        n = len(field_names)
        lista_attrib = []
        for i in range(0, n):
            f = field_names[i]
            if f == str(Elevation):
                number = i
                for feat in layer.getFeatures():
                    attrb = feat.attributes()
                    attribute_read = attrb[number]
                    lista_attrib = lista_attrib + [str(attribute_read)]
        # list of description on attribute table of shapefile
        lista_attributes = lista_attrib
        #QMessageBox.about(self, "teste", str(lista_attributes))

        # obtain the indexes of the description of shapefile attribute table
        description_common = set(lista_attributes).intersection(lista_table)
        listDescription = list(description_common)

        listElem = []
        listElements = []
        for j in range(0, len(listDescription)):
            elem = lista_table.index(listDescription[j])
            listElements = listElements + [elem]

            elem_index = lista_table[int(elem + 1)]
            listElem = listElem + [float(elem_index)]

        for l in range(0, len(listElem)):
            layer.startEditing()
            exp = QgsExpression(str(listElem[l]))

            #exp.prepare()
            elemDescription = lista_table[listElements[l]]
            for f in layer.getFeatures():
                # get attributes of column defined by the user
                attrb_elem = f[number]
                if attrb_elem == elemDescription:
                    f[newFieldIndex] = exp.evaluate()
                    layer.updateFeature(f)
            layer.commitChanges()
        list_attrb_newField = []
        for features in layer.getFeatures():
            attrb_newField = features.attributes()
            attrb_newField_read = attrb_newField[number + 1]

        # update and read the new field
        fieldsNew = layer.fields()
        field_names_new = [newField.name() for newField in fieldsNew]
        parameter_indexes = field_names_new[newFieldIndex]

        Processing.initialize()
        calculate = process_path + "/result.tif"
        #soil = QFileInfo(QgsApplication.qgisUserDatabaseFilePath()).path() + "/soil.sdat"
        Processing.runAlgorithm(
            "grass7:v.to.rast", {
                'input': inputLayer,
                'type': [0, 1, 3],
                'where': '',
                'use': 0,
                'attribute_column': parameter_indexes,
                'rgb_column': None,
                'label_column': None,
                'value': 1,
                'memory': 300,
                'output': calculate,
                'GRASS_REGION_PARAMETER': extent,
                'GRASS_REGION_CELLSIZE_PARAMETER': cellSize,
                'GRASS_RASTER_FORMAT_OPT': '',
                'GRASS_RASTER_FORMAT_META': '',
                'GRASS_SNAP_TOLERANCE_PARAMETER': -1,
                'GRASS_MIN_AREA_PARAMETER': 0.0001
            })

        out_raster = gdal.Open(calculate)
        gdal.Warp(outPath, out_raster, dstSRS="EPSG:3857")
    def calculate(self, process_path):
        qgs = QgsApplication([], False)
        qgs.initQgis()
        Processing.initialize()
            
        QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

        gdal.AllRegister()

        #for alg in QgsApplication.processingRegistry().algorithms():
        #    print(alg.id(), "->", alg.displayName())

        # read mdt data
        inputRaster = self.input_mdt
        process_path = process_path
        outPath2 = self.output_file
        
        
        gdalRaster = gdal.Open(str(inputRaster))

        x = gdalRaster.RasterXSize
        y = gdalRaster.RasterYSize
        geo = gdalRaster.GetGeoTransform()  
        minx = geo[0]
        maxy = geo[3]
        maxx = minx + geo[1]*x
        miny = maxy + geo[5]*y
        #extent_raster = str(minx) + "," + str(maxx) + "," + str(miny) + "," + str(maxy)  
        #pixelSize = geo[1]
        band_mdt = gdalRaster.GetRasterBand(1)
        #data_mdt = band_mdt.ReadAsArray(0, 0, x, y)
        
        Processing.initialize()
        # mdt_interp = QFileInfo(QgsApplication.qgisUserDatabaseFilePath()).path() + "/mdt_interp"
        # Processing.runAlgorithm("grass7:r.surf.idw", None, inputRaster, 12, False, extent_raster, pixelSize, mdt_interp)
        # mdt = mdt_interp + "." + "tif"
        #
        # gdalMDT = gdal.Open(str(mdt_interp) + "." + "tif")
        # x_mdt = gdalMDT.RasterXSize
        # y_mdt = gdalMDT.RasterYSize
        # geo_mdt = gdalMDT.GetGeoTransform()
        # band_mdt = gdalMDT.GetRasterBand(1)
        # data_mdt = band_mdt.ReadAsArray(0,0,x_mdt,y_mdt)
        # coeficients a and b of the regression lines, y = ax + b, used for mean monthly precipitation, y(mm), as a function of altitude, x(m)
        # a = 0.99
        # b = 542.22
        # precip_mul = numpy.multiply(data_mdt,a)
        # precipitat = precip_mul + b
        # precipitation = numpy.array(precipitat)
        # recharge = numpy.multiply(precipitation, 0.15)
        recharge_without_rec = process_path + "recharge_without_rec"
        #Processing.runAlgorithm("gdal:rastercalculator",{
        #    'INPUT_A': inputRaster,
        #    'BAND_A': 1,
        #    'INPUT_B': None,
        #    'BAND_B': -1,
        #    'INPUT_C': None,
        #    'BAND_C': -1,
        #    'INPUT_D': None,
        #    'BAND_D': -1,
        #    'INPUT_E': None,
        #    'BAND_E': -1,
        #    'INPUT_F': None,
        #    'BAND_F': -1,
        #    'FORMULA': '(A*0.99+542.22)*0.15',
        #    'NO_DATA': None,
        #    'RTYPE': 6,
        #    'EXTRA': '',
        #    'OPTIONS': '',
        #    'OUTPUT':recharge_without_rec
        #})
        Processing.runAlgorithm("grass7:r.mapcalc.simple", {
            'a': str(inputRaster),
            'b': None,
            'c': None,
            'd': None,
            'e': None,
            'f': None,
            'expression': "((A*0.99)+542.22)*0.15",
            'output': recharge_without_rec,
            'GRASS_REGION_PARAMETER': None,
            'GRASS_REGION_CELLSIZE_PARAMETER': 0,
            'GRASS_RASTER_FORMAT_OPT': '',
            'GRASS_RASTER_FORMAT_META': ''
        })
        # Create an output imagedriver with the multiplication result
        # driver2 = gdal.GetDriverByName( "GTiff" )
        # outData2 = driver2.Create(str(recharge_without_rec+'.'+'tif'), x,y,1, gdal.GDT_Float32)
        # outData2.GetRasterBand(1).WriteArray(recharge)
        # outData2.SetGeoTransform(geo)
        #outData2 = None
        
        recharge_without_rec_file = gdal.Open(recharge_without_rec)
        recharge_without_rec_rep = process_path + "recharge_without_rec_rep"
        gdal.Warp(recharge_without_rec_rep, recharge_without_rec_file, dstSRS="EPSG:3763")

        #Processing.runAlgorithm("gdal:assignprojection",
        #                {'INPUT': recharge_without_rec,
        #                'CRS': QgsCoordinateReferenceSystem('EPSG:3763')})

        # indexes for topography for the two methods
        #numberRows = int(self.tableWidget.rowCount())
        #numberColumns = int(self.tableWidget.columnCount())
        #classes = ''
        #lista = []
        #for i in range(0,numberRows):
        #    for j in range(0,numberColumns):
        #        self.line = self.tableWidget.item(i,j)
        #        lista = lista + [str(self.line.text())]
        #        string = ","
        #        intervalos = string.join(lista)
        #results = list(map(int, lista))
        #QMessageBox.about(self, 'teste', str(results))
        
        Processing.initialize()
        result = process_path + "/result.tif"
        Processing.runAlgorithm("native:reclassifybytable", {
            'INPUT_RASTER': recharge_without_rec_rep,
            'RASTER_BAND': 1, 'TABLE': self.rattings,
            'NO_DATA': -9999, 'RANGE_BOUNDARIES': 0, 'NODATA_FOR_MISSING': False, 'DATA_TYPE': 5,
            'OUTPUT': result})

        # add result into canvas
        #file_info_norm = QFileInfo(str(outPath2))
        # QMessageBox.about(self, "teste", str(file_info_norm))
        #rlayer_new_norm = QgsRasterLayer(outPath2, file_info_norm.fileName(), 'gdal')
        # QMessageBox.about(self, "teste", str(rlayer_new_norm))
        #QgsProject.instance().addMapLayer(rlayer_new_norm)
        #self.iface.canvas.setExtent(rlayer_new_norm.extent())
        # set the map canvas layer set
        #self.iface.canvas.setLayers([rlayer_new_norm])
        # add result into canvas
        # file_info_recharge = QFileInfo(outPath2)
        # if file_info_recharge.exists():
        #     layer_name_recharge = file_info_recharge.baseName()
        # else:
        #     return False
        # rlayer_new_recharge = QgsRasterLayer(outPath2, layer_name_recharge)
        # if rlayer_new_recharge.isValid():
        #     QgsMapLayerRegistry.instance().addMapLayer(rlayer_new_recharge)
        #     layer_prec_recharge = QgsMapCanvasLayer(rlayer_new_recharge)
        #     layerList_recharge = [layer_prec_recharge]
        #     extent_recharge = self.iface.canvas.setExtent(rlayer_new_recharge.extent())
        #     self.iface.canvas.setLayerSet(layerList_recharge)
        #     self.iface.canvas.setVisible(True)
        #     return True
        # else:
        #     return False
        #QMessageBox.information(self, self.tr( "Finished" ), self.tr( "Net Recharge completed." ) )
        
        out_raster = gdal.Open(result)
        gdal.Warp(outPath2, out_raster, dstSRS="EPSG:3857")