📅  最后修改于: 2023-12-03 15:13:27.831000             🧑  作者: Mango
arcpy模块是针对ArcGIS软件的Python库,提供了处理GIS数据的许多函数和工具。其中包括选择可见栅格的函数,可以方便快捷地选择用户视图范围内的栅格数据。
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
extent = df.extent
extent_polygon = arcpy.Polygon(arcpy.Array([arcpy.Point(extent.XMin, extent.YMin),
arcpy.Point(extent.XMin, extent.YMax),
arcpy.Point(extent.XMax, extent.YMax),
arcpy.Point(extent.XMax, extent.YMin)]))
raster_lyr = arcpy.mapping.Layer("raster_layer_name")
raster_lyr.definitionQuery = "INTERSECTED_BY_THE_VIEW = 1"
visible_rasters = [row[0] for row in arcpy.da.SearchCursor(raster_lyr, "OID@", spatial_reference=df.spatialReference) if extent_polygon.overlaps(row[1])]
arcpy.SelectLayerByAttribute_management(raster_lyr, "ADD_TO_SELECTION", "OBJECTID IN ({})".format(", ".join(map(str, visible_rasters))))
import arcpy
# 获取地图文档对象,并获取当前激活的数据框
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
# 获取数据框的可视范围,并转换为几何对象
extent = df.extent
extent_polygon = arcpy.Polygon(arcpy.Array([arcpy.Point(extent.XMin, extent.YMin),
arcpy.Point(extent.XMin, extent.YMax),
arcpy.Point(extent.XMax, extent.YMax),
arcpy.Point(extent.XMax, extent.YMin)]))
# 获取栅格图层的描述符对象,并使用Where Clause属性选择可见的栅格
raster_lyr = arcpy.mapping.Layer("raster_layer_name")
raster_lyr.definitionQuery = "INTERSECTED_BY_THE_VIEW = 1"
# 获取可见栅格的ID列表,并使用SelectLayerByAttribute进行选择
visible_rasters = [row[0] for row in arcpy.da.SearchCursor(raster_lyr, "OID@", spatial_reference=df.spatialReference) if extent_polygon.overlaps(row[1])]
arcpy.SelectLayerByAttribute_management(raster_lyr, "ADD_TO_SELECTION", "OBJECTID IN ({})".format(", ".join(map(str, visible_rasters))))
以上代码做了以下操作:
arcpy.mapping.MapDocument
获取当前地图文档对象;arcpy.mapping.ListDataFrames
获取当前激活的数据框对象;extent
属性获取当前视图的范围;arcpy.Polygon
函数将范围转换成几何对象;definitionQuery
属性设置Where Clause属性,用于选择可见栅格,其中INTERSECTED_BY_THE_VIEW
是一个ESRI默认属性,用于表示数据框视图范围与栅格图层的交集;arcpy.da.SearchCursor
获取栅格图层中与视图相交的栅格对象,其中spatial_reference
属性用于指定显示坐标系,row[1]
是栅格数据的几何对象;visible_rasters
列表中;arcpy.SelectLayerByAttribute_management
函数进行选择,并将可见栅格的ID列表转换成SQL语句中的形式;