def getConsoleCommands(self, parameters, context, feedback, executing=True): context = dataobjects.createContext() inLayer = QgsProcessingUtils.mapLayerFromString( self.getParameterValue(self.INPUT), context) inRasterLayer = QgsProcessingUtils.mapLayerFromString( self.getParameterValue(self.INPUT_RASTER), context) ogrLayer = GdalUtils.ogrConnectionString(inLayer, context)[1:-1] ogrRasterLayer = GdalUtils.ogrConnectionString(inRasterLayer, context)[1:-1] arguments = [] arguments.append('-a') arguments.append(str(self.getParameterValue(self.FIELD))) arguments.append('-l') arguments.append(GdalUtils.ogrLayerName(inLayer)) arguments.append(ogrLayer) arguments.append(ogrRasterLayer) return ['gdal_rasterize', GdalUtils.escapeAndJoin(arguments)]
def getOgrCompatibleSource(self, parameter_name, parameters, context, feedback, executing): """ Interprets a parameter as an OGR compatible source and layer name :param executing: """ input_layer = self.parameterAsVectorLayer(parameters, parameter_name, context) ogr_data_path = None ogr_layer_name = None if input_layer is None or input_layer.dataProvider().name( ) == 'memory': if executing: # parameter is not a vector layer - try to convert to a source compatible with OGR # and extract selection if required ogr_data_path = self.parameterAsCompatibleSourceLayerPath( parameters, parameter_name, context, QgsVectorFileWriter.supportedFormatExtensions(), feedback=feedback) ogr_layer_name = GdalUtils.ogrLayerName(ogr_data_path) else: #not executing - don't waste time converting incompatible sources, just return dummy strings #for the command preview (since the source isn't compatible with OGR, it has no meaning anyway and can't #be run directly in the command line) ogr_data_path = 'path_to_data_file' ogr_layer_name = 'layer_name' elif input_layer.dataProvider().name() == 'ogr': if executing: # parameter is a vector layer, with OGR data provider # so extract selection if required ogr_data_path = self.parameterAsCompatibleSourceLayerPath( parameters, parameter_name, context, QgsVectorFileWriter.supportedFormatExtensions(), feedback=feedback) ogr_layer_name = GdalUtils.ogrLayerName( input_layer.dataProvider().dataSourceUri()) else: #not executing - don't worry about 'selected features only' handling. It has no meaning #for the command line preview since it has no meaning outside of a QGIS session! ogr_data_path = GdalUtils.ogrConnectionString( input_layer.dataProvider().dataSourceUri(), context)[1:-1] ogr_layer_name = GdalUtils.ogrLayerName( input_layer.dataProvider().dataSourceUri()) else: # vector layer, but not OGR - get OGR compatible path # TODO - handle "selected features only" mode!! ogr_data_path = GdalUtils.ogrConnectionString( input_layer.dataProvider().dataSourceUri(), context)[1:-1] ogr_layer_name = GdalUtils.ogrLayerName( input_layer.dataProvider().dataSourceUri()) return ogr_data_path, ogr_layer_name
def getConsoleCommands(self, parameters, context, feedback, executing=True): arguments = [self.commandName(), '-al'] if self.parameterAsBool(parameters, self.SUMMARY_ONLY, context): arguments.append('-so') if self.parameterAsBool(parameters, self.NO_METADATA, context): arguments.append('-nomd') inLayer = self.parameterAsVectorLayer(parameters, self.INPUT, context) connectionString = GdalUtils.ogrConnectionString( inLayer.source(), context) arguments.append(connectionString) return arguments
def getConsoleCommands(self, parameters, context, feedback, executing=True): connection = self.DB_CONNECTIONS[self.getParameterValue(self.DATABASE)] uri = uri_from_name(connection) if executing: # to get credentials input when needed uri = GeoDB(uri=uri).uri inLayer = self.getParameterValue(self.INPUT_LAYER) ogrLayer = GdalUtils.ogrConnectionString(inLayer, context)[1:-1] shapeEncoding = self.getParameterValue(self.SHAPE_ENCODING) schema = str(self.getParameterValue(self.SCHEMA)) table = str(self.getParameterValue(self.TABLE)) pk = str(self.getParameterValue(self.PK)) pkstring = "-lco FID=" + pk primary_key = self.getParameterValue(self.PRIMARY_KEY) where = str(self.getParameterValue(self.WHERE)) wherestring = '-where "' + where + '"' gt = str(self.getParameterValue(self.GT)) overwrite = self.getParameterValue(self.OVERWRITE) append = self.getParameterValue(self.APPEND) addfields = self.getParameterValue(self.ADDFIELDS) launder = self.getParameterValue(self.LAUNDER) launderstring = "-lco LAUNDER=NO" skipfailures = self.getParameterValue(self.SKIPFAILURES) precision = self.getParameterValue(self.PRECISION) options = str(self.getParameterValue(self.OPTIONS)) arguments = [] arguments.append('-progress') arguments.append('--config PG_USE_COPY YES') if len(shapeEncoding) > 0: arguments.append('--config') arguments.append('SHAPE_ENCODING') arguments.append('"' + shapeEncoding + '"') arguments.append('-f') arguments.append('PostgreSQL') arguments.append('PG:"') for token in uri.connectionInfo(executing).split(' '): arguments.append(token) arguments.append('active_schema={}'.format(schema or 'public')) arguments.append('"') arguments.append(ogrLayer) arguments.append('-nlt NONE') arguments.append(GdalUtils.ogrLayerName(inLayer)) if launder: arguments.append(launderstring) if append: arguments.append('-append') if addfields: arguments.append('-addfields') if overwrite: arguments.append('-overwrite') if len(pk) > 0: arguments.append(pkstring) elif primary_key is not None: arguments.append("-lco FID=" + primary_key) if len(table) == 0: table = GdalUtils.ogrLayerName(inLayer).lower() if schema: table = '{}.{}'.format(schema, table) arguments.append('-nln') arguments.append(table) if skipfailures: arguments.append('-skipfailures') if where: arguments.append(wherestring) if len(gt) > 0: arguments.append('-gt') arguments.append(gt) if not precision: arguments.append('-lco PRECISION=NO') if len(options) > 0: arguments.append(options) commands = [] if isWindows(): commands = ['cmd.exe', '/C ', 'ogr2ogr.exe', GdalUtils.escapeAndJoin(arguments)] else: commands = ['ogr2ogr', GdalUtils.escapeAndJoin(arguments)] return commands