arcpy在图幅范围内增加200个随机位置的高程点。
时间: 2024-03-14 22:48:00 浏览: 76
用ArcPy来自动化制图
5星 · 资源好评率100%
以下是使用ArcPy在图幅范围内增加200个随机位置的高程点的示例代码:
```
import arcpy
import random
# 设置工作空间和输出路径
arcpy.env.workspace = r"C:\data"
out_path = r"C:\output"
# 定义输入DEM和输出点要素类名称和坐标系
dem_name = "dem.tif"
out_name = "random_elevation_points.shp"
spatial_reference = arcpy.Describe(dem_name).spatialReference
# 定义随机点数量和范围
num_points = 200
x_min, y_min, x_max, y_max = arcpy.Describe(dem_name).extent.XMin, arcpy.Describe(dem_name).extent.YMin, arcpy.Describe(dem_name).extent.XMax, arcpy.Describe(dem_name).extent.YMax
# 创建空要素类
arcpy.CreateFeatureclass_management(out_path, out_name, "POINT", spatial_reference=spatial_reference)
arcpy.AddField_management(out_path + "\\" + out_name, "ELEVATION", "DOUBLE")
# 添加点要素
with arcpy.da.InsertCursor(out_path + "\\" + out_name, ["SHAPE@XY", "ELEVATION"]) as cursor:
for i in range(num_points):
x = random.uniform(x_min, x_max)
y = random.uniform(y_min, y_max)
elevation = arcpy.GetCellValue_management(dem_name, str(x) + " " + str(y), "1").getOutput(0)
cursor.insertRow([(x, y), elevation])
```
以上代码将在输入的DEM图层范围内生成200个随机位置的高程点,并将其添加到名为`random_elevation_points.shp`的新要素类中。每个点的高程通过`GetCellValue_management`函数从输入DEM图层中获取,然后将其存储在`ELEVATION`字段中。
阅读全文