4

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)?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
  • Welcome to GIS SE! As a new user be sure to take the [Tour] to learn about our focussed Q&A format. What happened when you tried to do this? – PolyGeo Nov 10 '17 at 22:57
  • 2
    99% possible, similar to https://gis.stackexchange.com/questions/193681/calculating-sequential-numbers-into-sorted-table-using-arcgis-desktop/193684#193684 – FelixIP Nov 10 '17 at 22:57
  • 1
    @FelixIP do you think it is a duplicate or mind answering this? – fatih_dur Nov 11 '17 at 01:56
  • @fatih_dur I'll give it go, because it is not exactly duplicate. – FelixIP Nov 11 '17 at 04:00

2 Answers2

3

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

enter image description here

Another, use 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

enter image description here

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.

Midavalo
  • 29,696
  • 10
  • 48
  • 104
2

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.

FelixIP
  • 22,922
  • 3
  • 29
  • 61
  • After posting solution I realized, that OP is not so weird. It is a very cool way to count points in polygons without need of spatial join, summary statistics and attribute join ! No need to clean intermediate Results. – FelixIP Nov 11 '17 at 19:13
  • Ah, I just realized how you used the fid in a where_clause to select a single object for the feature class. That is useful. As per you hint in: https://gis.stackexchange.com/questions/261584/using-selectlayerbylocation-management-parameters). – Alexander Audet Nov 13 '17 at 21:35