ArcGis Python脚本——要素图斑自动编号,自上而下,从左到右
原理:
利用图斑最小外包矩形的左上角坐标(数学坐标)Y坐标将序、X坐标升序的方式获取自上而下,从左到右的要素记录排序,然后遍历编号。
"!shape.extent.xmin!"计算要素最小外包矩形的左上角X坐标;
"!shape.extent.ymax!" 计算要素最小外包矩形的左上角Y坐标;
字段计算器中可做Python脚本表达式用,Arcpy中其类型为Python_9.3,在“解析ArcGis字段计算器”相关博文中,已对上面代码信息进行过说明。
代码如下:
#要素类路径 fcpath="C:/Users/Administrator/Desktop/shp/demo.shp" #新建Ymax/Xmin两个字段,分别计算图斑最小外包矩形的左上角Y、X坐标(数学坐标) arcpy.AddField_management(fcpath,"Xmin","DOUBLE") arcpy.AddField_management(fcpath,"Ymax","DOUBLE") #字段计算,计算坐标,计算表达式类型为Python_9.3 arcpy.CalculateField_management(fcpath,"Xmin","!shape.extent.xmin!","PYTHON_9.3") arcpy.CalculateField_management(fcpath,"Ymax","!shape.extent.ymax!","PYTHON_9.3") #迭代更新游标,"Ymax D;Xmin A" 意为Ymax字段将序,Xmin字段升序 rows=arcpy.UpdateCursor(fcpath,"","","","Ymax D;Xmin A") i=0 #code source: https://www.cnblogs.com/yzhyingcool/ QQ:975601416 for row in rows: row.setValue("BSM",i+1) #BSM字段(整型)存放编号,每迭代一次+1 rows.updateRow(row) i+=1
del row
del rows arcpy.DeleteField_management(fcpath,"Xmin;Ymax")