Python: select by Location and assign value from the source to the target

1897
13
03-06-2014 05:10 PM
RyanFadd
New Contributor
Hi All

I have a polygon layer with a 'lot' field and a points layer. I would like to use python to select the points by location (to find what points are contained in what polygons) and assign the value in the 'lot' field from the polygon layer to the point that is contained in the relevant polygon feature. Not sure how to create that link between the 2 layers...

Any help is much appreciated.
Thank you
Tags (2)
0 Kudos
13 Replies
DavidBuehler
Occasional Contributor III

I am not quite sure what you mean by manual approach, but I have tried running things individually via arcmap, and each step of the script by itself.  The only thing that does not "work" is the field calculate.  I get a 000539 error Invalid field. Below is updated code.  Thoughts?

# import modules
import arcpy

# Set workspaces
workspace = r'C:\ESRITest\GeoReportingSystem\Testing9116\Testing9116.gdb'
outWorkspace = r'C:\ESRITest\GeoReportingSystem\Testing9116\Scratch.gdb'

targetFeatures = r'C:\ESRITest\GeoReportingSystem\Testing9116\Testing9116.gdb\GeoReporting'
joinFeatures = r'C:\ESRITest\GeoReportingSystem\Testing9116\Testing9116.gdb\GRSAldermanic'

outfc = r'C:\ESRITest\GeoReportingSystem\Testing9116\Scratch.gdb\AldermanicSJ'

fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(targetFeatures)
fieldmappings.addTable(joinFeatures)

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "GeoReporting Features", "GRSAldermanic"
arcpy.SpatialJoin_analysis(target_features=targetFeatures, join_features=joinFeatures, out_feature_class=outfc, join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_COMMON", field_mapping=fieldmappings, match_option="INTERSECT", search_radius="", distance_field_name="")

print "Done with Spatial Join"

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "AldermanicSJ"
arcpy.DeleteField_management(in_table=outfc, drop_field="SpecificConcern;DescriptionOfConcern;ReportingEmail;DateClosed;ConcernStatus;PriorityLevel;PrimaryDepartment;PrimaryContact;PrimaryInformation;DeptLogging;ReportingMethod;FollowUpRequest;FollowUpContactInfo;AldermanicDistrict;ApproriateYN;NUMVOTES;DaysToClose;Match_Addr;Loc_Name;created_user;created_date;last_edited_user;last_edited_date;Address;Visibility;Report_ID;DISTRICTID")

print "Done with Deleting Fields"

layerName1 = "outfc_view"
layerName2 = "GRE_view"

#calcExpression ="!AldermanicSJ.NAME!"


arcpy.MakeFeatureLayer_management(outfc, "outfc_view")
arcpy.MakeFeatureLayer_management(targetFeatures, "GRE_view")
# arcpy.MakeFeatureLayer_management(targetFeatures, "GRE_view")
arcpy.AddJoin_management("GRE_view", "ConcernID", "outfc_view", "ConcernID")

print [i.name for i in arcpy.ListFields('GRE_view')]

print "Joined Up Sir"

fieldName1 = "AldermanicDistrict"
fieldName2 = 'AldermanicSJ.NAME'
calcExpression ="!"+fieldName2+"!"


# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "GeoReporting Features"
arcpy.CalculateField_management(targetFeatures, fieldName1, calcExpression, "PYTHON")

#print "Field Calculation Done"

arcpy.RemoveJoin_management(layerName2,"AldermanicSJ")

print "Removed Join"

arcpy.Delete_management(r'C:\ESRITest\GeoReportingSystem\Testing9116\Scratch.gdb\AldermanicSJ')

print "Spatial Join feature class deleted"
0 Kudos
DavidBuehler
Occasional Contributor III

I should also add that when I did the line:

print [i.name for i in arcpy.ListFields('GRE_view')] 

It showed me:

[u'GeoReporting.OBJECTID', u'GeoReporting.ConcernID', u'GeoReporting.GeneralConcern', u'GeoReporting.SpecificConcern', u'GeoReporting.DescriptionOfConcern', u'GeoReporting.ReportingEmail', u'GeoReporting.DateClosed', u'GeoReporting.ConcernStatus', u'GeoReporting.PriorityLevel', u'GeoReporting.PrimaryDepartment', u'GeoReporting.PrimaryContact', u'GeoReporting.PrimaryInformation', u'GeoReporting.DeptLogging', u'GeoReporting.ReportingMethod', u'GeoReporting.FollowUpRequest', u'GeoReporting.FollowUpContactInfo', u'GeoReporting.AldermanicDistrict', u'GeoReporting.ApproriateYN', u'GeoReporting.NUMVOTES', u'GeoReporting.DaysToClose', u'GeoReporting.GlobalID', u'GeoReporting.Match_Addr', u'GeoReporting.Loc_Name', u'GeoReporting.SHAPE', u'GeoReporting.created_user', u'GeoReporting.created_date', u'GeoReporting.last_edited_user', u'GeoReporting.last_edited_date', u'GeoReporting.Address', u'GeoReporting.Visibility', u'GeoReporting.Report_ID', u'AldermanicSJ.OBJECTID', u'AldermanicSJ.Join_Count', u'AldermanicSJ.TARGET_FID', u'AldermanicSJ.ConcernID', u'AldermanicSJ.GeneralConcern', u'AldermanicSJ.NAME']

I have tried various combinations of u'AldermanicSJ.NAME', but get that 000539 error

0 Kudos
DarrenWiens2
MVP Honored Contributor

This would be my guess:

fieldName1 = "GeoReporting.AldermanicDistrict"
fieldName2 = 'AldermanicSJ.NAME'
calcExpression ="!"+fieldName2+"!"


# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "GeoReporting Features"
arcpy.CalculateField_management(targetFeatures, fieldName1, calcExpression, "PYTHON")
0 Kudos
DavidBuehler
Occasional Contributor III

I got it to work.  I had tried your recommendation with no success.  After some trial and error and research, I figured it out. I had to use the view instead of the feature class to calculate on, and change the field variables to the use the output of the join.  See below:

layerName1 = "outfc_view"
layerName2 = "GRE_view"

#calcExpression ="!AldermanicSJ.NAME!"


arcpy.MakeFeatureLayer_management(outfc, "outfc_view")
arcpy.MakeFeatureLayer_management(targetFeatures, "GRE_view")
# arcpy.MakeFeatureLayer_management(targetFeatures, "GRE_view")
arcpy.AddJoin_management("GRE_view", "ConcernID", "outfc_view", "ConcernID")

print [i.name for i in arcpy.ListFields('GRE_view')]

print "Joined Up Sir"

fieldName1 = u'GeoReporting.AldermanicDistrict'
fieldName2 = u'AldermanicSJ.NAME'
calcExpression ="!"+ fieldName2 +"!"


# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "GeoReporting Features"
arcpy.CalculateField_management(layerName2, fieldName1, calcExpression, "PYTHON_9.3")
0 Kudos