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)?
-
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
-
299% possible, similar to gis.stackexchange.com/questions/193681/…– FelixIPNov 10, 2017 at 22:57
-
1@FelixIP do you think it is a duplicate or mind answering this?– fatih_durNov 11, 2017 at 1:56
-
@fatih_dur I'll give it go, because it is not exactly duplicate.– FelixIPNov 11, 2017 at 4:00
2 Answers
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
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
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.
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)[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!)
Confirms that it is possible to use tools in field calculator.
-
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.– FelixIPNov 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