####Select by location on parcels with created point Parcellyr = "vector.DBO.Taxparcels" arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer") entries = int(arcpy.GetCount_management(fc).getOutput(0)) for i in xrange(entries): Sel_Point = arcpy.MakeFeatureLayer_management(fc, "point layer", "\"FID\"={}".format(str(i))) #FID arcpy.SelectLayerByLocation_management("Parcel layer", "INTERSECT", Sel_Point, "", "NEW_SELECTION") #### populates fields add_fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","SiteStreet","predir","StreetType","SubName"] # fix args if not isinstance(add_fields, list): # from script tool add_fields = add_fields.split(';') # do not need this scratch file fcOutput = "temp_join" arcpy.SpatialJoin_analysis("Parcel layer", fc, fcOutput, 'JOIN_ONE_TO_MANY') # grab oid field from points oid_t = arcpy.Describe(fc).OIDFieldName # init rowW and rowR curR = arcpy.SearchCursor(fcOutput) join_dict = dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR]) del curR # Now update the new target curW = arcpy.UpdateCursor(fc) for row in curW: t_oid = row.getValue(oid_t) if t_oid in join_dict: for f in add_fields: row.setValue(f, join_dict[t_oid][add_fields.index(f)]) curW.updateRow(row) del row, curW arcpy.Delete_management("Parcel layer")
Traceback (most recent call last): File "C:\GIS\Python Scripts\AddressPoints\Select by Location based on selected features_3a.py", line 61, in <module> arcpy.SpatialJoin_analysis("Parcel layer", fc, fcOutput, 'JOIN_ONE_TO_MANY') File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\analysis.py", line 480, in SpatialJoin raise e ExecuteError: ERROR 001156: Failed on input OID 387502, could not write value 'LN ' to output field StreetType_1 Failed to execute (SpatialJoin).
####Select by location on parcels with created point Parcellyr = "vector.DBO.Taxparcels" ParLyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer") entries = int(arcpy.GetCount_management(fc).getOutput(0)) for i in xrange(entries): Sel_Point = arcpy.MakeFeatureLayer_management(fc, "point layer", "\"FID\"={}".format(str(i) + "")) PL_Lyr = arcpy.SelectLayerByLocation_management(ParLyr, "INTERSECT", Sel_Point, "", "NEW_SELECTION") #if arcpy.Exists(pt_lyr): arcpy.Delete_management(pt_lyr) Sel_PT = "Sel_PTT" arcpy.MakeFeatureLayer_management(Sel_Point, Sel_PT) PL_Lyr2 = "PL_Layer" arcpy.MakeFeatureLayer_management(PL_Lyr, PL_Lyr2) fcOutput = r"C:\Temp\default.gdb\temp_join" #Field Mapping def Layers(PL_Lyr2, Sel_PT): FieldMapString = "" \ + """ACCOUNT "ACCOUNT" true true false 11 Text 0 0 ,First,#,""" + PL_Lyr2 + """,ACCOUNT,-1,-1;"""\ + """SiteNum "SiteNum" true true false 10 Double 0 0 ,First,#,""" + PL_Lyr2 + """,SiteNum,-1,-1;"""\ + """OwnerName "OwnerName" true true false 100 Text 0 0 ,First,#,""" + PL_Lyr2 + """,OwnerName,-1,-1;"""\ + """SiteAddress "SiteAddress" true true false 106 Text 0 0 ,First,#,""" + PL_Lyr2 + """,SiteAddress,-1,-1;"""\ + """SiteNumSfx "SiteNumSfx" true true false 6 Text 0 0 ,First,#,""" + PL_Lyr2 + """,SiteNumSfx,-1,-1;"""\ + """SiteStreet "SiteStreet" true true false 64 Text 0 0 ,First,#,""" + PL_Lyr2 + """,SiteStreet,-1,-1;"""\ + """Predir "Predir" true true false 2 Text 0 0 ,First,#,""" + PL_Lyr2 + """,Predir,-1,-1;"""\ + """StreetType "StreetType" true true false 8 Text 0 0 ,First,#,""" + PL_Lyr2+ """,StreetType,-1,-1;"""\ + """SubName "SubName" true true false 20 Text 0 0 ,First,#,""" + PL_Lyr2 + """,SubName,-1,-1;"""\ fieldmappings = arcpy.FieldMappings() fieldmappings.loadFromString(FieldMapString) return fieldmappings def main(args=None): if args is None: args = sys.argv arcpy.SpatialJoin_analysis(PL_Lyr2, Sel_PT, fcOutput, "JOIN_ONE_TO_MANY", "KEEP_ALL", Layers(PL_Lyr2, Sel_PT), "INTERSECT") #### populates fields add_fields = ["ACCOUNT","SiteNum","OwnerName","SiteAddres","SiteNumSfx","SiteStreet","Predir","StreetType","SubName"] # fix args if not isinstance(add_fields, list): # from script tool add_fields = add_fields.split(';') # do not need this scratch file #fcOutput = "temp_join" #'temp_join' when using workspace = r"C:\Temp\default.gdb" #arcpy.SpatialJoin_analysis("Parcel layer", fc, fcOutput, 'JOIN_ONE_TO_MANY') # grab oid field from points oid_t = arcpy.Describe(fc).OIDFieldName # init rowW and rowR curR = arcpy.SearchCursor(fcOutput) join_dict = dict([(r.JOIN_FID,[r.getValue(f) for f in add_fields]) for r in curR]) del curR # Now update the new target curW = arcpy.UpdateCursor(fc) for row in curW: t_oid = row.getValue(oid_t) if t_oid in join_dict: for f in add_fields: row.setValue(f, join_dict[t_oid][add_fields.index(f)]) curW.updateRow(row) del row, curW
Traceback (most recent call last): File "C:\GIS\Python Scripts\AddressPoints\Create_Point_2_a.py", line 66, in <module> arcpy.MakeFeatureLayer_management(PL_Lyr, PL_Lyr2) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 5748, in MakeFeatureLayer raise e ExecuteError: ERROR 999999: Error executing function. The table does not have an OID Field. Failed to execute (MakeFeatureLayer).
Solved! Go to Solution.
ParLyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer") #result object
ParLyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer").getOutput(0) #layer object ParLyr_name = ParLyr.name #string object
ParLyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer") entries = int(arcpy.GetCount_management(fc).getOutput(0)) for i in xrange(entries): Pt_sel = arcpy.MakeFeatureLayer_management(fc, "point layer", "\"FID\"={}".format(str(i) + "")) arcpy.SelectLayerByLocation_management(ParLyr, "INTERSECT", Pt_sel, "", "NEW_SELECTION") #if arcpy.Exists(pt_lyr): arcpy.Delete_management(pt_lyr) fcOutput = r"C:\Temp\default.gdb\temp_join" #Field Mapping def Layers(ParLyr, Pt_sel): FieldMapString = "" \ + """ACCOUNT "ACCOUNT" true true false 11 Text 0 0 ,First,#,""" + ParLyr + """,ACCOUNT,-1,-1;"""\ + """SiteNum "SiteNum" true true false 10 Double 0 0 ,First,#,""" + ParLyr + """,SiteNum,-1,-1;"""\ + """OwnerName "OwnerName" true true false 100 Text 0 0 ,First,#,""" + ParLyr + """,OwnerName,-1,-1;"""\ + """SiteAddress "SiteAddress" true true false 106 Text 0 0 ,First,#,""" + ParLyr + """,SiteAddress,-1,-1;"""\ + """SiteNumSfx "SiteNumSfx" true true false 6 Text 0 0 ,First,#,""" + ParLyr + """,SiteNumSfx,-1,-1;"""\ + """SiteStreet "SiteStreet" true true false 64 Text 0 0 ,First,#,""" + ParLyr + """,SiteStreet,-1,-1;"""\ + """Predir "Predir" true true false 2 Text 0 0 ,First,#,""" + ParLyr + """,Predir,-1,-1;"""\ + """StreetType "StreetType" true true false 8 Text 0 0 ,First,#,""" + ParLyr+ """,StreetType,-1,-1;"""\ + """SubName "SubName" true true false 20 Text 0 0 ,First,#,""" + ParLyr + """,SubName,-1,-1;"""\ fieldmappings = arcpy.FieldMappings() fieldmappings.loadFromString(FieldMapString) return fieldmappings def main(args=None): if args is None: args = sys.argv arcpy.SpatialJoin_analysis(ParLyr, Pt_sel, fcOutput, "JOIN_ONE_TO_MANY", "", Layers(ParLyr, Pt_sel), "INTERSECT")
Traceback (most recent call last): File "C:\GIS\Python Scripts\AddressPoints\Create_Point_2_a.py", line 94, in <module> "JOIN_ONE_TO_MANY", "", Layers(ParLyr, Pt_sel), "INTERSECT") File "C:\GIS\Python Scripts\AddressPoints\Create_Point_2_a.py", line 82, in Layers + """SubName "SubName" true true false 20 Text 0 0 ,First,#,""" + ParLyr + """,SubName,-1,-1;"""\ TypeError: cannot concatenate 'str' and 'Result' objects
ParLyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer") #result object
ParLyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer").getOutput(0) #layer object ParLyr_name = ParLyr.name #string object