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

4
  • 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, 2017 at 22:57
  • 2
    99% possible, similar to gis.stackexchange.com/questions/193681/…
    – FelixIP
    Nov 10, 2017 at 22:57
  • 1
    @FelixIP do you think it is a duplicate or mind answering this?
    – fatih_dur
    Nov 11, 2017 at 1:56
  • @fatih_dur I'll give it go, because it is not exactly duplicate.
    – FelixIP
    Nov 11, 2017 at 4:00

2 Answers 2

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.

0
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.

2
  • 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, 2017 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: gis.stackexchange.com/questions/261584/…). Nov 13, 2017 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.