Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PIN Number Tool
#1
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

Code:

# 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():
calPIN()

def calPIN(*argv):
try:
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
cursor.updateRow(row)

row[3] = xyvalue4
cursor.updateRow(row)


# 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]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020