<s> import os , sys , math , traceback <EOL> import arcpy <EOL> from arcpy import env <EOL> from arcpy import sa <EOL> import Utilities <EOL> areaToNumber = arcpy . GetParameterAsText ( <NUM_LIT:0> ) <EOL> pointFeatures = arcpy . GetParameterAsText ( <NUM_LIT:1> ) <EOL> numberingField = arcpy . GetParameterAsText ( <NUM_LIT:2> ) <EOL> outputFeatureClass = arcpy . GetParameterAsText ( <NUM_LIT:3> ) <EOL> DEBUG = True <EOL> appEnvironment = None <EOL> mxd , df , aprx , mp , mapList = None , None , None , None , None <EOL> def labelFeatures ( layer , field ) : <EOL> '''<STR_LIT>''' <EOL> if appEnvironment == "<STR_LIT>" : <EOL> if layer . supports ( "<STR_LIT>" ) : <EOL> for lblclass in layer . listLabelClasses ( ) : <EOL> lblclass . visible = True <EOL> lblclass . expression = "<STR_LIT>" + str ( field ) + "<STR_LIT:]>" <EOL> layer . showLabels = True <EOL> elif appEnvironment == "<STR_LIT>" : <EOL> if layer . supports ( "<STR_LIT>" ) : <EOL> for lblclass in layer . labelClasses : <EOL> lblclass . showClassLabels = True <EOL> lblclass . expression = "<STR_LIT>" + str ( field ) + "<STR_LIT:]>" <EOL> layer . showLabels = True <EOL> arcpy . RefreshActiveView ( ) <EOL> else : <EOL> pass <EOL> def findLayerByName ( layerName ) : <EOL> if appEnvironment == "<STR_LIT>" : <EOL> if DEBUG == True : <EOL> arcpy . AddMessage ( "<STR_LIT>" + layerName + "<STR_LIT>" ) <EOL> for layer in mapList . listLayers ( ) : <EOL> if layer . name == layerName : <EOL> arcpy . AddMessage ( "<STR_LIT>" + layer . name + "<STR_LIT:]>" ) <EOL> return layer <EOL> else : <EOL> arcpy . AddMessage ( "<STR_LIT>" + layer . name + "<STR_LIT:]>" ) <EOL> elif appEnvironment == "<STR_LIT>" : <EOL> if DEBUG == True : arcpy . AddMessage ( "<STR_LIT>" + layerName + "<STR_LIT>" ) <EOL> for layer in arcpy . mapping . ListLayers ( mxd ) : <EOL> if layer . name == layerName : <EOL> arcpy . AddMessage ( "<STR_LIT>" + layer . name + "<STR_LIT:]>" ) <EOL> return layer <EOL> else : <EOL> arcpy . AddMessage ( "<STR_LIT>" + layer . name + "<STR_LIT:]>" ) <EOL> else : <EOL> arcpy . AddMessage ( "<STR_LIT>" ) <EOL> def GetApplication ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> from arcpy import mp <EOL> return "<STR_LIT>" <EOL> except ImportError : <EOL> try : <EOL> from arcpy import mapping <EOL> mxd = arcpy . mapping . MapDocument ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" <EOL> except : <EOL> return "<STR_LIT>" <EOL> def main ( ) : <EOL> '''<STR_LIT>''' <EOL> pointFeatureName = os . path . basename ( pointFeatures ) <EOL> layerExists = False <EOL> try : <EOL> descArea = arcpy . Describe ( areaToNumber ) <EOL> areaGeom = descArea . shapeType <EOL> arcpy . AddMessage ( "<STR_LIT>" + str ( areaGeom ) ) <EOL> if ( descArea . shapeType != "<STR_LIT>" ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> gisVersion = arcpy . GetInstallInfo ( ) [ "<STR_LIT>" ] <EOL> global appEnvironment <EOL> appEnvironment = Utilities . GetApplication ( ) <EOL> if DEBUG == True : arcpy . AddMessage ( "<STR_LIT>" + appEnvironment ) <EOL> global mxd <EOL> global df <EOL> global aprx <EOL> global mp <EOL> global mapList <EOL> if appEnvironment == "<STR_LIT>" : <EOL> from arcpy import mp <EOL> aprx = arcpy . mp . ArcGISProject ( "<STR_LIT>" ) <EOL> mapList = aprx . listMaps ( ) [ <NUM_LIT:0> ] <EOL> for lyr in mapList . listLayers ( ) : <EOL> if lyr . name == pointFeatureName : <EOL> layerExists = True <EOL> if appEnvironment == "<STR_LIT>" : <EOL> from arcpy import mapping <EOL> mxd = arcpy . mapping . MapDocument ( '<STR_LIT>' ) <EOL> df = arcpy . mapping . ListDataFrames ( mxd ) [ <NUM_LIT:0> ] <EOL> for lyr in arcpy . mapping . ListLayers ( mxd ) : <EOL> if lyr . name == pointFeatureName : <EOL> layerExists = True <EOL> if layerExists == False : <EOL> arcpy . MakeFeatureLayer_management ( pointFeatures , pointFeatureName ) <EOL> else : <EOL> pointFeatureName = pointFeatures <EOL> arcpy . AddMessage ( "<STR_LIT>" + str ( os . path . basename ( pointFeatureName ) ) + "<STR_LIT>" + str ( os . path . basename ( areaToNumber ) ) + "<STR_LIT:)>" ) <EOL> selectionLayer = arcpy . SelectLayerByLocation_management ( pointFeatureName , "<STR_LIT>" , <EOL> areaToNumber , "<STR_LIT:#>" , "<STR_LIT>" ) <EOL> if DEBUG == True : <EOL> arcpy . AddMessage ( "<STR_LIT>" + str ( arcpy . GetCount_management ( pointFeatureName ) . getOutput ( <NUM_LIT:0> ) ) + "<STR_LIT>" ) <EOL> overwriteFC = False <EOL> global outputFeatureClass <EOL> if outputFeatureClass == "<STR_LIT>" : <EOL> outputFeatureClass = "<STR_LIT>" <EOL> overwriteFC = True ; <EOL> arcpy . AddMessage ( "<STR_LIT>" ) <EOL> arcpy . Sort_management ( selectionLayer , outputFeatureClass , [ [ "<STR_LIT>" , "<STR_LIT>" ] ] ) <EOL> arcpy . AddMessage ( "<STR_LIT>" ) <EOL> i = <NUM_LIT:1> <EOL> cursor = arcpy . UpdateCursor ( outputFeatureClass ) <EOL> for row in cursor : <EOL> row . setValue ( numberingField , i ) <EOL> cursor . updateRow ( row ) <EOL> i += <NUM_LIT:1> <EOL> arcpy . AddMessage ( "<STR_LIT>" ) <EOL> arcpy . SelectLayerByAttribute_management ( pointFeatureName , "<STR_LIT>" ) <EOL> targetLayerName = "<STR_LIT>" <EOL> if ( overwriteFC ) : <EOL> arcpy . AddMessage ( "<STR_LIT>" ) <EOL> desc = arcpy . Describe ( pointFeatureName ) <EOL> overwriteFC = os . path . join ( os . path . sep , desc . path , pointFeatureName ) <EOL> fields = ( numberingField , "<STR_LIT>" ) <EOL> overwriteCursor = arcpy . da . UpdateCursor ( overwriteFC , fields ) <EOL> for overwriteRow in overwriteCursor : <EOL> sortedPointsCursor = arcpy . da . SearchCursor ( outputFeatureClass , fields ) <EOL> for sortedRow in sortedPointsCursor : <EOL> if sortedRow [ <NUM_LIT:1> ] . equals ( overwriteRow [ <NUM_LIT:1> ] ) : <EOL> overwriteRow [ <NUM_LIT:0> ] = sortedRow [ <NUM_LIT:0> ] <EOL> overwriteCursor . updateRow ( overwriteRow ) <EOL> arcpy . Delete_management ( outputFeatureClass ) <EOL> targetLayerName = pointFeatureName <EOL> else : <EOL> targetLayerName = os . path . basename ( outputFeatureClass ) <EOL> if appEnvironment == "<STR_LIT>" : <EOL> results = arcpy . MakeFeatureLayer_management ( outputFeatureClass , targetLayerName ) . getOutput ( <NUM_LIT:0> ) <EOL> mapList . addLayer ( results , "<STR_LIT>" ) <EOL> layer = findLayerByName ( targetLayerName ) <EOL> if ( layer ) : <EOL> labelFeatures ( layer , numberingField ) <EOL> elif appEnvironment == "<STR_LIT>" : <EOL> arcpy . AddMessage ( "<STR_LIT>" + str ( targetLayerName ) + "<STR_LIT>" ) <EOL> arcpy . MakeFeatureLayer_management ( outputFeatureClass , targetLayerName ) <EOL> layer = arcpy . mapping . Layer ( targetLayerName ) <EOL> arcpy . mapping . AddLayer ( df , layer , "<STR_LIT>" ) <EOL> arcpy . AddMessage ( "<STR_LIT>" + str ( targetLayerName ) + "<STR_LIT>" ) <EOL> layer = findLayerByName ( targetLayerName ) <EOL> if ( layer ) : <EOL> labelFeatures ( layer , numberingField ) <EOL> else : <EOL> arcpy . AddMessage ( "<STR_LIT>" ) <EOL> arcpy . SetParameter ( <NUM_LIT:3> , outputFeatureClass ) <EOL> except arcpy . ExecuteError : <EOL> msgs = arcpy . GetMessages ( ) <EOL> arcpy . AddError ( msgs ) <EOL> print ( msgs ) <EOL> except : <EOL> tb = sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> tbinfo = traceback . format_tb ( tb ) [ <NUM_LIT:0> ] <EOL> pymsg = "<STR_LIT>" + tbinfo + "<STR_LIT>" + str ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> msgs = "<STR_LIT>" + arcpy . GetMessages ( ) + "<STR_LIT:\n>" <EOL> arcpy . AddError ( pymsg ) <EOL> arcpy . AddError ( msgs ) <EOL> print ( pymsg + "<STR_LIT:\n>" ) <EOL> print ( msgs ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
