def reportExtensionForQCGDB(singlePartPointErrors, singlePartLineErrors):
    # Get a count for the singlepart features (if they exist)
    # and append the count data to the end of the errorReportCSV.
    if Exists(singlePartPointErrors) and Exists(singlePartLineErrors):
        singlePartPointFeaturesName = returnFeatureClass(singlePartPointErrors)
        singlePartPointErrorsResult = GetCount_management(
            singlePartPointErrors)
        singlePartPointErrorsCount = int(
            singlePartPointErrorsResult.getOutput(0))

        singlePartLineFeaturesName = returnFeatureClass(singlePartLineErrors)
        singlePartLineErrorsResult = GetCount_management(singlePartLineErrors)
        singlePartLineErrorsCount = int(
            singlePartLineErrorsResult.getOutput(0))
        try:
            with open(errorReportCSV, 'a') as fHandle:
                fHandle.write(singlePartPointFeaturesName + ', ' +
                              str(singlePartPointErrorsCount) + '\n')
                fHandle.write(singlePartLineFeaturesName + ', ' +
                              str(singlePartLineErrorsCount) + '\n')
        except:
            print("There was an error writing to the file.")
    else:
        print("The Single Part output was not found.")
        print(
            "Will not add the Single Part information to the errors report csv."
        )
def writeStewardPointCounts():
    MakeFeatureLayer_management(originalPointsSource, originalPointsAsLayer)
    MakeFeatureLayer_management(simplifiedPointsSource, simplifiedPointsAsLayer)
    
    allStewardsDict = dict()
    
    #Programatically grab the stewards instead of manually listing them here
    newCursor = daSearchCursor(originalPointsAsLayer, ["OID@", "STEWARD"])
    
    for cursorItem in newCursor:
        allStewardsDict[cursorItem[1]] = 'True'
    
    if 'newCursor' in locals():
        try:
            del newCursor
        except:
            print("The cursor exists, but it could not be successfully removed.")
    else:
        print("The cursor has already been removed.")
    
    
    try:
        wHandle = open(outputFile,'w')
        columnNames = "StewardID , OriginalCount , SimplifiedCount\n"
        wHandle.write(columnNames)
        
        # For each steward in the list, get a count of all of the original centerline
        # points and the the simplified road centerlines points.
        # Next, write the data out to a text file in comma separated form.
        for stewardItem in allStewardsDict.keys():
            if stewardItem is not None:
                selectionQuery = """ "STEWARD" = '""" + stewardItem + """' """
                SelectLayerByAttribute_management(originalPointsAsLayer, selectionTypeToUse, selectionQuery)
                oCountResult = GetCount_management(originalPointsAsLayer)
                originalCount = int(oCountResult.getOutput(0))
                SelectLayerByAttribute_management(simplifiedPointsAsLayer, selectionTypeToUse, selectionQuery)
                sCountResult = GetCount_management(simplifiedPointsAsLayer)
                simplifiedCount = int(sCountResult.getOutput(0))
                strToWrite = "'" + stewardItem + "'" + ", " + str(originalCount) + ", " + str(simplifiedCount) + "\n"
                print("Writing " + strToWrite + " to the file: " + outputFile + ".")
                wHandle.write(strToWrite)
            else:
                print("The stewardItem is None, so it will be skipped.")
    
    except:
        errorInfo = sys.exc_info()[0]
        errorInfo1 = sys.exc_info()[1]
        print("An error occurred: " + str(errorInfo) + str(errorInfo1))
        try:
            wHandle.close()
        except:
            raise
        try:
            del errorInfo
            del errorInfo1
        except:
            pass
def reportExtensionForRAndHCheck(featuresToCheck):
    if Exists(featuresToCheck):
        featuresName = returnFeatureClass(featuresToCheck)
        errorsFromRAndH = 'RAndHErrorsAsFeatureLayer'
        MakeFeatureLayer_management(featuresToCheck, errorsFromRAndH)
        errorsFromRAndHResult = GetCount_management(errorsFromRAndH)
        errorsFromRAndHCount = int(errorsFromRAndHResult.getOutput(0))

        print("Roads & Highways Non-Monotonic Check output was found.")
        print(
            "Extending the errors report with information from the Roads & Highways Non-Monotonicity Check."
        )

        with open(errorReportCSV, 'a') as fHandle:
            fHandle.write('\n' + 'Roads & Highways checks follow: ' + '\n')
            fHandle.write(featuresName + ', ' + str(errorsFromRAndHCount) +
                          '\n')

        #errorsRHGDB = returnGDBOrSDEName(featuresToCheck)
        #errorsFeatureClass = returnFeatureClass(featuresToCheck)
        #previousWorkspace = env.workspace
        #env.workspace = errorsRHGDB

        #time.sleep(25)
        #print("Also adding ReviewUser and ReviewInfo text fields to the")
        #print("Roads & Highways Non-Monotonicity Check error output feature class.")
        #AddField_management(errorsFeatureClass, "OptionalInfo", "TEXT", "", "", 250, "ReviewingInfo", nullable)

        #env.workspace = previousWorkspace

    else:
        print("No Roads & Highways Non-Monotonic Check output found.")
        print("Will not add additional information to the errors report csv.")
def calculateMeasuresForLocalRoutes(routesToMeasure, subsetSelectionQuery):
    # Make a feature layer
    # Select it with the subsetSelectionQuery
    # If the number of selected features is at least 1
    # Then, run the calculateField_management calls for
    # the selected features.
    fcAsFeatureLayerForMeasuring = 'FCAsFeatureLayer_Measures'

    if Exists(fcAsFeatureLayerForMeasuring):
        Delete_management(fcAsFeatureLayerForMeasuring)
    else:
        pass

    MakeFeatureLayer_management(routesToMeasure, fcAsFeatureLayerForMeasuring)

    SelectLayerByAttribute_management(fcAsFeatureLayerForMeasuring,
                                      'CLEAR_SELECTION')
    SelectLayerByAttribute_management(fcAsFeatureLayerForMeasuring,
                                      'NEW_SELECTION', subsetSelectionQuery)

    countResult = GetCount_management(fcAsFeatureLayerForMeasuring)
    intCount = int(countResult.getOutput(0))
    print('There were ' + str(intCount) +
          ' features selected in the fcAsFeatureLayerForMeasuring layer.')
    if intCount >= 1:
        expressionText1 = 0
        CalculateField_management(fcAsFeatureLayerForMeasuring, startMeasure,
                                  expressionText1, "PYTHON_9.3")
        expressionText2 = 'float("{0:.3f}".format(!Shape_Length! / 5280.00))'
        CalculateField_management(fcAsFeatureLayerForMeasuring, endMeasure,
                                  expressionText2, "PYTHON_9.3")
    else:
        print "Not calculating due to lack of selected features."
예제 #5
0
def checkAddressPointFrequency(AddressPoints, gdb):
    from arcpy import Frequency_analysis, MakeTableView_management, DeleteRows_management, GetCount_management, Delete_management, Exists
    from os.path import join

    AP_Freq = join(gdb, "AP_Freq")
    fl = "fl"

    #remove the frequency table if it exists already
    if Exists(AP_Freq):
        Delete_management(AP_Freq)

    #run frequency analysis
    Frequency_analysis(
        AddressPoints, AP_Freq,
        "MUNI;HNO;HNS;PRD;STP;RD;STS;POD;POM;ZIP;BLD;FLR;UNIT;ROOM;SEAT;LOC;LOCTYPE",
        "")

    #get count of records
    rFreq = GetCount_management(AP_Freq)
    rCount = int(rFreq.getOutput(0))

    #delete records
    #make where clause
    wc = "Frequency = 1 or LOCTYPE <> 'Primary'"

    #make feature layer
    MakeTableView_management(AP_Freq, fl, wc)

    #get count of the results
    result = GetCount_management(fl)
    count = int(result.getOutput(0))

    if rCount != count:
        #Delete
        DeleteRows_management(fl)
        userMessage(
            "Checked frequency of address points. Results are in table " +
            AP_Freq)
    elif rCount == count:
        Delete_management(AP_Freq)
        userMessage("All address points are unique.")
예제 #6
0
def checkFeatureLocations(gdb):
    userMessage("Checking feature locations...")
    from os import path
    from arcpy import MakeFeatureLayer_management, SelectLayerByAttribute_management, SelectLayerByLocation_management, GetCount_management, Delete_management, da

    values = []
    #make sure feature are all inside authoritative boundary

    #get authoritative boundary
    authBound = path.join(gdb, "NG911", "AuthoritativeBoundary")
    ab = "ab"

    MakeFeatureLayer_management(authBound, ab)

    for dirpath, dirnames, filenames in da.Walk(gdb, True, '', False,
                                                ["FeatureClass"]):
        for filename in filenames:
            if filename != "AuthoritativeBoundary":
                #get full path name & create a feature layer
                fullPath = path.join(gdb, filename)
                fl = "fl"
                MakeFeatureLayer_management(fullPath, fl)

                #select by location to get count of features outside the authoritative boundary
                SelectLayerByLocation_management(fl, "INTERSECT", ab)
                SelectLayerByAttribute_management(fl, "SWITCH_SELECTION", "")
                #get count of selected records
                result = GetCount_management(fl)
                count = int(result.getOutput(0))

                #report results
                if count > 0:
                    fields = ("OBJECTID")
                    with da.SearchCursor(fl, fields) as rows:
                        for row in rows:
                            val = (today,
                                   "Feature not inside authoritative boundary",
                                   filename, "", row[0])
                            values.append(val)
                else:
                    userMessage(filename +
                                ": all records inside authoritative boundary")

                #clean up
                Delete_management(fl)

    userMessage("Completed check on feature locations")

    if values != []:
        RecordResults("fieldValues", values, gdb)
예제 #7
0
def checkFeatureLocations(pathsInfoObject):
    gdb = pathsInfoObject.gdbPath
    
    userMessage("Checking feature locations...")

    #get today's date
    today = strftime("%m/%d/%y")
    values = []

    #make sure features are all inside authoritative boundary

    #get authoritative boundary
    authBound = path.join(gdb, "NG911", "AuthoritativeBoundary")
    ab = "ab"

    MakeFeatureLayer_management(authBound, ab)

    for dirpath, dirnames, filenames in Walk(gdb, True, '', False, ["FeatureClass"]):  # @UnusedVariable
        for filename in filenames:
            if filename != "AuthoritativeBoundary":
                #get full path name & create a feature layer
                fullPath = path.join(gdb, filename)
                fl = "fl"
                MakeFeatureLayer_management(fullPath, fl)

                #select by location to get count of features outside the authoritative boundary
                SelectLayerByLocation_management(fl, "INTERSECT", ab)
                SelectLayerByAttribute_management(fl, "SWITCH_SELECTION", "")
                #get count of selected records
                result = GetCount_management(fl)
                count = int(result.getOutput(0))

                #report results
                if count > 0:
                    fields = ("OBJECTID")
                    with SearchCursor(fl, fields) as rows:
                        for row in rows:
                            val = (today, "Feature not inside authoritative boundary", filename, "", row[0])
                            values.append(val)
                else:
                    userMessage( filename + ": all records inside authoritative boundary")

                #clean up
                Delete_management(fl)

    userMessage("Completed check on feature locations")

    if values != []:
        RecordResults("fieldValues", values, gdb)
예제 #8
0
def checkAddressPointFrequency(AddressPoints, gdb):
    from arcpy import Frequency_analysis, MakeTableView_management, DeleteRows_management, GetCount_management, Delete_management, Exists
    from os.path import join

    AP_Freq = join(gdb, "AP_Freq")
    fl = "fl"

    #remove the frequency table if it exists already
    if Exists(AP_Freq):
        Delete_management(AP_Freq)

    #run frequency analysis
    Frequency_analysis(AddressPoints, AP_Freq, "MUNI;HNO;HNS;PRD;STP;RD;STS;POD;POM;ZIP;BLD;FLR;UNIT;ROOM;SEAT;LOC;LOCTYPE", "")

    #get count of records
    rFreq = GetCount_management(AP_Freq)
    rCount = int(rFreq.getOutput(0))

    #delete records
    #make where clause
    wc = "Frequency = 1 or LOCTYPE <> 'Primary'"

    #make feature layer
    MakeTableView_management(AP_Freq, fl, wc)

    #get count of the results
    result = GetCount_management(fl)
    count = int(result.getOutput(0))

    if rCount != count:
        #Delete
        DeleteRows_management(fl)
        userMessage("Checked frequency of address points. Results are in table " + AP_Freq)
    elif rCount == count:
        Delete_management(AP_Freq)
        userMessage("All address points are unique.")
def main():
    print("Starting to dissolve the routes source.")
    stateSystemSelectedFeatures = 'StateSystem_Features'
    selectQuery1 = '''LRS_ROUTE_PREFIX in ('I', 'U', 'K')'''
    MakeFeatureLayer_management (routesSourceCenterlines, stateSystemSelectedFeatures, selectQuery1)
    CopyFeatures_management(stateSystemSelectedFeatures, routesSourcePreDissolveOutput)
    
    #GetCount on the features in the layer here.
    countResult = GetCount_management(stateSystemSelectedFeatures)
    intCount = int(countResult.getOutput(0))
    print('Found ' + str(intCount) + ' state system features to be dissolved.')
    
    # Removed STATE_FLIP_FLAG because we've already flipped the data prior to this process.
    dissolveFields = "KDOT_DIRECTION_CALC;KDOT_LRS_KEY;LRS_COUNTY_PRE;LRS_ROUTE_PREFIX;LRS_ROUTE_SUFFIX;LRS_UNIQUE_IDENT"
    statisticsFields = "BEG_NODE MIN;BEG_NODE MAX;END_NODE MAX;END_NODE MIN;COUNTY_BEGIN_MP MIN;COUNTY_END_MP MAX"
    multipart = "SINGLE_PART"
    unsplitLines = "DISSOLVE_LINES"
    ## Use a selection for the state system so that it will work the first time.
    ## Then, later expand the selection and the amount of routes that are attempted for
    ## the dissolve.
    Dissolve_management(stateSystemSelectedFeatures, routesSourceDissolveOutput, dissolveFields, statisticsFields, multipart, unsplitLines)
    print("Completed dissolving the routes source.")
예제 #10
0
def checkFrequency(fc, freq, fields, gdb, version):
    fl = "fl"
    fl1 = "fl1"
    wc = "FREQUENCY > 1"

    #remove the frequency table if it exists already
    if Exists(freq):
        try:
            Delete_management(freq)
        except:
            userMessage("Please manually delete " + freq + " and then run the frequency check again")

    if not Exists(freq):
        try:
            #see if we're working with address points or roads, create a where clause
            filename = ""
            if freq == join(gdb, "AP_Freq"):
                filename = "AddressPoints"
                wc1 = "HNO <> 0"
            elif freq == join(gdb, "Road_Freq"):
                filename = "RoadCenterline"
                wc1 = "L_F_ADD <> 0 AND L_T_ADD <> 0 AND R_F_ADD <> 0 AND R_T_ADD <> 0"

            if version != "10":
                wc1 = wc1 + " AND SUBMIT = 'Y'"

            #run query on fc to make sure 0's are ignored
            MakeTableView_management(fc, fl1, wc1)

            #split field names
            fieldsList = fields.split(";")
            fieldCountList = []
            fl_fields = []
            for f in fieldsList:
                f = f.strip()
                fList = [f,"COUNT"]
                fieldCountList.append(fList)
                fl_fields.append(f)

            #run frequency analysis
            Statistics_analysis(fl1, freq, fieldCountList, fields)

            #make feature layer
            MakeTableView_management(freq, fl, wc)

            #get count of the results
            result = GetCount_management(fl)
            count = int(result.getOutput(0))

            if count > 0:

                #set up parameters to report duplicate records
                values = []
                recordType = "fieldValues"
                today = strftime("%m/%d/%y")

                #add information to FieldValuesCheckResults for all duplicates

                #get field count
                fCount = len(fl_fields)

                #get the unique ID field name
                id1 = getUniqueIDField(filename.upper())

                #run a search on the frequency table to report duplicate records
                with SearchCursor(freq, fl_fields, wc) as rows:
                    for row in rows:
                        i = 0
                        #generate where clause to find duplicate ID's
                        wc = ""
                        while i < fCount:
                            stuff = ""
                            if row[i] != None:
                                try:
                                    stuff = " = '" + row[i] + "' "
                                except:
                                    stuff = " = " + str(row[i]) + " "
                            else:
                                stuff = " is null "
                            wc = wc + fl_fields[i] + stuff + "and "
                            i += 1

                        #trim last "and " off where clause
                        wc = wc[0:-5]

                        #find records with duplicates to get their unique ID's
                        with SearchCursor(fl1, (id1), wc) as sRows:
                            for sRow in sRows:
                                fID = sRow[0]
                                report = str(fID) + " has duplicate field information"
                                val = (today, report, filename, "", fID)
                                values.append(val)

                #report duplicate records
                if values != []:
                    RecordResults(recordType, values, gdb)
                    userMessage("Checked frequency. Results are in table FieldValuesCheckResults")

            elif count == 0:
                userMessage("Checked frequency. All records are unique.")

            #clean up
            Delete_management(fl)
            Delete_management(fl1)

            try:
                Delete_management(freq)
            except:
                userMessage("Could not delete frequency table")
        except:
            userMessage("Could not fully run frequency check")
예제 #11
0
def geocodeAddressPoints(pathsInfoObject):
    gdb = pathsInfoObject.gdbPath

    env.workspace = gdb
    addressPointPath = "AddressPoints"
    streetPath = "RoadCenterline"
    roadAliasPath = "RoadAlias"

    userMessage("Geocoding address points...")

    gc_table = "GeocodeTable"
    sl_field = "SingleLineInput"
    Locator = "Locator"
    addyview = "addy_view"
    output = "gc_test"

    # Get the fields from the input
    fields = ListFields(addressPointPath)

    # Create a fieldinfo object
    fieldinfo = FieldInfo()

    # Iterate through the fields and set them to fieldinfo
    for field in fields:
        if field.name in ("LABEL", "ZIP"):
            fieldinfo.addField(field.name, field.name, "VISIBLE", "")
    else:
        fieldinfo.addField(field.name, field.name, "HIDDEN", "")

    userMessage("Preparing addresses...")
    # The created addyview layer will have fields as set in fieldinfo object
    MakeTableView_management(addressPointPath, addyview, "", "", fieldinfo)

    # To persist the layer on disk make a copy of the view
    if Exists(gc_table):
        try:
            Delete_management(gc_table)
        except:
            userMessage("Please manually delete the table called gc_table and then run the geocoding again")

    if not Exists(gc_table):
        CopyRows_management(addyview, gc_table)

        #add single line input field for geocoding
        AddField_management(gc_table, sl_field, "TEXT", "", "", 250)

        #calculate field
        exp = '[LABEL] & " " & [ZIP]'
        CalculateField_management(gc_table, sl_field, exp, "VB")

        #generate locator
        fieldMap = """'Primary Table:Feature ID' <None> VISIBLE NONE;'*Primary Table:From Left' RoadCenterline:L_F_ADD VISIBLE NONE;
        '*Primary Table:To Left' RoadCenterline:L_T_ADD VISIBLE NONE;'*Primary Table:From Right' RoadCenterline:R_F_ADD VISIBLE NONE;
        '*Primary Table:To Right' RoadCenterline:R_T_ADD VISIBLE NONE;'Primary Table:Prefix Direction' RoadCenterline:PRD VISIBLE NONE;
        'Primary Table:Prefix Type' RoadCenterline:STP VISIBLE NONE;'*Primary Table:Street Name' RoadCenterline:RD VISIBLE NONE;
        'Primary Table:Suffix Type' RoadCenterline:STS VISIBLE NONE;'Primary Table:Suffix Direction' RoadCenterline:POD VISIBLE NONE;
        'Primary Table:Left City or Place' RoadCenterline:MUNI_L VISIBLE NONE;
        'Primary Table:Right City or Place' RoadCenterline:MUNI_R VISIBLE NONE;
        'Primary Table:Left ZIP Code' RoadCenterline:ZIP_L VISIBLE NONE;'Primary Table:Right ZIP Code' RoadCenterline:ZIP_R VISIBLE NONE;
        'Primary Table:Left State' RoadCenterline:STATE_L VISIBLE NONE;'Primary Table:Right State' RoadCenterline:STATE_R VISIBLE NONE;
        'Primary Table:Left Street ID' <None> VISIBLE NONE;'Primary Table:Right Street ID' <None> VISIBLE NONE;
        'Primary Table:Min X value for extent' <None> VISIBLE NONE;'Primary Table:Max X value for extent' <None> VISIBLE NONE;
        'Primary Table:Min Y value for extent' <None> VISIBLE NONE;'Primary Table:Max Y value for extent' <None> VISIBLE NONE;
        'Primary Table:Left Additional Field' <None> VISIBLE NONE;'Primary Table:Right Additional Field' <None> VISIBLE NONE;
        'Primary Table:Altname JoinID' RoadCenterline:SEGID VISIBLE NONE;'*Alternate Name Table:JoinID' RoadAlias:SEGID VISIBLE NONE;
        'Alternate Name Table:Prefix Direction' RoadAlias:A_PRD VISIBLE NONE;'Alternate Name Table:Prefix Type' <None> VISIBLE NONE;
        'Alternate Name Table:Street Name' RoadAlias:A_RD VISIBLE NONE;'Alternate Name Table:Suffix Type' RoadAlias:A_STS VISIBLE NONE;
        'Alternate Name Table:Suffix Direction' RoadAlias:A_POD VISIBLE NONE"""

        userMessage("Creating address locator...")
        # Process: Create Address Locator
        if Exists(Locator):
            RebuildAddressLocator_geocoding(Locator)
        else:
            try:
                CreateAddressLocator_geocoding("US Address - Dual Ranges", streetPath + " 'Primary Table';" + roadAliasPath + " 'Alternate Name Table'", fieldMap, Locator, "")
            except:
                try:
                    fieldMap = """'Primary Table:Feature ID' <None> VISIBLE NONE;'*Primary Table:From Left' RoadCenterline:L_F_ADD VISIBLE NONE;
                    '*Primary Table:To Left' RoadCenterline:L_T_ADD VISIBLE NONE;'*Primary Table:From Right' RoadCenterline:R_F_ADD VISIBLE NONE;
                    '*Primary Table:To Right' RoadCenterline:R_T_ADD VISIBLE NONE;'Primary Table:Prefix Direction' RoadCenterline:PRD VISIBLE NONE;
                    'Primary Table:Prefix Type' RoadCenterline:STP VISIBLE NONE;'*Primary Table:Street Name' RoadCenterline:RD VISIBLE NONE;
                    'Primary Table:Suffix Type' RoadCenterline:STS VISIBLE NONE;'Primary Table:Suffix Direction' RoadCenterline:POD VISIBLE NONE;
                    'Primary Table:Left City or Place' RoadCenterline:MUNI_L VISIBLE NONE;
                    'Primary Table:Right City or Place' RoadCenterline:MUNI_R VISIBLE NONE;
                    'Primary Table:Left ZIP Code' RoadCenterline:ZIP_L VISIBLE NONE;'Primary Table:Right ZIP Code' RoadCenterline:ZIP_R VISIBLE NONE;
                    'Primary Table:Left State' RoadCenterline:STATE_L VISIBLE NONE;'Primary Table:Right State' RoadCenterline:STATE_R VISIBLE NONE;
                    'Primary Table:Left Street ID' <None> VISIBLE NONE;'Primary Table:Right Street ID' <None> VISIBLE NONE;
                    'Primary Table:Display X' <None> VISIBLE NONE;'Primary Table:Display Y' <None> VISIBLE NONE;
                    'Primary Table:Min X value for extent' <None> VISIBLE NONE;'Primary Table:Max X value for extent' <None> VISIBLE NONE;
                    'Primary Table:Min Y value for extent' <None> VISIBLE NONE;'Primary Table:Max Y value for extent' <None> VISIBLE NONE;
                    'Primary Table:Left Additional Field' <None> VISIBLE NONE;'Primary Table:Right Additional Field' <None> VISIBLE NONE;
                    'Primary Table:Altname JoinID' RoadCenterline:SEGID VISIBLE NONE;'*Alternate Name Table:JoinID' RoadAlias:SEGID VISIBLE NONE;
                    'Alternate Name Table:Prefix Direction' RoadAlias:A_PRD VISIBLE NONE;'Alternate Name Table:Prefix Type' <None> VISIBLE NONE;
                    'Alternate Name Table:Street Name' RoadAlias:A_RD VISIBLE NONE;'Alternate Name Table:Suffix Type' RoadAlias:A_STS VISIBLE NONE;
                    'Alternate Name Table:Suffix Direction' RoadAlias:A_POD VISIBLE NONE"""
                    CreateAddressLocator_geocoding("US Address - Dual Ranges", streetPath + " 'Primary Table';" + roadAliasPath + " 'Alternate Name Table'", fieldMap, Locator, "", "DISABLED")
                except Exception as E:
                    userMessage(Locator)
                    userMessage("Cannot create address locator. Please email [email protected] this error message: " + str(E))


        if Exists(Locator):
            userMessage("Geocoding addresses...")

            #geocode table address
            if Exists(output):
                Delete_management(output)

            i = 0

            #set up geocoding
            gc_fieldMap = "Street LABEL VISIBLE NONE;City MUNI VISIBLE NONE;State State VISIBLE NONE;ZIP ZIP VISIBLE NONE"

            #geocode addresses
            try:
                GeocodeAddresses_geocoding(gc_table, Locator, gc_fieldMap, output, "STATIC")
                i = 1
            except:
                gc_fieldMap = "Street LABEL VISIBLE NONE;City MUNI VISIBLE NONE;State State VISIBLE NONE"

                try:
                    GeocodeAddresses_geocoding(gc_table, Locator, gc_fieldMap, output, "STATIC")
                    i = 1
                except:
                    userMessage("Could not geocode address points")

            #report records that didn't geocode
            if i == 1:
                wc = "Status <> 'M'"
                lyr = "lyr"

                MakeFeatureLayer_management(output, lyr, wc)

                rStatus = GetCount_management(lyr)
                rCount = int(rStatus.getOutput(0))

                if rCount > 0:
                    #set up parameters to report records that didn't geocode
                    values = []
                    recordType = "fieldValues"
                    today = strftime("%m/%d/%y")
                    filename = "AddressPoints"

                    rfields = ("ADDID")
                    with SearchCursor(output, rfields, wc) as rRows:
                        for rRow in rRows:
                            fID = rRow[0]
                            report = str(fID) + " did not geocode against centerline"
                            val = (today, report, filename, "", fID)
                            values.append(val)

                    #report records
                    if values != []:
                        RecordResults(recordType, values, gdb)

                    userMessage("Completed geocoding with " + str(rCount) + " errors.")

                else:
                    #this means all the records geocoded
                    userMessage("All records geocoded successfully.")
                    try:
                        Delete_management(output)
                    except:
                        userMessage("Geocoding table could not be deleted")
        else:
            userMessage("Could not geocode addresses")
예제 #12
0
def checkUniqueIDFrequency(currentPathSettings):
    gdb = currentPathSettings.gdbPath
    esbList = currentPathSettings.esbList
    fcList = currentPathSettings.fcList

    layerList = []

    env.workspace = gdb
    table = "ESB_IDS"

    #create temp table of esbID's
    if esbList <> []:
        layerList = ["ESB_IDS"]

        if Exists(table):
            Delete_management(table)

        CreateTable_management(gdb, table)

        AddField_management(table, "ESBID", "TEXT", "", "", 38)
        AddField_management(table, "ESB_LYR", "TEXT", "", "", 15)

        esbFields = ("ESBID")

        #copy ID's & esb layer type into the table
        for esb in esbList:
            with SearchCursor(esb, esbFields) as rows:
                for row in rows:
                    cursor = InsertCursor(table, ('ESBID', 'ESB_LYR'))
                    cursor.insertRow((row[0], esb))

        try:
            #clean up
            del rows, row, cursor
        except:
            print "objects cannot be deleted, they don't exist"

    else:
        for fc in fcList:
            fc = basename(fc)
            layerList.append(fc)

    #loop through layers in the gdb that aren't esb & ESB_IDS
##    layers = getCurrentLayerList(esb)
##    layers.append("ESB_IDS")

    values = []
    recordType = "fieldValues"
    today = strftime("%m/%d/%y")

    for layer in layerList:
##        if layer not in esb:
        if layer != "ESB_IDS":
            #for each layer, get the unique ID field
            uniqueID = getUniqueIDField(layer.upper())

        else:
            #for esb layers, get the unique ID field
            uniqueID = "ESBID"

        Statistics_analysis(layer, layer + "_freq", [[uniqueID,"COUNT"]], uniqueID)

        #set parameters for the search cursor
        where_clause = "FREQUENCY > 1"
        fields = (uniqueID, "FREQUENCY")

        fl = "fl"

        MakeTableView_management(layer + "_freq", fl, where_clause)

        result = GetCount_management(fl)
        count = int(result.getOutput(0))

        if count > 0:

            #set a search cursor with just the unique ID field
            with SearchCursor(layer + "_freq", fields, where_clause) as rows2:
                stringESBReport = ""
                for row2 in rows2:
                    if layer == "ESB_IDS":
                        stringEsbInfo = []
                        wc2 = "ESBID = " + row2[0]
                        with SearchCursor("ESB_IDS", ("ESB_LYR"), wc2) as esbRows:
                            for esbRow in esbRows:
                                stringEsbInfo.append(esbRow[0])

                        stringESBReport = " and ".join(stringEsbInfo)
                    else:
                        lyr = layer

                    #report duplicate IDs
                    report = str(row2[0]) + " is a duplicate ID"
                    if stringESBReport != "":
                        report = report + " in " + stringESBReport
                    val = (today, report, lyr, uniqueID, row2[0])
                    values.append(val)

        Delete_management(layer + "_freq")
        Delete_management(fl)

    #report duplicate records
    if values != []:
        RecordResults(recordType, values, gdb)
        userMessage("Checked unique ID frequency. Results are in table FieldValuesCheckResults.")
    else:
        userMessage("All ID's are unique.")

    #if it exists, clean up table
    if Exists(table):
        Delete_management(table)
예제 #13
0
def ListSplitAndSelect():
    ## Fragment used for testing, will not run properly on its own.

    delcount = GetCount_management(
        r'Database Connections\CANT_CPMS.sde\CPMS.CPMS_FMIS_GIS_DEL_ROWS')
    print str(delcount) + " records to delete"
    deletelist = list()
    ## Only portion below changed.
    if (not True):  # Production version tests for delcount threshold.
        pass

    else:  #ORA 01795 - Oracle limited to 1000 statements with select in *
        MakeTableView_management(FMIS_LOAD, "FMIS_TABLE")  # @UndefinedVariable
        MakeTableView_management(deltbl, "deletes")  # @UndefinedVariable
        with da.SearchCursor(deltbl,
                             "PROJECT_NUMBER") as delcur:  # @UndefinedVariable
            for row in delcur:
                DelXID = ("{0}".format(row[0]))
                #print DelXID + " is being deleted from the FMIS table"
                #AddJoin_management(layer_name,"CROSSINGID", deltbl, "CROSSINGID", "KEEP_ALL")
                #delsel = "PROJECT_NUMBER LIKE '"+DelXID+"'"
                #print delsel
                deletelist.append(DelXID)
                #SelectLayerByAttribute_management("FMIS_TABLE", "ADD_TO_SELECTION", delsel) #delsel not yet defined
                #SelectLayerByAttribute_management("FMIS_TABLE","ADD_TO_SELECTION", delsel)
        #print str(deletelist)

        #Take care of > 1000 selection issue here by splitting the long list into a series of lists.

        maxListSize = 999
        listStart = 0
        listEnd = maxListSize
        i = 0
        curListSize = len(deletelist)
        loopNumber = mathCeil(curListSize / maxListSize)
        firstContainer = list()

        # Creates the list container that holds the lists with the project numbers.
        while i <= loopNumber:
            if listEnd > curListSize:
                listEnd = curListSize
            else:
                pass
            listToHold = deletelist[listStart:listEnd]
            firstContainer.append(listToHold)
            i += 1
            listStart = listEnd
            listEnd = listEnd + maxListSize

        for secondContainer in firstContainer:
            delsel = "PROJECT_NUMBER IN ("
            for projectNum in secondContainer:
                delsel = delsel + """'""" + projectNum + """', """
            delsel = delsel[:
                            -2]  # Slice that removes the last comma and trailing space.
            delsel = delsel + ")"  # Adds the closing parenthesis.
            SelectLayerByAttribute_management(
                "FMIS_TABLE", "ADD_TO_SELECTION", delsel
            )  # ADD_TO_SELECTION works like NEW_SELECTION when no current selection.
            print delsel

        countResult = GetCount_management("FMIS_TABLE")
        countNum = int(countResult.getOutput(0))
        print countNum

        #DeleteRows_management("FMIS_TABLE")
    print "Delete function completed"
예제 #14
0
def checkRequiredFieldValues(gdb, folder, esb):
    userMessage("Checking that required fields have all values...")
    from os import path
    from arcpy.da import Walk, SearchCursor
    from arcpy import MakeTableView_management, Delete_management, GetCount_management, ListFields
    from time import strftime

    #get today's date
    today = strftime("%m/%d/%y")

    #get required fields
    rfDict = getRequiredFields(folder)

    id = "OBJECTID"
    values = []

    #walk through the tables/feature classes
    for dirpath, dirnames, filenames in Walk(gdb, True, '', False,
                                             ["Table", "FeatureClass"]):
        for filename in filenames:
            fullPath = path.join(gdb, filename)

            #get the keyword to acquire required field names
            keyword = getKeyword(filename, esb)

            #goal: get list of required fields that are present in the feature class
            #get the appropriate required field list
            if keyword in rfDict:
                requiredFieldList = rfDict[keyword]

            rfl = []
            for rf in requiredFieldList:
                rfl.append(rf.upper())

            #get list of fields in the feature class
            allFields = ListFields(fullPath)

            #make list of field names
            fields = []
            for aF in allFields:
                fields.append(aF.name.upper())

            #convert lists to sets
            set1 = set(rfl)
            set2 = set(fields)

            #get the set of fields that are the same
            matchingFields = list(set1 & set2)

            #create where clause to select any records where required values aren't populated
            wc = ""

            for field in matchingFields:
                wc = wc + " " + field + " is null or "

            wc = wc[0:-4]

            #make table view using where clause
            lyr = "lyr"
            MakeTableView_management(fullPath, lyr, wc)

            #get count of the results
            result = GetCount_management(lyr)
            count = int(result.getOutput(0))

            #if count is greater than 0, it means a required value somewhere isn't filled in
            if count > 0:
                #make sure the objectID gets included in the search for reporting
                if id not in matchingFields:
                    matchingFields.append(id)

                i = len(matchingFields)
                k = 0

                #run a search cursor to get any/all records where a required field value is null
                with SearchCursor(fullPath, (matchingFields), wc) as rows:
                    for row in rows:

                        #get object ID of the field
                        oid = str(row[matchingFields.index(id)])

                        #loop through row
                        while k < 0:
                            #see if the value is nothing
                            if row[k] is None:
                                #report the value if it is indeed null
                                report = matchingFields[
                                    k] + " is null for ObjectID " + oid
                                userMessage(report)
                                val = (today, report, filename,
                                       matchingFields[k], oid)
                                values.append(val)

                            #iterate!
                            k = k + 1
            else:
                userMessage("All required values present for " + filename)

            Delete_management(lyr)

    if values != "":
        RecordResults("fieldValues", values, gdb)

    userMessage("Completed check for required field values")
예제 #15
0
def getFastCount(lyr):
    # return a fast count of records
    result = GetCount_management(lyr)
    count = int(result.getOutput(0))
    return count
예제 #16
0
def iteratorprocess():

    env.workspace = extractDataGDBPath
    accDataFeaturesList = ListFeatureClasses("CrashLocation.GEO.ACC*")
    # Use the FullTable for the overall total.
    accDataFullTable = os.path.join(extractDataGDBPath,
                                    r'CrashLocation.GEO.GIS_GEOCODE_ACC')

    withRoadsTotal = 0

    gcKTotal = 0
    gcNKTotal = 0
    ofsKTotal = 0
    ofsNKTotal = 0
    NG911CoAccidents = 0

    inMemoryTempLayer = 'inMemoryTempFC'

    for countyItem in coAbbrAndNoList:
        countyNumber = countyItem[1]
        countyName = countyItem[2]

        accDataPointsKDOT = "CrashLocation.GEO.ACC_PTS_" + countyNumber
        accDataPointsNK = "CrashLocation.GEO.ACC_PTS_" + countyNumber + "_NK"
        accDataOffsetKDOT = "CrashLocation.GEO.ACC_OFS_PTS_" + countyNumber
        accDataOffsetNK = "CrashLocation.GEO.ACC_OFS_PTS_" + countyNumber + "_NK"

        # Re-zero the loop variables here so that the table doesn't get incorrect information in it.

        totalAccidents = 0

        geocodedAccidents = 0
        geocodedAccidentsNK = 0
        offsetAccidents = 0
        offsetAccidentsNK = 0

        gcPercent = '0.00'
        gcNKPercent = '0.00'
        ofsPercent = '0.00'
        ofsNKPercent = '0.00'

        if (accDataPointsKDOT in accDataFeaturesList) or (accDataPointsNK in accDataFeaturesList) or \
            (accDataOffsetKDOT in accDataFeaturesList) or (accDataOffsetNK in accDataFeaturesList):

            if accDataPointsKDOT in accDataFeaturesList:

                try:
                    Delete_management(inMemoryTempLayer)
                except:
                    pass

                accDataPointsKDOTPath = os.path.join(extractDataGDBPath,
                                                     accDataPointsKDOT)

                MakeFeatureLayer_management(accDataPointsKDOTPath,
                                            inMemoryTempLayer)

                #SelectLayerByAttribute_management(inMemoryTempLayer, 'CLEAR_SELECTION')

                tempResult = GetCount_management(inMemoryTempLayer)

                totalAccidents = int(tempResult.getOutput(0))

                if totalAccidents > 0:
                    withRoadsTotal += totalAccidents
                else:
                    pass

                selectWhereClause = """ Status <> 'U' """

                SelectLayerByAttribute_management(inMemoryTempLayer,
                                                  'NEW_SELECTION',
                                                  selectWhereClause)

                tempResult = GetCount_management(inMemoryTempLayer)

                geocodedAccidents = int(tempResult.getOutput(0))

            else:
                pass

            if accDataPointsNK in accDataFeaturesList:

                try:
                    Delete_management(inMemoryTempLayer)
                except:
                    pass

                accDataPointsNKPath = os.path.join(extractDataGDBPath,
                                                   accDataPointsNK)

                MakeFeatureLayer_management(accDataPointsNKPath,
                                            inMemoryTempLayer)

                selectWhereClause = """ Status <> 'U' """

                SelectLayerByAttribute_management(inMemoryTempLayer,
                                                  'NEW_SELECTION',
                                                  selectWhereClause)

                tempResult = GetCount_management(inMemoryTempLayer)

                geocodedAccidentsNK = int(tempResult.getOutput(0))

            else:
                pass

            if accDataOffsetKDOT in accDataFeaturesList:

                try:
                    Delete_management(inMemoryTempLayer)
                except:
                    pass

                accDataOffsetKDOTPath = os.path.join(extractDataGDBPath,
                                                     accDataOffsetKDOT)

                MakeFeatureLayer_management(accDataOffsetKDOTPath,
                                            inMemoryTempLayer)

                selectWhereClause = """ isOffset IS NOT NULL """

                SelectLayerByAttribute_management(inMemoryTempLayer,
                                                  'NEW_SELECTION',
                                                  selectWhereClause)

                tempResult = GetCount_management(inMemoryTempLayer)

                offsetAccidents = int(tempResult.getOutput(0))

            else:
                pass

            if accDataOffsetNK in accDataFeaturesList:

                try:
                    Delete_management(inMemoryTempLayer)
                except:
                    pass

                accDataOffsetNKPath = os.path.join(extractDataGDBPath,
                                                   accDataOffsetNK)

                MakeFeatureLayer_management(accDataOffsetNKPath,
                                            inMemoryTempLayer)

                selectWhereClause = """ isOffset IS NOT NULL """

                SelectLayerByAttribute_management(inMemoryTempLayer,
                                                  'NEW_SELECTION',
                                                  selectWhereClause)

                tempResult = GetCount_management(inMemoryTempLayer)

                offsetAccidentsNK = int(tempResult.getOutput(0))

            else:
                pass

            try:
                gcPercent = "{0:.2f}".format(
                    (float(geocodedAccidents) / totalAccidents) * 100)
                gcNKPercent = "{0:.2f}".format(
                    (float(geocodedAccidentsNK) / totalAccidents) * 100)
                ofsPercent = "{0:.2f}".format(
                    (float(offsetAccidents) / totalAccidents) * 100)
                ofsNKPercent = "{0:.2f}".format(
                    (float(offsetAccidentsNK) / totalAccidents) * 100)
            except ZeroDivisionError:
                gcPercent = None
                gcNKPercent = None
                ofsPercent = None
                ofsNKPercent = None
            except:
                pass

            gcKTotal += geocodedAccidents
            gcNKTotal += geocodedAccidentsNK
            ofsKTotal += offsetAccidents
            ofsNKTotal += offsetAccidentsNK
            NG911CoAccidents += totalAccidents

            print("\n" + countyName + " County has " + str(totalAccidents) +
                  " totalAccidents.")
            print("gcPercent: " + gcPercent + " gcNKPercent: " + gcNKPercent +
                  " ofsPercent: " + ofsPercent + " ofsNKPercent: " +
                  ofsNKPercent)

        # To get the withRoadsTotal, sum the number for each county that
        # returned a non-zero result for totalAccidents.

        else:
            pass

        reportResult = [
            countyName, totalAccidents, gcPercent, gcNKPercent, ofsPercent,
            ofsNKPercent
        ]
        reportResultsList.append(reportResult)

    try:
        Delete_management(inMemoryTempLayer)
    except:
        pass

    MakeTableView_management(accDataFullTable, inMemoryTempLayer)

    tempResult = GetCount_management(inMemoryTempLayer)

    overallTotal = int(tempResult.getOutput(0))

    for reportResultItem in reportResultsList:
        print str(reportResultItem[0])

    gcNG911Percent = "{0:.2f}".format(
        (float(gcKTotal) / NG911CoAccidents) * 100)
    gcNKNG911Percent = "{0:.2f}".format(
        (float(gcNKTotal) / NG911CoAccidents) * 100)
    ofsNG911Percent = "{0:.2f}".format(
        (float(ofsKTotal) / NG911CoAccidents) * 100)
    ofsNKNG911Percent = "{0:.2f}".format(
        (float(ofsNKTotal) / NG911CoAccidents) * 100)

    print "\n" + "The NG911Total is: " + str(NG911CoAccidents)
    print(" with gcPercent: " + gcNG911Percent + " gcNKPercent: " +
          gcNKNG911Percent + " ofsPercent: " + ofsNG911Percent +
          " ofsNKPercent: " + ofsNKNG911Percent)

    reportResult = [
        "NG911Total", NG911CoAccidents, gcNG911Percent, gcNKNG911Percent,
        ofsNG911Percent, ofsNKNG911Percent
    ]
    reportResultsList.append(reportResult)

    gcOverallPercent = "{0:.2f}".format((float(gcKTotal) / overallTotal) * 100)
    gcNKOverallPercent = "{0:.2f}".format(
        (float(gcNKTotal) / overallTotal) * 100)
    ofsOverallPercent = "{0:.2f}".format(
        (float(ofsKTotal) / overallTotal) * 100)
    ofsNKOverallPercent = "{0:.2f}".format(
        (float(ofsNKTotal) / overallTotal) * 100)

    print "\n" + "The OverallTotal is: " + str(overallTotal)
    print(" with gcPercent: " + gcOverallPercent + " gcNKPercent: " +
          gcNKOverallPercent + " ofsPercent: " + ofsOverallPercent +
          " ofsNKPercent: " + ofsNKOverallPercent)

    reportResult = [
        "OverallTotal", overallTotal, gcOverallPercent, gcNKOverallPercent,
        ofsOverallPercent, ofsNKOverallPercent
    ]
    reportResultsList.append(reportResult)

    resultsTablePath = recreateResultsTable()

    # Delete the previous table information, if any, then create an insert cursor
    # and place all of the report result items in the table.

    newICursor = InsertCursor(resultsTablePath, insertCursorFields)

    for reportResultItem in reportResultsList:
        insertedRowID = newICursor.insertRow(reportResultItem)
        print "Inserted a new row into the REPORT_INFO table with OID: " + str(
            insertedRowID)
예제 #17
0
def getFastCount(lyr):
    from arcpy import GetCount_management
    result = GetCount_management(lyr)
    count = int(result.getOutput(0))
    return count
def recalculateKeyValues():
    # As long as the KDOT_LRS_KEY is not null, calculate from the
    # current fields.

    # Prior to doing any of this, I added a field to cache the
    # current KDOT_LRS_KEY to check for mistakes and recover from
    # them if any were found.

    # Use the prefix field to decide on what action to take to update the KDOTRouteId.
    # If the prefix is null, do nothing.
    # If the prefix is I, U, K, create the KDOTRouteId value from the SHS component parts.
    selectionQuery = """ "LRS_ROUTE_PREFIX" IN ('I', 'U', 'K') """
    necessaryFields = [
        "LRS_COUNTY_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "KDOT_DIRECTION_CALC"
    ]
    dynNonNullSelectionQuery = GenerateNonNullSelectionQuery(necessaryFields)
    fullSelectionQuery = selectionQuery + """ AND """ + dynNonNullSelectionQuery

    fieldsToUseForUpdating = [
        "LRS_COUNTY_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "KDOT_DIRECTION_CALC",
        "KDOT_LRS_KEY"
    ]

    newCursor = daUpdateCursor(fcAsFeatureLayer, fieldsToUseForUpdating,
                               fullSelectionQuery)

    for cursorRowItem in newCursor:
        cursorListItem = list(cursorRowItem)
        countyPre = cursorListItem[0]
        routePre = cursorListItem[1]
        routeNum = cursorListItem[2]
        routeSuf = cursorListItem[3]
        lrsUniqueIdent = cursorListItem[4]
        if len(lrsUniqueIdent) > 1:
            lrsUniqueIdent = lrsUniqueIdent[-1]
        else:
            pass
        directionCalc = cursorListItem[5]
        directionText = ''

        # Modified 2017-17-27 to fix the issue of non-primary sides, esp. on odd-numbered routes, receiving '-EB'.
        try:
            if int(routeNum) % 2 == 0:
                if directionCalc is not None and int(directionCalc) == 1:
                    directionText = '-WB'
                else:
                    # Default, if the non-primary side is receiving this, make sure that it has a 1 in the directionCalc.
                    directionText = '-EB'

            if int(routeNum) % 2 == 1:
                if directionCalc is not None and int(directionCalc) == 1:
                    directionText = '-SB'
                else:
                    # Default, if the non-primary side is receiving this, make sure that it has a 1 in the directionCalc.
                    directionText = '-NB'
            newKey = str(countyPre) + str(routePre) + str(routeNum) + str(
                routeSuf) + str(lrsUniqueIdent) + directionText
            cursorListItem[6] = newKey
            # For Debugging
            ##print("Updating the lrs key to: " + str(newKey) + ".")
            newCursor.updateRow(cursorListItem)
        except:
            try:
                print(traceback.format_exc())
                print("Could not calculate a new LRS_KEY for the given row.")
                print("The row looks like this: " + str(cursorListItem) + ".")
            except:
                pass
            newCursor.next()

    try:
        del newCursor
    except:
        pass

    ###------------------------------------------------------------------------------------------------------------###
    ### If the prefix is not I, U, K and not X, create the KDOTRouteID from the Non-SHS, Non-Ramp component parts. ###
    ###------------------------------------------------------------------------------------------------------------###

    # For prefix R & M
    selectionQuery = """ "LRS_ROUTE_PREFIX" IN ('R', 'M') """
    necessaryFields = [
        "LRS_COUNTY_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "LRS_ADMO"
    ]
    dynNonNullSelectionQuery = GenerateNonNullSelectionQuery(necessaryFields)
    fullSelectionQuery = selectionQuery + """ AND """ + dynNonNullSelectionQuery

    fieldsToUseForUpdating = [
        "LRS_COUNTY_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "LRS_ADMO",
        "KDOT_DIRECTION_CALC", "KDOT_LRS_KEY"
    ]

    newCursor = daUpdateCursor(fcAsFeatureLayer, fieldsToUseForUpdating,
                               fullSelectionQuery)

    for cursorRowItem in newCursor:
        cursorListItem = list(cursorRowItem)
        countyPre = cursorListItem[0]
        routePre = cursorListItem[1]
        routeNum = cursorListItem[2]
        routeSuf = cursorListItem[3]
        lrsUniqueIdent = cursorListItem[4]
        if len(lrsUniqueIdent) > 1:
            lrsUniqueIdent = lrsUniqueIdent[
                -1]  # Get the right-most value. e.g. 47 => 7, 52 => 2
        else:
            pass
        lrsAdmo = cursorListItem[5]
        directionCalc = cursorListItem[6]
        if directionCalc is None:
            directionCalc = '0'
        else:
            pass
        try:
            newKey = str(countyPre) + str(routePre) + str(routeNum) + str(
                routeSuf) + str(lrsUniqueIdent) + str(lrsAdmo) + str(
                    directionCalc)
            cursorListItem[7] = newKey
            newCursor.updateRow(cursorListItem)
        except:
            try:
                print(traceback.format_exc())
                print("Could not calculate a new LRS_KEY for the given row.")
                print("The row looks like this: " + str(cursorListItem) + ".")
            except:
                pass
            newCursor.next()

    try:
        del newCursor
    except:
        pass

    # For prefix C, Urban Classified, which uses LRS_URBAN_PRE.
    selectionQuery = """ "LRS_ROUTE_PREFIX" IN ('C') """
    necessaryFields = [
        "LRS_URBAN_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "LRS_ADMO"
    ]
    dynNonNullSelectionQuery = GenerateNonNullSelectionQuery(necessaryFields)
    # Uses LRS_ADMO
    ####LRS_ROUTE_NUM, LRS_ROUTE_SUFFIX, LRS_UNIQUE_IDENT, then LRS_ADMO, then 0 for inventory direction.
    fullSelectionQuery = selectionQuery + """ AND """ + dynNonNullSelectionQuery

    fieldsToUseForUpdating = [
        "LRS_URBAN_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "LRS_ADMO",
        "KDOT_DIRECTION_CALC", "KDOT_LRS_KEY"
    ]

    newCursor = daUpdateCursor(fcAsFeatureLayer, fieldsToUseForUpdating,
                               fullSelectionQuery)

    for cursorRowItem in newCursor:
        cursorListItem = list(cursorRowItem)
        urbanPre = cursorListItem[0]
        routePre = cursorListItem[1]
        routeNum = cursorListItem[2]
        routeSuf = cursorListItem[3]
        lrsUniqueIdent = cursorListItem[4]
        if len(lrsUniqueIdent) > 1:
            lrsUniqueIdent = lrsUniqueIdent[
                -1]  # Get the right-most value. e.g. 47 => 7, 52 => 2
        else:
            pass
        lrsAdmo = cursorListItem[5]
        directionCalc = cursorListItem[6]
        if directionCalc is None:
            directionCalc = '0'
        else:
            pass
        try:
            newKey = str(urbanPre) + str(routePre) + str(routeNum) + str(
                routeSuf) + str(lrsUniqueIdent) + str(lrsAdmo) + str(
                    directionCalc)
            cursorListItem[7] = newKey
            newCursor.updateRow(cursorListItem)
        except:
            try:
                print(traceback.format_exc())
                print("Could not calculate a new LRS_KEY for the given row.")
                print("The row looks like this: " + str(cursorListItem) + ".")
            except:
                pass
            newCursor.next()

    try:
        del newCursor
    except:
        pass

    # If the prefix is X, create the KDOTRouteID from the Ramp route component parts.
    selectionQuery = """ "LRS_ROUTE_PREFIX" = 'X' """
    # Doesn't make sense to require *_SUFFIX on ramps. - Just use '0' if it is null.
    # Only 12 Ramps have non-null LRS_ROUTE_SUFFIX values. For those, it is all '0' or 'No Suffix'.
    # If people set LRS_ROUTE_SUFFIX to 'G' or 'Z' for ramps though, that needs to be handled correctly.
    necessaryFields = [
        "LRS_COUNTY_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_UNIQUE_IDENT", "LRS_ADMO"
    ]
    dynNonNullSelectionQuery = GenerateNonNullSelectionQuery(necessaryFields)
    fullSelectionQuery = selectionQuery + """ AND """ + dynNonNullSelectionQuery

    fieldsToUseForUpdating = [
        "LRS_COUNTY_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "LRS_ADMO",
        "KDOT_DIRECTION_CALC", "KDOT_LRS_KEY"
    ]

    newCursor = daUpdateCursor(fcAsFeatureLayer, fieldsToUseForUpdating,
                               fullSelectionQuery)

    for cursorRowItem in newCursor:
        cursorListItem = list(cursorRowItem)
        countyPre = cursorListItem[0]
        routePre = cursorListItem[1]
        routeNum = cursorListItem[2]
        routeSuf = cursorListItem[3]
        if routeSuf is None:
            routeSuf = '0'
        else:  # Use whatever character is in the Route Suffix if it's not None/Null.
            pass
        lrsUniqueIdent = cursorListItem[4]
        if len(lrsUniqueIdent) > 1:
            lrsUniqueIdent = lrsUniqueIdent[-1]
        else:
            pass
        lrsAdmo = cursorListItem[5]
        directionCalc = cursorListItem[6]
        if directionCalc is None:
            directionCalc = '0'
        else:
            pass
        try:
            newKey = str(countyPre) + str(routePre) + str(routeNum) + str(
                routeSuf) + str(lrsUniqueIdent) + str(lrsAdmo) + str(
                    directionCalc)
            cursorListItem[7] = newKey
            newCursor.updateRow(cursorListItem)
        except:
            try:
                print(traceback.format_exc())
                print("Could not calculate a new LRS_KEY for the given row.")
                print("The row looks like this: " + str(cursorListItem) + ".")
            except:
                pass
            newCursor.next()

    try:
        del newCursor
    except:
        pass

    # For all other prefixes.
    selectionQuery = """ "LRS_ROUTE_PREFIX" NOT IN ('I', 'U', 'K', 'X', 'R', 'M', 'C') """
    necessaryFields = [
        "LRS_COUNTY_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "LRS_ADMO"
    ]
    dynNonNullSelectionQuery = GenerateNonNullSelectionQuery(necessaryFields)
    fullSelectionQuery = selectionQuery + """ AND """ + dynNonNullSelectionQuery

    fieldsToUseForUpdating = [
        "LRS_COUNTY_PRE", "LRS_ROUTE_PREFIX", "LRS_ROUTE_NUM",
        "LRS_ROUTE_SUFFIX", "LRS_UNIQUE_IDENT", "LRS_ADMO",
        "KDOT_DIRECTION_CALC", "KDOT_LRS_KEY"
    ]

    newCursor = daUpdateCursor(fcAsFeatureLayer, fieldsToUseForUpdating,
                               fullSelectionQuery)

    for cursorRowItem in newCursor:
        cursorListItem = list(cursorRowItem)
        countyPre = cursorListItem[0]
        routePre = cursorListItem[1]
        routeNum = cursorListItem[2]
        routeSuf = cursorListItem[3]
        lrsUniqueIdent = cursorListItem[4]
        if len(lrsUniqueIdent) > 1:
            lrsUniqueIdent = lrsUniqueIdent[-1]
        else:
            pass
        lrsAdmo = cursorListItem[5]
        directionCalc = cursorListItem[6]
        if directionCalc is None:
            directionCalc = '0'
        else:
            pass
        try:
            newKey = str(countyPre) + str(routePre) + str(routeNum) + str(
                routeSuf) + str(lrsUniqueIdent) + str(lrsAdmo) + str(
                    directionCalc)
            cursorListItem[7] = newKey
            newCursor.updateRow(cursorListItem)
        except:
            try:
                print(traceback.format_exc())
                print("Could not calculate a new LRS_KEY for the given row.")
                print("The row looks like this: " + str(cursorListItem) + ".")
            except:
                pass
            newCursor.next()

    try:
        del newCursor
    except:
        pass

    # Something's not right. The calculates should fail every time because the n1 fields don't exist in this layer yet. :(
    selectionQuery = """ "KDOT_LRS_KEY" IS NOT NULL """
    SelectLayerByAttribute_management(fcAsFeatureLayer, "NEW_SELECTION",
                                      selectionQuery)
    # SourceRouteId = KDOT_LRS_KEY
    CalculateField_management(fcAsFeatureLayer, n1RouteId,
                              "!" + str(KDOTRouteId) + "!", "PYTHON_9.3")
    # SourceFromMeasure = county_log_begin
    CalculateField_management(fcAsFeatureLayer, n1FromMeas,
                              "!" + str(KDOTMeasBeg) + "!", "PYTHON_9.3")
    # SourceToMeasure = county_log_end
    CalculateField_management(fcAsFeatureLayer, n1ToMeas,
                              "!" + str(KDOTMeasEnd) + "!", "PYTHON_9.3")
    selectionQuery = """ KDOT_LRS_KEY IS NOT NULL AND county_log_begin IS NULL AND county_log_end IS NULL AND (COUNTY_BEGIN_MP IS NOT NULL OR COUNTY_END_MP IS NOT NULL) """
    SelectLayerByAttribute_management(fcAsFeatureLayer, "NEW_SELECTION",
                                      selectionQuery)
    countResult = GetCount_management(fcAsFeatureLayer)
    intCount = int(countResult.getOutput(0))
    print(
        "After the new selection query to deal with the fact that some State routes did not have their begin and end measure populated correctly, "
        + str(intCount) + " segments were selected.")
    # SourceFromMeasure = COUNTY_BEGIN_MP
    CalculateField_management(fcAsFeatureLayer, n1FromMeas,
                              "!COUNTY_BEGIN_MP!", "PYTHON_9.3")
    # SourceToMeasure = COUNTY_END_MP
    CalculateField_management(fcAsFeatureLayer, n1ToMeas, "!COUNTY_END_MP!",
                              "PYTHON_9.3")
def featureReplacement(sourceFL, targetFL, featuresToSelect):
    # 1c.) Get the common fields so that you can search and insert correctly.
    targetFeatureDesc = Describe(targetFL)
    targetFeatureFields = targetFeatureDesc.fields
    targetFeatureOIDField = targetFeatureDesc.OIDFieldName
    targetFeatureShapeField = targetFeatureDesc.shapeFieldName
    targetFeatureFieldNames = [x.name for x in targetFeatureFields]
    
    sourceFeatureDesc = Describe(sourceFL)
    sourceFeatureFields = sourceFeatureDesc.fields
    sourceFeatureOIDField = sourceFeatureDesc.OIDFieldName
    sourceFeatureShapeField = sourceFeatureDesc.shapeFieldName
    sourceFeatureFieldNames = [x.name for x in sourceFeatureFields]
    
    excludeFieldNames = [targetFeatureOIDField, targetFeatureShapeField, sourceFeatureOIDField, sourceFeatureShapeField]
    
    searchCursorFields = [x for x in targetFeatureFieldNames if x in sourceFeatureFieldNames and x not in excludeFieldNames]
    searchCursorFields.append('SHAPE@')
    
    # Remove and then re-add the uniqueKeyField so that it is the last column and can be easily referenced.
    searchCursorFields.remove(str(uniqueKeyFieldToUse))
    searchCursorFields.append(str(uniqueKeyFieldToUse))
    insertCursorFields = searchCursorFields
    
    # Select the features in the source layer
    SelectLayerByAttribute_management(sourceFL, "NEW_SELECTION", featuresToSelect)
    # Repeat the selection in the target layer
    SelectLayerByAttribute_management(targetFL, "NEW_SELECTION", featuresToSelect)
    # Then select the common segments spatially, with some room for possible movement.
    SelectLayerByLocation_management(targetFL, 'WITHIN_A_DISTANCE', sourceFL, 50, 'SUBSET_SELECTION')
    
    # 5.) Count selected features in the target and delete them if there is at least 1.
    countResult0 = GetCount_management(targetFL)
    intCount0 = int(countResult0.getOutput(0))
    if intCount0 >= 1:
        # 12.) Delete the selected features in the input layer, if any.
        try:
            DeleteFeatures_management(targetFL)
        except:
            print("Could not delete features for the selection " + str(featuresToSelect) + ".")
    else:
        pass
    
    # 10.) Count to make sure that at least one feature is selected.
    countResult1 = GetCount_management(sourceFL)
    intCount1 = int(countResult1.getOutput(0))
    if intCount1 >= 1:
        # If so, cursor the features out
        featureList = list()
        
        newCursor = daSearchCursor(sourceFL, searchCursorFields)
        
        for cursorItem in newCursor:
            featureList.append(list(cursorItem))
        
        try:
            del newCursor
        except:
            pass
        
        # 11.) Insert the selected source features into the copy of the centerlines.
        newCursor = daInsertCursor(targetFL, insertCursorFields)
        
        for featureItem in featureList:
            newCursor.insertRow(featureItem)
        
        try:
            del newCursor
        except:
            pass
        try:
            del featureList
        except:
            pass
def iteratorprocess():
    
    env.workspace = extractDataGDBPath
    accDataFeaturesList = ListFeatureClasses("CrashLocation.GEO.ACC*")
    # Use the FullTable for the overall total.
    accDataFullTable = os.path.join(extractDataGDBPath, r'CrashLocation.GEO.GIS_GEOCODE_ACC')
    
    withRoadsTotal = 0
    
    gcKTotal = 0
    gcNKTotal = 0
    ofsKTotal = 0
    ofsNKTotal = 0
    NG911CoAccidents = 0
    
    inMemoryTempLayer = 'inMemoryTempFC'
    
    for countyItem in coAbbrAndNoList:
        countyNumber = countyItem[1]
        countyName = countyItem[2]
        
        accDataPointsKDOT = "CrashLocation.GEO.ACC_PTS_" + countyNumber
        accDataPointsNK = "CrashLocation.GEO.ACC_PTS_" + countyNumber + "_NK"
        accDataOffsetKDOT = "CrashLocation.GEO.ACC_OFS_PTS_" + countyNumber
        accDataOffsetNK = "CrashLocation.GEO.ACC_OFS_PTS_" + countyNumber + "_NK"
        
        # Re-zero the loop variables here so that the table doesn't get incorrect information in it.
        
        totalAccidents = 0
        
        geocodedAccidents = 0
        geocodedAccidentsNK = 0
        offsetAccidents = 0
        offsetAccidentsNK = 0
        
        gcPercent = '0.00'
        gcNKPercent = '0.00'
        ofsPercent = '0.00'
        ofsNKPercent = '0.00'
        
        if (accDataPointsKDOT in accDataFeaturesList) or (accDataPointsNK in accDataFeaturesList) or \
            (accDataOffsetKDOT in accDataFeaturesList) or (accDataOffsetNK in accDataFeaturesList):
            
            if accDataPointsKDOT in accDataFeaturesList:
                
                try:
                    Delete_management(inMemoryTempLayer)
                except:
                    pass
                
                accDataPointsKDOTPath = os.path.join(extractDataGDBPath, accDataPointsKDOT)
                
                MakeFeatureLayer_management(accDataPointsKDOTPath, inMemoryTempLayer)
                
                #SelectLayerByAttribute_management(inMemoryTempLayer, 'CLEAR_SELECTION')
                
                tempResult = GetCount_management(inMemoryTempLayer)
                
                totalAccidents = int(tempResult.getOutput(0))
                
                if totalAccidents > 0:
                    withRoadsTotal += totalAccidents
                else:
                    pass
                
                selectWhereClause = """ Status <> 'U' """
                
                SelectLayerByAttribute_management(inMemoryTempLayer, 'NEW_SELECTION', selectWhereClause)
                
                tempResult = GetCount_management(inMemoryTempLayer)
                
                geocodedAccidents = int(tempResult.getOutput(0))
                
            else:
                pass
            
            if accDataPointsNK in accDataFeaturesList:
                
                try:
                    Delete_management(inMemoryTempLayer)
                except:
                    pass
                
                accDataPointsNKPath = os.path.join(extractDataGDBPath, accDataPointsNK)
                
                MakeFeatureLayer_management(accDataPointsNKPath, inMemoryTempLayer)
                
                selectWhereClause = """ Status <> 'U' """
                
                SelectLayerByAttribute_management(inMemoryTempLayer, 'NEW_SELECTION', selectWhereClause)
                
                tempResult = GetCount_management(inMemoryTempLayer)
                
                geocodedAccidentsNK = int(tempResult.getOutput(0))
                
            else:
                pass
            
            if accDataOffsetKDOT in accDataFeaturesList:
                
                try:
                    Delete_management(inMemoryTempLayer)
                except:
                    pass
                
                accDataOffsetKDOTPath = os.path.join(extractDataGDBPath, accDataOffsetKDOT)
                
                MakeFeatureLayer_management(accDataOffsetKDOTPath, inMemoryTempLayer)
                
                selectWhereClause = """ isOffset IS NOT NULL """
                
                SelectLayerByAttribute_management(inMemoryTempLayer, 'NEW_SELECTION', selectWhereClause)
                
                tempResult = GetCount_management(inMemoryTempLayer)
                
                offsetAccidents = int(tempResult.getOutput(0))
                
            else:
                pass
            
            if accDataOffsetNK in accDataFeaturesList:
                
                try:
                    Delete_management(inMemoryTempLayer)
                except:
                    pass
                
                accDataOffsetNKPath = os.path.join(extractDataGDBPath, accDataOffsetNK)
                
                MakeFeatureLayer_management(accDataOffsetNKPath, inMemoryTempLayer)
                
                selectWhereClause = """ isOffset IS NOT NULL """
                
                SelectLayerByAttribute_management(inMemoryTempLayer, 'NEW_SELECTION', selectWhereClause)
                
                tempResult = GetCount_management(inMemoryTempLayer)
                
                offsetAccidentsNK = int(tempResult.getOutput(0))
                
            else:
                pass
            
            try:
                gcPercent = "{0:.2f}".format((float(geocodedAccidents) / totalAccidents) * 100)
                gcNKPercent = "{0:.2f}".format((float(geocodedAccidentsNK) / totalAccidents) * 100)
                ofsPercent = "{0:.2f}".format((float(offsetAccidents) / totalAccidents) * 100)
                ofsNKPercent = "{0:.2f}".format((float(offsetAccidentsNK) / totalAccidents) * 100)
            except ZeroDivisionError:
                gcPercent = None
                gcNKPercent = None
                ofsPercent = None
                ofsNKPercent = None
            except:
                pass
            
            gcKTotal += geocodedAccidents
            gcNKTotal += geocodedAccidentsNK
            ofsKTotal += offsetAccidents
            ofsNKTotal += offsetAccidentsNK
            NG911CoAccidents += totalAccidents
            
            print("\n" + countyName + " County has " + str(totalAccidents) + " totalAccidents.")
            print("gcPercent: " + gcPercent + " gcNKPercent: " + gcNKPercent +
                  " ofsPercent: " + ofsPercent + " ofsNKPercent: " + ofsNKPercent)
            
        # To get the withRoadsTotal, sum the number for each county that
        # returned a non-zero result for totalAccidents.
        
        else:
            pass
        
        reportResult = [countyName, totalAccidents, gcPercent, gcNKPercent, ofsPercent, ofsNKPercent]
        reportResultsList.append(reportResult)
    
    try:
        Delete_management(inMemoryTempLayer)
    except:
        pass
    
    MakeTableView_management(accDataFullTable, inMemoryTempLayer)
    
    tempResult = GetCount_management(inMemoryTempLayer)
    
    overallTotal = int(tempResult.getOutput(0))
    
    for reportResultItem in reportResultsList:
        print str(reportResultItem[0])
    
    gcNG911Percent = "{0:.2f}".format((float(gcKTotal) / NG911CoAccidents) * 100)
    gcNKNG911Percent = "{0:.2f}".format((float(gcNKTotal) / NG911CoAccidents) * 100)
    ofsNG911Percent = "{0:.2f}".format((float(ofsKTotal) / NG911CoAccidents) * 100)
    ofsNKNG911Percent = "{0:.2f}".format((float(ofsNKTotal) / NG911CoAccidents) * 100)
    
    print "\n" + "The NG911Total is: " + str(NG911CoAccidents)
    print( " with gcPercent: " + gcNG911Percent + " gcNKPercent: " + gcNKNG911Percent + 
           " ofsPercent: " + ofsNG911Percent + " ofsNKPercent: " + ofsNKNG911Percent)
    
    reportResult = ["NG911Total", NG911CoAccidents, gcNG911Percent, gcNKNG911Percent, ofsNG911Percent, ofsNKNG911Percent]
    reportResultsList.append(reportResult)
    
    gcOverallPercent = "{0:.2f}".format((float(gcKTotal) / overallTotal) * 100)
    gcNKOverallPercent = "{0:.2f}".format((float(gcNKTotal) / overallTotal) * 100)
    ofsOverallPercent = "{0:.2f}".format((float(ofsKTotal) / overallTotal) * 100)
    ofsNKOverallPercent = "{0:.2f}".format((float(ofsNKTotal) / overallTotal) * 100)
    
    print "\n" + "The OverallTotal is: " + str(overallTotal)
    print (" with gcPercent: " + gcOverallPercent + " gcNKPercent: " + gcNKOverallPercent +
           " ofsPercent: " + ofsOverallPercent + " ofsNKPercent: " + ofsNKOverallPercent)
    
    reportResult = ["OverallTotal", overallTotal, gcOverallPercent, gcNKOverallPercent, ofsOverallPercent, ofsNKOverallPercent]
    reportResultsList.append(reportResult)
    
    resultsTablePath = recreateResultsTable()
    
    # Delete the previous table information, if any, then create an insert cursor
    # and place all of the report result items in the table.
    
    newICursor = InsertCursor(resultsTablePath, insertCursorFields)
    
    for reportResultItem in reportResultsList:
        insertedRowID = newICursor.insertRow(reportResultItem)
        print "Inserted a new row into the REPORT_INFO table with OID: " + str(insertedRowID)
예제 #21
0
            nameCity = row[0]

            # 
            queryString = '"' + nameField + '" = ' + "'" + nameCity + "'"

            # Make a feature layer of just the current city polygon
            # The queryString is reponsible for select only the feature of city
            MakeFeatureLayer_management(cityBoundaries, "CurrentCityLayer", queryString) 
            MakeFeatureLayer_management(parkAndRide, "ParkAndRide_lry")

            # Selecty by location all feature of park and ride that contain in current city
            SelectLayerByLocation_management("ParkAndRide_lry", "CONTAINED_BY", "CurrentCityLayer")

            # Get the total value of park and ride for each city
            countPark = GetCount_management("ParkAndRide_lry")
            totalPark = int(countPark.getOutput(0))

            # Count of city in cityBoundaries
            totalCity += 1

            # If the total park and rise is bigger then 1
            if totalPark > 1:

                # The row in field HasTwoParkAndRides update to "True"
                row[1] = "True"
            
                # Count each city has than more that two park and ride in your limits
                CityWithTwoParkAndRides += 1

            else:
            
예제 #22
0
def main():
    layer = GetParameterAsText(0)
    updateBlanksOnly = GetParameterAsText(1)

    expression = ""
    a = ""
    field_list = []

    #define object & field list
    if basename(layer) in ("RoadCenterline", "AddressPoints"):
        a = getFCObject(layer)
        field_list = a.LABEL_FIELDS
    else:
        userMessage(layer + " does not work with this tool. Please select the NG911 road centerline or address point file.")

    #make sure the object is something
    if a != "":
        #start at 1 since 0 is the label field itself
        i = 1

        #create the expression
        while i < len(field_list):
            #since the house number needs a string conversion, we need to have a slightly different expression for the first piece
            if i == 1:
                if basename(layer) == "AddressPoints":
                    expression = 'str(!' +  field_list[i] + '!) + " " + !'
                else:
                    expression = '!' + field_list[i] + '! + " " + !'

            else:
                expression = expression + field_list[i] + '! + " " + !'

            i += 1

        expression = expression[:-10]

    userMessage(expression)

    labelField = a.LABEL

    userMessage(labelField)

    if expression != "":
        lyr = "lyr"
        MakeFeatureLayer_management(layer, lyr)

        qry = labelField + " is null or " + labelField + " = '' or " + labelField + " = ' '"

        #select only the blank ones to update if that's what the user wanted
        if updateBlanksOnly == "true":
            SelectLayerByAttribute_management(lyr, "NEW_SELECTION", qry)

        userMessage("Calculating label...")
        CalculateField_management(lyr, labelField, expression, "PYTHON_9.3")

        #make sure no records were left behind
        SelectLayerByAttribute_management(lyr, "NEW_SELECTION", qry)
        result = GetCount_management(lyr)
        count = int(result.getOutput(0))

        #if the count is higher than 0, it means the table had null values in some of the concatonated fields
        if count > 0:
            gdb = dirname(dirname(layer))
            fields = tuple(field_list)

            #start edit session
            edit = Editor(gdb)
            edit.startEditing(False, False)

            #run update cursor
            with UpdateCursor(layer, fields, qry) as rows:
                for row in rows:
                    field_count = len(fields)
                    start_int = 1
                    label = ""

                    #loop through the fields to see what's null & skip it
                    while start_int < field_count:
                        if row[start_int] is not None:
                            if row[start_int] not in ("", " "):
                                label = label + " " + str(row[start_int])
                        start_int = start_int + 1

                    row[0] = label
                    rows.updateRow(row)

            edit.stopEditing(True)


        #clean up all labels
        trim_expression = '" ".join(!' + labelField + '!.split())'
        CalculateField_management(layer, labelField, trim_expression, "PYTHON_9.3")
예제 #23
0
def transferBasedOnLocalRouteKeys():
    # Build a list of the unique route keys that match the given criteria:
    # Then, use that list to select the features in the source with those
    # keys.
    # Next, spatially select features in the target layer with the
    # selected features from the source layer.
    # If there are more than 0 features selected, delete the selected
    # target features.
    # Then, cursor in the selected source features.

    subsetSelectionQuery = """ KDOT_LRS_KEY LIKE '%L%' AND NOT KDOT_LRS_KEY LIKE '%W%' """

    fcAsFeatureLayerForTransferring = 'FCAsFeatureLayer_Transferring'

    if Exists(fcAsFeatureLayerForTransferring):
        Delete_management(fcAsFeatureLayerForTransferring)
    else:
        pass

    MakeFeatureLayer_management(fcWithroutesToTransferFrom,
                                fcAsFeatureLayerForTransferring)
    MakeFeatureLayer_management(targetFC1, targetFC1asFeatureLayer)

    lrsKeyFieldList = [str(lrsKeyToUse)]
    newCursor = daSearchCursor(fcWithroutesToTransferFrom, lrsKeyFieldList,
                               subsetSelectionQuery)
    uniqueLRSKeysDict = dict()
    for cursorRow in newCursor:
        uniqueLRSKeysDict[str(cursorRow[0])] = 1

    try:
        del newCursor
    except:
        pass

    uniqueLRSKeysList = uniqueLRSKeysDict.keys()
    try:
        uniqueLRSKeysList.remove('None')
    except:
        print(
            "Could not remove 'None' from the list of uniqueLRSKeys since it was not a part of the list."
        )

    print("LRSKey list creation successful.")
    print('Found ' + str(len(uniqueLRSKeysList)) +
          ' unique LRS Keys in the centerline data for this query:')
    print(str(subsetSelectionQuery))

    #Use multiSelection

    multiSelectionQueryBase = str(
        str(subsetSelectionQuery) + ''' AND ''' + ''' "''' + str(lrsKeyToUse) +
        '''" IS NOT NULL AND "''' + str(lrsKeyToUse) + '''" IN (''')
    multiSelectionQuery = multiSelectionQueryBase
    multiCounter = 0
    ##multiDissolveFields = [str(lrsKeyToUse), 'LRS_COUNTY_PRE', 'LRS_ROUTE_PREFIX', 'LRS_ROUTE_NUM', 'LRS_ROUTE_SUFFIX', 'LRS_UNIQUE_IDENT',
    ##    'LRS_UNIQUE_IDENT1', 'KDOT_COUNTY_L', 'KDOT_COUNTY_R']
    ##multiDissolveFields = str(lrsKeyToUse) + ';LRS_COUNTY_PRE;LRS_ROUTE_PREFIX;LRS_ROUTE_NUM;LRS_ROUTE_SUFFIX;LRS_UNIQUE_IDENT;LRS_UNIQUE_IDENT1'
    ##multiStatsFields = str(n1FromMeas) + " MIN;" + str(n1ToMeas) + " MAX"
    ##multiStatsFields = ""
    ##singlePart = "SINGLE_PART"
    ##unsplitLines = "UNSPLIT_LINES"

    # 3.) Loop through the list of unique LRS Keys
    for uniqueKeyItem in uniqueLRSKeysList:
        # Make a selection list that includes 50 keys, then select the keys and dissolve to make a new
        # feature class.
        # After the selection is dissolved, use a spatial select on the original feature class and
        # an attribute selection on the original feature class to see which original features should
        # be deleted.
        # Then, delete the selected features (if at least 1 selected).

        # Basically, doing it piece by piece is a problem since I'm not including LRS KEY
        # selections to prevent the spatial selection from deleting pieces that overlap, but
        # that should have different LRS Keys. Need to do all of the features
        # at once to make sure that I'm not deleting pieces that should actually be there.
        # Seems like it shouldn't be a problem, but the numbers say it is.
        # 4.) For groups of 200000 LRS Keys, select all the features with those LRS Keys.
        if multiCounter <= 199999:
            multiSelectionQuery += """'""" + str(
                uniqueKeyItem) + """'""" + """, """
            multiCounter += 1
        else:
            # Add the current item, then
            multiSelectionQuery += """'""" + str(
                uniqueKeyItem) + """'""" + """, """
            # Remove the trailing ", " and add a closing parenthesis.
            multiSelectionQuery = multiSelectionQuery[:-2] + """) """
            SelectLayerByAttribute_management(fcAsFeatureLayerForTransferring,
                                              "NEW_SELECTION",
                                              multiSelectionQuery)
            # Have to do from step 5 on here also.

            ### -shouldbeafunctionblock#1- ###
            # 5.) Count selected features.
            countResult0 = GetCount_management(fcAsFeatureLayerForTransferring)
            intCount0 = int(countResult0.getOutput(0))
            if intCount0 >= 1:
                print(
                    "Spatially selecting with the fcAsFeatureLayerForTransferring features, of which there are "
                    + str(intCount0) + " selected.")
                ##print("Selected by this query:")
                ##print(str(multiSelectionQuery))
                # 9.) Else, spatially select features in the original feature class with 'SHARE_A_LINE_SEGMENT_WITH'.
                SelectLayerByLocation_management(
                    targetFC1asFeatureLayer, "SHARE_A_LINE_SEGMENT_WITH",
                    fcAsFeatureLayerForTransferring, 0, "NEW_SELECTION")
                # Added to prevent the Selection from taking over '%W%' routes at this time.
                SelectLayerByAttribute_management(targetFC1asFeatureLayer,
                                                  "SUBSET_SELECTION",
                                                  subsetSelectionQuery)

                # 10.) Count to make sure that at least one feature is selected.
                countResult2 = GetCount_management(targetFC1asFeatureLayer)
                intCount2 = int(countResult2.getOutput(0))
                print(
                    'There were ' + str(intCount2) +
                    ' features selected for replacement in the targetFC1asFeatureLayer layer.'
                )
                if intCount2 >= 1:
                    # 11.) If so, cursor the features out of the dissolve layer.
                    featureList = list()
                    searchCursorFields = [
                        str(lrsKeyToUse),
                        str(startMeasure),
                        str(endMeasure), 'LRS_COUNTY_PRE', 'LRS_ROUTE_PREFIX',
                        'LRS_ROUTE_NUM', 'LRS_ROUTE_SUFFIX',
                        'LRS_UNIQUE_IDENT', 'LRS_UNIQUE_IDENT1',
                        'KDOT_COUNTY_L', 'KDOT_COUNTY_R', 'LABEL', 'SHAPE@'
                    ]
                    newCursor = daSearchCursor(fcAsFeatureLayerForTransferring,
                                               searchCursorFields)

                    for cursorItem in newCursor:
                        featureList.append(list(cursorItem))

                    try:
                        del newCursor
                    except:
                        pass

                    # 12.) Delete the selected features in the input layer.
                    try:
                        DeleteFeatures_management(targetFC1asFeatureLayer)
                    except:
                        print("Could not delete features for the selection " +
                              str(multiSelectionQuery) + ".")
                    # 13.) Insert the features from the dissolve layer into the copy of the centerlines.
                    insertCursorFields = [
                        str(lrsKeyToUse),
                        str(startMeasure),
                        str(endMeasure), 'LRS_COUNTY_PRE', 'LRS_ROUTE_PREFIX',
                        'LRS_ROUTE_NUM', 'LRS_ROUTE_SUFFIX',
                        'LRS_UNIQUE_IDENT', 'LRS_UNIQUE_IDENT1',
                        'KDOT_COUNTY_L', 'KDOT_COUNTY_R', 'LABEL', 'SHAPE@'
                    ]
                    newCursor = daInsertCursor(targetFC1asFeatureLayer,
                                               insertCursorFields)

                    for featureItem in featureList:
                        newCursor.insertRow(featureItem)

                    try:
                        del newCursor
                    except:
                        pass
                    try:
                        del featureList
                    except:
                        pass

                else:
                    pass
            ### -shouldbeafunctionblock#1- ###
            multiSelectionQuery = ''' "''' + str(
                lrsKeyToUse) + '''" IS NOT NULL AND "''' + str(
                    lrsKeyToUse) + '''" IN ('''
            multiCounter = 0

    # After the for loop, if there is still anything remaining which was unselected in the
    # the previous multiSelectionQuery steps.
    # Remove the trailing ", " and add a closing parenthesis.
    if multiSelectionQuery != multiSelectionQueryBase:
        multiSelectionQuery = multiSelectionQuery[:-2] + """) """
    else:
        # The selection query would not select anything.
        return
    SelectLayerByAttribute_management(fcAsFeatureLayerForTransferring,
                                      "NEW_SELECTION", multiSelectionQuery)

    # Then redo from step 5 on at the end of the loop IF there is anything left to select
    # which was not selected... so if selectionCounter != 0.

    ### -shouldbeafunctionblock#2- ###
    # 5.) Count selected features.
    countResult0 = GetCount_management(fcAsFeatureLayerForTransferring)
    intCount0 = int(countResult0.getOutput(0))
    if intCount0 >= 1:
        print(
            "Spatially selecting with the fcAsFeatureLayerForTransferring features, of which there are "
            + str(intCount0) + " selected.")
        ##print("Selected by this query:")
        ##print(str(multiSelectionQuery))
        # 9.) Else, spatially select features in the original feature class with 'SHARE_A_LINE_SEGMENT_WITH'.
        SelectLayerByLocation_management(targetFC1asFeatureLayer,
                                         "SHARE_A_LINE_SEGMENT_WITH",
                                         fcAsFeatureLayerForTransferring, 0,
                                         "NEW_SELECTION")
        # Added to prevent the Selection from taking over '%W%' routes at this time.
        SelectLayerByAttribute_management(targetFC1asFeatureLayer,
                                          "SUBSET_SELECTION",
                                          subsetSelectionQuery)

        # 10.) Count to make sure that at least one feature is selected.
        countResult2 = GetCount_management(targetFC1asFeatureLayer)
        intCount2 = int(countResult2.getOutput(0))
        print(
            'There were ' + str(intCount2) +
            ' features selected for replacement in the targetFC1asFeatureLayer layer.'
        )
        if intCount2 >= 1:
            # 11.) If so, cursor the features out of the dissolve layer.
            featureList = list()
            searchCursorFields = [
                str(lrsKeyToUse),
                str(startMeasure),
                str(endMeasure), 'LRS_COUNTY_PRE', 'LRS_ROUTE_PREFIX',
                'LRS_ROUTE_NUM', 'LRS_ROUTE_SUFFIX', 'LRS_UNIQUE_IDENT',
                'LRS_UNIQUE_IDENT1', 'KDOT_COUNTY_L', 'KDOT_COUNTY_R', 'LABEL',
                'SHAPE@'
            ]
            newCursor = daSearchCursor(fcAsFeatureLayerForTransferring,
                                       searchCursorFields)

            for cursorItem in newCursor:
                featureList.append(list(cursorItem))

            try:
                del newCursor
            except:
                pass

            # 12.) Delete the selected features in the input layer.
            try:
                DeleteFeatures_management(targetFC1asFeatureLayer)
            except:
                print("Could not delete features for the selection " +
                      str(multiSelectionQuery) + ".")
            # 13.) Insert the features from the dissolve layer into the copy of the centerlines.
            insertCursorFields = [
                str(lrsKeyToUse),
                str(startMeasure),
                str(endMeasure), 'LRS_COUNTY_PRE', 'LRS_ROUTE_PREFIX',
                'LRS_ROUTE_NUM', 'LRS_ROUTE_SUFFIX', 'LRS_UNIQUE_IDENT',
                'LRS_UNIQUE_IDENT1', 'KDOT_COUNTY_L', 'KDOT_COUNTY_R', 'LABEL',
                'SHAPE@'
            ]
            newCursor = daInsertCursor(targetFC1asFeatureLayer,
                                       insertCursorFields)

            for featureItem in featureList:
                newCursor.insertRow(featureItem)

            try:
                del newCursor
            except:
                pass
            try:
                del featureList
            except:
                pass

        else:
            pass
예제 #24
0
def checkRequiredFieldValues(pathsInfoObject):
    gdb = pathsInfoObject.gdbPath
    folder = pathsInfoObject.domainsFolderPath
    esb = pathsInfoObject.esbList
    version = pathsInfoObject.gdbVersion

    userMessage("Checking that required fields have all values...")

    #get today's date
    today = strftime("%m/%d/%y")

    #get required fields
    rfDict = getRequiredFields(folder, version)

    if rfDict != {}:

        values = []

        #walk through the tables/feature classes
        for dirpath, dirnames, filenames in Walk(gdb, True, '', False, ["Table","FeatureClass"]):
            for filename in filenames:
                if filename.upper() not in ("FIELDVALUESCHECKRESULTS", "TEMPLATECHECKRESULTS"):
                    fullPath = path.join(gdb, filename)
                    if filename.upper() in esb:
                        layer = "ESB"
                    else:
                        layer = filename.upper()
                    id1 = getUniqueIDField(layer)
                    if id1 != "":

                        #get the keyword to acquire required field names
                        keyword = getKeyword(filename, esb)

                        #goal: get list of required fields that are present in the feature class
                        #get the appropriate required field list
                        if keyword in rfDict:
                            requiredFieldList = rfDict[keyword]

                        rfl = []
                        for rf in requiredFieldList:
                            rfl.append(rf.upper())

                        #get list of fields in the feature class
                        allFields = ListFields(fullPath)

                        #make list of field names
                        fields = []
                        for aF in allFields:
                            fields.append(aF.name.upper())

                        #convert lists to sets
                        set1 = set(rfl)
                        set2 = set(fields)

                        #get the set of fields that are the same
                        matchingFields = list(set1 & set2)

                        #only work with records that are for submission
                        lyr2 = "lyr2"
                        if version == "10":
                            MakeTableView_management(fullPath, lyr2)
                        else:
                            wc2 = "SUBMIT = 'Y'"
                            MakeTableView_management(fullPath, lyr2, wc2)

                        #create where clause to select any records where required values aren't populated
                        wc = ""

                        for field in matchingFields:
                            wc = wc + " " + field + " is null or "

                        wc = wc[0:-4]

                        #make table view using where clause
                        lyr = "lyr"
                        MakeTableView_management(lyr2, lyr, wc)

                        #get count of the results
                        result = GetCount_management(lyr)
                        count = int(result.getOutput(0))


                        #if count is greater than 0, it means a required value somewhere isn't filled in
                        if count > 0:
                            #make sure the objectID gets included in the search for reporting
                            if id1 not in matchingFields:
                                matchingFields.append(id1)

                            #run a search cursor to get any/all records where a required field value is null
                            with SearchCursor(fullPath, (matchingFields), wc) as rows:
                                for row in rows:
                                    k = 0
                                    #get object ID of the field
                                    oid = str(row[matchingFields.index(id1)])

                                    #loop through row
                                    while k < len(matchingFields):
                                        #see if the value is nothing
                                        if row[k] is None:
                                            #report the value if it is indeed null
                                            report = matchingFields[k] + " is null for Feature ID " + oid
                                            userMessage(report)
                                            val = (today, report, filename, matchingFields[k], oid)
                                            values.append(val)

                                        #iterate!
                                        k = k + 1
                        else:
                            userMessage( "All required values present for " + filename)

                        Delete_management(lyr)
                        Delete_management(lyr2)

        if values != []:
            RecordResults("fieldValues", values, gdb)

        userMessage("Completed check for required field values: " + str(len(values)) + " issues found")

    else:
        userMessage("Could not check required field values")
예제 #25
0
# Try update field
try:

    # Select the field HasParkAndRide of CityBoundaries layer 
    with UpdateCursor("CityBoundaries_lyr", (nameField,)) as cursor:

        # For each row in field
        for row in cursor:

            # Update row to 'True'
            row[0] = "True"

# If happen some error            
except:
    # Show the massage
    print "It not possible update a field: ", nameField, "for the selected features"

# Get the total value of cities in CityBoundaries 
city = GetCount_management("CityBoundaries")
totalCity = int(city.getOutput(0))

# Get the total value of cities seleted in CityBoundaries layer
citySelected = GetCount_management("CityBoundaries_lyr")
totalCitySelected = int(citySelected.getOutput(0))

# ---------------------------------SHOW PERCENTAGE VALUE---------------------------------------

# Show the percentage value
percentage(totalCity, totalCitySelected)
예제 #26
0
def checkFeatureLocations(pathsInfoObject):
    gdb = pathsInfoObject.gdbPath
    fcList = pathsInfoObject.fcList
    esb = pathsInfoObject.esbList
    version = pathsInfoObject.gdbVersion

    RoadAlias = join(gdb, "RoadAlias")

    if RoadAlias in fcList:
        fcList.remove(RoadAlias)

    userMessage("Checking feature locations...")

    #get today's date
    today = strftime("%m/%d/%y")
    values = []

    #make sure features are all inside authoritative boundary

    #get authoritative boundary
    authBound = path.join(gdb, "NG911", "AuthoritativeBoundary")
    ab = "ab"

    MakeFeatureLayer_management(authBound, ab)

    for fullPath in fcList:
        fl = "fl"
        if version == "10":
            MakeFeatureLayer_management(fullPath, fl)
        else:
            if "RoadCenterline" in fullPath:
                wc = "SUBMIT = 'Y' AND EXCEPTION not in ('EXCEPTION INSIDE', 'EXCEPTION BOTH')"
            else:
                wc = "SUBMIT = 'Y'"
            MakeFeatureLayer_management(fullPath, fl, wc)

        try:

            #select by location to get count of features outside the authoritative boundary
            SelectLayerByLocation_management(fl, "WITHIN", ab)
            SelectLayerByAttribute_management(fl, "SWITCH_SELECTION", "")
            #get count of selected records
            result = GetCount_management(fl)
            count = int(result.getOutput(0))

            #report results
            if count > 0:
                layer = basename(fullPath)
                if layer in esb:
                    layerName = "ESB"
                else:
                    layerName = layer
                id1 = getUniqueIDField(layerName.upper())
                report = "Feature not inside authoritative boundary"
                if id1 != '':
                    with SearchCursor(fl, (id1)) as rows:
                        for row in rows:
                            fID = row[0]
                            val = (today, report, layer, " ", fID)
                            values.append(val)
                else:
                    userMessage("Could not process features in " + fullPath)
            else:
                userMessage( fullPath + ": all records inside authoritative boundary")
        except:
            userMessage("Could not check locations of " + fullPath)

        finally:

            #clean up
            Delete_management(fl)

    if values != []:
        RecordResults("fieldValues", values, gdb)


    userMessage("Completed check on feature locations: " + str(len(values)) + " issues found")
예제 #27
0
def KDOTRampReplacement_Old():
    # Until the KDOT process is included here,
    # this defaults to the Transcend process.
    
    # This should look at the Ramps_LRSKeys that exist in the
    # All_Road_Centerlines layer and the Interchange_Ramp layer
    # then create a list of the ones that are common between the
    # two. Then, reduce that list to the set which is unique so
    # that we don't have to worry about possible duplicates.
    #
    # The non-duplicated list will then be used as a source
    # to get a ramp in the All_Road_Centerlines
    # FC and replace it's geometry with that from
    # the Interchange_Ramp FC. Then, if there are any
    # other All_Road_Centerlines features with that same
    # Ramps_LRSKey, they should be removed.
    # -- We'll still have the key relationship in the
    # All_Roads_Centerline class, so might not need to
    # make an additional table that tells which additional
    # segments were removed, but it might be good for reporting
    # and for making sure that the data doesn't disappear if we
    # end up making changes to one of the feature classes that
    # could destroy the data later on.
    #
    # Can probably just make a table in the *_Source gdb that
    # lists all of the Ramp_LRSKeys and the GCIDs of the features
    # that had geometry copied into them and the ones that were
    # removed without having geometry copied into them.
    #
    # If there is a ramp in the Interchange_Ramps FC that does
    # not have any matches in the All_Road_Centerlines FC, then
    # it is probably from a region outside of the current set.
    # This won't be a problem once we have the properly conflated
    # R1-R5 with LRS_Backwards keys, but until that time, the
    # script will need to recognize that there will be some areas
    # that do not have features that correspond to everything
    # in the Interchange_Ramps FC, so we'll need to not transfer
    # those, but log them.
    #
    # After all of the regions are in the same GDB, the logging
    # of the unmatched Interchange_Ramp features will be useful
    # in determining conflation errors or missing data. We could
    # even use it to see if there are missing Ramp keys in the
    # All_Road_Centerline data at that point.
    
    MakeFeatureLayer_management(routesSourceCenterlines, featureLayer)
    
    SelectLayerByAttribute_management(featureLayer, "CLEAR_SELECTION")
    selectionQuery = """ LRS_ROUTE_PREFIX = 'X' AND Ramps_LRSKey IS NOT NULL AND Ramps_LRSKey <> '' """
    SelectLayerByAttribute_management(featureLayer, "NEW_SELECTION", selectionQuery)
    
    countResult = GetCount_management(featureLayer)
    intCount = int(countResult.getOutput(0))
    
    print('Selected ' + str(intCount) + ' ramp features to be replaced.')
    
    if intCount > 0:
        print("Deleting those ramp features from the " + returnFeatureClass(routesSourceCenterlines) + " layer.")
        DeleteFeatures_management(featureLayer)
    else:
        print("No features selected. Skipping feature deletion.")
    
    # Remove the matching routes to prepare for the Interchange_Ramps information.
    ## After error matching is achieved, use replace geometry and replace attributes to not lose data
    ## from using the less effective method of:
    ## deleting the old Interchange_Ramps information, then re-adding with append.
    
    # Add the Interchange_Ramps information.
    
    # Checking to see if the copy for repairing already exists.
    # If so, remove it.
    if Exists(interchangeRampFCRepairCopy):
        Delete_management(interchangeRampFCRepairCopy)
    else:
        pass
    # Create a new file for the copy for repairing since repair modifies the input.
    CopyFeatures_management(interchangeRampFC, interchangeRampFCRepairCopy)

    # Repairs the geometry, modifies input.
    # Deletes features with null geometry (2 expected, until Shared.Interchange_Ramp is fixed).
    print("Repairing ramp geometry in the " + returnFeatureClass(interchangeRampFCRepairCopy) + " layer.")
    RepairGeometry_management(interchangeRampFCRepairCopy, "DELETE_NULL")

    # Create a fieldmapping object so that the Interchange_Ramps can be correctly imported with append.
    appendInputs = [interchangeRampFCRepairCopy]
    appendTarget = routesSourceCenterlines
    schemaType = "NO_TEST"

    # Field mapping goes here.
    # Interchange_Ramp.LRS_KEY to RoutesSource_Test.LRSKEY
    fm_Field1 = FieldMap()
    fm_Field1.addInputField(interchangeRampFCRepairCopy, "LRS_KEY")
    fm_Field1_OutField = fm_Field1.outputField
    fm_Field1_OutField.name = 'LRSKEY'
    fm_Field1.outputField = fm_Field1_OutField

    # Interchange_Ramp.BEG_CNTY_LOGMILE to RoutesSource_Test.NON_STATE_BEGIN_MP
    fm_Field2 = FieldMap()
    fm_Field2.addInputField(interchangeRampFCRepairCopy, "BEG_CNTY_LOGMILE")
    fm_Field2_OutField = fm_Field2.outputField
    fm_Field2_OutField.name = 'NON_STATE_BEGIN_MP'
    fm_Field2.outputField = fm_Field2_OutField

    # Interchange_Ramp.END_CNTY_LOGMILE to RoutesSource_Test.NON_STATE_END_MP
    fm_Field3 = FieldMap()
    fm_Field3.addInputField(interchangeRampFCRepairCopy, "END_CNTY_LOGMILE")
    fm_Field3_OutField = fm_Field3.outputField
    fm_Field3_OutField.name = 'NON_STATE_END_MP'
    fm_Field3.outputField = fm_Field3_OutField

    # Interchange_Ramp.Shape_Length to RoutesSource_Test.Shape_Length
    fm_Field4 = FieldMap()
    fm_Field4.addInputField(interchangeRampFCRepairCopy, "Shape_Length")
    fm_Field4_OutField = fm_Field4.outputField
    fm_Field4_OutField.name = 'Shape_Length'
    fm_Field4.outputField = fm_Field4_OutField

    # Create the fieldMappings object and add the fieldMap objects to it.
    interchangeRampsMappings = FieldMappings()
    interchangeRampsMappings.addFieldMap(fm_Field1)
    interchangeRampsMappings.addFieldMap(fm_Field2)
    interchangeRampsMappings.addFieldMap(fm_Field3)
    interchangeRampsMappings.addFieldMap(fm_Field4)

    # Perform the append.
    print("Appending the features from " + returnFeatureClass(interchangeRampFCRepairCopy) + " into " + returnFeatureClass(routesSourceCenterlines) + ".")
    Append_management(appendInputs, appendTarget, schemaType, interchangeRampsMappings)
예제 #28
0
def checkRequiredFieldValues(gdb, folder, esb):
    userMessage("Checking that required fields have all values...")
    from os import path
    from arcpy.da import Walk, SearchCursor
    from arcpy import MakeTableView_management, Delete_management, GetCount_management, ListFields
    from time import strftime

    #get today's date
    today = strftime("%m/%d/%y")

    #get required fields
    rfDict = getRequiredFields(folder)

    id = "OBJECTID"
    values = []

    #walk through the tables/feature classes
    for dirpath, dirnames, filenames in Walk(gdb, True, '', False, ["Table","FeatureClass"]):
        for filename in filenames:
            fullPath = path.join(gdb, filename)

            #get the keyword to acquire required field names
            keyword = getKeyword(filename, esb)

            #goal: get list of required fields that are present in the feature class
            #get the appropriate required field list
            if keyword in rfDict:
                requiredFieldList = rfDict[keyword]

            rfl = []
            for rf in requiredFieldList:
                rfl.append(rf.upper())

            #get list of fields in the feature class
            allFields = ListFields(fullPath)

            #make list of field names
            fields = []
            for aF in allFields:
                fields.append(aF.name.upper())

            #convert lists to sets
            set1 = set(rfl)
            set2 = set(fields)

            #get the set of fields that are the same
            matchingFields = list(set1 & set2)

            #create where clause to select any records where required values aren't populated
            wc = ""

            for field in matchingFields:
                wc = wc + " " + field + " is null or "

            wc = wc[0:-4]

            #make table view using where clause
            lyr = "lyr"
            MakeTableView_management(fullPath, lyr, wc)

            #get count of the results
            result = GetCount_management(lyr)
            count = int(result.getOutput(0))

            #if count is greater than 0, it means a required value somewhere isn't filled in
            if count > 0:
                #make sure the objectID gets included in the search for reporting
                if id not in matchingFields:
                    matchingFields.append(id)

                i = len(matchingFields)
                k = 0

                #run a search cursor to get any/all records where a required field value is null
                with SearchCursor(fullPath, (matchingFields), wc) as rows:
                    for row in rows:

                        #get object ID of the field
                        oid = str(row[matchingFields.index(id)])

                        #loop through row
                        while k < 0:
                            #see if the value is nothing
                            if row[k] is None:
                                #report the value if it is indeed null
                                report = matchingFields[k] + " is null for ObjectID " + oid
                                userMessage(report)
                                val = (today, report, filename, matchingFields[k], oid)
                                values.append(val)

                            #iterate!
                            k = k + 1
            else:
                userMessage( "All required values present for " + filename)

            Delete_management(lyr)

    if values != "":
        RecordResults("fieldValues", values, gdb)

    userMessage("Completed check for required field values")
def thresholdRemoval(quarterOrHalf):
    # Change to look at the number of roads that are in the
    # county's erased polygon. -- the erased?
    # Then, if there are not at least that % of the
    # roads labeled as points in the pointIntersection
    # layer, remove ALL points for that county.

    # Rewrite this to test for the number of null points.

    # 1 through 100, percentage as integer. ## 25 seems to work well. Results in only 12 counties not having enough points.
    thresholdValue = 25
    # One county (42) doesn't have enough roads information for this script to do anything at all.

    if quarterOrHalf.lower() == "quarter":
        countyRoadNameRosette = countyRoadNameRosette_Q
    elif quarterOrHalf.lower() == "half":
        countyRoadNameRosette = countyRoadNameRosette_H
    else:
        print "quarterOrHalf variable not correctly defined."
        raise (Exception("quarterOrHalf error."))

    #makefeaturelayer1
    MakeFeatureLayer_management(countyRoadsFeature, "loadedCountyRoads")
    #makefeaturelayer2
    MakeFeatureLayer_management(countyRoadNameRosette, "loadedRoadNameRosette")

    for i in xrange(1, 106):
        roadThresholdWhereClause = """ "COUNTY_NUMBER" = """ + str(i) + """ """
        rosetteThresholdWhereClause = """ "COUNTY_NUMBER" = ' """ + str(
            i) + """ ' """

        #selectfeatures1
        SelectLayerByAttribute_management("loadedCountyRoads", "NEW_SELECTION",
                                          roadThresholdWhereClause)
        #selectfeatures2
        SelectLayerByAttribute_management("loadedRoadNameRosette",
                                          "NEW_SELECTION",
                                          rosetteThresholdWhereClause)

        #createfeaturelayer with whereclause, or do this then make a select clause.
        countyRoadsCount = GetCount_management("loadedCountyRoads")
        countyPointsCount = GetCount_management("loadedRoadNameRosette")

        countyRoadsCount = int(countyRoadsCount.getOutput(0))
        countyPointsCount = int(countyPointsCount.getOutput(0))

        if countyRoadsCount >= 1:
            if (float(countyPointsCount) / float(countyRoadsCount)) >= (
                    float(thresholdValue) /
                    float(100)) and countyPointsCount >= 20:
                print "Threshold value OK for County Number: " + str(i) + "."
                pass
            else:
                print "Threshold value not met for County Number: " + str(
                    i) + "."
                if countyPointsCount >= 1:
                    print "Removing road name rosette points from this county."
                    DeleteRows_management("loadedRoadNameRosette")
                else:
                    print "Would have deleted the points for this county, but none exist to delete."
        else:
            print "No County Roads found for County Number: " + str(i) + "."
예제 #30
0
def ListSplitAndSelect():
    ## Fragment used for testing, will not run properly on its own.
    
    delcount = GetCount_management(r'Database Connections\CANT_CPMS.sde\CPMS.CPMS_FMIS_GIS_DEL_ROWS')
    print str(delcount)+" records to delete"
    deletelist=list()
    ## Only portion below changed.
    if (not True): # Production version tests for delcount threshold.
        pass

    else: #ORA 01795 - Oracle limited to 1000 statements with select in *
        MakeTableView_management(FMIS_LOAD, "FMIS_TABLE")  # @UndefinedVariable
        MakeTableView_management(deltbl, "deletes")  # @UndefinedVariable
        with da.SearchCursor(deltbl, "PROJECT_NUMBER") as delcur:  # @UndefinedVariable
            for row in delcur:
                DelXID=  ("{0}".format(row[0]))
                #print DelXID + " is being deleted from the FMIS table"
                #AddJoin_management(layer_name,"CROSSINGID", deltbl, "CROSSINGID", "KEEP_ALL")
                #delsel = "PROJECT_NUMBER LIKE '"+DelXID+"'"
                #print delsel
                deletelist.append(DelXID)
                #SelectLayerByAttribute_management("FMIS_TABLE", "ADD_TO_SELECTION", delsel) #delsel not yet defined
                #SelectLayerByAttribute_management("FMIS_TABLE","ADD_TO_SELECTION", delsel)
        #print str(deletelist)
                
        #Take care of > 1000 selection issue here by splitting the long list into a series of lists.
        
        maxListSize = 999
        listStart = 0
        listEnd = maxListSize
        i = 0
        curListSize = len(deletelist)
        loopNumber = mathCeil(curListSize / maxListSize)
        firstContainer = list()
        
        # Creates the list container that holds the lists with the project numbers.
        while i <= loopNumber:
            if listEnd > curListSize:
                listEnd = curListSize
            else:
                pass
            listToHold = deletelist[listStart:listEnd]
            firstContainer.append(listToHold)
            i += 1
            listStart = listEnd
            listEnd = listEnd + maxListSize
        
        for secondContainer in firstContainer:
            delsel = "PROJECT_NUMBER IN ("
            for projectNum in secondContainer:
                delsel = delsel + """'"""+projectNum+"""', """
            delsel = delsel[:-2] # Slice that removes the last comma and trailing space.
            delsel = delsel + ")" # Adds the closing parenthesis.
            SelectLayerByAttribute_management("FMIS_TABLE", "ADD_TO_SELECTION", delsel) # ADD_TO_SELECTION works like NEW_SELECTION when no current selection.
            print delsel
        
        countResult = GetCount_management("FMIS_TABLE")
        countNum = int(countResult.getOutput(0))
        print countNum
        
        #DeleteRows_management("FMIS_TABLE")
    print "Delete function completed"
def removeSmallRoads():

    # Going to have to build a list of OIDs for roads
    # with a Shape length less than or equal to 1500.
    # Not going to have the SQL information to do a
    # selection based on a clause.

    # Could also add a field and then calculate the
    # length into it prior to running this selection.

    # Need to make a search cursor that gets the ObjectID and ShapeLength
    # for each road.

    # Then, need to add the ObjectID for roads with ShapeLength less than
    # 1500 to a list, then build SQL queries dynamically to select
    # and add features from that list, until the list is exhausted
    # and all features have been selected.

    print "Removing the small roads from the data."

    #CopyFeatures_management(countyRoadsFeature, countyRoadsFeaturePrereduction_Q)

    inMemoryRoadsLayer = 'inMemoryRoadsLayerFC'

    MakeFeatureLayer_management(countyRoadsFeature, inMemoryRoadsLayer)

    inMemRoadsFields = ListFields(inMemoryRoadsLayer)

    for inMemRoadField in inMemRoadsFields:
        print str(inMemRoadField.name)

    smallRoadsSCFields = ['ID2', 'Shape@Length']

    smallRoadsSearchCursor = daSearchCursor(inMemoryRoadsLayer,
                                            smallRoadsSCFields)

    roadIDsToRemove = list()
    '''
    for smallRoadRow in smallRoadsSearchCursor:
        if int(str(smallRoadRow[0])) % 500 == 0:
            print str(smallRoadRow[0])
        else:
            pass
        
    raise("Stop error.")
    '''

    for smallRoadRow in smallRoadsSearchCursor:
        if smallRoadRow[1] <= 1500:
            roadIDsToRemove.append(smallRoadRow[0])
        else:
            pass

    roadRemovalCounter = 0

    roadsReductionWhereClause = """ "ID2" IN ("""

    for roadID in roadIDsToRemove:
        if roadRemovalCounter <= 998:
            roadsReductionWhereClause = roadsReductionWhereClause + str(
                roadID) + """, """
            roadRemovalCounter += 1
        else:
            # Remove the trailing ", " and add a closing parenthesis.
            roadsReductionWhereClause = roadsReductionWhereClause[:-2] + """) """
            SelectLayerByAttribute_management(inMemoryRoadsLayer,
                                              "ADD_TO_SELECTION",
                                              roadsReductionWhereClause)

            # Debug only
            print "Selecting..."
            selectedRoadsResult = GetCount_management(inMemoryRoadsLayer)
            selectedRoadsCount = int(selectedRoadsResult.getOutput(0))
            print "Number of roads selected: " + str(selectedRoadsCount)

            roadRemovalCounter = 0
            roadsReductionWhereClause = """ "ID2" IN ("""
            roadsReductionWhereClause = roadsReductionWhereClause + str(
                roadID) + """, """

    # Remove the trailing ", " and add a closing parenthesis.
    roadsReductionWhereClause = roadsReductionWhereClause[:-2] + """) """
    SelectLayerByAttribute_management(inMemoryRoadsLayer, "ADD_TO_SELECTION",
                                      roadsReductionWhereClause)

    # Debug only
    print "Selecting..."
    selectedRoadsResult = GetCount_management(inMemoryRoadsLayer)
    selectedRoadsCount = int(selectedRoadsResult.getOutput(0))
    print "Number of roads selected: " + str(selectedRoadsCount)

    selectedRoadsResult = GetCount_management(inMemoryRoadsLayer)

    selectedRoadsCount = int(selectedRoadsResult.getOutput(0))

    if selectedRoadsCount >= 1:
        DeleteFeatures_management(inMemoryRoadsLayer)
    else:
        pass
예제 #32
0
def bufferCrashLocationAndIntersectWithRoads(crashObject, roadsLayer,
                                             outputIncrementInt,
                                             bufferAndIntersectSR,
                                             useParseMatchAddr):
    #pointToBufferXY = list(crashObject.initialShapeXY)
    #print("The crashObject.initialShapeXY is " + str(crashObject.initialShapeXY) + " and the pointToBufferXY is " + str(pointToBufferXY) + ".")
    ##pointToBufferWithoutGeometry = Point(pointToBufferXY)
    ##pointToBuffer = PointGeometry(pointToBufferXY, bufferAndIntersectSR)
    pointToBuffer = crashObject.initialShape
    offsetDistance = crashObject.offsetDistance

    # Perform layer selection here, then
    # intersect the buffer with the selected roads.

    roadNameColumns = crashObject.roadNameColumns

    accidentClusterTolerance = 2

    print(
        "Attempting to buffer and offset the crashObject with a unique key of: "
        + str(crashObject.uniqueKey) + ".")
    print("Using the roadNameColumns of: " + str(roadNameColumns) + ".")

    singlePartOffsetFeaturesList = list()

    try:
        if offsetDistance >= 5:
            ## Was previously failing here due to not having the name for intermediateAccidentBuffer
            offsetDistanceString = str(offsetDistance) + " Feet"
            Buffer_analysis(pointToBuffer, intermediateAccidentBuffer,
                            offsetDistanceString, "", "", "", "", "PLANAR")

            if debugOutputValue == True:
                # Save the buffer here. Call it bufferOutput_001 to start with, and increment from there. Get the number
                # from the calling script.
                bufferOutputLocation = bufferOutputLocationBase + "_" + str(
                    outputIncrementInt).zfill(4)
                copyFCToTempLocation(intermediateAccidentBuffer,
                                     bufferOutputLocation)
            else:
                pass

            firstRoadName = str(crashObject.onRoad)
            firstRoadName = firstRoadName.upper()

            roadNameValues = [firstRoadName]

            if useParseMatchAddr == True:
                parsedRoadNamesList = ParseMatchAddr(crashObject.matchAddress)
                secondRoadName = " "
                try:
                    secondRoadName = parsedRoadNamesList[0]
                    secondRoadName = secondRoadName.upper()
                except:
                    pass
                thirdRoadName = " "
                try:
                    thirdRoadName = parsedRoadNamesList[1]
                    thirdRoadName = thirdRoadName.upper()
                except:
                    pass

                roadNameValues = [firstRoadName, secondRoadName, thirdRoadName]
            else:
                pass

            streetWhereClause = generateWhereClause(roadNameColumns,
                                                    roadNameValues)
            print("The generated whereClause is: " + str(streetWhereClause) +
                  ".")
            SelectLayerByAttribute_management(roadsLayer, "NEW_SELECTION",
                                              streetWhereClause)

            selectionCount = str(GetCount_management(roadsLayer))

            if Exists(intermediateAccidentIntersect):
                try:
                    Delete_management(intermediateAccidentIntersect)
                except:
                    pass
            else:
                pass

            if int(selectionCount) != 0:
                featuresToIntersect = [roadsLayer, intermediateAccidentBuffer]
                Intersect_analysis(featuresToIntersect,
                                   intermediateAccidentIntersect, "ALL",
                                   accidentClusterTolerance, "POINT")

                time.sleep(0.25)
                # Wait a moment for the FC to settle.
                # And yes, it is ridiculous that the Intersect_analysis function would return without
                # its output being there and ready to use, but... *ahem*.
                # If it doesn't exist despite having been just created, then skip to the next record.
                if not (Exists(intermediateAccidentIntersect)):
                    print("There was no output from Intersect_analysis.")
                    crashObject.singlePartOffsetFeaturesList = None
                    return crashObject
                else:
                    pass

                if debugOutputValue == True:
                    # Save the intersect FC here. Call it intersectOutput_001 to start with, and increment from there. Get the number
                    # from the calling script.
                    intersectOutputLocation = intersectOutputLocationBase + "_" + str(
                        outputIncrementInt).zfill(4)
                    copyFCToTempLocation(intermediateAccidentIntersect,
                                         intersectOutputLocation)
                else:
                    pass

                # GetCount_management is not particularly Pythonic.
                countResult = GetCount_management(
                    intermediateAccidentIntersect)
                if int(countResult.getOutput(0)) > 0:
                    MultipartToSinglepart_management(
                        intermediateAccidentIntersect,
                        intermediateAccidentIntersectSinglePart)

                    # Maybe add a feature class to contain all of the single part points that get generated in multipart to singlepart.
                    singlePartsCursor = daSearchCursor(
                        intermediateAccidentIntersectSinglePart,
                        ['SHAPE@', 'SHAPE@XY'])
                    for singlePartRow in singlePartsCursor:
                        ##KDOTXYFieldsList = ['OBJECTID', 'STATUS', 'POINT_X', 'POINT_Y', 'ACCIDENT_KEY', 'ON_ROAD_KDOT_NAME',
                        ##                      'AT_ROAD_KDOT_DIRECTION', 'AT_ROAD_KDOT_DIST_FEET', 'AT_ROAD_KDOT_NAME',
                        ##                       'Match_addr']
                        print("The singlePartRow value is: " +
                              str(singlePartRow) + ".")
                        print(
                            "The singlePartRow[1][0] and singlePartRow[1][1] values are: "
                            + str(singlePartRow[1][0]) + " and " +
                            str(singlePartRow[1][1]) + ".")
                        print(
                            "The crashObject.initialShapeXY[0] and crashObject.initialShapeXY[1] values are: "
                            + str(crashObject.initialShapeXY[0]) + " and " +
                            str(crashObject.initialShapeXY[1]) + ".")
                        singlePartListItem = [
                            crashObject.initialShapeXY[0],
                            crashObject.initialShapeXY[1], singlePartRow[1][0],
                            singlePartRow[1][1], singlePartRow[0]
                        ]
                        #Previously used, but no longer necessary = , geocodedAccident[0]]
                        singlePartOffsetFeaturesList.append(singlePartListItem)

                    try:
                        del singlePartsCursor
                    except:
                        pass
                else:
                    print(
                        "There were zero output features counted in the intermediateAccidentIntersect feature class."
                    )
                    crashObject.singlePartOffsetFeaturesList = None
                    return crashObject
            else:
                pass  # Zero road segments selected. Will not attempt to offset.
        else:
            print(
                "This should have been caught by the lowOrZeroDistanceOffsetCheck function, but the accidentDistanceOffset is not >= 5."
            )
    except:
        print "WARNING:"
        print "An error occurred which prevented the crash point with Acc_Key: " + str(
            crashObject.uniqueKey)
        print "from being buffered and/or offset properly."
        print(traceback.format_exc())

    crashObject.singlePartOffsetFeaturesList = singlePartOffsetFeaturesList

    return crashObject
def dissolveBasedOnLocalRouteKeys(routesToDissolve, subsetSelectionQuery):
    # Moved out the selection building code to the other function where
    # it makes more sense.
    # Use similar code here to what is found in the main dissolve loop.
    # Just need to do multiselection on all of the possible routes that
    # match the subsetSelectionQuery and for each multiselection, create
    # a dissolve feature set, then use the same reintroduction tests
    # that are used in the main dissolve to reintroduce the dissolved
    # lines without removing any that weren't dissolved or adding
    # any new overlaps.

    fcAsFeatureLayerForDissolves = 'FCAsFeatureLayer_Dissolves'

    if Exists(fcAsFeatureLayerForDissolves):
        Delete_management(fcAsFeatureLayerForDissolves)
    else:
        pass

    MakeFeatureLayer_management(routesToDissolve, fcAsFeatureLayerForDissolves)

    lrsKeyFieldList = [str(lrsKeyToUse)]
    newCursor = daSearchCursor(routesToDissolve, lrsKeyFieldList,
                               subsetSelectionQuery)
    uniqueLRSKeysDict = dict()
    for cursorRow in newCursor:
        uniqueLRSKeysDict[str(cursorRow[0])] = 1

    try:
        del newCursor
    except:
        pass

    uniqueLRSKeysList = uniqueLRSKeysDict.keys()
    try:
        uniqueLRSKeysList.remove('None')
    except:
        print(
            "Could not remove 'None' from the list of uniqueLRSKeys since it was not a part of the list."
        )

    print("LRSKey list creation successful.")
    print('Found ' + str(len(uniqueLRSKeysList)) +
          ' unique LRS Keys in the centerline data for this query:')
    print(str(subsetSelectionQuery))

    #Use multiSelection

    multiSelectionQueryBase = str(
        str(subsetSelectionQuery) + ''' AND ''' + ''' "''' + str(lrsKeyToUse) +
        '''" IS NOT NULL AND "''' + str(lrsKeyToUse) + '''" IN (''')
    multiSelectionQuery = multiSelectionQueryBase
    multiCounter = 0
    multiDissolveFields = [
        str(lrsKeyToUse), 'LRS_COUNTY_PRE', 'LRS_ROUTE_PREFIX',
        'LRS_ROUTE_NUM', 'LRS_ROUTE_SUFFIX', 'LRS_UNIQUE_IDENT',
        'LRS_UNIQUE_IDENT1', 'KDOT_COUNTY_L', 'KDOT_COUNTY_R'
    ]
    ##multiDissolveFields = str(lrsKeyToUse) + ';LRS_COUNTY_PRE;LRS_ROUTE_PREFIX;LRS_ROUTE_NUM;LRS_ROUTE_SUFFIX;LRS_UNIQUE_IDENT;LRS_UNIQUE_IDENT1'
    ##multiStatsFields = str(n1FromMeas) + " MIN;" + str(n1ToMeas) + " MAX"
    multiStatsFields = ""
    singlePart = "SINGLE_PART"
    unsplitLines = "UNSPLIT_LINES"

    # 3.) Loop through the list of unique LRS Keys
    for uniqueKeyItem in uniqueLRSKeysList:
        # Make a selection list that includes 50 keys, then select the keys and dissolve to make a new
        # feature class.
        # After the selection is dissolved, use a spatial select on the original feature class and
        # an attribute selection on the original feature class to see which original features should
        # be deleted.
        # Then, delete the selected features (if at least 1 selected).
        #
        try:
            Delete_management(dissolveOutFC)
        except:
            print("Could not delete the dissolveOutFC layer.")

        # 4.) For groups of 2000 LRS Keys, select all the features with those LRS Keys.
        if multiCounter <= 1999:
            multiSelectionQuery += """'""" + str(
                uniqueKeyItem) + """'""" + """, """
            multiCounter += 1
        else:
            # Add the current item, then
            multiSelectionQuery += """'""" + str(
                uniqueKeyItem) + """'""" + """, """
            # Remove the trailing ", " and add a closing parenthesis.
            multiSelectionQuery = multiSelectionQuery[:-2] + """) """
            SelectLayerByAttribute_management(fcAsFeatureLayerForDissolves,
                                              "NEW_SELECTION",
                                              multiSelectionQuery)
            # Have to do from step 5 on here also.

            ### -shouldbeafunctionblock#1- ###
            # 5.) Count selected features.
            countResult0 = GetCount_management(fcAsFeatureLayerForDissolves)
            intCount0 = int(countResult0.getOutput(0))
            if intCount0 >= 1:
                # 6.) Make a new layer or dissolved layer from this selection.
                Dissolve_management(fcAsFeatureLayerForDissolves,
                                    dissolveOutFC, multiDissolveFields,
                                    multiStatsFields, singlePart, unsplitLines)

                # 7.) Count the number of dissolved features.
                countResult1 = GetCount_management(dissolveOutFC)
                intCount1 = int(countResult1.getOutput(0))
                print('Counted ' + str(intCount1) +
                      ' features returned for that dissolve.')
                # 8a.) If the number of dissolved features is 0, then append the error to the error file
                #       and go on to the next LRS Key in the loop.
                if intCount1 == 0:
                    with open(dissolveErrorsFile, 'a') as errorFile:
                        errorFile.write(str(multiSelectionQuery))
                # 8b.) From the spatial select, select the subset of features that also have a matching LRS Key.
                else:
                    SelectLayerByAttribute_management(
                        fcAsFeatureLayerForDissolves, 'NEW_SELECTION',
                        multiSelectionQuery)
                    # 9.) Else, spatially select features in the original feature class with 'SHARE_A_LINE_SEGMENT_WITH'.
                    SelectLayerByLocation_management(
                        fcAsFeatureLayerForDissolves,
                        'SHARE_A_LINE_SEGMENT_WITH', dissolveOutFC, 0,
                        'SUBSET_SELECTION')
                    # 10.) Count to make sure that at least one feature is selected.
                    countResult2 = GetCount_management(
                        fcAsFeatureLayerForDissolves)
                    intCount2 = int(countResult2.getOutput(0))
                    print(
                        'There were ' + str(intCount2) +
                        ' features selected for replacement in the fcAsFeatureLayerForDissolves layer.'
                    )
                    if intCount2 >= 1:
                        # 11.) If so, cursor the features out of the dissolve layer.
                        featureList = list()
                        searchCursorFields = [
                            str(lrsKeyToUse), 'LRS_COUNTY_PRE',
                            'LRS_ROUTE_PREFIX', 'LRS_ROUTE_NUM',
                            'LRS_ROUTE_SUFFIX', 'LRS_UNIQUE_IDENT',
                            'LRS_UNIQUE_IDENT1', 'KDOT_COUNTY_L',
                            'KDOT_COUNTY_R', 'SHAPE@'
                        ]
                        newCursor = daSearchCursor(dissolveOutFC,
                                                   searchCursorFields)

                        for cursorItem in newCursor:
                            featureList.append(list(cursorItem))

                        try:
                            del newCursor
                        except:
                            pass

                        # 12.) Delete the selected features in the input layer.
                        try:
                            DeleteFeatures_management(
                                fcAsFeatureLayerForDissolves)
                        except:
                            print(
                                "Could not delete features for the selection "
                                + str(multiSelectionQuery) + ".")
                        # 13.) Insert the features from the dissolve layer into the copy of the centerlines.
                        insertCursorFields = [
                            str(lrsKeyToUse), 'LRS_COUNTY_PRE',
                            'LRS_ROUTE_PREFIX', 'LRS_ROUTE_NUM',
                            'LRS_ROUTE_SUFFIX', 'LRS_UNIQUE_IDENT',
                            'LRS_UNIQUE_IDENT1', 'KDOT_COUNTY_L',
                            'KDOT_COUNTY_R', 'SHAPE@'
                        ]
                        newCursor = daInsertCursor(
                            fcAsFeatureLayerForDissolves, insertCursorFields)

                        for featureItem in featureList:
                            newCursor.insertRow(featureItem)

                        try:
                            del newCursor
                        except:
                            pass
                        try:
                            del featureList
                        except:
                            pass
                    else:
                        pass
            multiSelectionQuery = ''' "''' + str(
                lrsKeyToUse) + '''" IS NOT NULL AND "''' + str(
                    lrsKeyToUse) + '''" IN ('''
            multiCounter = 0
            ### -shouldbeafunctionblock#1- ###

    # After the for loop, if there is still anything remaining which was unselected in the
    # the previous multiSelectionQuery steps.
    # Remove the trailing ", " and add a closing parenthesis.
    if multiSelectionQuery != multiSelectionQueryBase:
        multiSelectionQuery = multiSelectionQuery[:-2] + """) """
    else:
        # The selection query would not select anything.
        return
    SelectLayerByAttribute_management(fcAsFeatureLayerForDissolves,
                                      "NEW_SELECTION", multiSelectionQuery)

    # Then redo from step 5 on at the end of the loop IF there is anything left to select
    # which was not selected... so if selectionCounter != 0.

    ### -shouldbeafunctionblock#2- ###
    # 5.) Count selected features.
    countResult0 = GetCount_management(fcAsFeatureLayerForDissolves)
    intCount0 = int(countResult0.getOutput(0))
    if intCount0 >= 1:
        # 6.) Make a new layer or dissolved layer from this selection. -- Question about fields.
        Dissolve_management(fcAsFeatureLayerForDissolves, dissolveOutFC,
                            multiDissolveFields, multiStatsFields, singlePart,
                            unsplitLines)

        # 7.) Count the number of dissolved features.
        countResult1 = GetCount_management(dissolveOutFC)
        intCount1 = int(countResult1.getOutput(0))
        print('Counted ' + str(intCount1) +
              ' features returned for that dissolve.')
        # 8a.) If the number of dissolved features is 0, then append the error to the error file
        #       and go on to the next LRS Key in the loop.
        if intCount1 == 0:
            with open(dissolveErrorsFile, 'a') as errorFile:
                errorFile.write(str(multiSelectionQuery))
        # 8b.) From the spatial select, select the subset of features that also have a matching LRS Key.
        else:
            SelectLayerByAttribute_management(fcAsFeatureLayerForDissolves,
                                              'NEW_SELECTION',
                                              multiSelectionQuery)
            # 9.) Else, spatially select features in the original feature class with 'SHARE_A_LINE_SEGMENT_WITH'.
            SelectLayerByLocation_management(fcAsFeatureLayerForDissolves,
                                             'SHARE_A_LINE_SEGMENT_WITH',
                                             dissolveOutFC, 0,
                                             'SUBSET_SELECTION')
            # 10.) Count to make sure that at least one feature is selected.
            countResult2 = GetCount_management(fcAsFeatureLayerForDissolves)
            intCount2 = int(countResult2.getOutput(0))
            print(
                'There were ' + str(intCount2) +
                ' features selected in the fcAsFeatureLayerForDissolves layer.'
            )
            if intCount2 >= 1:
                # 11.) If so, cursor the features out of the dissolve layer.
                featureList = list()
                searchCursorFields = [
                    str(lrsKeyToUse), 'LRS_COUNTY_PRE', 'LRS_ROUTE_PREFIX',
                    'LRS_ROUTE_NUM', 'LRS_ROUTE_SUFFIX', 'LRS_UNIQUE_IDENT',
                    'LRS_UNIQUE_IDENT1', 'KDOT_COUNTY_L', 'KDOT_COUNTY_R',
                    'SHAPE@'
                ]
                newCursor = daSearchCursor(dissolveOutFC, searchCursorFields)

                for cursorItem in newCursor:
                    featureList.append(list(cursorItem))

                try:
                    del newCursor
                except:
                    pass

                # 12.) Delete the selected features in the input layer.
                try:
                    DeleteFeatures_management(fcAsFeatureLayerForDissolves)
                except:
                    print("Could not delete features for the selection " +
                          str(multiSelectionQuery) + ".")
                # 13.) Insert the features from the dissolve layer into the copy of the centerlines.
                insertCursorFields = [
                    str(lrsKeyToUse), 'LRS_COUNTY_PRE', 'LRS_ROUTE_PREFIX',
                    'LRS_ROUTE_NUM', 'LRS_ROUTE_SUFFIX', 'LRS_UNIQUE_IDENT',
                    'LRS_UNIQUE_IDENT1', 'KDOT_COUNTY_L', 'KDOT_COUNTY_R',
                    'SHAPE@'
                ]
                newCursor = daInsertCursor(fcAsFeatureLayerForDissolves,
                                           insertCursorFields)

                for featureItem in featureList:
                    newCursor.insertRow(featureItem)

                try:
                    del newCursor
                except:
                    pass
                try:
                    del featureList
                except:
                    pass
            else:
                pass
def TranscendRampReplacement():
    MakeFeatureLayer_management (routesSourceCenterlines, routesSourceFeatureLayer)

    SelectLayerByAttribute_management(routesSourceFeatureLayer, "CLEAR_SELECTION")
    selectionQuery = """ "LRS_ROUTE_PREFIX" = 'X' AND "Ramps_LRSKey" IS NOT NULL AND "Ramps_LRSKey" <> '' """
    SelectLayerByAttribute_management(routesSourceFeatureLayer, "NEW_SELECTION", selectionQuery)

    countResult = GetCount_management(routesSourceFeatureLayer)
    intCount = int(countResult.getOutput(0))

    print('Selected ' + str(intCount) + ' ramp features to be replaced.')

    if intCount > 0:
        print("Deleting those ramp features from the " + returnFeatureClass(routesSourceCenterlines) + " layer.")
        DeleteFeatures_management(routesSourceFeatureLayer)
    else:
        print("No features selected. Skipping feature deletion.")

    # Remove the matching routes to prepare for the Interchange_Ramps information.
    ## After error matching is achieved, use replace geometry and replace attributes to not lose data
    ## from using the less effective method of:
    ## deleting the old Interchange_Ramps information, then re-adding with append.

    # Add the Interchange_Ramps information.

    # Checking to see if the copy for repairing already exists.
    # If so, remove it.
    if Exists(interchangeRampFCRepairCopy):
        Delete_management(interchangeRampFCRepairCopy)
    else:
        pass
    # Create a new file for the copy for repairing since repair modifies the input.
    CopyFeatures_management(interchangeRampFC, interchangeRampFCRepairCopy)

    # Repairs the geometry, modifies input.
    # Deletes features with null geometry (2 expected, until Shared.Interchange_Ramp is fixed).
    print("Repairing ramp geometry in the " + returnFeatureClass(interchangeRampFCRepairCopy) + " layer.")
    RepairGeometry_management(interchangeRampFCRepairCopy, "DELETE_NULL")

    # Create a fieldmapping object so that the Interchange_Ramps can be correctly imported with append.
    appendInputs = [interchangeRampFCRepairCopy]
    appendTarget = routesSourceCenterlines
    schemaType = "NO_TEST"

    # Field mapping goes here.
    # Interchange_Ramp.LRS_KEY to RoutesSource_Test.LRSKEY
    fm_Field1 = FieldMap()
    fm_Field1.addInputField(interchangeRampFCRepairCopy, "LRS_KEY")
    fm_Field1_OutField = fm_Field1.outputField
    fm_Field1_OutField.name = 'LRSKEY'
    fm_Field1.outputField = fm_Field1_OutField

    # Interchange_Ramp.BEG_CNTY_LOGMILE to RoutesSource_Test.NON_STATE_BEGIN_MP
    fm_Field2 = FieldMap()
    fm_Field2.addInputField(interchangeRampFCRepairCopy, "BEG_CNTY_LOGMILE")
    fm_Field2_OutField = fm_Field2.outputField
    fm_Field2_OutField.name = 'NON_STATE_BEGIN_MP'
    fm_Field2.outputField = fm_Field2_OutField

    # Interchange_Ramp.END_CNTY_LOGMILE to RoutesSource_Test.NON_STATE_END_MP
    fm_Field3 = FieldMap()
    fm_Field3.addInputField(interchangeRampFCRepairCopy, "END_CNTY_LOGMILE")
    fm_Field3_OutField = fm_Field3.outputField
    fm_Field3_OutField.name = 'NON_STATE_END_MP'
    fm_Field3.outputField = fm_Field3_OutField

    # Create the fieldMappings object
    interchangeRampsMappings = FieldMappings()
    interchangeRampsMappings.addFieldMap(fm_Field1)
    interchangeRampsMappings.addFieldMap(fm_Field2)
    interchangeRampsMappings.addFieldMap(fm_Field3)

    # Add the fieldMap objects to the fieldMappings object.
    print("Appending the features from " + returnFeatureClass(interchangeRampFCRepairCopy) + " into " + returnFeatureClass(routesSourceCenterlines) + ".")
    Append_management(appendInputs, appendTarget, schemaType, interchangeRampsMappings)
def exportErrorsToFeatureClasses(reviewTable, originGDB, errorOutputGDB,
                                 errorOutputGDBFolder):
    # Checking to see if the output already exists.
    # If so, remove it.
    if Exists(errorOutputGDB):
        Delete_management(errorOutputGDB)
    else:
        pass

    CreateFileGDB_management(errorOutputGDBFolder,
                             returnGDBOrSDEName(errorOutputGDB))

    previousWorkspace = env.workspace
    env.workspace = errorOutputGDB

    tableFields = ['ORIGINTABLE', 'CHECKTITLE', 'OBJECTID']
    newCursor = daSearchCursor(reviewTable, tableFields)

    revRows = list()

    for rowItem in newCursor:
        revRows.append(list(rowItem))

    try:
        del newCursor
    except:
        pass

    originTableList = list()
    checkTitleList = list()

    for revRowItem in revRows:
        originTableList.append(revRowItem[0])
        checkTitleList.append(revRowItem[1])

    print('Creating sets from the originTable and checkTitle lists.')
    originTableSet = set(originTableList)
    checkTitleSet = set(checkTitleList)
    print('Finished set creation.')

    originTableList = list(originTableSet)
    checkTitleList = list(checkTitleSet)

    tableAndCheckDataObjects = list()
    csvDictOfErrorFeatures = dict()

    for originTableItem in originTableList:
        print('Origin table = ' + originTableItem + '.')
        completeOriginTablePath = os.path.join(originGDB, originTableItem)
        print('The full path to the origin table is ' +
              str(completeOriginTablePath) + '.')
        tableViewName = "ReviewTable_View_" + str(originTableItem)
        originTableWhereClause = """"ORIGINTABLE" = '""" + str(
            originTableItem) + """'"""
        try:
            Delete_management(tableViewName)
        except:
            pass
        MakeTableView_management(reviewTable, tableViewName,
                                 originTableWhereClause)

        for checkTitleItem in checkTitleList:
            print('Check title = ' + checkTitleItem + '.')
            selectionWhereClause = """"CHECKTITLE" = '""" + str(
                checkTitleItem) + """'"""
            SelectLayerByAttribute_management(tableViewName, "NEW_SELECTION",
                                              selectionWhereClause)
            countResult = GetCount_management(tableViewName)
            intCount = int(countResult.getOutput(0))

            if intCount >= 1:
                tempTableAndCheckData = tableAndCheckData(
                    originTableItem, checkTitleItem)
                tableViewFields = ["RECORDID", "OBJECTID"]

                newCursor = daSearchCursor(tableViewName, tableViewFields,
                                           selectionWhereClause)

                newOIDList = list()

                for cursorItem in newCursor:
                    newOIDList.append(cursorItem[1])

                try:
                    del newCursor
                except:
                    pass

                tempTableAndCheckData.listOfOIDsToUse = newOIDList

                tableAndCheckDataObjects.append(tempTableAndCheckData)
            else:
                print("There were no features selected for the " +
                      tableViewName + " table.")

    print("There are " + str(len(tableAndCheckDataObjects)) +
          " different items in the tableAndCheckDataObjects list.")

    for listObject in tableAndCheckDataObjects:

        featureLayerForErrorOutput = 'FeatureClassAsFeatureLayer'

        if Exists(featureLayerForErrorOutput):
            Delete_management(featureLayerForErrorOutput)
        else:
            pass

        fullPathToFeatureClass = os.path.join(originTablesGDB,
                                              listObject.tableName)

        MakeFeatureLayer_management(fullPathToFeatureClass,
                                    featureLayerForErrorOutput)

        # build the selection list & select up to but not more than 999 features at at time
        OIDTotalCounter = 0
        errorOutputWhereClause = """ "OBJECTID" IN ("""

        for errorOID in listObject.listOfOIDsToUse:
            if OIDTotalCounter <= 998:
                errorOutputWhereClause = errorOutputWhereClause + str(
                    errorOID) + """, """
                OIDTotalCounter += 1
            else:
                # Remove the trailing ", " and add a closing parenthesis.
                errorOutputWhereClause = errorOutputWhereClause[:-2] + """) """
                SelectLayerByAttribute_management(featureLayerForErrorOutput,
                                                  "ADD_TO_SELECTION",
                                                  errorOutputWhereClause)

                OIDTotalCounter = 0
                errorOutputWhereClause = """ "OBJECTID" IN ("""
                errorOutputWhereClause = errorOutputWhereClause + str(
                    errorOID) + """, """

        # Remove the trailing ", " and add a closing parenthesis.
        errorOutputWhereClause = errorOutputWhereClause[:-2] + """) """
        SelectLayerByAttribute_management(featureLayerForErrorOutput,
                                          "ADD_TO_SELECTION",
                                          errorOutputWhereClause)

        ##print "Counting..."
        selectedErrorsResult = GetCount_management(featureLayerForErrorOutput)
        selectedErrorsCount = int(selectedErrorsResult.getOutput(0))

        # export the selected data with the correct tableName & checkTitle
        outputFeatureClassName = formatCheckTitle(
            listObject.checkTitle) + "ErrorsFrom_" + listObject.tableName
        fullPathToOutputFeatureClass = os.path.join(errorOutputGDB,
                                                    outputFeatureClassName)

        csvDictOfErrorFeatures[outputFeatureClassName] = str(
            selectedErrorsCount)

        print(
            str(selectedErrorsCount) + "\t features will be written to \t" +
            outputFeatureClassName)
        if selectedErrorsCount >= 1:
            CopyFeatures_management(featureLayerForErrorOutput,
                                    fullPathToOutputFeatureClass)
            time.sleep(25)
            AddField_management(outputFeatureClassName, "OptionalInfo", "TEXT",
                                "", "", 250, "ReviewingInfo", nullable)
        else:
            pass

    # Need to write a short CSV here that tells the number and type of errors.
    print('Writing error information to an error reports file called ' +
          str(errorReportCSVName) + '.')
    try:
        with open(errorReportCSV, 'w') as fHandle:
            for errorFeature in errorReportRowsOrder:
                if errorFeature in csvDictOfErrorFeatures:
                    errorFeatureCount = csvDictOfErrorFeatures[errorFeature]
                    fHandle.write(
                        str(errorFeature) + ', ' + str(errorFeatureCount) +
                        '\n')
                else:
                    fHandle.write(str(errorFeature) + ', ' + str(0) + '\n')
            # Add a blank line to match previous formatting.
            fHandle.write('\n')
    except:
        print("There was an error writing to the file.")

    # Modify this so that it just checks for the existence of the roads
    # and highways check output, rather than relying on the config
    # file for whether or not this should be ran.
    # The config file can tell the full process whether or not
    # to run the R&H check, but the error report should give
    # details on the R&H check whether or not the config file
    # currently states that the R&H check should be ran again
    # were the full process to run.

    env.workspace = previousWorkspace
예제 #36
0
def KDOTOverlappingRoutesDissolveFix_OLD():
    ######## --- Current Work Area --- ########
    ###TODO: IMPORTANT!!!: Reuse and adapt the ramp integration process for the dissolve results re-integration (per dissolve set)
    ### -- detect duplicates in the original data and only keep one piece per piece in the dissolved set -- may require
    ### -- additional QC however, as the dissolve set can return more than one feature per LRSKEY and there may need to
    ### -- be a spatial test involved to make sure that what gets returned covers all of the area that the original
    ### -- features for a given LRSKEY provided.
    
    ### -- Cont'd: Spatial select on attribute selected FC to see which ones can be safely deleted. -- Need to find
    ### -- which spatial selection method works on segments that are exactly the same as a another segment OR which
    ### -- segments (for the smaller segment share a start/end point and all other points with a larger segment, but which
    ### -- will not select smaller segments that share only a start/end point with 
    
    ### -- Test 'WITHIN' as the spatial selection method -- the dissolve will have to run prior to simplify
    ### -- or it will need to use the same simplification version -- okay, that should be fine. Just do it post simplify
    ### -- Except in cases where the geometry is identical except with there possibly being an additional vertex in one... hmm.
    
    ### -- Also test 'SHARE_A_LINE_SEGMENT_WITH' -- might work correctly, but not sure.
    
    ## Both 'WITHIN' and 'SHARE_A_LINE_SEGMENT_WITH' return 975 for the R route dissolve test data.
    ## Both 'WITHIN' and 'SHARE_A_LINE_SEGMENT_WITH' return 498 for the M route dissolve test data.
    ## 'SHARE_A_LINE_SEGMENT_WITH' is probably the safest, because one of the WITHIN types gave me an
    ## error in the Select By Location window the 3rd or 4th time I clicked through it, but then not
    ## again later. -- May have just been because I was clicking quickly, but try to:
    ## Stay away from buggy and buggish settings.
    
    ## Because of the ridiculously high number of fields that contain LRS information, you have to do the dissolve
    ## using all of them, and then compress their information into a smaller chunk and do the dissolve again.
    
    ## I.E. Five field min/max dissolve
    
    
    # -///////////////////- Current Work Area -///////////////////- #
    MakeFeatureLayer_management(routesSourceIntermediate, featureLayer)
    # Reparse the LRS_KEY_Prefix and use it to select dissolve targets.
    ParseLRS_ROUTE_PREFIX(featureLayer) #probably shouldn't count on this
    # existing if it's not passed into this function... use something else.
    
    # Do I, U, K, C, R, M, X, L, etc. separately to try to keep dissolve from
    # messing up and skipping some of the features, but returning 'Success'
    # as though nothing was wrong.
    # Then, combine them all back into one feature set.
    prefixesToDissolve = ['I', 'U', 'K', 'C', 'R', 'X']#, 'L']
    # -- Don't try dissolving prefix L until after new unique
    # keys are genned.
    
    undissolvedFeatureLayer = 'undissolvedFeatureLayer'
    dissolvedFC_gdb = 'in_memory'
    dissolvedFC_basename = 'dissolvedFC_'
    dissolveOutputCombined = 'dissolvedOutput'
    # Have to add MIN_BEG_NODE, MAX_BEG_NODE, MIN_END_NODE, MAX_END_NODE fields to
    # the output feature class.
    floatFieldsToAdd = ['MIN_BEG_NODE', 'MAX_BEG_NODE', 'MIN_END_NODE', 'MAX_END_NODE']
    
    CopyFeatures_management(routesSourceCenterlines, routesSourcePreDissolveOutput)
    MakeFeatureLayer_management(routesSourceCenterlines, stateSystemSelectedFeatures, selectQuery1)
    
    #///////////
    print("Starting to dissolve the routes source.")
    stateSystemSelectedFeatures = 'StateSystem_Features'
    selectQuery1 = '''LRS_ROUTE_PREFIX in ('I', 'U', 'K')'''
    MakeFeatureLayer_management (routesSourceCenterlines, stateSystemSelectedFeatures, selectQuery1)
    CopyFeatures_management(stateSystemSelectedFeatures, routesSourcePreDissolveOutput)
    
    #GetCount on the features in the layer here.
    countResult = GetCount_management(stateSystemSelectedFeatures)
    intCount = int(countResult.getOutput(0))
    print('Found ' + str(intCount) + ' state system features to be dissolved.')
    
    # Why isn't all of the relevant information already in the KDOT_LRS_KEY?
    # Why do we need 5 other fields if we're using an intelligent key?
    # Removed STATE_FLIP_FLAG because we've already flipped the data prior to this process,
    # as we should given that we want the geometry to dissolve correctly.
    dissolveFields = "KDOT_LRS_KEY"#"KDOT_DIRECTION_CALC;KDOT_LRS_KEY;LRS_COUNTY_PRE;LRS_ROUTE_PREFIX;LRS_ROUTE_SUFFIX;LRS_UNIQUE_IDENT"
    statisticsFields = "BEG_NODE MIN;BEG_NODE MAX;END_NODE MAX;END_NODE MIN;SourceFrom MIN;SourceTo MAX"
    multipart = "SINGLE_PART"
    unsplitLines = "DISSOLVE_LINES"
    ## Use a selection for the state system so that it will work the first time.
    ## Then, later expand the selection and the amount of routes that are attempted for
    ## the dissolve.
    Dissolve_management(stateSystemSelectedFeatures, routesSourceDissolveOutput, dissolveFields, statisticsFields, multipart, unsplitLines)
    print("Completed dissolving the routes source.")
    #///////////
    
    #Add the cursor process here.
    
    
    undissolvedFeatureLayer = 'undissolvedFeatureLayer'
    prefixesToDissolve = ['I', 'U', 'K', 'C', 'R', 'X']#, 'L']
    # -- Don't try dissolving prefix L until after new unique
    # keys are genned.
    dissolvedFC_gdb = 'in_memory'
    dissolvedFC_basename = 'dissolvedFC_'
    dissolveOutputCombined = 'dissolvedOutput'
    
    selectQuery0 = None
    for prefixItem in prefixesToDissolve:
        pass
    
    SelectLayerByAttribute_management(undissolvedFeatureLayer, "NEW_SELECTION", selectQuery0)
    
    
    for prefixItem in prefixesToDissolve:
        # Select by LRS_KEY_Prefix.
        # Move selection to a separate dataset or perform dissolve with selection.
        # Might work better to move it to a separate dataset, given the problems with
        # the feature layer dissolve attempts made earlier in this process, but
        # that may have been due to the fact that you had attempted at least one
        # from an in_memory location.
        # i.e. dissolvedFC_I
        dissolvedFC_partname = dissolvedFC_basename + prefixItem
        dissolvedFC_location = os.path.join(dissolvedFC_gdb, dissolvedFC_partname)
        selectQuery1 = """LRS_KEY_Prefix = '""" + prefixItem + """'"""
        SelectLayerByAttribute_management(undissolvedFeatureLayer, "NEW_SELECTION", selectQuery1)
        countResult = GetCount_management(undissolvedFeatureLayer)
        intCount = int(countResult.getOutput(0))
        
        print("Will dissolve " + str(intCount) + " features in the " + " dissolvedFC_partname " + " feature class.")
        
        Dissolve_management(undissolvedFeatureLayer, dissolvedFC_location, dissolveFields, statisticsFields, multipart, unsplitLines)
    
    rowsToInsert = list()
    
    for prefixItem in prefixesToDissolve:
        dissolvedFC_partname = dissolvedFC_basename + prefixItem
        dissolvedFC_location = os.path.join(dissolvedFC_gdb, dissolvedFC_partname)
        
        tableFields = ['ORIGINTABLE', 'CHECKTITLE', 'OBJECTID']
        newCursor = daSearchCursor(dissolvedFC_location, tableFields)
        
        for cursorRow in newCursor:  
            rowsToInsert.append(cursorRow)
            
        try:
            del newCursor
        except:
            pass
            
        newCursor = daInsertCursor()
        #cursor out the features
    
    # then write them to the output feature class which receives its structure from using the original feature class
    # as a template. -- Will need to update the fields using data from the original feature class prior to the dissolve
    # since dissolve destroys information -- not sure how to do that just yet. Maybe select the largest feature which
    # shares geometry with the dissolved feature...? -- Possibly not necessary since we'll just be using the dissolved
    # feature to create a route now and not using it for error correction, since we have people editing the
    # post-conflation base data for that.
    
    # Need to not just make a template but also copy everything where the 
    # LRS_KEY_Prefix is not in the list of prefixesToDissolve.


    def OverlapsMain():
        print("Starting to dissolve the routes source.")
        stateSystemSelectedFeatures = 'StateSystem_Features'
        selectQuery1 = '''LRS_ROUTE_PREFIX in ('I', 'U', 'K')'''
        MakeFeatureLayer_management (routesSourceCenterlines, stateSystemSelectedFeatures, selectQuery1)
        CopyFeatures_management(stateSystemSelectedFeatures, routesSourcePreDissolveOutput)
        
        #GetCount on the features in the layer here.
        countResult = GetCount_management(stateSystemSelectedFeatures)
        intCount = int(countResult.getOutput(0))
        print('Found ' + str(intCount) + ' state system features to be dissolved.')
        
        # Removed STATE_FLIP_FLAG because we've already flipped the data prior to this process.
        dissolveFields = "KDOT_DIRECTION_CALC;KDOT_LRS_KEY;LRS_COUNTY_PRE;LRS_ROUTE_PREFIX;LRS_ROUTE_SUFFIX;LRS_UNIQUE_IDENT"
        statisticsFields = "BEG_NODE MIN;BEG_NODE MAX;END_NODE MAX;END_NODE MIN;COUNTY_BEGIN_MP MIN;COUNTY_END_MP MAX"
        multipart = "SINGLE_PART"
        unsplitLines = "DISSOLVE_LINES"
        ## Use a selection for the state system so that it will work the first time.
        ## Then, later expand the selection and the amount of routes that are attempted for
        ## the dissolve.
        Dissolve_management(stateSystemSelectedFeatures, routesSourceDissolveOutput, dissolveFields, statisticsFields, multipart, unsplitLines)
        print("Completed dissolving the routes source.")
    
    import os
    
    from arcpy import (AddField_management, CalculateField_management,
        CopyFeatures_management, Dissolve_management, env,
        FeatureClassToFeatureClass_conversion,
        FlipLine_edit, GetCount_management,
        MakeFeatureLayer_management, SelectLayerByAttribute_management)
    
    from datareviewerchecks_config import (gdbForSourceCreation, routesSourceCenterlines,
        routesSourceDissolveOutput)
    
    env.workspace = gdbForSourceCreation
    env.overwriteOutput = True
    
    inMemFeatureClassName = 'State_System'
    inMemFeatureClass = os.path.join('in_memory', inMemFeatureClassName)
    stateSystemFeatureLayer = 'StateSystemFL'
    nonStateSystemFeatureLayer = 'NonStateSystemFL'


    def StateHighwaySystemDissolve():
        # Create an in-memory copy of state highay system routes based on LRS Route Prefix
        FeatureClassToFeatureClass_conversion(routesSourceCenterlines, "in_memory", inMemFeatureClassName, "LRS_ROUTE_PREFIX in ('I', 'U', 'K')")
        MakeFeatureLayer_management(inMemFeatureClass, stateSystemFeatureLayer)
        #about 941 records in Southwest Kansas had reverse mileages and need to be flipped
        #this should be corrected in the final conflation delivery
        #if it is not corrected, these route segments should be explored in more detail
        
        SelectLayerByAttribute_management(stateSystemFeatureLayer, "NEW_SELECTION", """("COUNTY_BEGIN_MP" > "COUNTY_END_MP" OR "STATE_BEGIN_MP" > "STATE_END_MP") AND "STATE_FLIP_FLAG" IS NULL""")
        CalculateField_management(stateSystemFeatureLayer, "STATE_FLIP_FLAG", """'Y'""", "PYTHON_9.3", "")
        
        SelectLayerByAttribute_management(stateSystemFeatureLayer, "NEW_SELECTION", """"STATE_FLIP_FLAG" = 'Y' """)
        FlipLine_edit(stateSystemFeatureLayer)
        #need to flip mileages where geometry was flipped so add fields
        AddField_management(stateSystemFeatureLayer, "F_CNTY_2", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        AddField_management(stateSystemFeatureLayer, "T_CNTY_2", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        AddField_management(stateSystemFeatureLayer, "F_STAT_2", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        AddField_management(stateSystemFeatureLayer, "T_STAT_2", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        
        #check if there are any state system segments where the to is greater than the from and flag them for review
        AddField_management(stateSystemFeatureLayer, "MileFlipCheck", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
        
        CalculateField_management(stateSystemFeatureLayer, "F_CNTY_2", "!COUNTY_END_MP!", "PYTHON_9.3", "")
        CalculateField_management(stateSystemFeatureLayer, "T_CNTY_2", "!COUNTY_BEGIN_MP!", "PYTHON_9.3", "")
        CalculateField_management(stateSystemFeatureLayer, "F_STAT_2", "!STATE_END_MP!", "PYTHON_9.3", "")
        CalculateField_management(stateSystemFeatureLayer, "T_STAT_2", "!STATE_BEGIN_MP!", "PYTHON_9.3", "")
        
        # Switch selection and calculate mileages
        
        SelectLayerByAttribute_management(in_layer_or_view=stateSystemFeatureLayer, selection_type="SWITCH_SELECTION", where_clause="")
        
        CalculateField_management(stateSystemFeatureLayer, "F_CNTY_2", "!COUNTY_BEGIN_MP!", "PYTHON_9.3", "")
        CalculateField_management(stateSystemFeatureLayer, "T_CNTY_2", "!COUNTY_END_MP!", "PYTHON_9.3", "")
        CalculateField_management(stateSystemFeatureLayer, "F_STAT_2", "!STATE_BEGIN_MP!", "PYTHON_9.3", "")
        CalculateField_management(stateSystemFeatureLayer, "T_STAT_2", "!STATE_END_MP!", "PYTHON_9.3", "")
        #KDOT Direction should already be calculated, by running "DualCarriagweayIdentity.py" and updating the KDOT_DIRECTION_CALC to 1 where dual carriagway is found
        #Validation_CheckOverlaps can also help do identify sausage link/parallel geometries that may indicate dual carriagway, but that script does not yet 
        #identify and calculate the KDOT_DIRECTION_CALC flag.  It probably could with more development
        # Select the EB routes and change the LRS_Direction to WB
        
        SelectLayerByAttribute_management(stateSystemFeatureLayer, "NEW_SELECTION", """"KDOT_DIRECTION_CALC" = '1' AND "LRS_DIRECTION" = 'EB'""")
        CalculateField_management(stateSystemFeatureLayer, "LRS_DIRECTION", "'WB'", "PYTHON_9.3", "")
        #Select the SB routes to chante hte LRS direction to SB
        SelectLayerByAttribute_management(stateSystemFeatureLayer, "NEW_SELECTION", """"KDOT_DIRECTION_CALC" = '1' AND "LRS_DIRECTION" = 'NB'""")
        CalculateField_management(stateSystemFeatureLayer, "LRS_DIRECTION", "'SB'", "PYTHON_9.3", "")
        # Clear the selections
        SelectLayerByAttribute_management(stateSystemFeatureLayer, "CLEAR_SELECTION", "")
        
        #Calculate County LRS Key in CountyKey1 field for State Highway system
        #Need to add CountyKey2 for iteration 2, also go ahead and add new LRS Key format
        CalculateField_management(stateSystemFeatureLayer, "CountyKey1", """[LRS_COUNTY_PRE] + [LRS_ROUTE_PREFIX] + [LRS_ROUTE_NUM] + [LRS_ROUTE_SUFFIX] + [LRS_UNIQUE_IDENT] +"-" + [LRS_DIRECTION]""", "VB")
        CalculateField_management(stateSystemFeatureLayer, "StateKey1", """[LRS_ROUTE_PREFIX] + [LRS_ROUTE_NUM] + [LRS_ROUTE_SUFFIX] + [LRS_UNIQUE_IDENT] +"-" + [LRS_DIRECTION]""", "VB")
        
        #this is the dissolve - the output of this is a feature class which is clean for route creation of the state highway system
        Dissolve_management(stateSystemFeatureLayer, routesSourceDissolveOutput+"_Dissolve_EO", "CountyKey1;LRS_COUNTY_PRE;LRS_ROUTE_PREFIX;LRS_ROUTE_NUM;LRS_ROUTE_SUFFIX;LRS_UNIQUE_IDENT;LRS_DIRECTION", "F_CNTY_2 MIN;T_CNTY_2 MAX", "SINGLE_PART", "DISSOLVE_LINES")
        Dissolve_management(stateSystemFeatureLayer, routesSourceDissolveOutput+"_Unsplit_EO", "CountyKey1;LRS_COUNTY_PRE;LRS_ROUTE_PREFIX;LRS_ROUTE_NUM;LRS_ROUTE_SUFFIX;LRS_UNIQUE_IDENT;LRS_DIRECTION", "F_CNTY_2 MIN;T_CNTY_2 MAX", "SINGLE_PART", "UNSPLIT_LINES")
        # Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
        # The following inputs are layers or table views: stateSystemFeatureLayer
        #review the dissolve output, go back and flag the input data
    
    OverlapsMain()
    StateHighwaySystemDissolve()
예제 #37
0
def mainProcessFeatureSimplification(inputFeatures, maxCount, outputFeatures):
    # Split the input features into intermediary features:
    # Add each intermediary feature class to a list and
    # pass one feature class of the intermediary features
    # to each subprocess.
    
    # When all of the subprocesses complete, use the
    # list of the intermediary feature classes to append
    # the data into the output features.
    
    countResult = GetCount_management(inputFeatures)
    intCount = int(countResult.getOutput(0))
    # debug print
    print("Counted " + str(intCount) + " features in the " + inputFeatures + " feature class.")
    
    if maxCount > 15000:
        maxCount = 15000
    elif maxCount < 2000:
        maxCount = 7000
    else:
        pass
    
    neededMirrors = intCount / maxCount + 1
    
    # debug print
    print("Will create " + str(neededMirrors) + " reflection gdbs.")
    
    infoForSubprocess = list()
    gdbToCreateList = list()
    
    for countItem in xrange(0, neededMirrors):
        gdbMirrorName = mirrorBaseName + '_' + '0' + str(countItem) + '.gdb'
        gdbMirrorFullPath = os.path.join(mainFolder, gdbMirrorName)
        gdbToCreateList.append(gdbMirrorFullPath)
        try:
            if Exists(gdbMirrorFullPath):
                try:
                    Delete_management(gdbMirrorFullPath)
                except:
                    pass
            else:
                pass
        except:
            pass
        
        CreateFileGDB_management(mainFolder, gdbMirrorName)
        
        # do a selection on the input features here
        # then copyfeatures to get the selected features
        # output to the target gdb.
        
        if Exists(simplifyTempLayer):
            try:
                Delete_management(simplifyTempLayer)
            except:
                pass
        else:
            pass
        
        MakeFeatureLayer_management(inputFeatures, simplifyTempLayer)
        
        currentSelectMin = int(countItem * maxCount) 
        currentSelectMax = int((countItem + 1) * maxCount)
        
        dynSelectClause = """"OBJECTID" >= """ + str(currentSelectMin) + """ AND "OBJECTID" < """ + str(currentSelectMax) + """"""
        
        SelectLayerByAttribute_management(simplifyTempLayer, "NEW_SELECTION", dynSelectClause)
        
        selectedSimplifyFeatures = os.path.join(gdbMirrorFullPath, simplifyInputName)
        
        CopyFeatures_management(simplifyTempLayer, selectedSimplifyFeatures)
        
        subprocessInfoItem = [mainFolder, gdbMirrorFullPath, simplifyAlgorithm, simplifyDistance]
        
        infoForSubprocess.append(subprocessInfoItem)
    
    # Predivide the list of data driven pages that each process needs to run
    # and pass it as a list of exportItems.
    
    coreCount = mp.cpu_count()
    
    # To support running this on the slow AR60, reduce the coreCount used to try to keep
    # this script from crashing there.
    if coreCount >= 3 and useMultithreading == True:
        coreCount = coreCount - 1
        
        print("Starting a multi-threaded job which will use (up to) " + str(coreCount) + " cores at once.")
        
        workPool = mp.Pool(processes=coreCount)
        # Note: This is a different usage of the word map than the one generally used in GIS.
        workPool.map(subProcessFeatureSimplification, infoForSubprocess)
        print("Multi-threaded job's done!")
        
        print("Waiting a few moments before closing down the worker processes...")
        time.sleep(20)
        workPool.close()
        time.sleep(20)
        workPool.join()
        
        print("Worker processes closed.")
        
    else:
        # Don't use multithreading here.
        print("Using the single threaded process for feature simplification.")
        print("This will be slower than the multi-threaded version,")
        print("but it should also be less likely to crash on slower machines")
        print("or those with low core counts.")
        for singleThreadedProcessInfoListItem in infoForSubprocess:
            singleThreadedProcessForSlowMachines(singleThreadedProcessInfoListItem)
        
        print("Waiting a few moments before continuing to the next part of the script...")
        time.sleep(20)
    
    # Delete the output target prior to recreating it and appending data into it.
    if Exists(outputFeatures):
        try:
            Delete_management(outputFeatures)
        except:
            pass
    else:
        pass
    
    # Need the gdb and fc name here from outputFeatures.
    outGDB = returnGDBOrSDEPath(outputFeatures)
    outGDBName = returnGDBOrSDEName(outGDB)
    outGDBFolder = returnGDBOrSDEFolder(outGDB)
    outFCName = returnFeatureClass(outputFeatures)
    
    if not Exists(outGDB):
        CreateFileGDB_management(outGDBFolder, outGDBName)
    
    # Use the inputFeatures as a template.
    CreateFeatureclass_management(outGDB, outFCName, "", inputFeatures)
    
    appendOutputFCList = list()
    
    for gdbToCreate in gdbToCreateList:
        appendOutputFC = os.path.join(gdbToCreate, 'simplificationOutput')
        appendOutputFCList.append(appendOutputFC)
    
    # Do appends here, then sleep again for a bit.
    # Shouldn't need a field mapping since they should all be the same.
    Append_management(appendOutputFCList, outputFeatures, "NO_TEST")
    
    print "Waiting a few moments to be sure that all of the locks have been removed prior to deleting the reflection gdbs..."
    time.sleep(20)
    
    # Then remove the mirror gdbs.
    for gdbToCreate in gdbToCreateList:
        try:
            if Exists(gdbToCreate):
                try:
                    Delete_management(gdbToCreate)
                except:
                    pass
            else:
                pass
        except:
            pass