def alghelp(name): alg = Sextante.getAlgorithm(name) if alg != None: print(str(alg)) algoptions(name) else: print "Algorithm not found"
def executeAlgorithm(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Sextante.getAlgorithm(item.alg.commandLineName()) message = alg.checkBeforeOpeningParametersDialog() if message: QtGui.QMessageBox.warning(self, "Warning", message) return alg = alg.getCopy() #copy.deepcopy(alg) dlg = alg.getCustomParametersDialog() if not dlg: dlg = ParametersDialog(alg) canvas = QGisLayers.iface.mapCanvas() prevMapTool = canvas.mapTool() dlg.show() dlg.exec_() if canvas.mapTool() != prevMapTool: try: canvas.mapTool().reset() except: pass canvas.setMapTool(prevMapTool) if dlg.executed: showRecent = SextanteConfig.getSetting( SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: self.fillTree() if isinstance(item, TreeActionItem): action = item.action action.setData(self) action.execute()
def alghelp(name): alg = Sextante.getAlgorithm(name) if alg != None: print(str(alg)) algoptions(name) else: print "Algorithm not found"
def executeAlgorithm(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Sextante.getAlgorithm(item.alg.commandLineName()) message = alg.checkBeforeOpeningParametersDialog() if message: dlg = MissingDependencyDialog(message) dlg.exec_() #QMessageBox.warning(self, self.tr("Warning"), message) return alg = alg.getCopy() dlg = alg.getCustomParametersDialog() if not dlg: dlg = ParametersDialog(alg) canvas = QGisLayers.iface.mapCanvas() prevMapTool = canvas.mapTool() dlg.show() dlg.exec_() if canvas.mapTool()!=prevMapTool: try: canvas.mapTool().reset() except: pass canvas.setMapTool(prevMapTool) if dlg.executed: showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: self.addRecentAlgorithms(True) if isinstance(item, TreeActionItem): action = item.action action.setData(self) action.execute()
def addRecentAlgorithms(self, updating): showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: recent = SextanteLog.getRecentAlgorithms() if len(recent) != 0: found = False if updating: recentItem = self.algorithmTree.topLevelItem(0) treeWidget = recentItem.treeWidget() treeWidget.takeTopLevelItem(treeWidget.indexOfTopLevelItem(recentItem)) #self.algorithmTree.removeItemWidget(first, 0) recentItem = QTreeWidgetItem() recentItem.setText(0, self.tr("Recently used algorithms")) for algname in recent: alg = Sextante.getAlgorithm(algname) if alg is not None: algItem = TreeAlgorithmItem(alg) recentItem.addChild(algItem) found = True if found: self.algorithmTree.insertTopLevelItem(0, recentItem) recentItem.setExpanded(True) self.algorithmTree.setWordWrap(True)
def addRecentAlgorithms(self, updating): showRecent = SextanteConfig.getSetting( SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: recent = SextanteLog.getRecentAlgorithms() if len(recent) != 0: found = False if updating: recentItem = self.algorithmTree.topLevelItem(0) treeWidget = recentItem.treeWidget() treeWidget.takeTopLevelItem( treeWidget.indexOfTopLevelItem(recentItem)) #self.algorithmTree.removeItemWidget(first, 0) recentItem = QTreeWidgetItem() recentItem.setText(0, self.tr("Recently used algorithms")) for algname in recent: alg = Sextante.getAlgorithm(algname) if alg is not None: algItem = TreeAlgorithmItem(alg) recentItem.addChild(algItem) found = True if found: self.algorithmTree.insertTopLevelItem(0, recentItem) recentItem.setExpanded(True) self.algorithmTree.setWordWrap(True)
def fillTree(self): useCategories = SextanteConfig.getSetting(SextanteConfig.USE_CATEGORIES) if useCategories: self.fillTreeUsingCategories() else: self.fillTreeUsingProviders() self.algorithmTree.sortItems(0, Qt.AscendingOrder) showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: recent = SextanteLog.getRecentAlgorithms() if len(recent) != 0: found = False recentItem = QTreeWidgetItem() recentItem.setText(0, self.tr("Recently used algorithms")) for algname in recent: alg = Sextante.getAlgorithm(algname) if alg is not None: algItem = TreeAlgorithmItem(alg) recentItem.addChild(algItem) found = True if found: self.algorithmTree.insertTopLevelItem(0, recentItem) recentItem.setExpanded(True) self.algorithmTree.setWordWrap(True)
def createTest(text): s = "" tokens = text[len("sextante.runalg("):-1].split(",") cmdname = tokens[0][1:-1]; methodname = "test_" + cmdname.replace(":","") s += "def " + methodname + "(self):\n" alg = Sextante.getAlgorithm(cmdname) execcommand = "sextante.runalg(" i = 0 for token in tokens: if i < alg.getVisibleParametersCount() + 1: if os.path.exists(token[1:-1]): token = os.path.basename(token[1:-1])[:-4] execcommand += token + "()," else: execcommand += "None," i+=1 s += "\toutputs=" + execcommand[:-1] + ")\n" i = -1 * len(alg.outputs) for out in alg.outputs: filename = tokens[i][1:-1] if (filename == str(None)): raise Exception("Cannot create unit test for that algorithm execution.\nThe output cannot be a temporary file") s+="\toutput=outputs['" + out.name + "']\n" if isinstance(out, (OutputNumber, OutputString)): s+="self.assertTrue(" + str(out) + ", output)\n" if isinstance(out, OutputRaster): dataset = gdal.Open(filename, GA_ReadOnly) array = dataset.ReadAsArray(1) s+="\tself.assertTrue(os.path.isfile(output))\n" s+="\tself.assertEqual(hashraster(output)," + str(hash(array)) + ")\n" if isinstance(out, OutputVector): layer = Sextante.getObject(filename) fields = layer.pendingFields() s+="\tlayer=QGisLayers.getObjectFromUri(output, True)\n" s+="\tfields=layer.pendingFields()\n" s+="\texpectednames=[" + ",".join(["'" + str(f.name()) + "'" for f in fields]) + "]\n" s+="\texpectedtypes=[" + ",".join(["'" + str(f.typeName()) +"'" for f in fields]) + "]\n" s+="\tnames=[str(f.name()) for f in fields]\n" s+="\ttypes=[str(f.typeName()) for f in fields]\n" s+="\tself.assertEqual(expectednames, names)\n" s+="\tself.assertEqual(expectedtypes, types)\n" features = QGisLayers.features(layer) numfeat = len(features) s+="\tfeatures=sextante.getfeatures(layer)\n" s+="\tself.assertEqual(" + str(numfeat) + ", len(features))\n" if numfeat > 0: feature = features.next() attrs = feature.attributes() s+="\tfeature=features.next()\n" s+="\tattrs=feature.attributes()\n" s+="\texpectedvalues=[" + ",".join(['"' + str(attr.toString()) + '"' for attr in attrs]) + "]\n" s+="\tvalues=[str(attr.toString()) for attr in attrs]\n" s+="\tself.assertEqual(expectedvalues, values)\n" dlg = ShowTestDialog(s) dlg.exec_()
def createTest(item): s = "" tokens = item.entry.text[len("sextante.runalg("):-1].split(",") cmdname = tokens[0][1:-1]; methodname = "test_" + cmdname.replace(":","") s += "def " + methodname + "():\n" alg = Sextante.getAlgorithm(cmdname) execcommand = "sextante.runalg(" i = 0 for token in tokens: if i < alg.getVisibleParametersCount(): execcommand+=token + "," else: execcommand+="None," i+=1 s += "\toutputs=" + execcommand[:-1] + ")\n" i = -1 * len(alg.outputs) for out in alg.outputs: filename = tokens[i][1:-1] s+="\toutput=outputs['" + out.name + "']\n" if isinstance(out, (OutputNumber, OutputString)): s+="self.assertTrue(" + str(out) + ", output)\n" if isinstance(out, OutputRaster): dataset = gdal.Open(filename, GA_ReadOnly) array = dataset.ReadAsArray(1) s+="\tself.assertTrue(os.path.isfile(output))\n" s+="\tself.assertEqual(hashraster(output)," + str(hash(array)) + ")\n" if isinstance(out, OutputVector): layer = Sextante.getObject(filename) fields = layer.pendingFields() s+="\tlayer=sextante.getobject(output)\n" s+="\tfields=layer.pendingFields()\n" s+="\texpectednames=[" + ",".join([str(f.name()) for f in fields]) + "]\n" s+="\texpectedtypes=[" + ",".join([str(f.typeName()) for f in fields]) + "]\n" s+="\tnames=[str(f.name()) for f in fields]\n" s+="\ttypes=[str(f.typeName()) for f in fields]\n" s+="\tself.assertEqual(exceptednames, names)\n" s+="\tself.assertEqual(exceptedtypes, types)\n" features = QGisLayers.features(layer) numfeat = len(features) s+="\tfeatures=sextante.getfeatures(layer))\n" s+="\tself.assertEqual(" + str(numfeat) + ", len(features)\n" if numfeat > 0: feature = features.next() attrs = feature.attributes() s+="\tfeature=features.next()\n" s+="\tattrs=feature.attributes()\n" s+="\texpectedvalues=[" + ",".join([str(attr.toString()) for attr in attrs]) + "]\n" s+="\tvalues=[str(attr.toString()) for attr in attrs]\n" s+="\tself.assertEqual(exceptedtypes, types)\n" dlg = ShowTestDialog(s) dlg.exec_()
def algoptions(name): alg = Sextante.getAlgorithm(name) if alg != None: s = "" for param in alg.parameters: if isinstance(param, ParameterSelection): s += param.name + "(" + param.description + ")\n" i = 0 for option in param.options: s += "\t" + str(i) + " - " + str(option) + "\n" i += 1 print(s) else: print "Algorithm not found"
def algoptions(name): alg = Sextante.getAlgorithm(name) if alg != None: s ="" for param in alg.parameters: if isinstance(param, ParameterSelection): s+=param.name + "(" + param.description + ")\n" i=0 for option in param.options: s+= "\t" + str(i) + " - " + str(option) + "\n" i+=1 print(s) else: print "Algorithm not found"
def createTest(text): s = "" tokens = text[len("sextante.runalg("):-1].split(",") cmdname = tokens[0][1:-1] methodname = "test_" + cmdname.replace(":", "") s += "def " + methodname + "(self):\n" alg = Sextante.getAlgorithm(cmdname) execcommand = "sextante.runalg(" i = 0 for token in tokens: if i < alg.getVisibleParametersCount() + 1: if os.path.exists(token[1:-1]): token = os.path.basename(token[1:-1])[:-4] + "()" execcommand += token + "," else: execcommand += "None," i += 1 s += "\toutputs=" + execcommand[:-1] + ")\n" i = -1 * len(alg.outputs) for out in alg.outputs: filename = tokens[i][1:-1] if (tokens[i] == str(None)): QtGui.QMessageBox.critical( None, "Error", "Cannot create unit test for that algorithm execution.\nThe output cannot be a temporary file" ) return s += "\toutput=outputs['" + out.name + "']\n" if isinstance(out, (OutputNumber, OutputString)): s += "self.assertTrue(" + str(out) + ", output.value)\n" if isinstance(out, OutputRaster): dataset = gdal.Open(filename, GA_ReadOnly) strhash = hash(str(dataset.ReadAsArray(0).tolist())) s += "\tself.assertTrue(os.path.isfile(output))\n" s += "\tdataset=gdal.Open(output, GA_ReadOnly)\n" s += "\tstrhash=hash(str(dataset.ReadAsArray(0).tolist()))\n" s += "\tself.assertEqual(strhash," + str(strhash) + ")\n" if isinstance(out, OutputVector): layer = Sextante.getObject(filename) fields = layer.pendingFields() s += "\tlayer=QGisLayers.getObjectFromUri(output, True)\n" s += "\tfields=layer.pendingFields()\n" s += "\texpectednames=[" + ",".join( ["'" + str(f.name()) + "'" for f in fields]) + "]\n" s += "\texpectedtypes=[" + ",".join( ["'" + str(f.typeName()) + "'" for f in fields]) + "]\n" s += "\tnames=[str(f.name()) for f in fields]\n" s += "\ttypes=[str(f.typeName()) for f in fields]\n" s += "\tself.assertEqual(expectednames, names)\n" s += "\tself.assertEqual(expectedtypes, types)\n" features = QGisLayers.features(layer) numfeat = len(features) s += "\tfeatures=sextante.getfeatures(layer)\n" s += "\tself.assertEqual(" + str(numfeat) + ", len(features))\n" if numfeat > 0: feature = features.next() attrs = feature.attributes() s += "\tfeature=features.next()\n" s += "\tattrs=feature.attributes()\n" s += "\texpectedvalues=[" + ",".join( ['"' + str(attr) + '"' for attr in attrs]) + "]\n" s += "\tvalues=[str(attr) for attr in attrs]\n" s += "\tself.assertEqual(expectedvalues, values)\n" s += "\twkt='" + str(feature.geometry().exportToWkt()) + "'\n" s += "\tself.assertEqual(wkt, str(feature.geometry().exportToWkt()))" dlg = ShowTestDialog(s) dlg.exec_()
def executeAlgorithmAsBatchProcess(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Sextante.getAlgorithm(item.alg.commandLineName()) dlg = BatchProcessingDialog(alg) dlg.exec_()
def editRenderingStyles(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Sextante.getAlgorithm(item.alg.commandLineName()) dlg = EditRenderingStylesDialog(alg) dlg.exec_()
def fillTree(self): self.algorithmTree.clear() text = unicode(self.searchBox.text()) for providerName in Sextante.algs.keys(): groups = {} provider = Sextante.algs[providerName] name = "ACTIVATE_" + providerName.upper().replace(" ", "_") if not SextanteConfig.getSetting(name): continue algs = provider.values() #add algorithms for alg in algs: if not alg.showInToolbox: continue if text =="" or text.lower() in alg.name.lower(): if alg.group in groups: groupItem = groups[alg.group] else: groupItem = QTreeWidgetItem() groupItem.setText(0,alg.group) groups[alg.group] = groupItem algItem = TreeAlgorithmItem(alg) groupItem.addChild(algItem) actions = Sextante.actions[providerName] for action in actions: if text =="" or text.lower() in action.name.lower(): if action.group in groups: groupItem = groups[action.group] else: groupItem = QTreeWidgetItem() groupItem.setText(0,action.group) groups[action.group] = groupItem algItem = TreeActionItem(action) groupItem.addChild(algItem) if len(groups) > 0: providerItem = QTreeWidgetItem() providerItem.setText(0, Sextante.getProviderFromName(providerName).getDescription() + " [" + str(len(provider)) + " geoalgorithms]") providerItem.setIcon(0, Sextante.getProviderFromName(providerName).getIcon()) for groupItem in groups.values(): providerItem.addChild(groupItem) self.algorithmTree.addTopLevelItem(providerItem) providerItem.setExpanded(text!="") for groupItem in groups.values(): if text != "": groupItem.setExpanded(True) self.algorithmTree.sortItems(0, Qt.AscendingOrder) showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: recent = SextanteLog.getRecentAlgorithms() if len(recent) != 0: found = False recentItem = QTreeWidgetItem() recentItem.setText(0, self.tr("Recently used algorithms")) for algname in recent: alg = Sextante.getAlgorithm(algname) if alg is not None: algItem = TreeAlgorithmItem(alg) recentItem.addChild(algItem) found = True if found: self.algorithmTree.insertTopLevelItem(0, recentItem) recentItem.setExpanded(True) self.algorithmTree.setWordWrap(True)
def fillTree(self): self.algorithmTree.clear() text = unicode(self.searchBox.text()) for providerName in Sextante.algs.keys(): groups = {} provider = Sextante.algs[providerName] name = "ACTIVATE_" + providerName.upper().replace(" ", "_") if not SextanteConfig.getSetting(name): continue algs = provider.values() #add algorithms for alg in algs: if not alg.showInToolbox: continue if text == "" or text.lower() in alg.name.lower(): if alg.group in groups: groupItem = groups[alg.group] else: groupItem = QtGui.QTreeWidgetItem() groupItem.setText(0, alg.group) groups[alg.group] = groupItem algItem = TreeAlgorithmItem(alg) groupItem.addChild(algItem) actions = Sextante.actions[providerName] for action in actions: if text == "" or text.lower() in action.name.lower(): if action.group in groups: groupItem = groups[action.group] else: groupItem = QtGui.QTreeWidgetItem() groupItem.setText(0, action.group) groups[action.group] = groupItem algItem = TreeActionItem(action) groupItem.addChild(algItem) if len(groups) > 0: providerItem = QtGui.QTreeWidgetItem() providerItem.setText( 0, Sextante.getProviderFromName( providerName).getDescription() + " [" + str(len(provider)) + " geoalgorithms]") providerItem.setIcon( 0, Sextante.getProviderFromName(providerName).getIcon()) for groupItem in groups.values(): providerItem.addChild(groupItem) self.algorithmTree.addTopLevelItem(providerItem) providerItem.setExpanded(text != "") for groupItem in groups.values(): if text != "": groupItem.setExpanded(True) self.algorithmTree.sortItems(0, Qt.AscendingOrder) showRecent = SextanteConfig.getSetting( SextanteConfig.SHOW_RECENT_ALGORITHMS) if showRecent: recent = SextanteLog.getRecentAlgorithms() if len(recent) != 0: found = False recentItem = QtGui.QTreeWidgetItem() recentItem.setText(0, "Recently used algorithms") for algname in recent: alg = Sextante.getAlgorithm(algname) if alg is not None: algItem = TreeAlgorithmItem(alg) recentItem.addChild(algItem) found = True if found: self.algorithmTree.insertTopLevelItem(0, recentItem) recentItem.setExpanded(True) self.algorithmTree.setWordWrap(True)
def executeAlgorithmAsBatchProcess(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Sextante.getAlgorithm(item.alg.commandLineName()) dlg = BatchProcessingDialog(alg) dlg.exec_()
def editRenderingStyles(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = Sextante.getAlgorithm(item.alg.commandLineName()) dlg = EditRenderingStylesDialog(alg) dlg.exec_()