ARCGIS中Python实现按属性字段批量掩膜提取
时间: 2024-03-12 08:49:32 浏览: 142
可以使用arcpy模块中的ExtractByAttributes函数实现按属性字段批量掩膜提取,具体步骤如下:
1. 首先,需要导入arcpy模块,并设置工作环境和工作空间。如下所示:
```python
import arcpy
arcpy.env.workspace = r"D:\data"
arcpy.env.overwriteOutput = True
```
2. 然后,定义一个要素类列表,获取需要进行掩膜提取的要素类路径,并使用for循环遍历要素类列表。如下所示:
```python
fcList = ["layer1.shp", "layer2.shp", "layer3.shp"]
for fc in fcList:
inFC = r"D:\data\\" + fc
```
3. 接下来,定义一个属性字段列表,获取需要进行掩膜提取的属性字段名称,并使用for循环遍历属性字段列表。如下所示:
```python
fieldList = ["field1", "field2", "field3"]
for field in fieldList:
whereClause = '"' + field + '" = 1'
```
4. 最后,使用ExtractByAttributes函数进行掩膜提取,并将输出结果保存到指定路径。如下所示:
```python
outFC = r"D:\output\\" + fc + "_" + field + ".shp"
arcpy.ExtractByAttributes(inFC, whereClause, outFC)
```
完整代码如下所示:
```python
import arcpy
arcpy.env.workspace = r"D:\data"
arcpy.env.overwriteOutput = True
fcList = ["layer1.shp", "layer2.shp", "layer3.shp"]
fieldList = ["field1", "field2", "field3"]
for fc in fcList:
inFC = r"D:\data\\" + fc
for field in fieldList:
whereClause = '"' + field + '" = 1'
outFC = r"D:\output\\" + fc + "_" + field + ".shp"
arcpy.ExtractByAttributes(inFC, whereClause, outFC)
```
注意:代码中的路径需要根据实际情况进行修改。
阅读全文