Select to view content in your preferred language

ArcPy: Can somebody explain this piece of code.

690
7
06-22-2018 08:17 AM
by Anonymous User
Not applicable

The script below field calculates the UGA feature UGA attribute in the Address feature UGA field. I can't remember exactly how I created this script but it works. Can someone explain which UGA in the script below is the UGA feature, UGA feature attribute name, or the Address UGA attribute name.

arcpy.MakeFeatureLayer_management("G:\GIS\Anthony\Workspaces\Planning\Addressing_Tool 62018\Data\gdb\Addressing.gdb\Address", "Address_lyr")
arcpy.MakeFeatureLayer_management("G:\GIS\Anthony\Workspaces\Planning\Addressing_Tool 62018\Data\gdb\Addressing.gdb\UGA", "UGA_lyr")

rows = arcpy.SearchCursor("UGA")
for row in rows:

     arcpy.SelectLayerByAttribute_management("UGA", "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))
     arcpy.SelectLayerByLocation_management("Address", "INTERSECT", "UGA", "", "NEW_SELECTION")
     arcpy.CalculateField_management("Address", "UGA", "'{0}'".format(str(row.getValue("UGA"))), "PYTHON_9.3", "")
‍‍‍‍‍‍‍‍‍
0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

This is the searchcursor 

rows = arcpy.SearchCursor("UGA")

which is looking at this featureclass UGA, which is in the Addressing.gdb geodatabase

arcpy.MakeFeatureLayer_management("G:\GIS\Anthony\Workspaces\Planning\Addressing_Tool 62018\Data\gdb\Addressing.gdb\UGA", "UGA_lyr")

The MakeFeatureLayer creates a layer file from the UGA featureclass which is in your Addressing.gdb geodatabase

So the SearchCursor are the records of the featureclass

by Anonymous User
Not applicable

Thanks! and how about the next three lines?

  arcpy.SelectLayerByAttribute_management("UGA", "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))
     arcpy.SelectLayerByLocation_management("Address", "INTERSECT", "UGA", "", "NEW_SELECTION")
     arcpy.CalculateField_management("Address", "UGA", "'{0}'".format(str(row.getValue("UGA"))), "PYTHON_9.3", "")

Which "UGA" inputs are the UGA Feature, UGA Attribute, or Address Attribute? I really appreciate your help on this.

0 Kudos
DanPatterson_Retired
MVP Emeritus

All those UGA as they appear in the three lines are featureclass reference since your only other UGA-thingy is the UGA_lyr which isn't used 

BUT

Select Layer By Attribute—Data Management toolbox | ArcGIS Desktop 

makes me think that the SelecLayerByAttribute shoud be using "UGA_lyr"  SINCE a layer or table view is needed and they specifically say

  • The input must be a feature layer or a table view. The input cannot be a feature class or table.

  • This tool works on layers or table views in the table of contents and on layers or table views created in models and scripts using the Make Feature Layer or Make Table View tool.

by Anonymous User
Not applicable

If I want to copy over a specific attribute from the UGA feature into one of the Address attributes then how would I modify that code?

0 Kudos
JoeBorgione
MVP Emeritus

In a general sense, you'd get the value of specific field in the selected UGA  feature and populate a variable with it I've used a search cursor to get that: then you could use arcpy.CalculateField_managment() to put the that value into the address attributes. 

That should just about do it....
DanPatterson_Retired
MVP Emeritus

You have way too many things going on there... all within a loop of a searchcursor!!

They shouldn't even be within a search cursor to begin with.

Perhaps you should focus on the 3 lines, since that is apparently what you are really trying to do.

1  select something by an attribute

2  then subset by location

3 finally do some field calculation

Those 3 steps should not be within a searchcursor... If you have multiple things that you want to 'calculate' they should be within a loop of calculatefield replacing whatever it is you want to replace... otherwise an updatecursor would be used...but again those 3 lines shouldn't be within an update cursor either

DanPatterson_Retired
MVP Emeritus

marked as assumed answered... assuming it is closed