I am working on a add-on button that i would like to be able to click on a parcel and create a point where i click and i need the new populate with the Parcels attributes.
I currently have two scripts that work, one creates the point and the other does spatial join and populates the selected point with the tax parcels attributes but the point has to already be there and selected. I need to be able to combine the two scripts into one. I think my biggest problem is that i don't know how to select the point once it's created to be able to spatial join and update the point with the Parcels attributes. I would gratefully appreciate any help on combining these two.
My initial try was to use the sort to get the highest AddressID which was created in the first script (CC_list.sort()) and then was going to use selecLayerByLocation to select the parcel to do the spatial join with the point but nothing is selected.
for row in sorted(arcpy.da.SearchCursor(fc, ["OBJECTID", "AddressID"]),reverse=True):
mostRecentOID=row[0]
mostRecentAddressID=row[1]
break
#Select parcel within selected Address Points
arcpy.SelectLayerByLocation_management(polygonLayer, "INTERSECT", pointGeom)
Create point script
import arcpy
from arcpy import env
import time
import datetime
# Allow overwrite
arcpy.env.overwriteOutput = True
arcpy.env.qualifiedFieldNames = False
arcpy.env.workspace = r"Database Servers\DSD15_SQLEXPRESS.gds\TwoWay (VERSION:dbo.DEFAULT)"
# Script user input parameters
#pointLayer = "TEST.DBO.CCAPTEST" #pointlayer
fc = "TEST"
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(arcpy.env.workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(True)
# Start an edit operation
edit.startOperation()
input = arcpy.GetParameterAsText(0)
rows = arcpy.SearchCursor(input)
for row in rows:
geom = row.Shape
X = geom.centroid.X
Y = geom.centroid.Y
del rows
CC_list = []
with arcpy.da.SearchCursor(fc, ["AddressID"]) as cursor:
for row in cursor:
try:
if "CC" in row[0]:
CC_list.append(int(row[0].strip("CC")))
except TypeError:
pass
del cursor
CC_list.sort()
AddressID = CC_list[-1] + 1
AddressID = 'CC' + str(AddressID)
pointGeom = arcpy.PointGeometry(arcpy.Point(X, Y))
row_values = [(X, Y, (X, Y), AddressID)]
cursor = arcpy.da.InsertCursor(fc, ["POINT_X", "POINT_Y", "SHAPE@XY", "AddressID"])
for row in row_values:
cursor.insertRow(row)
#del cursor
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
arcpy.RefreshActiveView()
Spatial join Update point attributes script.
# Import arcpy module
import arcpy
# Allow overwrite
arcpy.env.overwriteOutput = True
# Script user input parameters
polygonLayer = "Taxparcels"
pointLayer = "TEST" #Point Layer
arcpy.env.workspace = r"Database Servers\DSD15_SQLEXPRESS.gds\TwoWay (VERSION:dbo.DEFAULT)"
poly = "ACCOUNT_1"
Pnt = "Account"
sjpoints = "In_memory\sjpoints"
parcelsCount = int(arcpy.GetCount_management(pointLayer).getOutput(0))
dsc = arcpy.Describe(pointLayer)
selection_set = dsc.FIDSet
if len(selection_set) == 0:
print "There are no features selected"
elif parcelsCount >= 1:
#Run the Spatial Join tool, using the defaults for the join operation and join type
arcpy.SpatialJoin_analysis(pointLayer, polygonLayer, sjpoints)
# define the field list from the spatial join
sourceFieldsList = ["TARGET_FID", poly,"ACCOUNT_1","SiteAddres_1",'SiteNum_1', 'SiteStreet_1','SiteNumSfx_1','Predir_1','SiteStreet_1', 'StreetType_1', 'Postdir_1', 'SiteCity_1', 'SiteZIP_1', 'OwnerName_1']
# define the field list to the original points
updateFieldsList = ["OID@", Pnt,"Account","SiteAddres", 'SiteNum', 'StreetName', 'SiteNumSfx','Predir','SiteStreet', 'StreetType', 'Postdir', 'SiteCity', 'SiteZip', 'OwnerName']
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(arcpy.env.workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(True)
# Start an edit operation
edit.startOperation()
manualFields = ["FacltyType", "StructType", "Verified", "Status", "StructCat", "APA_CODE", "ACCOUNT", 'SiteStreet']
with arcpy.da.UpdateCursor(pointLayer, manualFields) as rows:
for row in rows:
row[0] = ("Single Family Home")
row[1] = ("Primary, Private")
row[2] = ("Yes, GRM, TA")
row[3] = ("Active")
row[4] = ("Residential")
row[5] = ("1110")
rows.updateRow(row)
del row
del rows
# populate the dictionary from the polygon
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sjpoints, sourceFieldsList)}
with arcpy.da.UpdateCursor(pointLayer, updateFieldsList) as updateRows:
for updateRow in updateRows:
keyValue = updateRow[0]
if keyValue in valueDict:
for n in range (1,len(sourceFieldsList)):
updateRow = valueDict[keyValue][n-1]
updateRows.updateRow(updateRow)
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
arcpy.RefreshActiveView()
#arcpy.Delete_management(sjpoints)