Пример #1
0
 def testZeroArgFunctionsTakeNoArgs(self):
     QgsExpression.registerFunction(self.special)
     special = self.special
     self.assertEqual(special.name(), "special")
     exp = QgsExpression("special()")
     result = exp.evaluate()
     self.assertEqual("test", result)
Пример #2
0
 def testZeroArgFunctionsTakeNoArgs(self):
     QgsExpression.registerFunction(self.special)
     special = self.special
     self.assertEqual(special.name(), 'special')
     exp = QgsExpression('special()')
     result = exp.evaluate()
     self.assertEqual('test', result)
Пример #3
0
 def testHandlesNull(self):
     context = QgsExpressionContext()
     QgsExpression.registerFunction(self.null_mean)
     exp = QgsExpression('null_mean(1, 2, NULL, 3)')
     result = exp.evaluate(context)
     self.assertFalse(exp.hasEvalError())
     self.assertEqual(result, 2)
Пример #4
0
 def testCanUnregisterFunction(self):
     QgsExpression.registerFunction(self.testfun)
     index = QgsExpression.functionIndex('testfun')
     self.assertNotEqual(index, -1)
     error = QgsExpression.unregisterFunction('testfun')
     self.assertTrue(error)
     index = QgsExpression.functionIndex('testfun')
     self.assertEqual(index, -1)
Пример #5
0
 def testCanUnregisterFunction(self):
     QgsExpression.registerFunction(self.testfun)
     index = QgsExpression.functionIndex("testfun")
     self.assertTrue(not index == -1)
     error = QgsExpression.unregisterFunction("testfun")
     self.assertTrue(error)
     index = QgsExpression.functionIndex("testfun")
     self.assertTrue(index == -1)
Пример #6
0
    def testHelp(self):
        QgsExpression.registerFunction(self.help_with_variable)
        html = ('<h3>help_with_variable function</h3><br>'
                'The help comes from a variable.')
        self.assertEqual(self.help_with_variable.helpText(), html)

        QgsExpression.registerFunction(self.help_with_docstring)
        html = ('<h3>help_with_docstring function</h3><br>'
                'The help comes from the python docstring.')
        self.assertEqual(self.help_with_docstring.helpText(), html)
Пример #7
0
 def wrapper(func):
     name = kwargs.get('name', func.__name__)
     help = func.__doc__ or ''
     help = help.strip()
     if args == 0 and not name[0] == '$':
         name = '${0}'.format(name)
     func.__name__ = name
     f = QgsExpressionFunction(name, args, group, '')
     f.func = func
     register = kwargs.get('register', True)
     if register:
         QgsExpression.registerFunction(f)
     return f
Пример #8
0
 def wrapper(func):
     name = kwargs.get("name", func.__name__)
     help = func.__doc__ or ""
     help = help.strip()
     if args == 0 and not name[0] == "$":
         name = "${0}".format(name)
     func.__name__ = name
     help = helptemplate.safe_substitute(name=name, doc=help)
     f = QgsExpressionFunction(name, args, group, help)
     f.func = func
     register = kwargs.get("register", True)
     if register:
         QgsExpression.registerFunction(f)
     return f
Пример #9
0
 def wrapper(func):
   name = kwargs.get('name', func.__name__)
   usesgeometry = kwargs.get('usesgeometry', False)
   help = func.__doc__ or ''
   help = help.strip()
   if args == 0 and not name[0] == '$':
     name = '${0}'.format(name)
   func.__name__ = name
   help = helptemplate.safe_substitute(name=name, doc=help)
   f = QgsExpressionFunction(name, args, group, help, usesgeometry)
   f.func = func
   register = kwargs.get('register', True)
   if register:
     QgsExpression.registerFunction(f)
   return f
Пример #10
0
 def testCanBeRegistered(self):
     QgsExpression.registerFunction(self.testfun)
     index = QgsExpression.functionIndex('testfun')
     self.assertTrue(not index == -1)
Пример #11
0
 def testCantReregister(self):
     QgsExpression.registerFunction(self.testfun)
     success = QgsExpression.registerFunction(self.testfun)
     self.assertFalse(success)
def register_qgis_expressions():
    from qgis.core import QgsExpression
    from .data import qgis_functions
    QgsExpression.registerFunction(qgis_functions.get_zoom_for_scale)
    QgsExpression.registerFunction(qgis_functions.if_not_exists)
    QgsExpression.registerFunction(qgis_functions.interpolate_exp)
Пример #13
0
 def testCanRegisterGeometryFunction(self):
     success = QgsExpression.registerFunction(self.geomtest)
     self.assertTrue(success)
Пример #14
0
 def testReferencedColumnsSet(self):
     QgsExpression.registerFunction(self.referenced_columns_set)
     exp = QgsExpression('referenced_columns_set()')
     self.assertEqual(set(exp.referencedColumns()), set(['a', 'b']))
Пример #15
0
 def testReferencedColumnsNoSet(self):
     success = QgsExpression.registerFunction(
         self.no_referenced_columns_set)
     exp = QgsExpression('no_referenced_columns_set()')
     self.assertEqual(exp.referencedColumns(),
                      {QgsFeatureRequest.AllAttributes})
Пример #16
0
 def testCanReregisterAfterUnregister(self):
     QgsExpression.registerFunction(self.testfun)
     QgsExpression.unregisterFunction("testfun")
     success = QgsExpression.registerFunction(self.testfun)
     self.assertTrue(success)
Пример #17
0
    def initGui(self):
        self.mask_geometry_function = MaskGeometryFunction(self)
        QgsExpression.registerFunction(self.mask_geometry_function)
        self.in_mask_function = InMaskFunction(self)
        QgsExpression.registerFunction(self.in_mask_function)

        #
        self.disable_remove_mask_signal = False
        self.disable_add_layer_signal = False
        self.project = QgsProject.instance()
        self.project.layerWillBeRemoved.connect(self.on_remove_mask)

        self.act_aeag_mask = QAction(QIcon(":plugins/mask/aeag_mask.png"),
                                     self.tr("Create a mask"),
                                     self.iface.mainWindow())

        self.toolBar = self.iface.pluginToolBar()
        self.toolBar.addAction(self.act_aeag_mask)
        self.iface.addPluginToMenu("&Mask", self.act_aeag_mask)

        # turn it to true to enable test
        if False:
            self.act_test = QAction(QIcon(":plugins/mask/aeag_mask.png"),
                                    "Test", self.iface.mainWindow())
            self.toolBar.addAction(self.act_test)
            self.iface.addPluginToMenu("&Mask", self.act_test)
            self.act_test.triggered.connect(self.do_test)

        # Add documentation links to the menu
        self.act_aeag_about = QAction(self.tr("About"), self.iface.mainWindow())
        self.act_aeag_about.triggered.connect(self.on_about)
        self.act_aeag_doc = QAction(self.tr("Documentation"), self.iface.mainWindow())
        self.act_aeag_doc.triggered.connect(self.on_doc)
        self.iface.addPluginToMenu("&Mask", self.act_aeag_about)
        self.iface.addPluginToMenu("&Mask", self.act_aeag_doc)

        # Add actions to the toolbar
        self.act_aeag_mask.triggered.connect(self.run)

        # look for an existing mask layer
        mask_id, ok = QgsProject.instance().readEntry("Mask", "layer_id")
        self.layer = self.project.mapLayer(mask_id)

        # register layout signals
        lm = QgsProject.instance().layoutManager()
        lm.layoutAdded.connect(self.on_layout_added)
        lm.layoutAboutToBeRemoved.connect(self.on_layout_removed)

        # register already existing layouts
        for layout in lm.printLayouts():
            self.on_layout_added(layout)

        # register to the change of active layer for enabling/disabling
        #   of the action
        self.old_active_layer = None
        self.iface.mapCanvas().currentLayerChanged.connect(self.on_current_layer_changed)
        self.on_current_layer_changed(None)

        # register to project reading
        # connect to QgisApp::projectRead to make sure MemoryLayerSaver has
        # been called before (it connects to QgsProject::readProject)
        self.iface.mainWindow().projectRead.connect(self.on_project_open)
Пример #18
0
 def testCanBeRegistered(self):
     QgsExpression.registerFunction(self.testfun)
     index = QgsExpression.functionIndex('testfun')
     self.assertTrue(not index == -1)
Пример #19
0
 def testReferencedColumnsSet(self):
     QgsExpression.registerFunction(self.referenced_columns_set)
     exp = QgsExpression('referenced_columns_set()')
     self.assertEqual(set(exp.referencedColumns()), set(['a', 'b']))
Пример #20
0
 def testReferencedColumnsNoSet(self):
     QgsExpression.registerFunction(self.no_referenced_columns_set)
     exp = QgsExpression('no_referenced_columns_set()')
     self.assertEqual(exp.referencedColumns(),
                      {QgsFeatureRequest.ALL_ATTRIBUTES})
Пример #21
0
 def testCanRegisterGeometryFunction(self):
     success = QgsExpression.registerFunction(self.geomtest)
     self.assertTrue(success)
Пример #22
0
 def testCantOverrideBuiltinsWithRegister(self):
     success = QgsExpression.registerFunction(self.sqrt)
     self.assertFalse(success)
Пример #23
0
 def testCanReregisterAfterUnregister(self):
     QgsExpression.registerFunction(self.testfun)
     QgsExpression.unregisterFunction("testfun")
     success = QgsExpression.registerFunction(self.testfun)
     self.assertTrue(success)
Пример #24
0
 def testCanEvaluateFunction(self):
     QgsExpression.registerFunction(self.testfun)
     exp = QgsExpression('testfun(1)')
     result = exp.evaluate()
     self.assertEqual('Testing_1', result)
Пример #25
0
def registerFunctions():
    for func in functions:
        if QgsExpression.registerFunction(func):
            yield func.name()
Пример #26
0
 def testCantReregister(self):
     QgsExpression.registerFunction(self.testfun)
     success = QgsExpression.registerFunction(self.testfun)
     self.assertFalse(success)
Пример #27
0
 def testCanBeRegistered(self):
     QgsExpression.registerFunction(self.testfun)
     index = QgsExpression.functionIndex('testfun')
     self.assertNotEqual(index, -1)
Пример #28
0
 def testCantOverrideBuiltinsWithRegister(self):
     success = QgsExpression.registerFunction(self.sqrt)
     self.assertFalse(success)
Пример #29
0
 def register_exp_functions():
     QgsExpression.registerFunction(nearest_feature_attr_value)
     QgsExpression.registerFunction(nearest_pt_on_line_interpolate_z)
     QgsExpression.registerFunction(intersecting_features_attr_average)
     QgsExpression.registerFunction(interpolate_from_mesh)
Пример #30
0
 def testReferencedColumnsNoSet(self):
     QgsExpression.registerFunction(self.no_referenced_columns_set)
     exp = QgsExpression('no_referenced_columns_set()')
     self.assertEqual(exp.referencedColumns(),
                      {QgsFeatureRequest.ALL_ATTRIBUTES})
Пример #31
0
 def testReferencedColumnsSet(self):
     success = QgsExpression.registerFunction(self.referenced_columns_set)
     exp = QgsExpression("referenced_columns_set()")
     self.assertEqual(set(exp.referencedColumns()), set(["a", "b"]))
Пример #32
0
 def testCanBeRegistered(self):
     QgsExpression.registerFunction(self.testfun)
     index = QgsExpression.functionIndex('testfun')
     self.assertNotEqual(index, -1)
Пример #33
0
 def testCanEvaluateFunction(self):
     QgsExpression.registerFunction(self.testfun)
     exp = QgsExpression('testfun(1)')
     result = exp.evaluate()
     self.assertEqual('Testing_1', result)
Пример #34
0
 def testReferencedColumnsNoSet(self):
     success = QgsExpression.registerFunction(self.no_referenced_columns_set)
     exp = QgsExpression("no_referenced_columns_set()")
     self.assertEqual(exp.referencedColumns(), {QgsFeatureRequest.AllAttributes})
Пример #35
0
    def initGui(self):
        self.mask_geometry_function = MaskGeometryFunction(self)
        QgsExpression.registerFunction(self.mask_geometry_function)
        self.in_mask_function = InMaskFunction(self)
        QgsExpression.registerFunction(self.in_mask_function)

        #
        self.disable_remove_mask_signal = False
        self.disable_add_layer_signal = False
        self.project = QgsProject.instance()
        self.project.layerWillBeRemoved.connect(self.on_remove_mask)

        self.act_aeag_mask = QAction(
            QIcon(":plugins/mask/aeag_mask.png"),
            self.tr("Create a mask"),
            self.iface.mainWindow(),
        )

        self.toolBar = self.iface.pluginToolBar()
        self.toolBar.addAction(self.act_aeag_mask)
        self.iface.addPluginToMenu("&Mask", self.act_aeag_mask)

        # turn it to true to enable test
        if False:
            self.act_test = QAction(QIcon(":plugins/mask/aeag_mask.png"),
                                    "Test", self.iface.mainWindow())
            self.toolBar.addAction(self.act_test)
            self.iface.addPluginToMenu("&Mask", self.act_test)
            self.act_test.triggered.connect(self.do_test)

        # Add documentation links to the menu
        self.act_aeag_about = QAction(self.tr("About"),
                                      self.iface.mainWindow())
        self.act_aeag_about.triggered.connect(self.on_about)
        self.act_aeag_doc = QAction(self.tr("Documentation"),
                                    self.iface.mainWindow())
        self.act_aeag_doc.triggered.connect(self.on_doc)
        self.iface.addPluginToMenu("&Mask", self.act_aeag_about)
        self.iface.addPluginToMenu("&Mask", self.act_aeag_doc)

        # Add actions to the toolbar
        self.act_aeag_mask.triggered.connect(self.run)

        # look for an existing mask layer
        mask_id, ok = QgsProject.instance().readEntry("Mask", "layer_id")
        self.layer = self.project.mapLayer(mask_id)

        # register layout signals
        lm = QgsProject.instance().layoutManager()
        lm.layoutAdded.connect(self.on_layout_added)
        lm.layoutAboutToBeRemoved.connect(self.on_layout_removed)

        # register already existing layouts
        for layout in lm.printLayouts():
            self.on_layout_added(layout.name())

        # register to the change of active layer for enabling/disabling
        #   of the action
        self.old_active_layer = None
        self.iface.mapCanvas().currentLayerChanged.connect(
            self.on_current_layer_changed)
        self.on_current_layer_changed(None)

        # register to project reading
        # connect to QgisApp::projectRead to make sure MemoryLayerSaver has
        # been called before (it connects to QgsProject::readProject)
        self.iface.mainWindow().projectRead.connect(self.on_project_open)