I have a raster representing a street network, a raster with values on that street network (sum_raster), and a vector layer with a number of polygons. Those polygons get iterated over and rasterizet with a certain value (leisure_raster) which then is to be added to the sum_raster.
My problem is, that after every summation only that area still contains values which intersects with all (rasterized) polygons. That means the area gets smaller and smaller as can be seen on the appended screenshot, but it should remain the same as the input sum_raster. Only the values on the streets should become greater where added.
I tried several formulas to accomplish this, but I suspect that it's a null value problem.
QGIS 3.34.11
All rasters have the same extent (only the not-null area gets smaller), CRS, etc.
Image may be NSFW.
Clik here to view.
for feature_id in feature_ids: expression = f'"fid" = {int(feature_id)} ' extract_params = {'INPUT': results["leisure_polygon_buffer"],'EXPRESSION': expression,'OUTPUT': f'memory:single_feature{feature_id}' } single_feature_output = processing.run('native:extractbyexpression', extract_params, context=context, feedback=feedback, is_child_algorithm=True) single_feature_layer = QgsProcessingUtils.mapLayerFromString(single_feature_output['OUTPUT'], context) if single_feature_layer is None or not single_feature_layer.isValid(): feedback.reportError(f"Failed to extract feature with ID {feature_id}.") continue else: feedback.pushInfo("extracted feature with ID {feature_id} succsessfully") QgsProject.instance().addMapLayer(single_feature_layer) # Rasterize the single feature feedback.pushInfo(f"Rasterizing feature ID {feature_id}...") params = {'input': single_feature_layer,'type': [0, 1, 3],'use': 2, # Use attribute value (or constant if specified)'value': 75, # Assign constant value to raster cells'memory': None,'output': 'TEMPORARY_OUTPUT','GRASS_REGION_CELLSIZE_PARAMETER': cellsize,'GRASS_SNAP_TOLERANCE_PARAMETER': 10,'GRASS_REGION_PARAMETER': extent,'GRASS_MIN_AREA_PARAMETER': 0.0001,'NULL': 0 } leisure_polygon_raster_output = processing.run('grass7:v.to.rast', params) if 'output' in leisure_polygon_raster_output: feedback.pushInfo("output for leisure_polygon_raster_output exists") leisure_raster = leisure_polygon_raster_output['output'] # leisure_raster_layer = QgsRasterLayer(leisure_raster, "leisure_raster_layer") # QgsProject.instance().addMapLayer(leisure_raster_layer) calc_params = {'INPUT_A': leisure_raster,'BAND_A': 1,'INPUT_B': highway_raster,'BAND_B': 1,'FORMULA': '(B != 0) * A','RTYPE': 1,'OUTPUT': 'TEMPORARY_OUTPUT' } calc_raster_output = processing.run('gdal:rastercalculator', calc_params) leisure_raster = calc_raster_output['OUTPUT'] # Verify if the raster output is valid leisure_raster_layer = QgsRasterLayer(leisure_raster, "leisure_raster_layer") if leisure_raster_layer.isValid(): QgsProject.instance().addMapLayer(leisure_raster_layer) feedback.pushInfo("Raster layer added successfully after replacing 255 with 0.") else: feedback.reportError("Failed to create raster layer after replacing 255 with 0.") # SUMMING UP RASTERS ---------- feedback.pushInfo("Summing up rasters...") if sum_lei_raster is None: # Initialize the sum raster with the first raster input = sum_raster feedback.pushInfo("using sum_raster as input") QgsProject.instance().addMapLayer(sum_raster) else: input = sum_lei_raster_layer feedback.pushInfo("using sum_raster + buffer rasters ans input for summation") # Use raster calculator to sum rasters formula = 'A + B' # formula = f'((C > 0) * (A + ((B >= 0) * B)))' # formula = f'((C > 0) * (A + B) + ((C <= 0) * A))' # formula = f'((C > 0) * (A + B)) + ((C <= 0) * A)' # formula = f'((C > 0) * ((A > 0) * (B > 0) * (A + B) + (A <= 0) * (B > 0) * B + (A > 0) * (B <= 0) * A))' params = {'FORMULA': formula,'INPUT_A': input,'BAND_A': 1,'INPUT_B': leisure_raster_layer,'BAND_B': 1,'INPUT_C': highway_raster_layer,'BAND_C': 1,'RTYPE': 1,'OUTPUT': 'TEMPORARY_OUTPUT' } sum_lei_result = processing.run('gdal:rastercalculator', params) sum_lei_raster = sum_lei_result['OUTPUT'] sum_lei_raster_layer = QgsRasterLayer(sum_lei_raster, "sum_lei_raster_layer") QgsProject.instance().addMapLayer(sum_lei_raster_layer)