arcgis python 参数验证
import arcpy class ToolValidator(object): """Class for validating a tool's parameter values and controlling the behavior of the tool's dialog.""" def __init__(self): """Setup arcpy and the list of tool parameters.""" self.params = arcpy.GetParameterInfo() def initializeParameters(self): """Refine the properties of a tool's parameters. This method is called when the tool is opened.""" return def updateParameters(self): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" if self.params[0].value: try: desc = arcpy.Describe(self.params[0].value) shapeType = desc.shapeType sr = desc.spatialReference gcsOrMercator = False if not sr.GCSName and sr.PCSName.upper().find("MERCATOR") == -1 else True if shapeType.upper() == "POINT": self.params[1].filter.list = ["POINT_X_Y_Z_M"] elif shapeType.upper() == "MULTIPOINT": self.params[1].filter.list = ["CENTROID", "PART_COUNT", "EXTENT"] elif shapeType.upper() == "POLYLINE": if desc.hasZ: lineList = ["LENGTH", "LENGTH_GEODESIC", "LENGTH_3D", "LINE_START_MID_END", "CENTROID", "CENTROID_INSIDE", "PART_COUNT", "POINT_COUNT", "LINE_BEARING", "EXTENT"] else: lineList = ["LENGTH", "LENGTH_GEODESIC", "LINE_START_MID_END", "CENTROID", "CENTROID_INSIDE", "PART_COUNT", "POINT_COUNT", "LINE_BEARING", "EXTENT"] self.params[1].filter.list = lineList if gcsOrMercator: lineList.remove("LENGTH") if desc.hasZ: lineList.remove("LENGTH_3D") self.params[1].filter.list = lineList elif shapeType.upper() == "POLYGON": polyList = ["AREA", "AREA_GEODESIC", "PERIMETER_LENGTH", "PERIMETER_LENGTH_GEODESIC", "CENTROID", "CENTROID_INSIDE", "LINE_START_MID_END", "PART_COUNT", "POINT_COUNT", "EXTENT"] self.params[1].filter.list = polyList if gcsOrMercator: polyList.remove("AREA") polyList.remove("PERIMETER_LENGTH") self.params[1].filter.list = polyList if self.params[1].value: self.params[5].parameterDependencies = [0] self.params[5].schema.clone = True newFields = [] propDict = {"POINT_X_Y_Z_M": ["POINT_X", "POINT_Y", "POINT_Z", "POINT_M"], "PART_COUNT": ["PART_COUNT"], "CENTROID": ["CENTROID_X", "CENTROID_Y", "CENTROID_Z", "CENTROID_M"], "EXTENT": ["EXT_MIN_X", "EXT_MIN_Y", "EXT_MAX_X", "EXT_MAX_Y"], "POINT_COUNT": ["PNT_COUNT"], "LINE_START_MID_END": ["START_X", "START_Y", "START_Z", "START_M", "MID_X", "MID_Y", "MID_Z", "MID_M", "END_X", "END_Y", "END_Z", "END_M"], "LINE_BEARING": ["BEARING"], "CENTROID_INSIDE": ["INSIDE_X", "INSIDE_Y", "INSIDE_Z", "INSIDE_M"], "LENGTH": ["LENGTH"], "PERIMETER_LENGTH": ["PERIMETER"], "AREA": ["POLY_AREA"], "LENGTH_GEODESIC": ["LENGTH_GEO"], "AREA_GEODESIC": ["AREA_GEO"], "LENGTH_3D": ["LENGTH_3D"], "PERIMETER_LENGTH_GEODESIC":["PERIM_GEO"], } currentFields = [x.name for x in desc.fields] for prop in str(self.params[1].value).split(";"): try: for field in propDict[prop]: if not field in currentFields: newField = arcpy.Field() newField.type, newField.name = "DOUBLE", field newFields.append(newField) except: pass self.params[5].schema.additionalFields = newFields except: pass return def updateMessages(self): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return
============================================================
class ToolValidator: """Class for validating a tool's parameter values and controlling the behavior of the tool's dialog.""" def __init__(self): """Setup the Geoprocessor and the list of tool parameters.""" import arcpy self.params = arcpy.GetParameterInfo() def initializeParameters(self): """Refine the properties of a tool's parameters. This method is called when the tool is opened.""" return def updateParameters(self): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parmater has been changed.""" import arcpy if self.params[0].value: mxd = arcpy.mapping.MapDocument(self.params[0].value.value) #The following will display an error if mxd is not DDP enabled testDDP = mxd.dataDrivenPages #Generate unique list of layer names lyrs = arcpy.mapping.ListLayers(mxd) layerList = [] for lyr in lyrs: layerList.append(lyr.name) uniqueLayerList = list(set(layerList)) uniqueLayerList.sort() if not self.params[1].altered: self.params[1].filter.list = uniqueLayerList #Generate unique list of field names if self.params[1].altered: selectedLayer = self.params[1].value layerRef = arcpy.mapping.ListLayers(mxd, selectedLayer)[0] fields = arcpy.ListFields(layerRef.dataSource) fieldList = [] for field in fields: fieldList.append(field.name) uniqueFieldList = list(set(fieldList)) uniqueFieldList.sort() self.params[2].filter.list = uniqueFieldList #Generate unique list of page names if self.params[2].altered: fieldName = self.params[2].value rows = arcpy.SearchCursor(layerRef.dataSource) row = rows.next() pageNameList = [] while row: if row.getValue(fieldName) not in pageNameList: pageNameList.append(row.getValue(fieldName)) row = rows.next() pageNameList.sort() self.params[3].filter.list = pageNameList #Generate the printer list printerList = arcpy.mapping.ListPrinterNames() if not self.params[1].altered: self.params[4].filter.list = printerList return def updateMessages(self): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return