def remove_all_domains(fcs):
    for fc in fcs:
        print(fc)
        fieldNames = [f.name for f in arcpy.ListFields(fc)]
        for field in fieldNames:
            print(f"Unassigning domain for field {field}")
            try:
                arcpy.RemoveDomainFromField_management(fc, field)
            except:
                print("Error removing domain for {}".format(field))
                pass
Exemple #2
0
def PrepFeatClassForLibrary(featPath, logger, lstErrCnt):
    # get all subtype codes in order to remove domains from subtypes
    subtypeCodes = []
    subtypes = arcpy.da.ListSubtypes(featPath)
    for stcode, stdict in list(subtypes.items()):
        subtypeCodes.append(stcode)  # append subtype codes to a list

    # Remove domains from subtypes and fields
    lstDomains = []
    ndx = featPath.rfind('/')
    featWrkspc = featPath[0:ndx]  # get just the workspace part of the featPath
    featName = featPath.split('/')[-2] + '/' + featPath.split('/')[
        -1]  # geodatabase/name for log
    lstFields = arcpy.ListFields(featPath)
    for fld in lstFields:
        if len(fld.domain) > 0:
            if fld.domain not in lstDomains:
                lstDomains.append(fld.domain)
            try:
                arcpy.RemoveDomainFromField_management(
                    featPath, fld.name,
                    subtypeCodes)  # remove domain from all subtypes
            except Exception as e:
                logger.warning(
                    '\tUnable to remove domain from subtype on field ' +
                    fld.name)
                #lstErrCnt[1] += 1 # Don't count warning so that email notification will ignore
            try:
                arcpy.RemoveDomainFromField_management(
                    featPath, fld.name)  # remove domain from field
            except Exception as e:
                logger.warning('\tUnable to remove domain from field ' +
                               fld.name)
                #lstErrCnt[1] += 1 # Don't count warning so that email notification will ignore
    # delete the domains from the workspace
    for dmn in lstDomains:
        try:
            arcpy.DeleteDomain_management(featWrkspc, dmn)
        except Exception as e:
            logger.warning('\tUnable to delete domain ' + dmn + ' on ' +
                           featName)
def save_defaultvalue_domain():  #保存
    #a = domain_dictionary()
    all_lst = []
    del_domain = []
    ds_dic ={}
    for ds in list_feature_batch('Pipe.gdb'):
        print ds
        arcpy.env.workspace = ds
        fc_dic = {}
        for fc in arcpy.ListFeatureClasses(): # 为要素类是使用
            fld_dic = {}
            print "正在执行函数"
            print fc + '\n'
            for fld in arcpy.ListFields(fc):
                if fld.domain:
                    #try:
                        if fld.type == 'Integer': # 读取字段类型时,长整型:integer   字符串:String  短整型:SmallInteger
                            domname_defvalue = []
                            default_val = fld.defaultValue
                            domain_name = fld.domain
                            domname_defvalue.append(default_val)
                            domname_defvalue.append(domain_name)
                            print "\t正在执行函数"
                            print '\t',domain_name,default_val
                            if domain_name not in del_domain:
                                   del_domain.append(domain_name)
                            #arcpy.AssignDefaultToField_management(fc,fld.name,"","","True")#有默认值的情况
                            # 去除域值,去除default value
                            try:
                                arcpy.AssignDefaultToField_management(fc, fld.name, "", "", "true")  # 去除default value
                                arcpy.RemoveDomainFromField_management(fc, fld.name)  # 去除域值
                                arcpy.AlterField_management(fc,fld.name,'','','Short')
                            except arcpy.ExecuteError:
                                print arcpy.GetMessages()
                        #fld_dic[fld.name] = domname_defvalue
                            #print domname_defvalue
                        #fldname_domname_defvalue.append(fld_dic)
                            fld_dic[fld.name] = domname_defvalue
           # print fld_dic
            fc_dic[fc] = fld_dic
        ds_dic[os.path.basename(ds)] = fc_dic
        #print fc_dic
    all_lst.append(ds_dic)
    all_lst.append(del_domain)
    return all_lst
Exemple #4
0
 def detruireDomaineAttribut(self, workspace, classe):
 #-------------------------------------------------------------------------------------
     """
     Permet de détruire tous les domaines existants dans la Géodatabase.
     
     Paramètres:
     -----------
     workspace   : Nom de la géodatabase ou les domaines seront détruits.
     classe      : Liste des noms de classe contenus dans la géodatabase utilisés pour créer les domaines.
     
     """
     
     #Définir le workspace par défaut
     arcpy.env.workspace = workspace
     
     #Extraire les FeatureClass de la Géodatabase
     for fc in arcpy.ListFeatureClasses():
         #Vérifier si la classe est présente dans la liste des classes
         if fc.upper() in classe:
             #Afficher le message pour détruire les domaines de la classe traité
             arcpy.AddMessage(" ")
             arcpy.AddMessage(u"- Détruire les subtypes de la classe : " + fc)
             #Définir tous les Subtypes
             subtypes = arcpy.da.ListSubtypes(fc)
             #Vérifier si un Subtype est présent
             if subtypes.values()[0]['SubtypeField'] <> "":
                 #Afficher le message
                 arcpy.AddMessage("SetSubtypeField_management('" + fc + "', #, True)")
                 #Enlever les subtypes d'une classe
                 arcpy.SetSubtypeField_management(fc, "#", True)
                 
                 #Afficher les subtypes
                 #for stcode, stdict in list(subtypes.items()):
                     #Afficher le message
                     #arcpy.AddMessage(" RemoveSubtype_management('" + fc + "', '" + str(stcode) + "')")
                     #Détruire un subtype d'une classe
                     #arcpy.RemoveSubtype_management(fc, stcode)
             
             #Afficher le message pour détruire les domaines de la classe traité
             arcpy.AddMessage(u"- Détruire le domaine et la valeur par défaut pour chaque attribut de la classe : " + fc)
             #Traiter tous les fields
             for field in arcpy.ListFields(fc):
                 #Vérifier la présence d'un domaine
                 if field.domain <> "":
                     #Afficher le message
                     arcpy.AddMessage("RemoveDomainFromField_management('" + fc + "', '" + field.name + "')")
                     #Détruire un domaine dans un attribut d'une classe
                     arcpy.RemoveDomainFromField_management(fc, field.name)
                 
                 #Vérifier si une valeur par défaut est présente
                 if field.defaultValue <> None:
                     #Afficher le message
                     arcpy.AddMessage("AssignDefaultToField_management('" + fc + "', '" + field.name + "' # # True)")
                     #Détruire la valeur par défaut de l'attribut d'une classe
                     arcpy.AssignDefaultToField_management(fc, field.name, "#", "#", True)
                 
     #Extraire les tables de la Géodatabase
     for tbl in arcpy.ListTables():
         #Vérifier si la table est présente dans la liste des classes
         if tbl.upper() in classe:
             #Afficher le message pour détruire les domaines de la classe traité
             arcpy.AddMessage(" ")
             arcpy.AddMessage(u"- Détruire les subtypes de la table : " + tbl)
             #Définir tous les Subtypes
             subtypes = arcpy.da.ListSubtypes(tbl)
             #Vérifier si un Subtype est présent
             if subtypes.values()[0]['SubtypeField'] <> "":
                 #Afficher le message
                 arcpy.AddMessage("SetSubtypeField_management('" + tbl + "', #, True)")
                 #Enlever les subtypes d'une classe
                 arcpy.SetSubtypeField_management(tbl, "#", True)
                 
                 #Afficher les subtypes
                 #for stcode, stdict in list(subtypes.items()):
                     #Afficher le message
                     #arcpy.AddMessage(" RemoveSubtype_management('" + tbl + "', '" + str(stcode) + "')")
                     #Détruire un subtype d'une table
                     #arcpy.RemoveSubtype_management(tbl, stcode)
             
             #Afficher le message pour détruire les domaines de la classe traité
             arcpy.AddMessage(u"- Détruire le domaine et la valeur par défaut pour chaque attribut de la table : " + tbl)
             #Traiter tous les fields
             for field in arcpy.ListFields(tbl):
                 #Vérifier la présence d'un domaine
                 if field.domain <> "":
                     #Afficher le message
                     arcpy.AddMessage("RemoveDomainFromField_management('" + tbl + "', '" + field.name + "')")
                     #Détruire un domaine dans un attribut d'une classe
                     arcpy.RemoveDomainFromField_management(tbl, field.name)
                 
                 #Vérifier si une valeur par défaut est présente
                 if field.defaultValue <> None:
                     #Afficher le message
                     arcpy.AddMessage("AssignDefaultToField_management('" + tbl + "', '" + field.name + "' # # True)")
                     #Détruire la valeur par défaut de l'attribut d'une table
                     arcpy.AssignDefaultToField_management(tbl, field.name, "#", "#", True)
     
     #Envoyer un message
     arcpy.AddMessage(" ")
     arcpy.AddMessage(u"- Détruire les domaines non-utilisés dans la Géodatabase")
     
     #Extraire la description de la Géodatabase
     desc = arcpy.Describe(workspace)
     
     #Extraire tous les domaines existants de la Géodatabase
     domains = desc.domains
     
     #Traiter tous les domaines
     for domain in domains:
         try:
             #Détruire un domaine
             arcpy.DeleteDomain_management(workspace, domain)
             #Afficher le message
             arcpy.AddMessage("DeleteDomain_management('" + workspace + "', '" + domain + "')")
         #Gestion des erreurs
         except Exception, err:
             #Afficher l'erreur
             arcpy.AddWarning(u"Le domaine est encore utilisé et ne peut être détruit : " + domain)
Exemple #5
0
def main():
    # GET PARAMETER VALUES
    Analysis_Area = arcpy.GetParameterAsText(0)
    Current_Anthro_Features_Provided = arcpy.GetParameterAsText(1)  # optional
    Dist_Lek = arcpy.GetParameterAsText(2)

    # DEFINE DIRECTORIES
    # Get the pathname to this script
    scriptPath = sys.path[0]
    arcpy.AddMessage("Script folder: " + scriptPath)
    # Construct pathname to workspace
    workspace = arcpy.Describe(Analysis_Area).path
    arcpy.AddMessage("Project geodatabase: " + workspace)

    # Instantiate a ccsStandard object
    ccsStandard = ccslib.ccsStandard(workspace, scriptPath)

    # ENVIRONMENT SETTINGS
    # Set workspaces
    arcpy.env.workspace = workspace
    scratch_folder = os.path.join(arcpy.Describe(workspace).path, 'scratch')
    if arcpy.Exists(scratch_folder):
        pass
    else:
        arcpy.CreateFolder_management(
            arcpy.Describe(workspace).path, 'scratch')
    arcpy.env.scratchWorkspace = scratch_folder
    # Overwrite outputs
    arcpy.env.overwriteOutput = True

    # DEFINE VARIABLES FOR INPUT DATA
    AnthroAttributeTable = ccsStandard.AnthroAttributeTable
    emptyRaster = ccsStandard.EmptyRaster
    inputDataPath = ccsStandard.InputDataPath

    # Filenames for feature classes and raster used by this script
    ELIGIBLE_PROPOSED_FEATURES = "Proposed_Surface_Disturbance_Eligible"
    DEBIT_PROJECT_AREA = "Debit_Project_Area"

    # Filenames for feature classes and rasters created by this script
    CURRENT_ANTHRO_FEATURES = "Current_Anthro_Features"
    PROJECTED_ANTHRO_FEATURES = "Projected_Anthro_Features"
    PERMANENT_ANTHRO_FEATURES = "Permanent_Anthro_Features"
    CURRENT_ANTHRO_DISTURBANCE = "Current_Anthro_Disturbance"
    PROJECTED_ANTHRO_DISTURBANCE = "Projected_Anthro_Disturbance"
    PERMANENT_ANTHRO_DISTURBANCE = "Permanent_Anthro_Disturbance"
    MAP_UNITS = "Map_Units"

    # ------------------------------------------------------------------------

    # FUNCTION CALLS
    # Check tool version
    ccslib.CheckToolVersion()

    # Check out Spatial Analyst extension
    ccslib.CheckOutSpatialAnalyst()

    # Check Analysis_Area
    expected_fcs = [ELIGIBLE_PROPOSED_FEATURES, DEBIT_PROJECT_AREA]
    ccslib.CheckPolygonInput(Analysis_Area, expected_fcs=expected_fcs)

    # Create Current_Anthro_Features layer, or copy provided into geodatabase
    if Current_Anthro_Features_Provided:
        # Clear selection, if present
        ccslib.ClearSelectedFeatures(Current_Anthro_Features_Provided)

        # Check Current_Anthro_Features
        required_fields = [
            "Type", "Subtype", "Overlap_Status", "Returned",
            "Subtype_As_Modified"
        ]
        no_null_fields = None
        expected_fcs = None
        ccslib.CheckPolygonInput(Current_Anthro_Features_Provided,
                                 required_fields, expected_fcs, no_null_fields)

        # Update message
        arcpy.AddMessage("Copying Current_Anthro_Features to project "
                         "geodatabase")

        # Copy Current_Anthro_Features to geodatabase
        provided_input = Current_Anthro_Features_Provided
        parameter_name = CURRENT_ANTHRO_FEATURES
        Current_Anthro_Features = ccslib.AdoptParameter(provided_input,
                                                        parameter_name,
                                                        preserve_existing=True)

    else:
        # Update message
        arcpy.AddMessage("Merging all clipped anthropogenic features to "
                         "create the Current_Anthro_Features layer")

        # Merge features (selecting only polygon features)
        fileList = arcpy.ListFeatureClasses("Anthro*Clip",
                                            feature_type="Polygon")
        out_name = CURRENT_ANTHRO_FEATURES
        Current_Anthro_Features = ccslib.MergeFeatures(fileList, out_name)

    # Simplify fields. critical to remove any fields named
    # 'Surface_Disturbance' in Current_Anthro_Features before joining
    # Proposed_Surface_Disturbance to create Projected_Anthro_Features
    allowable_fields = [
        "Type", "Subtype", "SubtypeID", "Subtype_As_Modified",
        "Overlap_Status", "Returned"
    ]
    ccslib.SimplifyFields(Current_Anthro_Features, allowable_fields)

    # Remove subtypes from Current_Anthro_Features
    feature = Current_Anthro_Features
    subtypes = arcpy.da.ListSubtypes(feature)
    try:
        if len(subtypes) == 1 and subtypes[0]['SubtypeField'] == '':
            pass
        else:
            for subtype in subtypes:
                arcpy.RemoveSubtype_management(feature, subtype)
                arcpy.AddMessage("Subtype removed")
    except arcpy.ExecuteError:
        arcpy.AddMessage("Could not remove Subtype Domain from "
                         "Current_Anthro_Features")

    # Add Domains for Type and Subtype
    try:
        arcpy.RemoveDomainFromField_management(feature, "Type")
        arcpy.AssignDomainToField_management(feature, "Type", "Type")
    except arcpy.ExecuteError:
        arcpy.AddMessage("Could not update Type Domain for "
                         "Current_Anthro_Features")
    try:
        arcpy.RemoveDomainFromField_management(feature, "Subtype")
        arcpy.AssignDomainToField_management(feature, "Subtype", "Subtype")
    except arcpy.ExecuteError:
        arcpy.AddMessage("Could not update Subtype Domain for "
                         "Current_Anthro_Features")
    try:
        arcpy.RemoveDomainFromField_management(feature, "Subtype_As_Modified")
        arcpy.AssignDomainToField_management(feature, "Subtype", "Subtype")
    except arcpy.ExecuteError:
        arcpy.AddMessage("Could not update Type Domain for "
                         "Current_Anthro_Features")

    # Calculate Current_Anthro_Disturbance
    extent_fc = Analysis_Area
    anthro_features = Current_Anthro_Features
    term = ccsStandard.DebitTerms[0]
    Current_Anthro_Disturbance = ccslib.CalcAnthroDist(extent_fc,
                                                       anthro_features,
                                                       emptyRaster,
                                                       AnthroAttributeTable,
                                                       term)
    Current_Anthro_Disturbance.save(CURRENT_ANTHRO_DISTURBANCE)

    # Update message
    arcpy.AddMessage("Current_Anthro_Disturbance Calculated")
    arcpy.AddMessage("Removing any anthropogenic features from the "
                     "Current_Anthro_Features layer that will be "
                     "replaced or upgraded from one subtype to another "
                     "by the debit project")

    # Calculate post-project anthropogenic disturbance
    mod_field = "Overlap_Status"
    removed_code = "Removed"
    subtype_mod_field = "Subtype_As_Modified"
    out_name = PROJECTED_ANTHRO_FEATURES
    Projected_Anthro_Features = ccslib.SelectProposed(
        Current_Anthro_Features, ELIGIBLE_PROPOSED_FEATURES, mod_field,
        removed_code, subtype_mod_field, out_name)

    # Simplify fields (do not remove Surface Disturbance or Reclassified
    # Subtype field for use in SelectPermanent())
    allowable_fields = [
        "Type", "Subtype", "SubtypeID", "Overlap_Status", "Returned",
        "Subtype_As_Modified"
        "Surface_Disturbance", "Reclassified_Subtype"
    ]
    ccslib.SimplifyFields(Projected_Anthro_Features, allowable_fields)

    # Calculate Projected_Anthro_Disturbance
    extent_fc = Analysis_Area
    anthro_features = Projected_Anthro_Features
    term = ccsStandard.DebitTerms[1]
    Projected_Anthro_Disturbance = ccslib.CalcAnthroDist(
        extent_fc, anthro_features, emptyRaster, AnthroAttributeTable, term)
    Projected_Anthro_Disturbance.save(PROJECTED_ANTHRO_DISTURBANCE)

    # Update message
    arcpy.AddMessage("Projected_Anthro_Disturbance Calculated")
    arcpy.AddMessage("Creating Permanent Anthro Features and calculating "
                     "disturbance")

    # Calculate permanent anthropogenic disturbance
    # Select permanent anthropogenic features from Projected Anthro Features
    mod_field = "Overlap_Status"
    returned_field = "Returned"
    subtype_mod_field = "Subtype_As_Modified"
    duration_field = "Surface_Disturbance"
    permanent_codes = ["Term_Reclassified", "Permanent"]
    reclass_code = "Term_Reclassified"
    reclass_subtype_field = "Reclassified_Subtype"
    out_name = PERMANENT_ANTHRO_FEATURES
    Permanent_Anthro_Features = ccslib.SelectPermanent(
        Current_Anthro_Features, ELIGIBLE_PROPOSED_FEATURES, mod_field,
        returned_field, subtype_mod_field, duration_field, permanent_codes,
        reclass_code, reclass_subtype_field, out_name)

    # Simplify fields
    allowable_fields = [
        "Type", "Subtype", "SubtypeID", "Overlap_Status", "Returned",
        "Subtype_As_Modified"
        "Surface_Disturbance", "Reclassified_Subtype"
    ]
    ccslib.SimplifyFields(Permanent_Anthro_Features, allowable_fields)

    # Calculate Permanent Anthro Disturbance
    extent_fc = Analysis_Area
    anthro_features = Permanent_Anthro_Features
    term = ccsStandard.DebitTerms[2]
    Permanent_Anthro_Disturbance = ccslib.CalcAnthroDist(
        extent_fc, anthro_features, emptyRaster, AnthroAttributeTable, term)
    Permanent_Anthro_Disturbance.save(PERMANENT_ANTHRO_DISTURBANCE)

    # Update message
    arcpy.AddMessage("Permanent_Anthro_Disturbance Calculated")

    # Calculate local scale modifiers for Current, Projected, and
    # Permanent condition
    extent_fc = Analysis_Area
    terms = ccsStandard.DebitTerms
    for term in terms:
        anthro_disturbance = term + "_Anthro_Disturbance"
        ccslib.CalcModifiers(extent_fc, inputDataPath, Dist_Lek,
                             anthro_disturbance, term)

    # Calculate impact intensity for debit project
    try:
        ccslib.calcDebitImpact(inputDataPath)

        # Add debit project impact to map
        layerFile = ccsStandard.getLayerFile("Debit_Project_Impact.lyr")
        ccslib.AddToMap("Debit_Project_Impact", layerFile, zoom_to=True)
    except:
        pass

    # Update message
    arcpy.AddMessage("Creating Map Units layer")

    # Create Map_Units layer
    Project_Area = DEBIT_PROJECT_AREA
    out_name = MAP_UNITS
    Map_Units = ccslib.CreateMapUnits(Project_Area, out_name)

    # Update message
    arcpy.AddMessage("Creating pre-defined map units of Wet Meadows")

    # Intersect the Map_Units layer with the NV Wet Meadows layer
    in_feature = ccsStandard.Wet_Meadows
    field_name = "Meadow"
    na_value = "No Meadow"
    ccslib.CreatePreDefinedMapUnits(Map_Units, in_feature, field_name,
                                    na_value)

    # Update message
    arcpy.AddMessage("Creating pre-defined map units of PJ")

    # Intersect the Map_Units layer with the Phase III PJ layer
    in_feature = ccsStandard.PJ_Phase_III
    field_name = "Conifer_Phase"
    ccslib.CreatePreDefinedMapUnits(Map_Units, in_feature, field_name)

    # Update message
    arcpy.AddMessage("Creating pre-defined map units of proposed surface "
                     "disturbance")

    # Intersect the Map_Units layer with the proposed surface disturbance
    in_features = ELIGIBLE_PROPOSED_FEATURES
    ccslib.CreatePreDefinedMapUnits(Map_Units, in_features, field_name)

    # Remove unwanted fields from Map Units feature class
    allowable_fields = [
        "Disturbance_Type", "BROTEC", "Conifer_Phase", "Meadow"
    ]
    ccslib.SimplifyFields(Map_Units, allowable_fields)

    # Populate empty attributes with Indirect
    feature = Map_Units
    fieldName = "Disturbance_Type"
    where_clause = "{} = ''".format(
        arcpy.AddFieldDelimiters(feature, fieldName))
    with arcpy.da.UpdateCursor(feature, fieldName, where_clause) as cursor:
        for row in cursor:
            row[0] = "Indirect"
            cursor.updateRow(row)

    # Add Map_Units to map
    layerFile = ccsStandard.getLayerFile("Map_Units.lyr")
    ccslib.AddToMap(Map_Units, layerFile)

    # Add fields Map_Unit_ID, Map_Unit_Name, and Precip to map unit
    input_feature = Map_Units
    fields = ["Map_Unit_ID", "Map_Unit_Name", "Notes"]
    fieldTypes = ["SHORT", "TEXT", "TEXT"]
    ccslib.AddFields(input_feature, fields, fieldTypes, copy_existing=True)

    # Add Domains to Map_Units layer
    # Create Domain for Map_Unit_ID attributes
    input_feature = Map_Units
    domainName = "Map_Unit_ID"
    range_low = 0
    range_high = 10000
    ccslib.AddRangeDomain(input_feature, workspace, domainName, range_low,
                          range_high)

    # Create Domain for Meadow attributes
    featureList = [Map_Units]
    domainName = "Meadow"
    codeList = ["Altered", "Unaltered", "No Meadow"]
    ccslib.AddCodedTextDomain(featureList, workspace, domainName, codeList)

    # Clean up
    arcpy.Delete_management("in_memory")

    # Save map document and exit
    if arcpy.ListInstallations()[0] == 'arcgispro':
        p = arcpy.mp.ArcGISProject("CURRENT")
        p.save()
    else:
        mxd = arcpy.mapping.MapDocument("CURRENT")
        mxd.save()
def main():
    try:
        xStep = 'Starting script...'
        import os, sys, timeit, time, csv, shutil
        import datetime
        from time import gmtime, strftime
        from shutil import copyfile
        startTime = timeit.default_timer()
        startFtime = strftime("%Y-%m-%dT%H:%M:%S")
        print 'Started at {0}'.format(time.ctime())
        xFlag = 0
        import arcpy
        from openpyxl import Workbook
        # Set paths
        sys.path.append(
            "//ccgisfiles01m/gisdata/prdba/crupdates/CCPythonLib/Appl/")

        # Get parameters from config file
        import getConfig, mongoJobUpdate
        emailList = getConfig.main('user', 'crscl', 'email')
        emailFrom = getConfig.main('globalEmail', 'from')

        # Import Mailer3 and set arrays
        import Mailer3
        successMsg = []
        failedMsg = []

        arcpy.env.overwriteOutput = True
        arcpy.env.qualifiedFieldNames = True

        #-----------------------------------------------------------------------
        # This is temporary just until zip'n'ship is ready on new server
        # Copy ccgisfiles01m-crscl files back to ccgis1-crscl
        xStep = "copy files from ccgisfiles01m back to ccgis1 for zip'n'ship"
        print xStep
        dirSource = getConfig.main('user', 'crscl', 'path', 'outWorkspace')
        dirTarget = getConfig.main('user', 'crscl', 'path',
                                   'outWorkspaceCCGIS1')

        source = os.listdir(dirSource)
        for i in source:
            shutil.copy(dirSource + i, dirTarget + i)

        successMsg.append('<br>Shapefiles copied back to CCGIS1')
        #-----------------------------------------------------------------------

        # Update FTP site
        ftpUpdateIni = getConfig.main('globalPath', 'ftpUpdateIniCCGIS1')
        xStep = 'write to crupdates FTP file'
        ftpUpdateFile = open(ftpUpdateIni, 'a')
        ftpUpdateFile.write('#crscl#\n')
        ftpUpdateFile.close()
        successMsg.append('<br>FTP site set to update')

        #-----------------------------------------------------------------------
        # Get current date for file name
        today = (str(datetime.datetime.now())).split()[0]

        #-----------------------------------------------------------------------
        # Start SCL Change Report
        #-----------------------------------------------------------------------

        # Set variables for the environment
        workspace = getConfig.main('user', 'crscl', 'path', 'workspace')[:-1]
        prsclGdbWorkspace = getConfig.main('user', 'crscl', 'path',
                                           'workspace')[:-1]
        newIntFileGdb = prsclGdbWorkspace + "\\InternetFile.gdb"
        newIntFile = newIntFileGdb + "\\internetFile"
        oldIntFileGdb = prsclGdbWorkspace + "\\InternetFile_LastMonth.gdb"
        oldIntFile = oldIntFileGdb + "\\internetFile"
        chgrepGdb = workspace + "\\ChangeReport.gdb"
        intFileNew = chgrepGdb + "\\internetFileNew"
        intFileOld = chgrepGdb + "\\internetFileOld"
        delete = chgrepGdb + "\\deletes"
        change = chgrepGdb + "\\changes"
        changeOld = chgrepGdb + "\\changes_old"
        changeReport = chgrepGdb + "\\change_report"
        changeReportSort = chgrepGdb + "\\change_report_sort"
        changeFreq = chgrepGdb + "\\changes_freq"
        deleteFreq = chgrepGdb + "\\deletes_freq"
        changeReportWkspace = getConfig.main('user', 'crscl', 'path',
                                             'changeReportWkspace')

        xStep = 'Delete last months GDB and add layers and tables from Internet Files'
        if arcpy.Exists(chgrepGdb):
            arcpy.Delete_management(chgrepGdb, "")
            print "\n", arcpy.GetMessages()
        arcpy.CreateFileGDB_management(workspace, "ChangeReport.gdb",
                                       "CURRENT")
        print "\n", arcpy.GetMessages()
        arcpy.TableToTable_conversion(newIntFile, chgrepGdb, "internetFileNew")
        print "\n", arcpy.GetMessages()
        arcpy.DeleteField_management(
            intFileNew,
            "FNODE;TNODE;FELEV;TELEV;LMailZip;RMailZip;STRSUF;LEFT_COUNTY;RIGHT_COUNTY;LEFT_STATE;RIGHT_STATE;LEFT_TRACT;RIGHT_TRACT;LEFT_BLOCK;RIGHT_BLOCK;LEFT_ZIP;RIGHT_ZIP;LEFT_LOC;RIGHT_LOC;LEFT_MCD;RIGHT_MCD;LEFT_TRACTBLKGRP;RIGHT_TRACTBLKGRP;FR_X;FR_LAT;FR_Y;FR_LONG;FMAPBASIC;FMAPSUF;TO_X;TO_LAT;TO_Y;TO_LONG;TMAPBASIC;TMAPSUF;STRCLASS;FFDAD;TFDAD"
        )
        print "\n", arcpy.GetMessages()
        arcpy.TableToTable_conversion(oldIntFile, chgrepGdb, "internetFileOld")
        print "\n", arcpy.GetMessages()
        arcpy.DeleteField_management(
            intFileOld,
            "FNODE;TNODE;FELEV;TELEV;LMailZip;RMailZip;STRSUF;LEFT_COUNTY;RIGHT_COUNTY;LEFT_STATE;RIGHT_STATE;LEFT_TRACT;RIGHT_TRACT;LEFT_BLOCK;RIGHT_BLOCK;LEFT_ZIP;RIGHT_ZIP;LEFT_LOC;RIGHT_LOC;LEFT_MCD;RIGHT_MCD;LEFT_TRACTBLKGRP;RIGHT_TRACTBLKGRP;FR_X;FR_LAT;FR_Y;FR_LONG;FMAPBASIC;FMAPSUF;TO_X;TO_LAT;TO_Y;TO_LONG;TMAPBASIC;TMAPSUF;STRCLASS;FFDAD;TFDAD"
        )
        print "\n", arcpy.GetMessages()

        xStep = 'Remove domains from fields'
        arcpy.RemoveDomainFromField_management(intFileNew, "STRDIR")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileNew, "STRTYPE")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileNew, "LEFT_PLACE")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileNew, "RIGHT_PLACE")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileNew, "FSTRDIR")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileNew, "TSTRDIR")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileOld, "STRDIR")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileOld, "STRTYPE")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileOld, "LEFT_PLACE")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveDomainFromField_management(intFileOld, "RIGHT_PLACE")
        print "\n", arcpy.GetMessages()

        xStep = 'Remove extra spaces from STRNAME fields on new and old internetFile'
        arcpy.CalculateField_management(intFileNew, "STRDIR",
                                        "!strdir!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileNew, "STRNAME",
                                        "!strname!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileNew, "STRTYPE",
                                        "!strtype!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileNew, "FSTRNAME",
                                        "!fstrname!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileNew, "F2STRNAME",
                                        "!f2strname!.strip()", "PYTHON_9.3",
                                        "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileNew, "F3STRNAME",
                                        "!f3strname!.strip()", "PYTHON_9.3",
                                        "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileNew, "TSTRNAME",
                                        "!tstrname!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileNew, "T2STRNAME",
                                        "!t2strname!.strip()", "PYTHON_9.3",
                                        "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileNew, "T3STRNAME",
                                        "!t3strname!.strip()", "PYTHON_9.3",
                                        "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "STRDIR",
                                        "!strdir!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "STRNAME",
                                        "!strname!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "STRTYPE",
                                        "!strtype!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "FSTRNAME",
                                        "!fstrname!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "F2STRNAME",
                                        "!f2strname!.strip()", "PYTHON_9.3",
                                        "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "F3STRNAME",
                                        "!f3strname!.strip()", "PYTHON_9.3",
                                        "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "TSTRNAME",
                                        "!tstrname!.strip()", "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "T2STRNAME",
                                        "!t2strname!.strip()", "PYTHON_9.3",
                                        "")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(intFileOld, "T3STRNAME",
                                        "!t3strname!.strip()", "PYTHON_9.3",
                                        "")
        print "\n", arcpy.GetMessages()

        xStep = 'Add attribute indexes based on CENSUSID field'
        arcpy.AddIndex_management(intFileNew, "CENSUSID", "ArcNewIndex",
                                  "UNIQUE", "ASCENDING")
        print "\n", arcpy.GetMessages()
        arcpy.AddIndex_management(intFileOld, "CENSUSID", "ArcOldIndex",
                                  "UNIQUE", "ASCENDING")
        print "\n", arcpy.GetMessages()

        xStep = 'Add new fields for change report'
        arcpy.AddField_management(intFileNew, "A", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(intFileNew, "S", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(intFileNew, "R", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(intFileNew, "NINE", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(intFileNew, "J", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(intFileNew, "T", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(intFileNew, "M", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(intFileOld, "NINE", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(intFileOld, "D", "TEXT", "", "", "1", "",
                                  "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()

        xStep = 'Make Table View for internetFileNew for Add Join'
        arcpy.MakeTableView_management(intFileNew, "intFileNew_view")
        print "\n", arcpy.GetMessages()

        xStep = 'Add Join between new and old internetFile to calculate A, S, R, NINE, and J fields'
        arcpy.AddJoin_management("intFileNew_view", "CENSUSID", intFileOld,
                                 "CENSUSID", "KEEP_ALL")
        print "\n", arcpy.GetMessages()

        xStep = 'Set variable for codeblock to calculate A field'
        aCodeblock = """def CalcField(CidOld):
            if str(CidOld) == 'None':
                return 'A'
            else:
                return ''"""

        xStep = 'Calculate A field on internetFileNew'
        arcpy.CalculateField_management(
            "intFileNew_view", "A", "CalcField(!internetFileOld.CENSUSID!)",
            "PYTHON_9.3", aCodeblock)
        print "\n", arcpy.GetMessages()

        # Set variable for codeblock to calculate S field
        sCodeblock = """def CalcField(StrDirNew,StrNameNew,StrTypeNew,StrDirOld,StrNameOld,StrTypeOld,Afield):
            if ((StrDirNew != StrDirOld) or (StrNameNew != StrNameOld) or (StrTypeNew != StrTypeOld)) and (Afield != 'A'):
                return 'S'
            else:
                return ''"""

        xStep = 'Calculate S field on internetFileNew'
        arcpy.CalculateField_management(
            "intFileNew_view", "S",
            "CalcField(!internetFileNew.STRDIR!,!internetFileNew.STRNAME!,!internetFileNew.STRTYPE!,!internetFileOld.STRDIR!,!internetFileOld.STRNAME!,!internetFileOld.STRTYPE!,!internetFileNew.A!)",
            "PYTHON_9.3", sCodeblock)
        print "\n", arcpy.GetMessages()

        xStep = 'Set variable for codeblock to calculate R field'
        rCodeblock = """def CalcField(LfaddNew,LtaddNew,RfaddNew,RtaddNew,LfaddOld,LtaddOld,RfaddOld,RtaddOld,Afield):
            if ((LfaddNew != LfaddOld) or (LtaddNew != LtaddOld) or (RfaddNew != RfaddOld) or (RtaddNew != RtaddOld)) and (Afield != 'A'):
                return 'R'
            else:
                return ''"""

        xStep = 'Calcuate R field on internetFileNew'
        arcpy.CalculateField_management(
            "intFileNew_view", "R",
            "CalcField(!internetFileNew.LFADD!,!internetFileNew.LTADD!,!internetFileNew.RFADD!,!internetFileNew.RTADD!,!internetFileOld.LFADD!,!internetFileOld.LTADD!,!internetFileOld.RFADD!,!internetFileOld.RTADD!,!internetFileNew.A!)",
            "PYTHON_9.3", rCodeblock)
        print "\n", arcpy.GetMessages()

        # Set variable for codeblock to calculate J field
        jCodeblock = """def CalcField(LplaceNew,RplaceNew,LplaceOld,RplaceOld,nscodeNew,nscodeOld,Afield):
            if ((LplaceNew != LplaceOld) or (RplaceNew != RplaceOld) or (nscodeNew != nscodeOld)) and (Afield != 'A'):
                return 'J'
            else:
                return ''"""

        xStep = 'Calculate J field on internetFileNew'
        arcpy.CalculateField_management(
            "intFileNew_view", "J",
            "CalcField(!internetFileNew.LEFT_PLACE!,!internetFileNew.RIGHT_PLACE!,!internetFileOld.LEFT_PLACE!,!internetFileOld.RIGHT_PLACE!,!internetFileNew.NSCODE!,!internetFileOld.NSCODE!,!internetFileNew.A!)",
            "PYTHON_9.3", jCodeblock)
        print "\n", arcpy.GetMessages()

        xStep = 'Set variable for codeblock to calculate T field'
        tCodeblock = """def CalcField(fNew,f2New,f3New,tNew,t2New,t3New,fOld,f2Old,f3Old,tOld,t2Old,t3Old):
            if (fNew == fOld or fNew == f2Old or fNew == f3Old) and (f2New == f2Old or f2New == fOld or f2New == f3Old) and (f3New == f3Old or f3New == f2Old or f3New == fOld) and (fOld == fNew or fOld == f2New or fOld == f3New) and (f2Old == f2New or f2Old == fNew or f2Old == f3New) and (f3Old ==f3New or f3Old == fNew or f3Old == f2New) and (tNew == tOld or tNew == t2Old or tNew == t3Old) and (t2New == t2Old or t2New == tOld or t2New == t3Old) and (t3New == t3Old or t3New == t2Old or t3New == tOld) and (tOld == tNew or tOld == t2New or tOld == t3New) and (t2Old == t2New or t2Old == tNew or t2Old == t3New) and (t3Old == t3New or t3Old == tNew or t3Old == t2New):
                return ''
            else:
                return 'T'"""

        xStep = 'Calculate T field on internetFileNew'
        arcpy.CalculateField_management(
            "intFileNew_view", "T",
            "CalcField(!internetFileNew.FSTRNAME!,!internetFileNew.F2STRNAME!,!internetFileNew.F3STRNAME!,!internetFileNew.TSTRNAME!,!internetFileNew.T2STRNAME!,!internetFileNew.T3STRNAME!,!internetFileOld.FSTRNAME!,!internetFileOld.F2STRNAME!,!internetFileOld.F3STRNAME!,!internetFileOld.TSTRNAME!,!internetFileOld.T2STRNAME!,!internetFileOld.T3STRNAME!)",
            "PYTHON_9.3", tCodeblock)
        print "\n", arcpy.GetMessages()

        xStep = 'Set variable for codeblock to calculate M field'
        mCodeblock = """def CalcField(fstrnameNew,f2strnameNew,f3strnameNew,tstrnameNew,t2strnameNew,t3strnameNew,tfield):
            if (tfield == 'T' and fstrnameNew != f2strnameNew and str(f2strnameNew) != 'None') or (tfield == 'T' and tstrnameNew != t2strnameNew and str(t2strnameNew) != 'None'):
                return 'M'
            elif (tfield == 'T' and fstrnameNew != f3strnameNew and str(f3strnameNew) != 'None') or (tfield == 'T' and tstrnameNew != t3strnameNew and str(t3strnameNew) != 'None'):
                return 'M'
            else:
                return ''"""

        xStep = 'Calculate M field on internetFileNew'
        arcpy.CalculateField_management(
            "intFileNew_view", "M",
            "CalcField(!internetFileNew.FSTRNAME!,!internetFileNew.F2STRNAME!,!internetFileNew.F3STRNAME!,!internetFileNew.TSTRNAME!,!internetFileNew.T2STRNAME!,!internetFileNew.T3STRNAME!,!internetFileNew.T!)",
            "PYTHON_9.3", mCodeblock)
        print "\n", arcpy.GetMessages()

        xStep = 'Set variable for codeblock to recalculate T field'
        t2Codeblock = """def CalcField(Afield,Tfield):
            if (Afield == 'A'):
                return ''
            else:
                return Tfield"""

        xStep = 'Recalculate T field on internetFileNew'
        arcpy.CalculateField_management(
            "intFileNew_view", "T",
            "CalcField(!internetFileNew.A!,!internetFileNew.T!)", "PYTHON_9.3",
            t2Codeblock)
        print "\n", arcpy.GetMessages()

        xStep = 'Remove Join between new and old internetFile'
        arcpy.RemoveJoin_management("intFileNew_view")
        print "\n", arcpy.GetMessages()

        xStep = 'Make Table View for internetFileOld for Add Join'
        arcpy.MakeTableView_management(intFileOld, "intFileOld_view")
        print "\n", arcpy.GetMessages()

        xStep = 'Add Join between internetFileOld and internetFileNew to calculate D field'
        arcpy.AddJoin_management("intFileOld_view", "CENSUSID", intFileNew,
                                 "CENSUSID", "KEEP_ALL")
        print "\n", arcpy.GetMessages()

        xStep = 'Set variable for codeblock to calculate D field'
        dCodeblock = """def CalcField(CidNew):
            if str(CidNew) == 'None':
                return 'D'
            else:
                return ''"""

        xStep = 'Calculate D field on internetFileOld'
        arcpy.CalculateField_management(
            "intFileOld_view", "D", "CalcField(!internetFileNew.CENSUSID!)",
            "PYTHON_9.3", dCodeblock)
        print "\n", arcpy.GetMessages()

        xStep = 'Remove Join between internetFileOld and internetFileNew'
        arcpy.RemoveJoin_management("intFileOld_view")
        print "\n", arcpy.GetMessages()

        xStep = 'Export deletes to new feature class'
        arcpy.TableToTable_conversion(intFileOld, chgrepGdb, "deletes",
                                      """ "D" = 'D'""")
        print "\n", arcpy.GetMessages()

        xStep = 'Export fields with changes to new feature class'
        arcpy.TableToTable_conversion(
            intFileNew, chgrepGdb, "changes",
            """ "A" = 'A' or "S" = 'S' or "R" = 'R' or "J" = 'J' or "T" = 'T' or "M" = 'M' """
        )
        print "\n", arcpy.GetMessages()

        xStep = 'Set variable for codeblock to calculate NINE field'
        nineCodeblock = """def CalcField(nscodeBoth):
            if (nscodeBoth == 9 ):
                return '9'
            else:
                return ''"""

        xStep = 'Calculate NINE field on changes and deletes'
        arcpy.CalculateField_management(change, "NINE", "CalcField(!nscode!)",
                                        "PYTHON_9.3", nineCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(delete, "NINE", "CalcField(!nscode!)",
                                        "PYTHON_9.3", nineCodeblock)
        print "\n", arcpy.GetMessages()

        xStep = 'Add Attribute Index on change table for CENSUSID field'
        arcpy.AddIndex_management(change, "CENSUSID", "cidIndex", "UNIQUE",
                                  "ASCENDING")
        print "\n", arcpy.GetMessages()

        xStep = 'Add Join between internetFileOld and changes, Select records when CENSUSID is equal, Remove Join, and Copy Rows to new table'
        arcpy.AddJoin_management("intFileOld_view", "CENSUSID", change,
                                 "CENSUSID", "KEEP_COMMON")
        print "\n", arcpy.GetMessages()
        arcpy.SelectLayerByAttribute_management(
            "intFileOld_view", "NEW_SELECTION",
            "internetFileOld.CENSUSID = changes.CENSUSID")
        print "\n", arcpy.GetMessages()
        arcpy.RemoveJoin_management("intFileOld_view")
        print "\n", arcpy.GetMessages()
        arcpy.CopyRows_management("intFileOld_view", changeOld)
        print "\n", arcpy.GetMessages()

        xStep = 'Merge changes and deletes'
        arcpy.Merge_management([change, changeOld, delete], changeReport)
        print "\n", arcpy.GetMessages()

        xStep = 'Set variable for codeblock to remove nulls from fields'
        nullCodeblock = """def CalcField(Record):
            if str(Record) == 'None':
                return ''
            else:
                return Record"""

        xStep = 'Remove null records in A, S, R, NINE, J, T, M, D'
        arcpy.CalculateField_management(changeReport, "A", "CalcField(!a!)",
                                        "PYTHON_9.3", nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "S", "CalcField(!s!)",
                                        "PYTHON_9.3", nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "R", "CalcField(!r!)",
                                        "PYTHON_9.3", nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "NINE",
                                        "CalcField(!nine!)", "PYTHON_9.3",
                                        nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "J", "CalcField(!j!)",
                                        "PYTHON_9.3", nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "T", "CalcField(!t!)",
                                        "PYTHON_9.3", nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "M", "CalcField(!m!)",
                                        "PYTHON_9.3", nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "D", "CalcField(!d!)",
                                        "PYTHON_9.3", nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "FSTRDIR",
                                        "CalcField(!fstrdir!)", "PYTHON_9.3",
                                        nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "FSTRTYPE",
                                        "CalcField(!fstrtype!)", "PYTHON_9.3",
                                        nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "TSTRDIR",
                                        "CalcField(!tstrdir!)", "PYTHON_9.3",
                                        nullCodeblock)
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(changeReport, "TSTRTYPE",
                                        "CalcField(!tstrtype!)", "PYTHON_9.3",
                                        nullCodeblock)
        print "\n", arcpy.GetMessages()

        xStep = 'Add new field and concantenate all change fields'
        arcpy.AddField_management(changeReport, "ACTION", "TEXT", "", "", "8",
                                  "", "NULLABLE", "NON_REQUIRED")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(
            changeReport, "ACTION",
            "\"{0}{1}{2}{3}{4}{5}{6}{7}\".format(!a!,!d!,!s!,!r!,!t!,!m!,!nine!,!j!)",
            "PYTHON_9.3", "")
        print "\n", arcpy.GetMessages()

        xStep = 'Delete fields not needed in final output'
        arcpy.DeleteField_management(changeReport, "A;S;R;NINE;J;T;M;D")
        print "\n", arcpy.GetMessages()

        xStep = 'Sort the fields'
        arcpy.Sort_management(changeReport, changeReportSort,
                              [("STRNAME", "ASCENDING"),
                               ("CENSUSID", "ASCENDING"),
                               ("ACTION", "ASCENDING")])
        print "\n", arcpy.GetMessages()

        result = arcpy.GetCount_management(changeReportSort)
        count = int(result.getOutput(0))
        successMsg.append(
            '<br><br>There were {0} SCL changes last week'.format(count))

        if count == 0:
            print 'no changes'
            xStep = 'Copy no changes file to excel'
            copyfile(
                changeReportWkspace + 'no_changes.xls', changeReportWkspace +
                'change_reports_for_users/{0}_scl_change_report.xls'.format(
                    today))

        else:
            xStep = 'Export change_report_sort to Excel'
            arcpy.TableToExcel_conversion(
                changeReportSort, changeReportWkspace +
                'change_reports_for_users/{0}_scl_change_report.xls'.format(
                    today))
            print 'Report exported to Excel'

        successMsg.append('<br><br>SCL Change Code Descriptions:')
        successMsg.append('<br>A - Add')
        successMsg.append('<br>D - Delete')
        successMsg.append('<br>S - Street Attribute Change')
        successMsg.append('<br>R - Address Range Change')
        successMsg.append('<br>T - Cross Street Change')
        successMsg.append('<br>M - Multiple Cross Streets')
        successMsg.append('<br>9 - Nine Record')
        successMsg.append('<br>J - Jurisdiction Change')

        #-----------------------------------------------------------------------
        # Start 9 Record Change Report
        #-----------------------------------------------------------------------

        # Script variables
        workspace = getConfig.main('user', 'crscl', 'path', 'workspace')
        reportWorkspace = getConfig.main('user', 'crscl', 'path',
                                         'reportsWkspace')
        nineChangeGdb = workspace + "Nine_Change.gdb/"
        newNine = workspace + 'InternetFile.gdb/Data/sclnine'
        oldNine = workspace + 'InternetFile_LastMonth.gdb/Data/sclnine'
        nineNew = nineChangeGdb + "sclnine_new"
        nineOld = nineChangeGdb + "sclnine_old"
        nineAdds = nineChangeGdb + "additions"
        nineDeletes = nineChangeGdb + "deletes"
        nineChange = nineChangeGdb + "nine_change"
        changeReportWkspace = getConfig.main('user', 'crscl', 'path',
                                             'changeReportWkspace')

        xStep = 'Delete and recreate output GDB and add sclnine layers'
        if arcpy.Exists(nineChangeGdb):
            arcpy.Delete_management(nineChangeGdb, "")
            print "\n", arcpy.GetMessages()
        arcpy.CreateFileGDB_management(workspace, "Nine_Change", "CURRENT")
        print "\n", arcpy.GetMessages()
        arcpy.FeatureClassToFeatureClass_conversion(newNine, nineChangeGdb,
                                                    "sclnine_new")
        print "\n", arcpy.GetMessages()
        arcpy.FeatureClassToFeatureClass_conversion(oldNine, nineChangeGdb,
                                                    "sclnine_old")
        print "\n", arcpy.GetMessages()

        xStep = 'Join field between sclnine_new and sclnine_old to find additions'
        arcpy.JoinField_management(nineNew, "ALIASID", nineOld, "ALIASID")
        print "\n", arcpy.GetMessages()
        arcpy.Select_analysis(nineNew, nineChangeGdb + "additions",
                              "ALIASID_1 IS NULL")
        print "\n", arcpy.GetMessages()
        arcpy.DeleteField_management(
            nineAdds,
            "PARCEL;STRPRETYPE;PARCEL_1;CENSUSID_1;ALIASID_1;ADDR_1;SIDE_1;STRDIR_1;STRPRETYPE_1;STRNAME_1;STRTYPE_1"
        )
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(nineAdds, "CHANGE", "TEXT", "", "", "10", "",
                                  "NULLABLE")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(nineAdds, "CHANGE", "'Add'",
                                        "PYTHON_9.3")
        print "\n", arcpy.GetMessages()
        arcpy.DeleteField_management(
            nineNew,
            "PARCEL_1;CENSUSID_1;ALIASID_1;ADDR_1;SIDE_1;STRDIR_1;STRPRETYPE_1;STRNAME_1;STRTYPE_1"
        )
        print "\n", arcpy.GetMessages()

        xStep = 'Join field between sclnine_old and sclnine_new to find deletes'
        arcpy.JoinField_management(nineOld, "ALIASID", nineNew, "ALIASID")
        print "\n", arcpy.GetMessages()
        arcpy.Select_analysis(nineOld, nineChangeGdb + "deletes",
                              "ALIASID_1 IS NULL")
        print "\n", arcpy.GetMessages()
        arcpy.DeleteField_management(
            nineDeletes,
            "PARCEL;STRPRETYPE;PARCEL_1;CENSUSID_1;ALIASID_1;ADDR_1;SIDE_1;STRDIR_1;STRPRETYPE_1;STRNAME_1;STRTYPE_1"
        )
        print "\n", arcpy.GetMessages()
        arcpy.AddField_management(nineDeletes, "CHANGE", "TEXT", "", "", "10",
                                  "", "NULLABLE")
        print "\n", arcpy.GetMessages()
        arcpy.CalculateField_management(nineDeletes, "CHANGE", "'Delete'",
                                        "PYTHON_9.3")
        print "\n", arcpy.GetMessages()

        xStep = 'Merge Additions and Deletes to create new output layer'
        arcpy.Merge_management([nineAdds, nineDeletes],
                               nineChangeGdb + "nine_change")
        print "\n", arcpy.GetMessages()

        xStep = 'Only do report if there were changes'
        fc = getConfig.main('user', 'crscl', 'path',
                            'workspace') + 'Nine_Change.gdb/nine_change'

        if arcpy.management.GetCount(fc)[0] == "0":
            print 'No changes'
            successMsg.append('<br><br>No nine record changes last week.')

            xStep = 'Copy no changes file to excel'
            copyfile(
                changeReportWkspace + 'no_changes.xls', changeReportWkspace +
                'change_reports_for_users/{0}_nine_change_report.xls'.format(
                    today))

        else:
            xStep = 'Create a variable to remove null values from fields'
            nullCodeblock = """def CalcField(Record):
                if str(Record) == 'None':
                    return ''
                else:
                    return Record"""

            xStep = 'Calculate fields to remove null values'
            arcpy.CalculateField_management(nineChange, "STRDIR",
                                            "CalcField(!strdir!)",
                                            "PYTHON_9.3", nullCodeblock)
            print "\n", arcpy.GetMessages()
            arcpy.CalculateField_management(nineChange, "STRTYPE",
                                            "CalcField(!strtype!)",
                                            "PYTHON_9.3", nullCodeblock)
            print "\n", arcpy.GetMessages()

            xStep = 'Export to table to create report'
            arcpy.TableToTable_conversion(nineChange, nineChangeGdb,
                                          "nine_change_table")
            print "\n", arcpy.GetMessages()

            xStep = 'Create CSV'
            result = arcpy.GetCount_management(nineChangeGdb +
                                               'nine_change_table')
            count = int(result.getOutput(0))
            successMsg.append(
                '<br><br>There were {0} SCL_Nine changes last week.'.format(
                    count))

            xStep = 'Export change_report_sort to Excel'
            arcpy.TableToExcel_conversion(
                nineChangeGdb + 'nine_change_table', changeReportWkspace +
                'change_reports_for_users/{0}_nine_change_report.xls'.format(
                    today))
            print 'Report exported to Excel'

        #-----------------------------------------------------------------------
        print '\nSucceeded at ' + time.ctime()
        successMsg.append('<br><br>Change reports successfully created!')

    except:
        xFlag = 1
        err = sys.exc_info()[1]
        xStep = '{0} {1}'.format(err, xStep)
        print 'Error occured: {0}'.format(xStep)
        failedMsg.append(xStep)

    finally:
        if len(failedMsg) != 0:
            emailSubject = '##--ERROR--## SCL Weekly Change Report'
            jobStatus = 'error'
        if len(failedMsg) == 0:
            emailSubject = 'SCL Weekly Change Report'
            jobStatus = 'ok'

        # Call Mailer3 and send email
        Mailer3.mailReport(successMsg, failedMsg, emailList, emailFrom,
                           emailSubject)

        endTime = timeit.default_timer()
        seconds = endTime - startTime
        minutes = seconds / 60
        endFtime = strftime("%Y-%m-%dT%H:%M:%S")
        print '\nRuntime {0} seconds ({1} minutes)'.format(
            round(seconds, 2), round(minutes, 2))
        print ' '
        # Call mongoJobUpdate to update mongo
        mongoJobUpdate.mongoUpdate('scl - crscl change report', startTime,
                                   endTime, startFtime, endFtime, jobStatus,
                                   failedMsg)
Exemple #7
0
    u'SSURFM_Q2', u'SSURFM_ST2', u'SSURF_E2A', u'SSURF_E2B', u'SSURF_E2C',
    u'TTTEX_2C', u'TTTEX_2B', u'TTTEX_2A', u'TSURFM_2', u'TSURFM_Q2',
    u'TSURFM_ST2', u'TSURF_E2A', u'TSURF_E2B', u'TSURF_E2C', u'COMREL2_3',
    u'TDEC_3', u'PRTFLG_3', u'TTEX_3C', u'TTEX_3B', u'TTEX_3A', u'SURFM_3',
    u'SURFM_Q3', u'SURFM_ST3', u'SURF_E3A', u'SURF_E3B', u'SURF_E3C',
    u'BEDROCK_3', u'STTEX_3C', u'STTEX_3B', u'STTEX_3A', u'SSURFM_3',
    u'SSURFM_Q3', u'SSURFM_ST3', u'SSURF_E3A', u'SSURF_E3B', u'SSURF_E3C',
    u'TTTEX_3C', u'TTTEX_3B', u'TTTEX_3A', u'TSURFM_3', u'TSURFM_Q3',
    u'TSURFM_ST3', u'TSURF_E3A', u'TSURF_E3B', u'TSURF_E3C', u'GEOP_1',
    u'GEOP_Q1', u'GEOP_ST1', u'GEOP_INZ1', u'GEOP_INZ1A', u'GEOP_SCM1A',
    u'GEOP_INZ1B', u'GEOP_SCM1B', u'GEOP_INZ1C', u'GEOP_SCM1C', u'GEOP_2',
    u'GEOP_Q2', u'GEOP_ST2', u'GEOP_INZ2', u'GEOP_INZ2A', u'GEOP_SCM2A',
    u'GEOP_INZ2B', u'GEOP_SCM2B', u'GEOP_INZ2C', u'GEOP_SCM2C', u'GEOP_3',
    u'GEOP_Q3', u'GEOP_ST3', u'GEOP_INZ3', u'GEOP_INZ3A', u'GEOP_SCM3A',
    u'GEOP_INZ3B', u'GEOP_SCM3B', u'GEOP_INZ3C', u'GEOP_SCM3C', u'DRAIN_1',
    u'DRAIN_SEP', u'DRAIN_2', u'SLPLL_1', u'SLPUL_1', u'SLPLL_2', u'SLPUL_2',
    u'SLPSTB_CLS', u'RDSTB_FLG', u'SFCERO_POT', u'LSSED_CLS', u'SESED_CLS',
    u'DRAIN_1A', u'DRAIN_SEP1', u'DRAIN_1B', u'DRAIN_2A', u'DRAIN_SEP2',
    u'DRAIN_2B', u'DRAIN_3A', u'DRAIN_SEP3', u'DRAIN_3B'
]

import arcpy

table = r'\\spatialfiles.bcgov\work\env\esd\eis\tei\TEI_Working\BAuger\contractor_package_20200319\Operational_Data_6581.gdb\TEI_Long_Tbl'

for f in fieldslist:
    try:
        arcpy.RemoveDomainFromField_management(table, f)
    except:
        print("{} does not have a domain").format(f)
    # --------------------------------------------------------------------------- Remove domains from fields if they exist
    # If Watershed Runoff Curve Number Tools were previously used on specified watershed, domain will remain on fields
    vectorLanduse = watershedFD + os.sep + wsName + "_Landuse"
    desc = arcpy.Describe(watershedGDB_path)
    listOfDomains = []

    domains = desc.Domains

    for domain in domains:
        listOfDomains.append(domain)

    del desc, domains

    if "LandUse_Domain" in listOfDomains:
        try:
            arcpy.RemoveDomainFromField_management(vectorLanduse, "LANDUSE")
        except:
            pass
    if "Condition_Domain" in listOfDomains:
        try:
            arcpy.RemoveDomainFromField_management(vectorLanduse, "CONDITION")
        except:
            pass
    if "Hydro_Domain" in listOfDomains:
        try:
            arcpy.RemoveDomainFromField_management(wsSoils, "HYDGROUP")
        except:
            pass

    del listOfDomains
Exemple #9
0
def main():
    # GET PARAMETER VALUES
    Analysis_Area = arcpy.GetParameterAsText(0)
    Dist_Lek = arcpy.GetParameterAsText(1)
    Current_Anthro_Features_Provided = arcpy.GetParameterAsText(2)  # optional
    Project_Folder = arcpy.GetParameterAsText(3)
    Project_Name = arcpy.GetParameterAsText(4)  # optional

    # DEFINE DIRECTORIES & PATH NAMES FOR FOLDERS & GBDs
    # Get the pathname to this script
    scriptPath = sys.path[0]
    arcpy.AddMessage("Script folder: " + scriptPath)
    # Construct pathname to workspace
    workspace = arcpy.Describe(Analysis_Area).path
    arcpy.AddMessage("Project geodatabase: " + workspace)
    # Instantiate a ccsStandard object
    ccsStandard = ccslib.ccsStandard(workspace, scriptPath)

    # ENVIRONMENT SETTINGS
    # Set workspaces
    arcpy.env.workspace = workspace
    scratch_folder = os.path.join(arcpy.Describe(workspace).path, 'scratch')
    if arcpy.Exists(scratch_folder):
        pass
    else:
        arcpy.CreateFolder_management(
            arcpy.Describe(workspace).path, 'scratch')
    arcpy.env.scratchWorkspace = scratch_folder
    # Overwrite outputs
    arcpy.env.overwriteOutput = True

    # DEFINE GLOBAL VARIABLES
    AnthroAttributeTable = ccsStandard.AnthroAttributeTable
    emptyRaster = ccsStandard.EmptyRaster
    inputDataPath = ccsStandard.InputDataPath
    # Filenames for feature classes or rasters used by this script
    MAP_UNITS = "Map_Units"
    ANALYSIS_AREA = "Analysis_Area"  # provided
    CURRENT_ANTHRO_FEATURES = "Current_Anthro_Features"
    CREDIT_PROJECT_AREA = "Credit_Project_Area"
    PROPOSED_MODIFIED_FEATURES = "Proposed_Modified_Features"
    # Filenames for feature classes or rasters created by this script
    CURRENT_ANTHRO_DISTURBANCE = "Current_Anthro_Disturbance"
    PROJECTED_ANTHRO_DISTURBANCE = "Projected_Anthro_Disturbance"
    MAP_UNITS_DISSOLVE = "Map_Units_Dissolve"
    CURRENT_MGMT_CAT = "Current_Mgmt_Cat"
    CURRENT_WMZ = "Current_WMZ"
    CURRENT_PMU = "Current_PMU"
    CURRENT_PRECIP = "Current_Precip"

    # ------------------------------------------------------------------------

    # FUNCTION CALLS
    # Check out Spatial Analyst extension
    ccslib.CheckOutSpatialAnalyst()

    # Check Analysis_Area
    feature = Analysis_Area
    expected_fcs = [MAP_UNITS, ANALYSIS_AREA, CREDIT_PROJECT_AREA]
    ccslib.CheckPolygonInput(feature, expected_fcs=expected_fcs)

    # Set up flag for projects that propose to modify anthro features
    includes_anthro_mod = False

    # Check for existence of 'Proposed_Modified_Features'
    if arcpy.Exists(PROPOSED_MODIFIED_FEATURES):
        # Update flag
        includes_anthro_mod = True

    # Copy Dist_Lek to geodatabase

    # Create Current_Anthro_Features layer, or copy provided into geodatabase
    if Current_Anthro_Features_Provided:
        # Clear selection, if present
        ccslib.ClearSelectedFeatures(Current_Anthro_Features_Provided)

        # Check Current_Anthro_Features
        feature = Current_Anthro_Features_Provided
        required_fields = ["Type", "Subtype"]
        no_null_fields = None
        expected_fcs = None
        ccslib.CheckPolygonInput(feature, required_fields, expected_fcs,
                                 no_null_fields)

        # Update message
        arcpy.AddMessage("Copying Current_Anthro_Features to project "
                         "geodatabase")

        # Copy Current_Anthro_Features to geodatabase
        provided_input = Current_Anthro_Features_Provided
        parameter_name = CURRENT_ANTHRO_FEATURES
        preserve_existing = True
        Current_Anthro_Features = ccslib.AdoptParameter(
            provided_input, parameter_name, preserve_existing)

    else:
        # Update message
        arcpy.AddMessage("Merging all clipped anthropogenic features to "
                         "create the Current_Anthro_Features layer")

        # Merge features (selecting only polygon features)
        fileList = arcpy.ListFeatureClasses("Anthro*Clip",
                                            feature_type="Polygon")
        out_name = CURRENT_ANTHRO_FEATURES
        Current_Anthro_Features = ccslib.MergeFeatures(fileList, out_name)

    # Simplify fields
    allowable_fields = ["Type", "Subtype", "SubtypeID", "Subtype_As_Modified"]
    ccslib.SimplifyFields(Current_Anthro_Features, allowable_fields)

    # Remove subtypes from Current_Anthro_Features
    feature = Current_Anthro_Features
    try:
        subtypes = arcpy.da.ListSubtypes(feature)
        for subtype in subtypes:
            arcpy.RemoveSubtype_management(feature, subtype)
            arcpy.AddMessage("Subtype removed")
    except arcpy.ExecuteError:
        arcpy.AddMessage("Could not remove subtypes from "
                         "Current_Anthro_Features")

    # Add Domains for Type and Subtype
    arcpy.RemoveDomainFromField_management(feature, "Type")
    try:
        domainName = "Type"
        arcpy.CreateDomain_management(workspace, domainName,
                                      "Valid " + domainName + "s", "TEXT",
                                      "CODED")
        typeList = [
            row[0]
            for row in arcpy.da.SearchCursor(AnthroAttributeTable, "Type")
        ]
        for code in typeList:
            arcpy.AddCodedValueToDomain_management(workspace, domainName, code,
                                                   code)
    except arcpy.ExecuteError:
        arcpy.AddMessage("Could not add domains for "
                         "Current_Anthro_Features")

    arcpy.AssignDomainToField_management(feature, "Type", "Type")

    arcpy.RemoveDomainFromField_management(feature, "Subtype")
    arcpy.TableToDomain_management(AnthroAttributeTable, "Subtype", "Subtype",
                                   workspace, "Subtype",
                                   "Valid anthropogenic subtypes", "REPLACE")
    arcpy.AssignDomainToField_management(feature, "Subtype", "Subtype")

    # Update Message
    arcpy.AddMessage("Calculating Current Anthropogenic Disturbance")

    # Calculate Current_Anthro_Disturbance
    extent_fc = Analysis_Area
    anthro_features = Current_Anthro_Features
    term = ccsStandard.CreditTerms[0]
    Current_Anthro_Disturbance = ccslib.CalcAnthroDist(extent_fc,
                                                       anthro_features,
                                                       emptyRaster,
                                                       AnthroAttributeTable,
                                                       term)
    Current_Anthro_Disturbance.save(CURRENT_ANTHRO_DISTURBANCE)

    # Update message
    arcpy.AddMessage("Current_Anthro_Disturbance Calculated")

    # If the project proposes to modify existing anthropogenic features,
    # calculate post-project anthropogenic disturbance (uplift)
    if includes_anthro_mod:
        # Calculate uplift
        extent_fc = Analysis_Area
        anthro_features = Current_Anthro_Features
        term = ccsStandard.CreditTerms[1]
        field = "Subtype_As_Modified"
        Projected_Anthro_Disturbance = ccslib.CalcAnthroDist(
            extent_fc, anthro_features, emptyRaster, AnthroAttributeTable,
            term, field)
        Projected_Anthro_Disturbance.save(PROJECTED_ANTHRO_DISTURBANCE)

        # Update message
        arcpy.AddMessage("Projected_Anthro_Disturbance Calculated")
        arcpy.AddMessage("Creating pre-defined map units of PJ")

        Map_Units = MAP_UNITS

        if len(arcpy.ListFields(Map_Units, "Conifer_Phase")) == 0:
            # Create pre-defined map units for PJ
            # Intersect the Map_Units layer with the PJ layer
            in_feature = ccsStandard.PJ_Phases
            field_name = "Conifer_Phase"
            ccslib.CreatePreDefinedMapUnits(Map_Units, in_feature, field_name)

            # Remove unwanted fields from Map Units feature class
            allowable_fields = [
                "Conifer_Phase", "Map_Unit_ID", "Map_Unit_Name", "Meadow",
                "Notes", "Indirect"
            ]
            ccslib.SimplifyFields(Map_Units, allowable_fields)

        # Update message
        arcpy.AddMessage("Merging indirect benefits area and map units layer")

        # Combine the Map Units layer and Indirect Impact Layer
        indirect_benefit_area = CREDIT_PROJECT_AREA
        mgmt_map_units = Map_Units
        Map_Units = ccslib.AddIndirectBenefitArea(indirect_benefit_area,
                                                  mgmt_map_units)

        # Add Map Units layer to map document
        layerFile = ccsStandard.getLayerFile("Map_Units.lyr")
        ccslib.AddToMap(Map_Units, layerFile)

    else:
        # Add Indirect field to Map Units layer and populate with False
        # Add field "Indirect"
        feature = MAP_UNITS
        fieldsToAdd = ["Indirect"]
        fieldTypes = ["TEXT"]
        ccslib.AddFields(feature, fieldsToAdd, fieldTypes)

        # Update field to equal "False"
        with arcpy.da.UpdateCursor(feature, fieldsToAdd) as cursor:
            for row in cursor:
                row[0] = "False"
                cursor.updateRow(row)

    # Calculate local scale modifiers for Current condition
    extent_fc = Analysis_Area
    anthro_disturbance = CURRENT_ANTHRO_DISTURBANCE
    term = ccsStandard.CreditTerms[0]
    ccslib.CalcModifiers(extent_fc, inputDataPath, Dist_Lek,
                         anthro_disturbance, term)

    # Calculate local scale modifiers for Projected condition
    # Determine which anthropogenic disturbance raster to use
    extent_fc = Analysis_Area
    if arcpy.Exists(PROJECTED_ANTHRO_DISTURBANCE):
        anthro_disturbance = PROJECTED_ANTHRO_DISTURBANCE
    else:
        anthro_disturbance = CURRENT_ANTHRO_DISTURBANCE
    term = ccsStandard.CreditTerms[1]
    ccslib.CalcModifiers(extent_fc,
                         inputDataPath,
                         Dist_Lek,
                         anthro_disturbance,
                         term,
                         PJ_removal=True)

    # Update message
    arcpy.AddMessage("Dissolving all multi-part map units to create "
                     "Map_Units_Dissolve")

    # Dissolve Map Units
    allowable_fields = [
        "Map_Unit_ID", "Map_Unit_Name", "Meadow", "Conifer_Phase", "BROTEC",
        "Indirect"
    ]
    out_name = MAP_UNITS_DISSOLVE
    anthro_features = Current_Anthro_Features
    Map_Units_Dissolve = ccslib.DissolveMapUnits(MAP_UNITS, allowable_fields,
                                                 out_name, anthro_features)

    # Update message
    arcpy.AddMessage("Adding Map_Units_Dissolve to map")

    # Add layer to map document
    feature = Map_Units_Dissolve
    layerFile = ccsStandard.getLayerFile("Map_Units.lyr")
    ccslib.AddToMap(feature, layerFile, zoom_to=True)

    # Update message
    arcpy.AddMessage("Calculating area in acres for each map unit")

    # Calculate Area
    ccslib.CalcAcres(Map_Units_Dissolve)

    # Initialize a list to track proportion feature classes
    prop_fcs = []

    # Update message
    arcpy.AddMessage("Calculating Proportion within each precipitation zone")

    # Calculate Proportion of each map unit in each Precip Zone
    in_feature = os.path.join(inputDataPath, "Precip")
    out_feature_class = CURRENT_PRECIP
    field_name = "Precip_Proportion"
    ccslib.CalcProportion(Map_Units_Dissolve, in_feature, out_feature_class,
                          field_name)
    prop_fcs.append(out_feature_class)

    # Update message
    arcpy.AddMessage("Calculating Management Importance Factor")

    # Calculate Proportion of each map unit in each Management Category
    in_feature = os.path.join(inputDataPath, "Mgmt_Cat")
    out_feature_class = CURRENT_MGMT_CAT
    field_name = "Mgmt_Proportion"
    ccslib.CalcProportion(Map_Units_Dissolve, in_feature, out_feature_class,
                          field_name)
    prop_fcs.append(out_feature_class)

    # Update message
    arcpy.AddMessage("Evaluating WAFWA Management Zone")

    # Calculate Proportion in each map unit in each WAFWA Zone
    in_feature = os.path.join(inputDataPath, "NV_WAFWA")
    out_feature_class = CURRENT_WMZ
    field_name = "WMZ_Proportion"
    ccslib.CalcProportion(Map_Units_Dissolve, in_feature, out_feature_class,
                          field_name)
    prop_fcs.append(out_feature_class)

    # Update message
    arcpy.AddMessage("Evaluating Priority Management Unit")

    # Calculate Proportion in each map unit in each PMU
    in_feature = os.path.join(inputDataPath, "NV_PMU")
    out_feature_class = CURRENT_PMU
    field_name = "PMU_Proportion"
    ccslib.CalcProportion(Map_Units_Dissolve, in_feature, out_feature_class,
                          field_name)
    prop_fcs.append(out_feature_class)

    # Delete unnecessary fields in proportion feature classes
    allowable_fields = [
        "Map_Unit_ID", "Management", "Mgmt_zone", "PMU_NAME", "Precip",
        "Mgmt_Proportion", "WMZ_Proportion", "PMU_Proportion",
        "Precip_Proportion"
    ]
    for feature in prop_fcs:
        ccslib.SimplifyFields(feature, allowable_fields)

    # Set processing extent to Map_Units layer
    arcpy.env.extent = arcpy.Describe(Map_Units_Dissolve).extent

    # Calculate the average HSI values per map unit for each map unit
    HSIseasons = ccsStandard.HSISeasons
    for season in HSIseasons:
        # Update message
        arcpy.AddMessage("Summarizing " + season + " HSI")

        # Calculate zonal statistics for each map unit
        inZoneData = Map_Units_Dissolve
        inValueRaster = os.path.join(inputDataPath, season + "_HSI")
        zoneField = "Map_Unit_ID"
        outTable = "ZonalStats_" + season + "_HSI"
        ccslib.CalcZonalStats(inZoneData, zoneField, inValueRaster, outTable)

        # Join the zonal statistic to the Map Units Dissolve table
        fieldName = season + "_HSI"
        ccslib.JoinMeanToTable(inZoneData, outTable, zoneField, fieldName)

    # Update message
    arcpy.AddMessage("Calculating PJ cover per map unit")

    # Calculate the average pinon-juniper cover per map unit
    inZoneData = Map_Units_Dissolve
    inValueRaster = os.path.join(inputDataPath, "PJ_Cover")
    zoneField = "Map_Unit_ID"
    outTable = "ZonalStats_PJCover"
    ccslib.CalcZonalStats(inZoneData, zoneField, inValueRaster, outTable)

    # Join the zonal statistic to the Map Units Dissolve table
    fieldName = "PJ_Cover"
    ccslib.JoinMeanToTable(inZoneData, outTable, zoneField, fieldName)

    # Calculate the average seasonal modifier values per map unit and
    # join to Map_Unit_Dissolve table
    terms = ccsStandard.CreditTerms
    seasons = ccsStandard.Seasons
    for term in terms:
        for season in seasons:
            # Update message
            arcpy.AddMessage("Summarizing " + term + "_Local_" + season)

            # Calculate zonal statistics for each map unit
            inZoneData = Map_Units_Dissolve
            inValueRaster = term + "_Local_" + season
            zoneField = "Map_Unit_ID"
            outTable = "ZonalStats_" + term + season
            ccslib.CalcZonalStats(inZoneData, zoneField, inValueRaster,
                                  outTable)

            # Join the zonal statistic to the Map Units Dissolve table
            fieldName = term + "_" + season
            ccslib.JoinMeanToTable(inZoneData, outTable, zoneField, fieldName)

    # Calculate impact intensity for credit project
    try:
        ccslib.calcCreditBenefit(inputDataPath, includes_anthro_mod)

        # Add credit project quality to map
        layerFile = ccsStandard.getLayerFile("Credit_Project_Benefit.lyr")
        ccslib.AddToMap("Credit_Quality", layerFile, zoom_to=True)
    except:
        pass

    # Remove uplift modifier for map units that do not qualify
    # Select map units of Indirect if project involves anthro
    # feature modification
    if includes_anthro_mod:
        feature = MAP_UNITS_DISSOLVE
        arcpy.MakeFeatureLayer_management(feature, "lyr")
        where_clause = """({} = '{}') AND ({} <> {} OR {} <> {} OR {} <> {})""".format(
            arcpy.AddFieldDelimiters(feature, "Indirect"), "True",
            arcpy.AddFieldDelimiters(feature, "Projected_Breed"),
            arcpy.AddFieldDelimiters(feature, "Current_Breed"),
            arcpy.AddFieldDelimiters(feature, "Projected_LBR"),
            arcpy.AddFieldDelimiters(feature, "Current_LBR"),
            arcpy.AddFieldDelimiters(feature, "Projected_Winter"),
            arcpy.AddFieldDelimiters(feature, "Current_Winter"))
        arcpy.SelectLayerByAttribute_management(feature, "NEW_SELECTION",
                                                where_clause)
        test = arcpy.GetCount_management(feature)
        count = int(test.getOutput(0))
        if count > 0:
            # Update message
            arcpy.AddMessage("Confirming removal of PJ cover credits meet "
                             "eligibility criteria (if applicable)")

            # Substitute Projected_Anthro_Disturbance if it exists
            extent_fc = Analysis_Area
            if arcpy.Exists(PROJECTED_ANTHRO_DISTURBANCE):
                anthroDisturbance = PROJECTED_ANTHRO_DISTURBANCE
            else:
                anthroDisturbance = CURRENT_ANTHRO_DISTURBANCE

            # Repeat calculation of modifiers w/o PJ_uplift
            term = ccsStandard.CreditTerms[1]
            ccslib.CalcModifiers(extent_fc,
                                 inputDataPath,
                                 Dist_Lek,
                                 anthroDisturbance,
                                 term,
                                 PJ_removal=False,
                                 suffix="noPJ")

            # Repeat joins to table
            for season in seasons:
                # Calculate zonal statistics for each map unit
                inZoneData = Map_Units_Dissolve
                inValueRaster = term + "_Local_" + season + "_noPJ"
                zoneField = "Map_Unit_ID"
                outTable = "ZonalStats_" + term + season
                ccslib.CalcZonalStats(inZoneData, zoneField, inValueRaster,
                                      outTable)

                # Join the zonal statistic to the Map Units Dissolve table
                fieldName = term + "_" + season + "_noPJ"
                ccslib.JoinMeanToTable(inZoneData, outTable, zoneField,
                                       fieldName)

                # Overwrite Projected seasonal local scale scores
                overwrite_field = ccsStandard.CreditTerms[1] + "_" + season
                with arcpy.da.UpdateCursor(
                        feature, [fieldName, overwrite_field]) as cursor:
                    for row in cursor:
                        row[1] = row[0]
                        cursor.updateRow(row)

                # Clean up
                arcpy.DeleteField_management(feature, fieldName)

        # Clean up
        arcpy.SelectLayerByAttribute_management(feature, "CLEAR_SELECTION")
        arcpy.Delete_management("lyr")

    # Add transect field to Map_Units_Dissolve
    fields = ["Transects"]
    fieldTypes = ["SHORT"]
    ccslib.AddFields(Map_Units_Dissolve, fields, fieldTypes)

    # Export data to Excel
    input_Tables = [
        MAP_UNITS_DISSOLVE, CURRENT_MGMT_CAT, CURRENT_WMZ, CURRENT_PMU,
        CURRENT_PRECIP
    ]
    for table in input_Tables:
        ccslib.ExportToExcel(table, Project_Folder, Project_Name)

    # Clean up
    arcpy.Delete_management("in_memory")

    # Save map document
    if arcpy.ListInstallations()[0] == 'arcgispro':
        p = arcpy.mp.ArcGISProject("CURRENT")
        p.save()
    else:
        mxd = arcpy.mapping.MapDocument("CURRENT")
        mxd.save()
Exemple #10
0
    # --------------------------------------------------------------------------- Remove domains from fields if they exist
    desc = arcpy.Describe(watershedGDB_path)
    listOfDomains = []
    domains = desc.Domains
    for domain in domains:
        listOfDomains.append(domain)
    del desc, domains

    if arcpy.Exists(landuse):
        if "LandUse_Domain" in listOfDomains:
            if len(arcpy.ListFields(landuse, "LANDUSE")) > 0:
                my_field = arcpy.ListFields(landuse, "LANDUSE")[0]
                if len(my_field.domain):
                    if my_field.domain == "LandUse_Domain":
                        arcpy.RemoveDomainFromField_management(
                            landuse, "LANDUSE")
                del my_field
        if "Condition_Domain" in listOfDomains:
            if len(arcpy.ListFields(landuse, "CONDITION")) > 0:
                my_field = arcpy.ListFields(landuse, "CONDITION")[0]
                if len(my_field.domain):
                    if my_field.domain == "Condition_Domain":
                        arcpy.RemoveDomainFromField_management(
                            landuse, "CONDITION")
                del my_field

    if arcpy.Exists(wsSoils):
        if "Hydro_Domain" in listOfDomains:
            if len(arcpy.ListFields(wsSoils, inputField)) > 0:
                my_field = arcpy.ListFields(wsSoils, inputField)[0]
                if len(my_field.domain):
 def detruireDomaineAttribut(self, workspace):
 #-------------------------------------------------------------------------------------
     """
     Permet de détruire tous les domaines existants dans la Géodatabase.
     
     Paramètres:
     -----------
     workspace   : Nom de la géodatabase ou les domaines seront détruits.
     
     """
     
     #Envoyer un message
     arcpy.AddMessage(" ")
     arcpy.AddMessage("- Détruire tous les domaines existants dans les classes de la Géodatabase")
     
     #Définir le workspace par défaut
     arcpy.env.workspace = workspace
     
     #Extraire la description de la Géodatabase
     #for fc in classe.split(","):
     for fc in arcpy.ListFeatureClasses():
         #Extraire les fields
         fields = arcpy.ListFields(fc)
         
         #Traiter tous les fields
         for field in fields:
             #Vérifier la présence d'un domaine
             if len(field.domain) > 0:
                 #Vérifier si l'attribut est présent dans la liste des attribut
                 #if field.name in attribut:
                 
                 #Afficher le message
                 arcpy.AddMessage(" RemoveDomainFromField_management('" + fc + "', '" + field.name + "')")
                 
                 #Détruire un domaine dans un attribut d'une classe
                 arcpy.RemoveDomainFromField_management(fc, field.name)
     
     #Envoyer un message
     arcpy.AddMessage(" ")
     arcpy.AddMessage("- Détruire tous les domaines existants dans la Géodatabase")
     
     #Extraire la description de la Géodatabase
     desc = arcpy.Describe(workspace)
     
     #Extraire tous les domaines existants de la Géodatabase
     domains = desc.domains
     
     #Traiter tous les domaines
     for domain in domains:
         #Vérifier si c'est un domaine
         #if "DOM_" in domain:
         
         #Afficher le message
         arcpy.AddMessage(" DeleteDomain_management('" + workspace + "', '" + domain + "')")
         
         try:
             #Détruire un domaine
             arcpy.DeleteDomain_management(workspace, domain)
         #Gestion des erreurs
         except Exception, err:
             #Afficher l'erreur
             arcpy.AddWarning(err.message)
Exemple #12
0
def AddRangeDomain(feature, workspace, domain_name, range_low, range_high):
    """
    Applies the range domain to the feature. Removes domain from any existing
    features if necessary.
    :param feature: a feature class
    :param workspace: the project's unique gdb
    :param domain_name: the name of the domain as a string
    :param range_low: integer or float
    :param range_high: integer or float
    :return: None
    """
    # Check for existence of domain; update domain if present, add domain if
    # not
    desc = arcpy.Describe(workspace)
    domains = desc.domains

    if domain_name in domains:
        arcpy.AddMessage(domain_name + " is already specified as a domain")
        try:
            # try removing from all fields in all feature classes
            existingFeatures = arcpy.ListFeatureClasses()
            for existingFeature in existingFeatures:
                fields = arcpy.ListFields(existingFeature)
                for field in fields:
                    if field.domain == domain_name:
                        table = os.path.join(workspace, existingFeature)
                        arcpy.RemoveDomainFromField_management(
                            table, field.name)
                        arcpy.AddMessage(domain_name +
                                         " domain removed from " +
                                         existingFeature + " " + field.name +
                                         " field")
                # try removing from all fields in all subtypes
                subtypes = arcpy.da.ListSubtypes(existingFeature)
                for stcode, stdict in list(subtypes.items()):
                    for stkey in list(stdict.keys()):
                        # if there is a Subtype Field
                        if not stdict['SubtypeField'] == '':
                            st_code = "'{}: {}'".format(stcode, stdict['Name'])
                        # if no Subtype Field, use "#" in RemoveDomainFromField
                        # for subtype_code
                        else:
                            st_code = "#"
                        if stkey == 'FieldValues':
                            fields = stdict[stkey]
                            for field, fieldvals in list(fields.items()):
                                # if field has a domain
                                if not fieldvals[1] is None:
                                    # and the domain is in our list
                                    if fieldvals[1].name == domain_name:
                                        # remove the domain
                                        arcpy.AddMessage(fieldvals[1].name +
                                                         " domain removed " +
                                                         "from " +
                                                         existingFeature +
                                                         " field: " + field +
                                                         " subtype: " +
                                                         st_code)
                                        arcpy.RemoveDomainFromField_management(
                                            existingFeature, field, st_code)
            arcpy.DeleteDomain_management(workspace, domain_name)
            arcpy.CreateDomain_management(workspace, domain_name,
                                          domain_name + " must be integer",
                                          "SHORT", "RANGE")
            arcpy.SetValueForRangeDomain_management(workspace, domain_name,
                                                    range_low, range_high)
            arcpy.AssignDomainToField_management(feature, domain_name,
                                                 domain_name)
            arcpy.AddMessage(domain_name + " domain updated")
        except arcpy.ExecuteError:
            arcpy.AddMessage(domain_name + " domain could not be updated")
    else:
        arcpy.CreateDomain_management(workspace, domain_name,
                                      domain_name + " must be integer",
                                      "SHORT", "RANGE")
        arcpy.SetValueForRangeDomain_management(workspace, domain_name,
                                                range_low, range_high)
        arcpy.AssignDomainToField_management(feature, domain_name, domain_name)
        arcpy.AddMessage(domain_name + " domain updated")
Exemple #13
0
def AddCodedTextDomain(feature_list,
                       workspace,
                       domain_name,
                       code_list,
                       assign_default=False,
                       populate_default=False):
    """
    Applies the code_list as a domain to the list of feature classes.
    Domain must be the same as the field name to which it is being applied.
    :param feature_list: list of feature classes
    :param workspace: the project's unique gdb
    :param domain_name: name of the domain as a string, must be same as name
    of field to which it is applied
    :param code_list: list of codes as strings
    :param assign_default: True to assign the first code in the code_list as
    default
    :param populate_default: True to populate existing features with the
    default code
    :return: None
    """
    # Create unique list from provided
    uniqueCodes = []
    for code in code_list:
        if code not in uniqueCodes:
            uniqueCodes.append(code)
    # Check for existence of domain; update domain if present, add domain if not
    desc = arcpy.Describe(workspace)
    domains = desc.domains
    if domain_name in domains:
        arcpy.AddMessage(domain_name + " is already specified as a domain")
        try:
            # try removing from all fields in all feature classes
            existingFeatures = arcpy.ListFeatureClasses()
            for existingFeature in existingFeatures:
                fields = arcpy.ListFields(existingFeature)
                for field in fields:
                    if field.domain == domain_name:
                        arcpy.RemoveDomainFromField_management(
                            existingFeature, field.name)
                        arcpy.AddMessage(domain_name +
                                         " domain removed from " +
                                         existingFeature + " " + field.name +
                                         " field")
                # try removing from all fields in all subtypes
                # Credit to:(https://community.esri.com/thread/
                # 198384-how-to-remove-domain-from-field-for-gdb)
                subtypes = arcpy.da.ListSubtypes(existingFeature)
                for stcode, stdict in list(subtypes.items()):
                    for stkey in list(stdict.keys()):
                        # if there is a Subtype Field
                        if not stdict['SubtypeField'] == '':
                            st_code = "'{}: {}'".format(stcode, stdict['Name'])
                        # if no Subtype Field, use "#" in RemoveDomainFromField
                        # for subtype_code
                        else:
                            st_code = "#"
                        if stkey == 'FieldValues':
                            fields = stdict[stkey]
                            for field, fieldvals in list(fields.items()):
                                # if field has a domain
                                if not fieldvals[1] is None:
                                    # and the domain is in our list
                                    if fieldvals[1].name == domain_name:
                                        # remove the domain
                                        arcpy.AddMessage(fieldvals[1].name +
                                                         " domain removed " +
                                                         "from " +
                                                         existingFeature +
                                                         " field: " + field +
                                                         " subtype: " +
                                                         st_code)
                                        arcpy.RemoveDomainFromField_management(
                                            existingFeature, field, st_code)

            arcpy.DeleteDomain_management(workspace, domain_name)
            arcpy.CreateDomain_management(workspace, domain_name,
                                          "Valid " + domain_name + "s", "TEXT",
                                          "CODED")

            for code in uniqueCodes:
                arcpy.AddCodedValueToDomain_management(workspace, domain_name,
                                                       code, code)
            for feature in feature_list:
                try:
                    arcpy.AssignDomainToField_management(
                        feature, domain_name, domain_name)
                    # Check to make sure subtypes exist
                    subtypes = arcpy.da.ListSubtypes(feature)
                    if len(subtypes
                           ) == 1 and subtypes[0]['SubtypeField'] == '':
                        pass
                    else:
                        st_codes = [
                            str(stcode)
                            for stcode, stdict in list(subtypes.items())
                        ]
                        arcpy.AssignDomainToField_management(
                            feature, domain_name, domain_name, st_codes)

                except arcpy.ExecuteError:
                    arcpy.AddMessage("--------------------------------"
                                     "\n" + domain_name +
                                     " domain for feature \n\n" +
                                     str(feature) + "\n\n" +
                                     "could not be updated. Use "
                                     "caution when populating attribute\n"
                                     "---------------------------------")
            arcpy.AddMessage(domain_name + " domain updated")

        except arcpy.ExecuteError:
            arcpy.AddMessage(domain_name + " domain could not be updated. Use "
                             "caution when populating attribute")
    else:
        arcpy.CreateDomain_management(workspace, domain_name,
                                      "Valid " + domain_name + "s", "TEXT",
                                      "CODED")
        for code in uniqueCodes:
            arcpy.AddCodedValueToDomain_management(workspace, domain_name,
                                                   code, code)
        for feature in feature_list:
            try:
                arcpy.AssignDomainToField_management(feature, domain_name,
                                                     domain_name)
                # Check to make sure subtypes exist
                subtypes = arcpy.da.ListSubtypes(feature)
                if len(subtypes) == 1 and subtypes[0]['SubtypeField'] == '':
                    pass
                else:
                    st_codes = [
                        str(stcode)
                        for stcode, stdict in list(subtypes.items())
                    ]
                    arcpy.AssignDomainToField_management(
                        feature, domain_name, domain_name, st_codes)

            except arcpy.ExecuteError:
                arcpy.AddMessage(domain_name +
                                 " domain could not be updated. Use "
                                 "caution when populating attribute")

        arcpy.AddMessage(domain_name + " domain updated")

    # Assign the first value as the default
    if assign_default:
        for feature in feature_list:
            subtypes = arcpy.da.ListSubtypes(feature)
            if len(subtypes) == 1 and subtypes[0]['SubtypeField'] == '':
                arcpy.AssignDefaultToField_management(feature, domain_name,
                                                      uniqueCodes[0])
            else:
                st_codes = [
                    str(stcode) for stcode, stdict in list(subtypes.items())
                ]
                arcpy.AssignDefaultToField_management(feature, domain_name,
                                                      uniqueCodes[0], st_codes)

    # Populate field with default values if Null
    if populate_default:
        arcpy.AddMessage("Populating default values")
        for feature in feature_list:
            where_clause = "{0} = '' OR {0} IS NULL".format(
                arcpy.AddFieldDelimiters(feature, domain_name))
            with arcpy.da.UpdateCursor(feature, domain_name,
                                       where_clause) as cursor:
                for row in cursor:
                    row[0] = uniqueCodes[0]
                    cursor.updateRow(row)
def getDomains(geodatabase, datasetList, configFile, dataType):
    # Logging
    if (enableLogging == "true"):
        # Setup logging
        logger, logMessage = setLogging(logFile)

    assignedDomains = []
    # Loop through the datasets
    for dataset in datasetList:
        # Setup the source and destination paths
        sourceDatasetPath = os.path.join(geodatabase, dataset)

        # If feature datasets
        if (dataType == "Feature Dataset"):
            # Get a list of the feature classes in the feature dataset
            featureClassList = arcpy.ListFeatureClasses("", "", dataset)

            # Change dataset name to be just name (remove user and schema if SDE database)
            splitDataset = dataset.split('.')
            dataset = splitDataset[-1]

            # Loop through the feature classes in the feature dataset
            for featureClass in featureClassList:
                # Change feature class name to be just name (remove user and schema if SDE database)
                splitDataset = featureClass.split('.')
                featureClass = splitDataset[-1]

                # Don't include _H or VW
                if ("_H" not in featureClass) and (
                        "VW" not in featureClass) and ("vw"
                                                       not in featureClass):
                    # Setup the source and destination paths
                    sourceDatasetPath = os.path.join(
                        geodatabase + "\\" + dataset, featureClass)

                    # List fields in feature class
                    fields = arcpy.ListFields(featureClass)

                    # Loop through fields
                    for field in fields:
                        # Check if field has domain
                        if field.domain != "":
                            # If configuration provided
                            if (configFile):
                                # Set CSV delimiter
                                csvDelimiter = ","

                                domain = field.domain
                                # Look through configuration file to see if domain exists
                                # Open the CSV file
                                with open(configFile, 'rb') as csvFile:
                                    # Read the CSV file
                                    rows = csv.reader(csvFile,
                                                      delimiter=csvDelimiter)

                                    # For each row in the CSV
                                    count = 0
                                    for row in rows:
                                        # Ignore the first line containing headers
                                        if (count > 0):
                                            originalDomain = row[0]
                                            duplicateDomain = row[1]
                                            # If duplicate domain is in config file
                                            if (field.domain == duplicateDomain
                                                ):
                                                arcpy.AddMessage(
                                                    "Reassigning domain on feature class "
                                                    + featureClass + " from " +
                                                    field.domain + " to " +
                                                    originalDomain +
                                                    " as it is duplicated...")
                                                # Logging
                                                if (enableLogging == "true"):
                                                    logger.info(
                                                        "Reassigning domain on feature class "
                                                        + featureClass +
                                                        " from " +
                                                        field.domain + " to " +
                                                        originalDomain +
                                                        " as it is duplicated..."
                                                    )

                                                # Check for subtypes on the dataset
                                                describeDataset = arcpy.Describe(
                                                    featureClass)
                                                defaultSubtype = describeDataset.defaultSubtypeCode
                                                # If a subtype exists
                                                if (defaultSubtype != -1):
                                                    # Get a list of subtypes on the dataset
                                                    datasetSubtypes = arcpy.da.ListSubtypes(
                                                        dataset)
                                                    for subtypeCode, subtypeDesc in datasetSubtypes.iteritems(
                                                    ):
                                                        # Remove existing domain
                                                        arcpy.RemoveDomainFromField_management(
                                                            sourceDatasetPath,
                                                            field.name,
                                                            subtypeCode)
                                                        # Re-assign domain to other domain for subtype
                                                        arcpy.AssignDomainToField_management(
                                                            sourceDatasetPath,
                                                            field.name,
                                                            originalDomain,
                                                            subtypeCode)
                                                        # Re-assign domain to other domain
                                                        arcpy.AssignDomainToField_management(
                                                            sourceDatasetPath,
                                                            field.name,
                                                            originalDomain, "")
                                                # Else no subtypes on dataset
                                                else:
                                                    # Re-assign domain to other domain
                                                    arcpy.AssignDomainToField_management(
                                                        sourceDatasetPath,
                                                        field.name,
                                                        originalDomain, "")

                                        count = count + 1

                                    # Add the domain to the list
                                    assignedDomains.append(domain)
                            else:
                                # Add the domain to the list
                                assignedDomains.append(field.domain)

        # If feature classes/tables/mosaics
        else:
            # List fields in feature class
            fields = arcpy.ListFields(dataset)

            # Change dataset name to be just name (remove user and schema if SDE database)
            splitDataset = dataset.split('.')
            dataset = splitDataset[-1]

            # Don't include _H or VW
            if ("_H" not in dataset) and ("VW"
                                          not in dataset) and ("vw"
                                                               not in dataset):
                # Loop through fields
                for field in fields:
                    # Check if field has domain
                    if field.domain != "":
                        # If configuration provided
                        if (configFile):
                            # Set CSV delimiter
                            csvDelimiter = ","

                            domain = field.domain
                            # Look through configuration file to see if domain exists
                            # Open the CSV file
                            with open(configFile, 'rb') as csvFile:
                                # Read the CSV file
                                rows = csv.reader(csvFile,
                                                  delimiter=csvDelimiter)

                                # For each row in the CSV
                                count = 0
                                for row in rows:
                                    # Ignore the first line containing headers
                                    if (count > 0):
                                        originalDomain = row[0]
                                        duplicateDomain = row[1]

                                        # If duplicate domain is in config file
                                        if (field.domain == duplicateDomain):
                                            arcpy.AddMessage(
                                                "Reassigning domain on feature class "
                                                + dataset + " from " +
                                                field.domain + " to " +
                                                originalDomain +
                                                " as it is duplicated...")
                                            # Logging
                                            if (enableLogging == "true"):
                                                logger.info(
                                                    "Reassigning domain on feature class "
                                                    + dataset + " from " +
                                                    field.domain + " to " +
                                                    originalDomain +
                                                    " as it is duplicated...")

                                            # Check for subtypes on the dataset
                                            describeDataset = arcpy.Describe(
                                                dataset)
                                            defaultSubtype = describeDataset.defaultSubtypeCode

                                            # If a subtype exists
                                            if (defaultSubtype != -1):
                                                # Get a list of subtypes on the dataset
                                                datasetSubtypes = arcpy.da.ListSubtypes(
                                                    dataset)
                                                for subtypeCode, subtypeDesc in datasetSubtypes.iteritems(
                                                ):
                                                    # Remove existing domain
                                                    arcpy.RemoveDomainFromField_management(
                                                        sourceDatasetPath,
                                                        field.name,
                                                        subtypeCode)
                                                    # Re-assign domain to other domain for subtype
                                                    arcpy.AssignDomainToField_management(
                                                        sourceDatasetPath,
                                                        field.name,
                                                        originalDomain,
                                                        subtypeCode)
                                                    # Re-assign domain to other domain
                                                    arcpy.AssignDomainToField_management(
                                                        sourceDatasetPath,
                                                        field.name,
                                                        originalDomain, "")
                                            # Else no subtypes on dataset
                                            else:
                                                # Re-assign domain to other domain
                                                arcpy.AssignDomainToField_management(
                                                    sourceDatasetPath,
                                                    field.name, originalDomain,
                                                    "")

                                            domain = originalDomain

                                    count = count + 1

                                # Add the domain to the list
                                assignedDomains.append(domain)
                        else:

                            # Add the domain to the list
                            assignedDomains.append(field.domain)

    # Return a list of assigned domains
    return assignedDomains
Exemple #15
0

descSelected=arcpy.Describe(Default_gdb+"\\tempSDE")
print("Name: {} Feature Class was successfully created".format(descSelected.name))
resultSelect = int(arcpy.GetCount_management(Default_gdb+"\\tempSDE").getOutput(0))
print("The record count for the SDE fault selection is " + str(resultSelect))

#Step 3 - Remove domain from SDE layer if its there so that domain issues don't end up in final version
#Before running this tool, make sure your dataset has been moved to a standalone database!

domains = ['QFaultMapScale','DipDirection','SlipSense',
           'QFaultSlipRate','QFaultConstraint_1','QFaultClass','QFaultAge']

for field in arcpy.ListFields(Default_gdb+"\\tempSDE"):
        if field.domain in domains:
            arcpy.RemoveDomainFromField_management(Default_gdb+"\\tempSDE",field.name)
            print "%s domain removed."%str(field.domain)


#Step 4 create lower case variables for Dip Direction and Slip Sense
arcpy.CalculateField_management(Default_gdb+"\\tempSDE", "DipDirection", "!DipDirection!.lower()", "PYTHON", "")
arcpy.CalculateField_management(Default_gdb+"\\tempSDE", "SlipSense", "!SlipSense!.lower()", "PYTHON", "")


# Step 5 - Process: Feature Class to Feature Class - Create a new feature class of the Hazard app data so that the original is not overwritten
arcpy.FeatureClassToFeatureClass_conversion(Qfaults_in_Haz_App, Default_gdb, 'HazFaults2')

descHaz = arcpy.Describe('HazFaults2')
print("Name: {} is a copy of the Hazards app feature service. It was successfully created.".format(descHaz.name))
resultHaz = int(arcpy.GetCount_management('HazFaults2').getOutput(0))
print("The record count for the Hazards App Fault Data is " + str(resultHaz))