1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
|
"""
***************************************************************************
grass_algorithm.py
---------------------
Date : February 2015
Copyright : (C) 2014-2015 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
__author__ = "Victor Olaya"
__date__ = "February 2015"
__copyright__ = "(C) 2012-2015, Victor Olaya"
from typing import Dict, Optional
import sys
import os
import uuid
import math
import importlib
from pathlib import Path
from qgis.PyQt.QtCore import QCoreApplication, QUrl
from qgis.core import (
Qgis,
QgsMapLayer,
QgsRasterLayer,
QgsApplication,
QgsMapLayerType,
QgsCoordinateReferenceSystem,
QgsProcessingUtils,
QgsProcessing,
QgsMessageLog,
QgsVectorFileWriter,
QgsProcessingContext,
QgsProcessingAlgorithm,
QgsProcessingParameterDefinition,
QgsProcessingException,
QgsProcessingParameterCrs,
QgsProcessingParameterExtent,
QgsProcessingParameterEnum,
QgsProcessingParameterNumber,
QgsProcessingParameterString,
QgsProcessingParameterField,
QgsProcessingParameterPoint,
QgsProcessingParameterBoolean,
QgsProcessingParameterRange,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterMultipleLayers,
QgsProcessingParameterVectorDestination,
QgsProcessingParameterRasterDestination,
QgsProcessingParameterFileDestination,
QgsProcessingParameterFile,
QgsProcessingParameterFolderDestination,
QgsProcessingOutputHtml,
QgsVectorLayer,
QgsProviderRegistry,
)
from qgis.utils import iface
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
from osgeo import ogr
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.parameters import getParameterFromString
from grassprovider.parsed_description import ParsedDescription
from grassprovider.grass_utils import GrassUtils
from processing.tools.system import isWindows, getTempFilename
pluginPath = os.path.normpath(
os.path.join(os.path.split(os.path.dirname(__file__))[0], os.pardir)
)
class GrassAlgorithm(QgsProcessingAlgorithm):
GRASS_OUTPUT_TYPE_PARAMETER = "GRASS_OUTPUT_TYPE_PARAMETER"
GRASS_MIN_AREA_PARAMETER = "GRASS_MIN_AREA_PARAMETER"
GRASS_SNAP_TOLERANCE_PARAMETER = "GRASS_SNAP_TOLERANCE_PARAMETER"
GRASS_REGION_EXTENT_PARAMETER = "GRASS_REGION_PARAMETER"
GRASS_REGION_CELLSIZE_PARAMETER = "GRASS_REGION_CELLSIZE_PARAMETER"
GRASS_REGION_ALIGN_TO_RESOLUTION = "GRASS_REGION_ALIGN_TO_RESOLUTION"
GRASS_RASTER_FORMAT_OPT = "GRASS_RASTER_FORMAT_OPT"
GRASS_RASTER_FORMAT_META = "GRASS_RASTER_FORMAT_META"
GRASS_VECTOR_DSCO = "GRASS_VECTOR_DSCO"
GRASS_VECTOR_LCO = "GRASS_VECTOR_LCO"
GRASS_VECTOR_EXPORT_NOCAT = "GRASS_VECTOR_EXPORT_NOCAT"
OUTPUT_TYPES = ["auto", "point", "line", "area"]
QGIS_OUTPUT_TYPES = {
QgsProcessing.SourceType.TypeVectorAnyGeometry: "auto",
QgsProcessing.SourceType.TypeVectorPoint: "point",
QgsProcessing.SourceType.TypeVectorLine: "line",
QgsProcessing.SourceType.TypeVectorPolygon: "area",
}
def __init__(
self,
description_file: Optional[Path] = None,
json_definition: Optional[dict] = None,
description_folder: Optional[Path] = None,
):
super().__init__()
self._name = ""
self._display_name = ""
self._short_description = ""
self._group = ""
self._groupId = ""
self.grass_name = ""
self.params = []
self.hardcodedStrings = []
self.inputLayers = []
self.commands = []
self.outputCommands = []
self.exportedLayers = {}
self.fileOutputs = {}
self._description_file: Optional[Path] = description_file
self._json_definition: Optional[dict] = json_definition
self._description_folder: Optional[Path] = description_folder
# Default GRASS parameters
self.region = None
self.cellSize = None
self.snapTolerance = None
self.outputType = None
self.minArea = None
self.alignToResolution = None
# destination Crs for combineLayerExtents, will be set from layer or mapSettings
self.destination_crs = QgsCoordinateReferenceSystem()
# Load parameters from a description file
if self._description_file is not None:
self._define_characteristics_from_file()
else:
self._define_characteristics_from_json()
self.numExportedLayers = 0
# Do we need this anymore?
self.uniqueSuffix = str(uuid.uuid4()).replace("-", "")
# Use the ext mechanism
self.module = None
try:
extpath = None
ext_name = None
if self._description_file:
ext_name = self.name().replace(".", "_")
extpath = self._description_file.parents[1].joinpath(
"ext", ext_name + ".py"
)
elif self._json_definition.get("ext_path"):
ext_name = self._json_definition["ext_path"]
extpath = self._description_folder.parents[0].joinpath(
"ext", ext_name + ".py"
)
# this check makes it a bit faster
if extpath and extpath.exists():
spec = importlib.util.spec_from_file_location(
"grassprovider.ext." + ext_name, extpath
)
self.module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(self.module)
except Exception as e:
QgsMessageLog.logMessage(
self.tr("Failed to load: {0}\n{1}").format(extpath, e),
"Processing",
Qgis.MessageLevel.Critical,
)
pass
def createInstance(self):
return self.__class__(
description_file=self._description_file,
json_definition=self._json_definition,
description_folder=self._description_folder,
)
def name(self):
return self._name
def displayName(self):
return self._display_name
def shortDescription(self):
return self._short_description
def group(self):
return self._group
def groupId(self):
return self._groupId
def icon(self):
return QgsApplication.getThemeIcon("/providerGrass.svg")
def svgIconPath(self):
return QgsApplication.iconPath("providerGrass.svg")
def flags(self):
# TODO - maybe it's safe to background thread this?
return (
super().flags()
| QgsProcessingAlgorithm.Flag.FlagNoThreading
| QgsProcessingAlgorithm.Flag.FlagDisplayNameIsLiteral
)
def tr(self, string, context=""):
if context == "":
context = self.__class__.__name__
return QCoreApplication.translate(context, string)
def helpUrl(self):
helpPath = GrassUtils.grassHelpPath()
if helpPath == "":
return None
if os.path.exists(helpPath):
return QUrl.fromLocalFile(
os.path.join(helpPath, f"{self.grass_name}.html")
).toString()
else:
return helpPath + f"{self.grass_name}.html"
def initAlgorithm(self, config=None):
"""
Algorithm initialization
"""
for p in self.params:
# We use createOutput argument for automatic output creation
self.addParameter(p, True)
def _define_characteristics_from_file(self):
"""
Create algorithm parameters and outputs from a text file.
"""
results = ParsedDescription.parse_description_file(self._description_file)
self._define_characteristics_from_parsed_description(results)
def _define_characteristics_from_json(self):
"""
Create algorithm parameters and outputs from JSON definition.
"""
results = ParsedDescription.from_dict(self._json_definition)
self._define_characteristics_from_parsed_description(results)
def _define_characteristics_from_parsed_description(
self, description: ParsedDescription
):
"""
Create algorithm parameters and outputs from parsed description
"""
self.grass_name = description.grass_command
self._name = description.name
self._short_description = description.short_description
self._display_name = description.display_name
self._group = description.group
self._groupId = description.group_id
self.hardcodedStrings = description.hardcoded_strings[:]
self.params = []
has_raster_input: bool = False
has_vector_input: bool = False
has_raster_output: bool = False
has_vector_outputs: bool = False
for param_string in description.param_strings:
try:
parameter = getParameterFromString(param_string, "GrassAlgorithm")
except Exception as e:
QgsMessageLog.logMessage(
QCoreApplication.translate(
"GrassAlgorithm", "Could not open GRASS GIS algorithm: {0}"
).format(self._name),
QCoreApplication.translate("GrassAlgorithm", "Processing"),
Qgis.MessageLevel.Critical,
)
raise e
if parameter is None:
continue
self.params.append(parameter)
if isinstance(
parameter,
(
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource,
),
):
has_vector_input = True
elif isinstance(parameter, QgsProcessingParameterRasterLayer):
has_raster_input = True
elif isinstance(parameter, QgsProcessingParameterMultipleLayers):
if parameter.layerType() < 3 or parameter.layerType() == 5:
has_vector_input = True
elif parameter.layerType() == 3:
has_raster_input = True
elif isinstance(parameter, QgsProcessingParameterVectorDestination):
has_vector_outputs = True
elif isinstance(parameter, QgsProcessingParameterRasterDestination):
has_raster_output = True
param = QgsProcessingParameterExtent(
self.GRASS_REGION_EXTENT_PARAMETER,
self.tr("GRASS GIS region extent"),
optional=True,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
self.params.append(param)
if has_raster_output or has_raster_input:
# Add a cellsize parameter
param = QgsProcessingParameterNumber(
self.GRASS_REGION_CELLSIZE_PARAMETER,
self.tr("GRASS GIS region cellsize (leave 0 for default)"),
type=QgsProcessingParameterNumber.Type.Double,
minValue=0.0,
maxValue=sys.float_info.max + 1,
defaultValue=0.0,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
self.params.append(param)
if has_raster_output:
# Add a createopt parameter for format export
param = QgsProcessingParameterString(
self.GRASS_RASTER_FORMAT_OPT,
self.tr("Output Rasters format options (createopt)"),
multiLine=True,
optional=True,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
param.setHelp(self.tr("Creation options should be comma separated"))
self.params.append(param)
# Add a metadata parameter for format export
param = QgsProcessingParameterString(
self.GRASS_RASTER_FORMAT_META,
self.tr("Output Rasters format metadata options (metaopt)"),
multiLine=True,
optional=True,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
param.setHelp(self.tr("Metadata options should be comma separated"))
self.params.append(param)
if has_vector_input:
param = QgsProcessingParameterNumber(
self.GRASS_SNAP_TOLERANCE_PARAMETER,
self.tr("v.in.ogr snap tolerance (-1 = no snap)"),
type=QgsProcessingParameterNumber.Type.Double,
minValue=-1.0,
maxValue=sys.float_info.max + 1,
defaultValue=-1.0,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
self.params.append(param)
param = QgsProcessingParameterNumber(
self.GRASS_MIN_AREA_PARAMETER,
self.tr("v.in.ogr min area"),
type=QgsProcessingParameterNumber.Type.Double,
minValue=0.0,
maxValue=sys.float_info.max + 1,
defaultValue=0.0001,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
self.params.append(param)
if has_vector_outputs:
# Add an optional output type
param = QgsProcessingParameterEnum(
self.GRASS_OUTPUT_TYPE_PARAMETER,
self.tr("v.out.ogr output type"),
self.OUTPUT_TYPES,
defaultValue=0,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
self.params.append(param)
# Add a DSCO parameter for format export
param = QgsProcessingParameterString(
self.GRASS_VECTOR_DSCO,
self.tr("v.out.ogr output data source options (dsco)"),
multiLine=True,
optional=True,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
self.params.append(param)
# Add a LCO parameter for format export
param = QgsProcessingParameterString(
self.GRASS_VECTOR_LCO,
self.tr("v.out.ogr output layer options (lco)"),
multiLine=True,
optional=True,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
self.params.append(param)
# Add a -c flag for export
param = QgsProcessingParameterBoolean(
self.GRASS_VECTOR_EXPORT_NOCAT,
self.tr(
"Also export features without category (not labeled). Otherwise only features with category are exported"
),
False,
)
param.setFlags(
param.flags() | QgsProcessingParameterDefinition.Flag.FlagAdvanced
)
self.params.append(param)
def getDefaultCellSize(self):
"""
Determine a default cell size from all the raster layers.
"""
cellsize = 0.0
layers = [l for l in self.inputLayers if isinstance(l, QgsRasterLayer)]
for layer in layers:
cellsize = max(layer.rasterUnitsPerPixelX(), cellsize)
if cellsize == 0.0:
cellsize = 100.0
return cellsize
def grabDefaultGrassParameters(self, parameters, context):
"""
Imports default GRASS parameters (EXTENT, etc) into
object attributes for faster retrieving.
"""
# GRASS region extent
self.region = self.parameterAsExtent(
parameters, self.GRASS_REGION_EXTENT_PARAMETER, context
)
# GRASS cell size
if self.parameterDefinition(self.GRASS_REGION_CELLSIZE_PARAMETER):
self.cellSize = self.parameterAsDouble(
parameters, self.GRASS_REGION_CELLSIZE_PARAMETER, context
)
# GRASS snap tolerance
self.snapTolerance = self.parameterAsDouble(
parameters, self.GRASS_SNAP_TOLERANCE_PARAMETER, context
)
# GRASS min area
self.minArea = self.parameterAsDouble(
parameters, self.GRASS_MIN_AREA_PARAMETER, context
)
# GRASS output type
self.outputType = self.parameterAsString(
parameters, self.GRASS_OUTPUT_TYPE_PARAMETER, context
)
# GRASS align to resolution
self.alignToResolution = self.parameterAsBoolean(
parameters, self.GRASS_REGION_ALIGN_TO_RESOLUTION, context
)
def processAlgorithm(self, original_parameters, context, feedback):
if isWindows():
path = GrassUtils.grassPath()
if path == "":
raise QgsProcessingException(
self.tr(
"GRASS GIS folder is not configured. Please "
"configure it before running GRASS GIS algorithms."
)
)
# make a copy of the original parameters dictionary - it gets modified by grass algorithms
parameters = {k: v for k, v in original_parameters.items()}
# Create brand new commands lists
self.commands = []
self.outputCommands = []
self.exportedLayers = {}
self.fileOutputs = {}
# If GRASS session has been created outside of this algorithm then
# get the list of layers loaded in GRASS otherwise start a new
# session
existingSession = GrassUtils.sessionRunning
if existingSession:
self.exportedLayers = GrassUtils.getSessionLayers()
else:
GrassUtils.startGrassSession()
# Handle default GRASS parameters
self.grabDefaultGrassParameters(parameters, context)
# Handle ext functions for inputs/command/outputs
for fName in ["Inputs", "Command", "Outputs"]:
fullName = f"process{fName}"
if self.module and hasattr(self.module, fullName):
getattr(self.module, fullName)(self, parameters, context, feedback)
else:
getattr(self, fullName)(parameters, context, feedback)
# Run GRASS
loglines = [self.tr("GRASS GIS execution commands")]
for line in self.commands:
feedback.pushCommandInfo(line)
loglines.append(line)
if ProcessingConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS):
QgsMessageLog.logMessage(
"\n".join(loglines), self.tr("Processing"), Qgis.MessageLevel.Info
)
GrassUtils.executeGrass(self.commands, feedback, self.outputCommands)
# If the session has been created outside of this algorithm, add
# the new GRASS GIS layers to it otherwise finish the session
if existingSession:
GrassUtils.addSessionLayers(self.exportedLayers)
else:
GrassUtils.endGrassSession()
# Return outputs map
outputs = {}
for out in self.outputDefinitions():
outName = out.name()
if outName in parameters:
if outName in self.fileOutputs:
outputs[outName] = self.fileOutputs[outName]
else:
outputs[outName] = parameters[outName]
if isinstance(out, QgsProcessingOutputHtml):
if self.module and hasattr(self.module, "convertToHtml"):
func = getattr(self.module, "convertToHtml")
func(self, self.fileOutputs[outName], outputs)
else:
self.convertToHtml(self.fileOutputs[outName])
return outputs
def processInputs(self, parameters, context, feedback):
"""Prepare the GRASS import commands"""
inputs = [
p
for p in self.parameterDefinitions()
if isinstance(
p,
(
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterMultipleLayers,
),
)
]
for param in inputs:
paramName = param.name()
if paramName not in parameters:
continue
# Handle Null parameter
if parameters[paramName] is None:
continue
elif (
isinstance(parameters[paramName], str)
and len(parameters[paramName]) == 0
):
continue
# Raster inputs needs to be imported into temp GRASS DB
if isinstance(param, QgsProcessingParameterRasterLayer):
if paramName not in self.exportedLayers:
self.loadRasterLayerFromParameter(paramName, parameters, context)
# Vector inputs needs to be imported into temp GRASS DB
elif isinstance(
param,
(
QgsProcessingParameterFeatureSource,
QgsProcessingParameterVectorLayer,
),
):
if paramName not in self.exportedLayers:
# Attribute tables are also vector inputs
if QgsProcessing.SourceType.TypeFile in param.dataTypes():
self.loadAttributeTableFromParameter(
paramName, parameters, context
)
else:
self.loadVectorLayerFromParameter(
paramName,
parameters,
context,
external=None,
feedback=feedback,
)
# For multiple inputs, process each layer
elif isinstance(param, QgsProcessingParameterMultipleLayers):
layers = self.parameterAsLayerList(parameters, paramName, context)
for idx, layer in enumerate(layers):
layerName = f"{paramName}_{idx}"
# Add a raster layer
if layer.type() == QgsMapLayerType.RasterLayer:
self.loadRasterLayer(layerName, layer, context)
# Add a vector layer
elif layer.type() == QgsMapLayerType.VectorLayer:
self.loadVectorLayer(
layerName, layer, context, external=None, feedback=feedback
)
self.postInputs(context)
def postInputs(self, context: QgsProcessingContext):
"""
After layer imports, we need to update some internal parameters
"""
# If projection has not already be set, use the project
self.setSessionProjectionFromProject(context)
# Build GRASS region
if self.region.isEmpty():
self.region = QgsProcessingUtils.combineLayerExtents(
self.inputLayers, self.destination_crs, context
)
command = "g.region n={} s={} e={} w={}".format(
self.region.yMaximum(),
self.region.yMinimum(),
self.region.xMaximum(),
self.region.xMinimum(),
)
# Handle cell size
if self.parameterDefinition(self.GRASS_REGION_CELLSIZE_PARAMETER):
if self.cellSize:
cellSize = self.cellSize
else:
cellSize = self.getDefaultCellSize()
command += f" res={cellSize}"
# Handle align to resolution
if self.alignToResolution:
command += " -a"
# Add the default parameters commands
self.commands.append(command)
QgsMessageLog.logMessage(
self.tr("processInputs end. Commands: {}").format(self.commands),
"Grass",
Qgis.MessageLevel.Info,
)
def processCommand(self, parameters, context, feedback, delOutputs=False):
"""
Prepare the GRASS algorithm command
:param parameters:
:param context:
:param delOutputs: do not add outputs to commands.
"""
noOutputs = [
o
for o in self.parameterDefinitions()
if o not in self.destinationParameterDefinitions()
]
command = f"{self.grass_name} "
command += "{}".join(self.hardcodedStrings)
# Add algorithm command
for param in noOutputs:
paramName = param.name()
value = None
# Exclude default GRASS parameters
if paramName in [
self.GRASS_REGION_CELLSIZE_PARAMETER,
self.GRASS_REGION_EXTENT_PARAMETER,
self.GRASS_MIN_AREA_PARAMETER,
self.GRASS_SNAP_TOLERANCE_PARAMETER,
self.GRASS_OUTPUT_TYPE_PARAMETER,
self.GRASS_REGION_ALIGN_TO_RESOLUTION,
self.GRASS_RASTER_FORMAT_OPT,
self.GRASS_RASTER_FORMAT_META,
self.GRASS_VECTOR_DSCO,
self.GRASS_VECTOR_LCO,
self.GRASS_VECTOR_EXPORT_NOCAT,
]:
continue
# Raster and vector layers
if isinstance(
param,
(
QgsProcessingParameterRasterLayer,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterFeatureSource,
),
):
if paramName in self.exportedLayers:
value = self.exportedLayers[paramName]
else:
value = self.parameterAsCompatibleSourceLayerPath(
parameters,
paramName,
context,
QgsVectorFileWriter.supportedFormatExtensions(),
)
# MultipleLayers
elif isinstance(param, QgsProcessingParameterMultipleLayers):
layers = self.parameterAsLayerList(parameters, paramName, context)
values = []
for idx in range(len(layers)):
layerName = f"{paramName}_{idx}"
values.append(self.exportedLayers[layerName])
value = ",".join(values)
# For booleans, we just add the parameter name
elif isinstance(param, QgsProcessingParameterBoolean):
if self.parameterAsBoolean(parameters, paramName, context):
command += f" {paramName}"
# For Extents, remove if the value is null
elif isinstance(param, QgsProcessingParameterExtent):
if self.parameterAsExtent(parameters, paramName, context):
value = self.parameterAsString(parameters, paramName, context)
# For enumeration, we need to grab the string value
elif isinstance(param, QgsProcessingParameterEnum):
# Handle multiple values
if param.allowMultiple():
indexes = self.parameterAsEnums(parameters, paramName, context)
else:
indexes = [self.parameterAsEnum(parameters, paramName, context)]
if indexes:
value = '"{}"'.format(
",".join([param.options()[i] for i in indexes])
)
# For strings, we just translate as string
elif isinstance(param, QgsProcessingParameterString):
data = self.parameterAsString(parameters, paramName, context)
# if string is empty, we don't add it
if len(data) > 0:
value = '"{}"'.format(
self.parameterAsString(parameters, paramName, context)
)
# For fields, we just translate as string
elif isinstance(param, QgsProcessingParameterField):
value = ",".join(self.parameterAsFields(parameters, paramName, context))
elif isinstance(param, QgsProcessingParameterFile):
if self.parameterAsString(parameters, paramName, context):
value = '"{}"'.format(
self.parameterAsString(parameters, paramName, context)
)
elif isinstance(param, QgsProcessingParameterPoint):
if self.parameterAsString(parameters, paramName, context):
# parameter specified, evaluate as point
# TODO - handle CRS transform
point = self.parameterAsPoint(parameters, paramName, context)
value = f"{point.x()},{point.y()}"
# For numbers, we translate as a string
elif isinstance(
param, (QgsProcessingParameterNumber, QgsProcessingParameterPoint)
):
value = self.parameterAsString(parameters, paramName, context)
elif isinstance(param, QgsProcessingParameterRange):
v = self.parameterAsRange(parameters, paramName, context)
if (
param.flags() & QgsProcessingParameterDefinition.Flag.FlagOptional
) and (math.isnan(v[0]) or math.isnan(v[1])):
continue
else:
value = f"{v[0]},{v[1]}"
elif isinstance(param, QgsProcessingParameterCrs):
if self.parameterAsCrs(parameters, paramName, context):
# TODO: ideally we should be exporting to WKT here, but it seems not all grass algorithms
# will accept a wkt string for a crs value (e.g. r.tileset)
value = f'"{self.parameterAsCrs(parameters, paramName, context).toProj()}"'
# For everything else, we assume that it is a string
else:
value = '"{}"'.format(
self.parameterAsString(parameters, paramName, context)
)
if value:
command += " {}={}".format(paramName.replace("~", ""), value)
# Handle outputs
if not delOutputs:
for out in self.destinationParameterDefinitions():
# We exclude hidden parameters
if out.flags() & QgsProcessingParameterDefinition.Flag.FlagHidden:
continue
outName = out.name()
# For File destination
if isinstance(out, QgsProcessingParameterFileDestination):
if outName in parameters and parameters[outName] is not None:
outPath = self.parameterAsFileOutput(
parameters, outName, context
)
self.fileOutputs[outName] = outPath
# for HTML reports, we need to redirect stdout
if out.defaultFileExtension().lower() == "html":
if outName == "html":
# for "fake" outputs redirect command stdout
command += f' > "{outPath}"'
else:
# for real outputs only output itself should be redirected
command += f' {outName}=- > "{outPath}"'
else:
command += f' {outName}="{outPath}"'
# For folders destination
elif isinstance(out, QgsProcessingParameterFolderDestination):
# We need to add a unique temporary basename
uniqueBasename = outName + self.uniqueSuffix
command += f" {outName}={uniqueBasename}"
else:
if outName in parameters and parameters[outName] is not None:
# We add an output name to make sure it is unique if the session
# uses this algorithm several times.
uniqueOutputName = outName + self.uniqueSuffix
command += f" {outName}={uniqueOutputName}"
# Add output file to exported layers, to indicate that
# they are present in GRASS
self.exportedLayers[outName] = uniqueOutputName
command += " --overwrite"
self.commands.append(command)
QgsMessageLog.logMessage(
self.tr("processCommands end. Commands: {}").format(self.commands),
"Grass",
Qgis.MessageLevel.Info,
)
def vectorOutputType(self, parameters, context):
"""Determine vector output types for outputs"""
self.outType = "auto"
if self.parameterDefinition(self.GRASS_OUTPUT_TYPE_PARAMETER):
typeidx = self.parameterAsEnum(
parameters, self.GRASS_OUTPUT_TYPE_PARAMETER, context
)
self.outType = "auto" if typeidx is None else self.OUTPUT_TYPES[typeidx]
def processOutputs(self, parameters, context, feedback):
"""Prepare the GRASS v.out.ogr commands"""
# Determine general vector output type
self.vectorOutputType(parameters, context)
for out in self.destinationParameterDefinitions():
outName = out.name()
if outName not in parameters:
# skipped output
continue
if isinstance(out, QgsProcessingParameterRasterDestination):
self.exportRasterLayerFromParameter(outName, parameters, context)
elif isinstance(out, QgsProcessingParameterVectorDestination):
self.exportVectorLayerFromParameter(outName, parameters, context)
elif isinstance(out, QgsProcessingParameterFolderDestination):
self.exportRasterLayersIntoDirectory(outName, parameters, context)
def loadRasterLayerFromParameter(
self, name, parameters, context: QgsProcessingContext, external=None, band=1
):
"""
Creates a dedicated command to load a raster into
the temporary GRASS DB.
:param name: name of the parameter.
:param parameters: algorithm parameters dict.
:param context: algorithm context.
:param external: use r.external if True, r.in.gdal otherwise.
:param band: imports only specified band. None for all bands.
"""
layer = self.parameterAsRasterLayer(parameters, name, context)
self.loadRasterLayer(name, layer, context, external, band)
def loadRasterLayer(
self,
name,
layer,
context: QgsProcessingContext,
external=None,
band=1,
destName=None,
):
"""
Creates a dedicated command to load a raster into
the temporary GRASS DB.
:param name: name of the parameter.
:param layer: QgsMapLayer for the raster layer.
:param context: Processing context
:param external: use r.external if True, r.in.gdal if False.
:param band: imports only specified band. None for all bands.
:param destName: force the destination name of the raster.
"""
if external is None:
external = ProcessingConfig.getSetting(GrassUtils.GRASS_USE_REXTERNAL)
self.inputLayers.append(layer)
self.setSessionProjectionFromLayer(layer, context)
if not destName:
destName = f"rast_{os.path.basename(getTempFilename(context=context))}"
self.exportedLayers[name] = destName
command = '{} input="{}" {}output="{}" --overwrite -o'.format(
"r.external" if external else "r.in.gdal",
os.path.normpath(layer.source()),
f"band={band} " if band else "",
destName,
)
self.commands.append(command)
def exportRasterLayerFromParameter(
self, name, parameters, context, colorTable=True
):
"""
Creates a dedicated command to export a raster from
temporary GRASS DB into a file via gdal.
:param name: name of the parameter.
:param parameters: Algorithm parameters dict.
:param context: Algorithm context.
:param colorTable: preserve color Table.
"""
fileName = self.parameterAsOutputLayer(parameters, name, context)
if not fileName:
return
fileName = os.path.normpath(fileName)
grassName = f"{name}{self.uniqueSuffix}"
outFormat = GrassUtils.getRasterFormatFromFilename(fileName)
createOpt = self.parameterAsString(
parameters, self.GRASS_RASTER_FORMAT_OPT, context
)
metaOpt = self.parameterAsString(
parameters, self.GRASS_RASTER_FORMAT_META, context
)
self.exportRasterLayer(
grassName, fileName, colorTable, outFormat, createOpt, metaOpt
)
self.fileOutputs[name] = fileName
def exportRasterLayer(
self,
grassName,
fileName,
colorTable=True,
outFormat="GTiff",
createOpt=None,
metaOpt=None,
):
"""
Creates a dedicated command to export a raster from
temporary GRASS DB into a file via gdal.
:param grassName: name of the raster to export.
:param fileName: file path of raster layer.
:param colorTable: preserve color Table.
:param outFormat: file format for export.
:param createOpt: creation options for format.
:param metaOpt: metadata options for export.
"""
createOpt = createOpt or GrassUtils.GRASS_RASTER_FORMATS_CREATEOPTS.get(
outFormat
)
for cmd in [self.commands, self.outputCommands]:
# Adjust region to layer before exporting
cmd.append(f"g.region raster={grassName}")
cmd.append(
'r.out.gdal -t -m{} input="{}" output="{}" format="{}" {}{} --overwrite'.format(
"" if colorTable else " -c",
grassName,
fileName,
outFormat,
f' createopt="{createOpt}"' if createOpt else "",
f' metaopt="{metaOpt}"' if metaOpt else "",
)
)
def exportRasterLayersIntoDirectory(
self, name, parameters, context, colorTable=True, wholeDB=False
):
"""
Creates a dedicated loop command to export rasters from
temporary GRASS DB into a directory via gdal.
:param name: name of the output directory parameter.
:param parameters: Algorithm parameters dict.
:param context: Algorithm context.
:param colorTable: preserve color Table.
:param wholeDB: export every raster layer from the GRASSDB
"""
# Grab directory name and temporary basename
outDir = os.path.normpath(self.parameterAsString(parameters, name, context))
basename = ""
if not wholeDB:
basename = name + self.uniqueSuffix
# Add a loop export from the basename
for cmd in [self.commands, self.outputCommands]:
# TODO Format/options support
if isWindows():
cmd.append("if not exist {0} mkdir {0}".format(outDir))
cmd.append(
"for /F %%r IN ('g.list type^=rast pattern^=\"{}*\"') do r.out.gdal -m{} input=%%r output={}/%%r.tif {}".format(
basename,
" -t" if colorTable else "",
outDir,
'--overwrite -c createopt="TFW=YES,COMPRESS=LZW"',
)
)
else:
cmd.append(f"for r in $(g.list type=rast pattern='{basename}*'); do")
cmd.append(
" r.out.gdal -m{0} input=${{r}} output={1}/${{r}}.tif {2}".format(
" -t" if colorTable else "",
outDir,
'--overwrite -c createopt="TFW=YES,COMPRESS=LZW"',
)
)
cmd.append("done")
def loadVectorLayerFromParameter(
self, name, parameters, context, feedback, external=False
):
"""
Creates a dedicated command to load a vector into
the temporary GRASS DB.
:param name: name of the parameter
:param parameters: Parameters of the algorithm.
:param context: Processing context
:param external: use v.external (v.in.ogr if False).
"""
layer = self.parameterAsVectorLayer(parameters, name, context)
is_ogr_disk_based_layer = (
layer is not None and layer.dataProvider().name() == "ogr"
)
if is_ogr_disk_based_layer:
# we only support direct reading of disk based ogr layers -- not ogr postgres layers, etc
source_parts = QgsProviderRegistry.instance().decodeUri(
"ogr", layer.source()
)
if not source_parts.get("path"):
is_ogr_disk_based_layer = False
elif source_parts.get("layerId"):
# no support for directly reading layers by id in grass
is_ogr_disk_based_layer = False
if not is_ogr_disk_based_layer:
# parameter is not a vector layer or not an OGR layer - try to convert to a source compatible with
# grass OGR inputs and extract selection if required
path = self.parameterAsCompatibleSourceLayerPath(
parameters,
name,
context,
QgsVectorFileWriter.supportedFormatExtensions(),
feedback=feedback,
)
ogr_layer = QgsVectorLayer(path, "", "ogr")
self.loadVectorLayer(
name, ogr_layer, context, external=external, feedback=feedback
)
else:
# already an ogr disk based layer source
self.loadVectorLayer(
name, layer, context, external=external, feedback=feedback
)
def loadVectorLayer(
self, name, layer, context: QgsProcessingContext, external=False, feedback=None
):
"""
Creates a dedicated command to load a vector into
temporary GRASS DB.
:param name: name of the parameter
:param layer: QgsMapLayer for the vector layer.
:param context: Processing context
:param external: use v.external (v.in.ogr if False).
:param feedback: feedback object
"""
# TODO: support multiple input formats
if external is None:
external = ProcessingConfig.getSetting(GrassUtils.GRASS_USE_VEXTERNAL)
source_parts = QgsProviderRegistry.instance().decodeUri("ogr", layer.source())
file_path = source_parts.get("path")
layer_name = source_parts.get("layerName")
# safety check: we can only use external for ogr layers which support random read
if external:
if feedback is not None:
feedback.pushInfo(
self.tr("Attempting to use v.external for direct layer read")
)
ds = ogr.Open(file_path)
if ds is not None:
ogr_layer = ds.GetLayer()
if ogr_layer is None or not ogr_layer.TestCapability(ogr.OLCRandomRead):
if feedback is not None:
feedback.reportError(
self.tr(
"Cannot use v.external: layer does not support random read"
)
)
external = False
else:
if feedback is not None:
feedback.reportError(
self.tr("Cannot use v.external: error reading layer")
)
external = False
self.inputLayers.append(layer)
self.setSessionProjectionFromLayer(layer, context)
destFilename = f"vector_{os.path.basename(getTempFilename(context=context))}"
self.exportedLayers[name] = destFilename
command = '{}{}{} input="{}"{} output="{}" --overwrite -o'.format(
"v.external" if external else "v.in.ogr",
f" min_area={self.minArea}" if not external else "",
f" snap={self.snapTolerance}" if not external else "",
os.path.normpath(file_path),
f' layer="{layer_name}"' if layer_name else "",
destFilename,
)
if layer.subsetString():
escaped_subset = layer.subsetString().replace('"', '\\"')
command += f' where="{escaped_subset}"'
self.commands.append(command)
def exportVectorLayerFromParameter(
self, name, parameters, context, layer=None, nocats=False
):
"""
Creates a dedicated command to export a vector from
a QgsProcessingParameter.
:param name: name of the parameter.
:param context: parameters context.
:param layer: for vector with multiples layers, exports only one layer.
:param nocats: do not export GRASS categories.
"""
fileName = os.path.normpath(
self.parameterAsOutputLayer(parameters, name, context)
)
grassName = f"{name}{self.uniqueSuffix}"
# Find if there is a dataType
dataType = self.outType
if self.outType == "auto":
parameter = self.parameterDefinition(name)
if parameter:
layerType = parameter.dataType()
if layerType in self.QGIS_OUTPUT_TYPES:
dataType = self.QGIS_OUTPUT_TYPES[layerType]
outFormat = QgsVectorFileWriter.driverForExtension(
os.path.splitext(fileName)[1]
).replace(" ", "_")
dsco = self.parameterAsString(parameters, self.GRASS_VECTOR_DSCO, context)
lco = self.parameterAsString(parameters, self.GRASS_VECTOR_LCO, context)
exportnocat = self.parameterAsBoolean(
parameters, self.GRASS_VECTOR_EXPORT_NOCAT, context
)
self.exportVectorLayer(
grassName,
fileName,
layer,
nocats,
dataType,
outFormat,
dsco,
lco,
exportnocat,
)
self.fileOutputs[name] = fileName
def exportVectorLayer(
self,
grassName,
fileName,
layer=None,
nocats=False,
dataType="auto",
outFormat=None,
dsco=None,
lco=None,
exportnocat=False,
):
"""
Creates a dedicated command to export a vector from
temporary GRASS DB into a file via OGR.
:param grassName: name of the vector to export.
:param fileName: file path of vector layer.
:param dataType: export only this type of data.
:param layer: for vector with multiples layers, exports only one layer.
:param nocats: do not export GRASS categories.
:param outFormat: file format for export.
:param dsco: datasource creation options for format.
:param lco: layer creation options for format.
:param exportnocat: do not export features without categories.
"""
if outFormat is None:
outFormat = QgsVectorFileWriter.driverForExtension(
os.path.splitext(fileName)[1]
).replace(" ", "_")
for cmd in [self.commands, self.outputCommands]:
cmd.append(
'v.out.ogr{} type="{}" input="{}" output="{}" format="{}" {}{}{}{} --overwrite'.format(
"" if nocats else "",
dataType,
grassName,
fileName,
outFormat,
f"layer={layer}" if layer else "",
f' dsco="{dsco}"' if dsco else "",
f' lco="{lco}"' if lco else "",
" -c" if exportnocat else "",
)
)
def loadAttributeTableFromParameter(self, name, parameters, context):
"""
Creates a dedicated command to load an attribute table
into the temporary GRASS DB.
:param name: name of the parameter
:param parameters: Parameters of the algorithm.
:param context: Processing context
"""
table = self.parameterAsVectorLayer(parameters, name, context)
self.loadAttributeTable(name, table, context)
def loadAttributeTable(
self, name, layer, context: QgsProcessingContext, destName=None
):
"""
Creates a dedicated command to load an attribute table
into the temporary GRASS DB.
:param name: name of the input parameter.
:param layer: a layer object to import from.
:param context: processing context
:param destName: force the name for the table into GRASS DB.
"""
self.inputLayers.append(layer)
if not destName:
destName = f"table_{os.path.basename(getTempFilename(context=context))}"
self.exportedLayers[name] = destName
command = 'db.in.ogr --overwrite input="{}" output="{}"'.format(
os.path.normpath(layer.source()), destName
)
self.commands.append(command)
def exportAttributeTable(self, grassName, fileName, outFormat="CSV", layer=1):
"""
Creates a dedicated command to export an attribute
table from the temporary GRASS DB into a file via ogr.
:param grassName: name of the parameter.
:param fileName: file path of raster layer.
:param outFormat: file format for export.
:param layer: In GRASS a vector can have multiple layers.
"""
for cmd in [self.commands, self.outputCommands]:
cmd.append(
'db.out.ogr input="{}" output="{}" layer={} format={} --overwrite'.format(
grassName, fileName, layer, outFormat
)
)
def setSessionProjectionFromProject(self, context: QgsProcessingContext):
"""
Set the projection from the project.
We create a WKT definition which is transmitted to Grass
"""
if not GrassUtils.projectionSet and iface:
self.setSessionProjection(
iface.mapCanvas().mapSettings().destinationCrs(), context
)
def setSessionProjectionFromLayer(
self, layer: QgsMapLayer, context: QgsProcessingContext
):
"""
Set the projection from a QgsVectorLayer.
We create a WKT definition which is transmitted to Grass
"""
if not GrassUtils.projectionSet:
self.setSessionProjection(layer.crs(), context)
def setSessionProjection(self, crs, context: QgsProcessingContext):
"""
Set the session projection to the specified CRS
"""
self.destination_crs = crs
file_name = GrassUtils.exportCrsWktToFile(crs, context)
command = f'g.proj -c wkt="{file_name}"'
self.commands.append(command)
GrassUtils.projectionSet = True
def convertToHtml(self, fileName):
# Read HTML contents
lines = []
with open(fileName, encoding="utf-8") as f:
lines = f.readlines()
if len(lines) > 1 and "<html>" not in lines[0]:
# Then write into the HTML file
with open(fileName, "w", encoding="utf-8") as f:
f.write("<html><head>")
f.write(
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>'
)
f.write("<body><p>")
for line in lines:
f.write(f"{line}</br>")
f.write("</p></body></html>")
def canExecute(self):
message = GrassUtils.checkGrassIsInstalled()
return not message, message
def checkParameterValues(self, parameters, context):
grass_parameters = {k: v for k, v in parameters.items()}
if self.module:
if hasattr(self.module, "checkParameterValuesBeforeExecuting"):
func = getattr(self.module, "checkParameterValuesBeforeExecuting")
return func(self, grass_parameters, context)
return super().checkParameterValues(grass_parameters, context)
|