Python Forum
PIN Number Tool - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: General Coding Help (
+--- Thread: PIN Number Tool (/Thread-PIN-Number-Tool)

PIN Number Tool - jgarrido - Oct-23-2017

Good morning everyone,

I'm kinda new to Python but have been able to create a lot of tasks that allow me to automate certain functions that we used to do manually. Here is my current dilemma. I have been tasked to create a PIN generator that uses the centroid of the parcel to create the PIN. I have created that and it is working beautifully. The problem is I need it to run in ArcMap as a button and ONLY on selected parcels - the current code runs it on all parcels.

How can I modify this code to only run on selected parcels within the MXD I'm using and also use a button created through the Add-In manager.

Thank you in advance.

Julio Garrido


# ArcGIS 10.4.1 script that creates Parcel ID Numbers (PIN)
# and PIN4 numbers for parcels. It uses the centroid of the
# parcel to calculate the PIN.
# Created by Julio A. Garrido

# Import ArcGIS modules
import os
import sys
import arcpy

arcpy.env.workspace = r'C:\Workspace'

def main():

def calPIN(*argv):
arcpy.env.overwriteOutput = True
inputFC = "L:\Planning\Projects\MXD\Julio\WIP Features.gdb\ParcelsCC" #Test parcel

# Add the temporary X/Y Centroid fields
arcpy.AddField_management(inputFC, "xCentroid", "DOUBLE", 18, 11)
arcpy.AddField_management(inputFC, "yCentroid", "DOUBLE", 18, 11)

xExpression = "float(!SHAPE.LabelPoint!.split()[0])"
yExpression = "float(!SHAPE.LabelPoint!.split()[1])"

# Calculate the centroid of the parcel
arcpy.CalculateField_management(inputFC, "xCentroid", xExpression, "PYTHON")
arcpy.CalculateField_management(inputFC, "yCentroid", yExpression, "PYTHON")

with arcpy.da.UpdateCursor(inputFC,["xCentroid","yCentroid","PIN","PIN4"]) as cursor:
for row in cursor:
# Get the current value in field TSDateTime
xvalue = row[0]
yvalue = row[1]

xL = list(str(xvalue))
yL = list(str(yvalue))

# Create the PIN using the centroid data
xyvalue = str(xL[1]) + str(yL[0]) + str(xL[2]) + str(yL[1]) + str(xL[3]) + str(yL[2]) + str(xL[4]) + str(yL[3]) + str(xL[5]) + str(yL[4])
xyvalue4 = str(xL[4]) + str(yL[3]) + str(xL[5]) + str(yL[4])

row[2] = xyvalue

row[3] = xyvalue4

# Delete the temporary X/Y Centroid fields
arcpy.DeleteField_management(inputFC, "xCentroid;yCentroid")

except arcpy.ExecuteError:
print arcpy.GetMessages(2)
except Exception as e:
print e.args[0]