Tuesday, 14 March 2017

arcpy - Can ArcGIS geoprocessing tools be used in ArcGIS Field calculator?


Can you put ArcGIS geoprocessing tools such as arcpy.SelectLayerByLocation_management() or arcpy.MakeFeatureLayer_management() into the code block of the ArcGIS Field-calculator (ArcGIS Desktop 10.4)?



Answer




I generated random points inside 4 polygons shapefile, using field "Points_Cnt". The source of polygons is "C:\FELIX_DATA\SCRARCH\fish_net.shp":


enter image description here


The result of this field caluclator expression on a new field "RESULT":


mxd = arcpy.mapping.MapDocument("CURRENT")
LR=arcpy.mapping.ListLayers(mxd)[0]
d=arcpy.Describe(LR)
def getPoints(fid):
arcpy.MakeFeatureLayer_management ("C:/FELIX_DATA/SCRARCH/fish_net.shp","A",'"FID" ='+str(fid))
arcpy.SelectLayerByLocation_management(LR,"INTERSECT","A")
nSel=len(d.FIDset.split(';'))

return nSel


getPoints( !FID!)

enter image description here


Confirms that it is possible to use tools in field calculator.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...