Qgis 自動clips with 2 layer in a batch files
时间: 2023-06-11 19:06:23 浏览: 96
To perform batch clipping in QGIS using Python, you can use the following code:
```python
import os
from qgis.core import QgsVectorLayer, QgsClipper
# Set the source directory containing the layers to be clipped
src_dir = '/path/to/source/directory/'
# Set the destination directory for the clipped layers
dst_dir = '/path/to/destination/directory/'
# Set the clipping layer
clip_layer = QgsVectorLayer('/path/to/clip/layer.shp', 'clip_layer', 'ogr')
# Loop through the files in the source directory
for filename in os.listdir(src_dir):
if filename.endswith('.shp'):
# Load the layer to be clipped
layer = QgsVectorLayer(os.path.join(src_dir, filename), filename[:-4], 'ogr')
# Perform the clip operation
result = processing.run('native:clip', {
'INPUT': layer,
'OVERLAY': clip_layer,
'OUTPUT': os.path.join(dst_dir, filename)
})
```
In this code, you need to replace the following:
- `src_dir`: The path to the directory containing the layers to be clipped.
- `dst_dir`: The path to the directory where the clipped layers will be saved.
- `clip_layer`: The path to the layer that will be used to clip the other layers.
Save this code as a Python file (e.g., `batch_clip.py`) and run it in the QGIS Python Console or in a terminal using the `qgis_process` command.
阅读全文