-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector_Buffer_difference_UniqueID.py
More file actions
95 lines (80 loc) · 3.5 KB
/
Vector_Buffer_difference_UniqueID.py
File metadata and controls
95 lines (80 loc) · 3.5 KB
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
from qgis.core import (
QgsApplication,
QgsVectorLayer,
QgsProject,
QgsVectorFileWriter
)
from qgis.analysis import QgsNativeAlgorithms
import processing
# Start QGIS application
QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
qgs = QgsApplication([], False)
qgs.initQgis()
# Add algorithms
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
# User-defined file paths
roads_file = 'C:/Users/enoks/Desktop/test qgis/24000_HOME/05 - Data/Data In/The List/1721175336247330818096/RoadCentrelines.shp'
overlay_file = 'C:/Users/enoks/Desktop/test qgis/24000_HOME/05 - Data/Data In/The List/1721175336247330818096/BuildingFootprints.shp'
output_path = 'C:/Users/enoks/Desktop/test qgis/24000_HOME/08 - Model Files/QGIS/ICM_ROADS.shp'
# Load the roads shapefile
roads_layer = QgsVectorLayer(roads_file, 'Roads', 'ogr')
if not roads_layer.isValid():
print("Layer failed to load!")
else:
# Set buffer parameters
buffer_distance = 4 # in meters
end_cap_style = 0 # 0=Round, 1=Flat, 2=Square
join_style = 0 # 0=Round, 1=Miter, 2=Bevel
dissolve = True
# Prepare the buffer processing parameters
buffer_params = {
'INPUT': roads_layer,
'DISTANCE': buffer_distance,
'END_CAP_STYLE': end_cap_style,
'JOIN_STYLE': join_style,
'MITER_LIMIT': 2,
'DISSOLVE': dissolve,
'OUTPUT': 'memory:' # Temporary layer in memory
}
# Run the buffer process and load result as a temporary layer
buffered_result = processing.run("native:buffer", buffer_params)['OUTPUT']
# Run Multipart to Singleparts
singleparts_params = {
'INPUT': buffered_result,
'OUTPUT': 'memory:' # Temporary layer in memory
}
singleparts_result = processing.run("native:multiparttosingleparts", singleparts_params)['OUTPUT']
# Add auto-incremental field
auto_increment_params = {
'INPUT': singleparts_result,
'FIELD_NAME': 'UID',
'START': 888,
'GROUP_FIELDS': None, # No grouping field
'SORT_ASCENDING': True,
'SORT_NULLS_FIRST': False,
'OUTPUT': 'memory:' # Temporary layer in memory
}
auto_increment_result = processing.run("native:addautoincrementalfield", auto_increment_params)['OUTPUT']
# Load the overlay layer (Building footprints)
overlay_layer = QgsVectorLayer(overlay_file, 'BuildingFootprints', 'ogr')
if not overlay_layer.isValid():
print("Overlay layer failed to load!")
else:
# Perform the Difference operation
difference_params = {
'INPUT': auto_increment_result,
'OVERLAY': overlay_layer,
'OUTPUT': 'memory:' # Temporary layer in memory
}
difference_result = processing.run("native:difference", difference_params)['OUTPUT']
# Save the final layer to the specified output path
QgsVectorFileWriter.writeAsVectorFormat(difference_result, output_path, "UTF-8", difference_result.crs(), "ESRI Shapefile")
# Load the saved layer into QGIS
final_layer = QgsVectorLayer(output_path, "ICM ROADS", "ogr")
if final_layer.isValid():
QgsProject.instance().addMapLayer(final_layer)
print(f"Final processed layer has been saved to {output_path} and loaded into QGIS.")
else:
print("Failed to load the saved layer into QGIS.")
# Do not exit QGIS if running within the application
# qgs.exitQgis()