import arcpy class Toolbox(object): def __init__(self): """Define the toolbox (the name of the toolbox is the name of the .pyt file).""" self.label = "CalcGeom" self.alias = "RBNPythonToolBox" # List of tool classes associated with this toolbox self.tools = [Tool1] class Tool1(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "CalcGeom" self.description = "Tool to calculate geometry" self.canRunInBackground = True def getParameterInfo(self): # First parameter in_features = arcpy.Parameter( displayName="Input Features", name="in_features", datatype="Feature Layer", parameterType="Required", direction="Input") in_features.filter.list = ["Point", "Polyline", "Polygon"] # Second parameter field = arcpy.Parameter( displayName="Field Name", name="field_name", datatype="Field", parameterType="Required", direction="Input") field.parameterDependencies = [in_features.name] field.filter.list = ["Short","Long","Double","Float","Text"] # Third parameter geomProperty = arcpy.Parameter( displayName="Property", name="geomProperty", datatype="String", parameterType="Required", direction="input") # Fourth parameter units = arcpy.Parameter( displayName="Units", name="units", datatype="String", parameterType="Optional", direction="input", enabled=False) # Fifth parameter out_features = arcpy.Parameter( displayName="Output Features", name="out_features", datatype="Feature Layer", parameterType="Derived", direction="output") out_features.parameterDependencies = [in_features.name] out_features.schema.clone = True params = [in_features, field, geomProperty, units, out_features] return params def isLicensed(self): """Set whether tool is licensed to execute.""" return True def updateParameters(self, parameters): #Get Inputs in_features = parameters[0] geomProperty = parameters[2] units = parameters[3] #Geometry Property Filter Lists pointPropertyList = ['X Coordiante of Point', 'Y Coordinate of Point'] linePropertyList = ['Length', 'X Coordinat of Line Start', 'Y Coordinate of Line Start', 'X Coordinate of line End', 'Y Coordinate of Line End'] polygonPropertyList = ['Area', 'Perimeter', 'X Coordinate of Centroid', 'Y Coordinate of Centroid'] #Get shape type of input to determine #filter list for geometry property parameter if in_features.value: desc = arcpy.Describe(in_features.valueAsText) if desc.shapeType == 'Point': geomProperty.filter.list = pointPropertyList elif desc.shapeType == 'Polyline': geomProperty.filter.list = linePropertyList elif desc.shapeType == 'Polygon': geomProperty.filter.list = polygonPropertyList #Unit Filter List areaUnitList = ['Acres', 'Ares', 'Hectares', 'Square Centimeters', 'Square Inches', 'Square Feet', 'Square Kilometers', 'Square Meters', 'Square Miles', 'Square Millimeters', 'Square Yards', 'Square Decimeters'] lengthUnitList = ['Centimeters', 'Feet', 'Inches', 'Kilometers', 'Meters', 'Miles', 'Yards', 'Decimal Degrees'] #Get geometry property input to determine filter list for unit parameter if geomProperty.value: if geomProperty.valueAsText == 'Area': units.enabled = True units.filter.list = areaUnitList elif geomProperty.valueAsText in ['Length', 'Perimeter']: units.enabled = True units.filter.list = lengthUnitList else: units.value = "" units.filter.list = [] units.enabled = False return def updateMessages(self, parameters): in_features = parameters [0] geomProperty = parameters[2] units = parameters[3] #Check if input is Multipoint or Multipatch and if so set an error if in_features.value: desc = arcpy.Describe(in_features.valueAsText) if desc.shapeType in ['Multipoint', 'Multipatch']: in_features.setErrorMessage( "{0} features are not supported.".format(desc.shapeType)) #Check if certain geometry property value is set with no units and add if geomProperty.valueAsText in ['Length', 'Perimeter', 'Area']: if not units.value: units.setErrorMessage( "Units required for {0} property".format(geomProperty.valueAsText)) return def execute(self, parameters, messages): #Get Inputs in_features = parameters[0].valueAsText field = parameters[1].valueAsText geomProperty = parameters[2].valueAsText units = parameters[3].valueAsText shapeFieldName = arcpy.Describe(in_features).shapeFieldName #Create the expression exp = "!" + shapeFieldName if geomProperty == "Area": exp += ".area@" + units.replace(' ', '') elif geomProperty in ['Length', 'Perimeter']: exp += ".length@" + units.replace(' ', '') else: propertyList = geomProperty.split(' ') coord = propertyList[0] if propertyList[-1] in ['Point','Start']: exp += ".firstPoint." + coord elif propertyList[-1] == 'End': exp += ".lastPoint." + coord elif propertyList[-1] == 'Centroid': exp += ".centroid." + coord exp += "!" messages.addMessage( "\nExpression used for field calculation: {0}\n".format(exp)) #Calculate Field arcpy.CalculateField_management(in_features, field, exp, "Python_9.3") return def isLicensed(self): """Set whether tool is licensed to execute.""" return True def updateParameters(self, parameters): #Get Inputs in_features = parameters[0] geomProperty = parameters[2] units = parameters[3] #Geometry Property Filter Lists pointPropertyList = ['X Coordiante of Point', 'Y Coordinate of Point'] linePropertyList = ['Length', 'X Coordinat of Line Start', 'Y Coordinate of Line Start', 'X Coordinate of line End', 'Y Coordinate of Line End'] polygonPropertyList = ['Area', 'Perimeter', 'X Coordinate of Centroid', 'Y Coordinate of Centroid'] #Get shape type of input to determine #filter list for geometry property parameter if in_features.value: desc = arcpy.Describe(in_features.valueAsText) if desc.shapeType == 'Point': geomProperty.filter.list = pointPropertyList elif desc.shapeType == 'Polyline': geomProperty.filter.list = linePropertyList elif desc.shapeType == 'Polygon': geomProperty.filter.list = polygonPropertyList #Unit Filter List areaUnitList = ['Acres', 'Ares', 'Hectares', 'Square Centimeters', 'Square Inches', 'Square Feet', 'Square Kilometers', 'Square Meters', 'Square Miles', 'Square Millimeters', 'Square Yards', 'Square Decimeters'] #Get geometry property input to determine filter list for unit parameter if geomProperty.value: if geomProperty.valueAsText == 'Area': units.enabled = True units.filter.list = areaUnitList elif geomProperty.valueAsText in ['Length', 'Perimeter']: units.enabled = True units.filter.list = lengthUnitList else: units.value = "" units.filter.list = [] units.enabled = False return def updateMessages(self, parameters): in_features = parameters [0] geomProperty = parameters[2] units = parameters[3] #Check if input is Multipoint or Multipatch and if so set an error if in_features.value: desc = arcpy.Describe(in_features.valueAsText) if desc.shapeType in ['Multipoint', 'Multipatch']: in_features.setErrorMessage( "{0} features are not supported.".format(desc.shapeType)) #Check if certain geometry property value is set with no units and add if geomProperty.valueAsText in ['Length', 'Perimeter', 'Area']: if not units.value: units.setErrorMessage( "Units required for {0} property".format(geomProperty.valueAsText)) return def execute(self, parameters, messages): #Get Inputs in_features = parameters[0].valueAsText field = parameters[1].valueAsText geomProperty = parameters[2].valueAsText units = parameters[3].valueAsText shapeFieldName = arcpy.Describe(in_features).shapeFieldName #Create the expression exp = "!" + shapeFieldName if geomProperty == "Area": exp += ".area@" + units.replace(' ', '') elif geomProperty in ['Length', 'Perimeter']: exp += ".length@" + units.replace(' ', '') else: propertyList = geomProperty.split(' ') coord = propertyList[0] if propertyList[-1] in ['Point','Start']: exp += ".firstPoint." + coord elif propertyList[-1] == 'End': exp += ".lastPoint." + coord elif propertyList[-1] == 'Centroid': exp += ".centroid." + coord exp += "!" messages.addMessage( "\nExpression used for field calculation: {0}\n".format(exp)) #Calculate Field arcpy.CalculateField_management(in_features, field, exp, "Python_9.3") return
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question