Can you put ArcGIS geoprocessing tools such as
arcpy.MakeFeatureLayer_management() into the code block of the ArcGIS Field-calculator (ArcGIS Desktop 10.4)?
I generated random points inside 4 polygons shapefile, using field "Points_Cnt". The source of polygons is "C:\FELIX_DATA\SCRARCH\fish_net.shp":
The result of this field caluclator expression on a new field "RESULT":
mxd = arcpy.mapping.MapDocument("CURRENT") LR=arcpy.mapping.ListLayers(mxd) 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
Confirms that it is possible to use tools in field calculator.
Yes, this can be done with some of the geoprocessing tools.
Proof of concept - use
arcpy.GetCount_management() to count the selected features and write that value to those selected features:
def myTest(): count = int(arcpy.GetCount_management('testPoint').getOutput(0)) return count
arcpy.SelectLayerByAttribute_management() to select every ObjectID divisible by
4 and write a value to those newly selected features:
arcpy.SelectLayerByAttribute_management('testPoint','NEW_SELECTION','mod(OBJECTID,4)=0') def myTest(): return 123
Of course you can make these more complex if you require, however I would recommend using arcpy script tools and update cursors if you're getting too clever, rather than Field Calculator.