Certainly! To modify the script so that it can cut your shapefile polylines with both points and polygons, we can add an additional function to handle the polygon-based cutting. This function will be similar to the existing one but will target polygon layers instead of point layers. We'll integrate both functionalities into the script. Here's the updated script: ```python from qgis.core import ( QgsVectorLayer, QgsProject, QgsGeometry, QgsFeature, QgsProcessingFeedback, QgsCoordinateReferenceSystem, QgsWkbTypes ) from qgis.processing import run as processing_run def get_uncovered_lines(buffer_distance=15): """ Extract polyline segments that are NOT covered by point buffers """ # Get the root of the project root = QgsProject.instance().layerTreeRoot() # Find group2 points_group = root.findGroup("group2") if not points_group: print("group2 not found") return # Get the Nikaia polyline layer nikaia_layer = QgsProject.instance().mapLayersByName("Nikaia Rentis Backpack 15254 — nikaia_rentis_backpack_15254")[0] if not nikaia_layer: print("Nikaia layer not found") return print("Starting process...") # Fix the input layer geometries print("Fixing input layer geometries...") fix_params = { 'INPUT': nikaia_layer, 'OUTPUT': 'TEMPORARY_OUTPUT' # Changed to TEMPORARY_OUTPUT } fixed_nikaia = processing_run("native:fixgeometries", fix_params)['OUTPUT'] print("Geometry fixing completed") # Merge all POINT layers print("Merging point layers...") layers_to_merge = [] for layer in points_group.children(): if isinstance(layer.layer(), QgsVectorLayer): if layer.layer().geometryType() == QgsWkbTypes.PointGeometry: layers_to_merge.append(layer.layer()) if not layers_to_merge: print("No point layers found!") return merge_params = { 'LAYERS': layers_to_merge, 'CRS': 'EPSG:32634', 'OUTPUT': 'TEMPORARY_OUTPUT' # Changed to TEMPORARY_OUTPUT } merged_points = processing_run("native:mergevectorlayers", merge_params)['OUTPUT'] print(f"Total points merged: {merged_points.featureCount()}") try: # Create buffer around points print(f"Creating buffer with {buffer_distance} meters around points...") buffer_params = { 'INPUT': merged_points, 'DISTANCE': buffer_distance, 'SEGMENTS': 16, 'END_CAP_STYLE': 0, 'JOIN_STYLE': 0, 'MITER_LIMIT': 2, 'DISSOLVE': True, 'OUTPUT': 'TEMPORARY_OUTPUT' # Changed to TEMPORARY_OUTPUT } buffer_result = processing_run("native:buffer", buffer_params) points_buffer_layer = buffer_result['OUTPUT'] # Add buffer to project with specific name points_buffer_layer.setName("Points_Buffer") QgsProject.instance().addMapLayer(points_buffer_layer) print("Creating difference layer for points buffer...") # Use difference to get uncovered parts difference_params = { 'INPUT': fixed_nikaia, 'OVERLAY': points_buffer_layer, 'GRID_SIZE': None, 'IGNORE_INVALID_INPUT_FEATURES': True, 'OUTPUT': 'TEMPORARY_OUTPUT' # Changed to TEMPORARY_OUTPUT } difference_result = processing_run("native:difference", difference_params) if difference_result and 'OUTPUT' in difference_result: uncovered_by_points_layer = difference_result['OUTPUT'] if uncovered_by_points_layer and uncovered_by_points_layer.isValid(): uncovered_by_points_layer.setName("Nikaia_Uncovered_Lines_By_Points") QgsProject.instance().addMapLayer(uncovered_by_points_layer) print("Successfully created uncovered lines layer by points!") else: print("Failed to create valid uncovered lines layer by points") else: print("Difference operation failed for points buffer") except Exception as e: print(f"Error occurred: {str(e)}") print("Trying alternative method for points buffer...") try: # Alternative method using extract by location extract_params = { 'INPUT': fixed_nikaia, 'PREDICATE': [0], # Disjoint 'INTERSECT': points_buffer_layer, 'OUTPUT': 'TEMPORARY_OUTPUT' } extract_result = processing_run("native:extractbylocation", extract_params) if extract_result and 'OUTPUT' in extract_result: alt_layer = extract_result['OUTPUT'] if alt_layer and alt_layer.isValid(): alt_layer.setName("Nikaia_Uncovered_Lines_By_Points_Alt") QgsProject.instance().addMapLayer(alt_layer) print("Successfully created alternative uncovered lines layer by points!") else: print("Failed to create valid alternative layer by points") else: print("Alternative method failed for points buffer") except Exception as e2: print(f"Alternative method also failed for points buffer: {str(e2)}") def get_uncovered_lines_by_polygons(buffer_distance=15): """ Extract polyline segments that are NOT covered by polygon buffers """ # Get the root of the project root = QgsProject.instance().layerTreeRoot() # Find group2 polygons_group = root.findGroup("group2") if not polygons_group: print("group2 not found") return # Get the Nikaia polyline layer nikaia_layer = QgsProject.instance().mapLayersByName("Nikaia Rentis Backpack 15254 — nikaia_rentis_backpack_15254")[0] if not nikaia_layer: print("Nikaia layer not found") return print("Starting process for polygons...") # Fix the input layer geometries print("Fixing input layer geometries...") fix_params = { 'INPUT': nikaia_layer, 'OUTPUT': 'TEMPORARY_OUTPUT' # Changed to TEMPORARY_OUTPUT } fixed_nikaia = processing_run("native:fixgeometries", fix_params)['OUTPUT'] print("Geometry fixing completed") # Merge all POLYGON layers print("Merging polygon layers...") layers_to_merge = [] for layer in polygons_group.children(): if isinstance(layer.layer(), QgsVectorLayer): if layer.layer().geometryType() == QgsWkbTypes.PolygonGeometry: layers_to_merge.append(layer.layer()) if not layers_to_merge: print("No polygon layers found!") return merge_params = { 'LAYERS': layers_to_merge, 'CRS': 'EPSG:32634', 'OUTPUT': 'TEMPORARY_OUTPUT' # Changed to TEMPORARY_OUTPUT } merged_polygons = processing_run("native:mergevectorlayers", merge_params)['OUTPUT'] print(f"Total polygons merged: {merged_polygons.featureCount()}") try: # Create buffer around polygons print(f"Creating buffer with {buffer_distance} meters around polygons...") buffer_params = { 'INPUT': merged_polygons, 'DISTANCE': buffer_distance, 'SEGMENTS': 16, 'END_CAP_STYLE': 0, 'JOIN_STYLE': 0, 'MITER_LIMIT': 2, 'DISSOLVE': True, 'OUTPUT': 'TEMPORARY_OUTPUT' # Changed to TEMPORARY_OUTPUT } buffer_result = processing_run("native:buffer", buffer_params) polygons_buffer_layer = buffer_result['OUTPUT'] # Add buffer to project with specific name polygons_buffer_layer.setName("Polygons_Buffer") QgsProject.instance().addMapLayer(polygons_buffer_layer) print("Creating difference layer for polygons buffer...") # Use difference to get uncovered parts difference_params = { 'INPUT': fixed_nikaia, 'OVERLAY': polygons_buffer_layer, 'GRID_SIZE': None, 'IGNORE_INVALID_INPUT_FEATURES': True, 'OUTPUT': 'TEMPORARY_OUTPUT' # Changed to TEMPORARY_OUTPUT } difference_result = processing_run("native:difference", difference_params) if difference_result and 'OUTPUT' in difference_result: uncovered_by_polygons_layer = difference_result['OUTPUT'] if uncovered_by_polygons_layer and uncovered_by_polygons_layer.isValid(): uncovered_by_polygons_layer.setName("Nikaia_Uncovered_Lines_By_Polygons") QgsProject.instance().addMapLayer(uncovered_by_polygons_layer) print("Successfully created uncovered lines layer by polygons!") else: print("Failed to create valid uncovered lines layer by polygons") else: print("Difference operation failed for polygons buffer") except Exception as e: print(f"Error occurred: {str(e)}") print("Trying alternative method for polygons buffer...") try: # Alternative method using extract by location extract_params = { 'INPUT': fixed_nikaia, 'PREDICATE': [0], # Disjoint 'INTERSECT': polygons_buffer_layer, 'OUTPUT': 'TEMPORARY_OUTPUT' } extract_result = processing_run("native:extractbylocation", extract_params) if extract_result and 'OUTPUT' in extract_result: alt_layer = extract_result['OUTPUT'] if alt_layer and alt_layer.isValid(): alt_layer.setName("Nikaia_Uncovered_Lines_By_Polygons_Alt") QgsProject.instance().addMapLayer(alt_layer) print("Successfully created alternative uncovered lines layer by polygons!") else: print("Failed to create valid alternative layer by polygons") else: print("Alternative method failed for polygons buffer") except Exception as e2: print(f"Alternative method also failed for polygons buffer: {str(e2)}") # Run with 15-meter buffer print("Starting script with 15 meter buffer for points...") get_uncovered_lines(buffer_distance=15) print("Starting script with 15 meter buffer for polygons...") get_uncovered_lines_by_polygons(buffer_distance=15) ``` This script now includes two functions: 1. `get_uncovered_lines(buffer_distance=15)`: Cuts the polylines with point buffers. 2. `get_uncovered_lines_by_polygons(buffer_distance=15)`: Cuts the polylines with polygon buffers. Both functions are called at the end of the script with a 15-meter buffer distance. This ensures that the original functionality remains intact while adding the new functionality to handle polygon layers.

formatt well this text