我有一个txt文件,里面包含17个数据的编号、名称、类型、范围坐标,范围坐标包含5个点的坐标,我该怎么用arcpy将这17个多边形做成shapefile文件,我需要具体代码
时间: 2024-06-10 16:08:10 浏览: 155
您好!关于您的问题,以下是代码示例:
```python
import arcpy
# 设置工作空间
arcpy.env.workspace = r"C:\data"
# 创建新的 shapefile 文件
out_path = r"C:\data\polygon.shp"
geometry_type = "POLYGON"
template = ""
spatial_reference = arcpy.SpatialReference(4326) # 设置空间参考
arcpy.CreateFeatureclass_management(out_path, "polygon", geometry_type, template, "DISABLED", "DISABLED", spatial_reference)
# 添加字段
fields = ["ID", "Name", "Type"]
for i in range(5):
fields.append("Coordinate_{}".format(i+1))
for field in fields:
arcpy.AddField_management(out_path, field, "text")
# 读取 txt 文件并写入 shapefile
txt_file = r"C:\data\input.txt"
with open(txt_file) as f:
for line in f:
line = line.strip()
data = line.split()
# 添加新的 feature
feature = arcpy.InsertCursor(out_path)
new_row = feature.newRow()
for i in range(len(fields)):
new_row.setValue(fields[i], data[i])
# 构建多边形
array = arcpy.Array()
for j in range(5):
coordinate = arcpy.Point(data[3+j*2], data[4+j*2])
array.add(coordinate)
polygon = arcpy.Polygon(array)
new_row.shape = polygon
feature.insertRow(new_row)
del feature
```
希望以上代码对您有所帮助!如果您还有其他问题,欢迎继续提问。
阅读全文