def HighwayCalc(): """Pull out State Highways to preserve KDOT LRS Key (CANSYS FORMAT - non directional CRAD) for the KDOT primary route""" if Exists(gdb + "\RoadAlias_Sort"): Delete(gdb + "\RoadAlias_Sort") else: pass Sort_management(Alias, gdb + "\RoadAlias_Sort", "KDOT_CODE ASCENDING;KDOT_ROUTENAME ASCENDING", "UR") #Remember to check the primary route heirarchy calculates correctly where US rides US and I rides I Heriarchy = ["K", "U", "I"] for routeClass in Heriarchy: rideselect = "KDOT_PREFIX LIKE '" + routeClass + "%'" print rideselect, routeClass TableView(gdb + "\RoadAlias_Sort", "RoadAlias_Sort", rideselect) JoinTbl(lyr, "SEGID", "RoadAlias_Sort", "SEGID", "KEEP_COMMON") CalcField(lyr, lyr + ".KDOTPreType", "!RoadAlias_Sort.KDOT_PREFIX!", "PYTHON_9.3", "#") CalcField( lyr, lyr + ".Soundex", "!RoadAlias_Sort.KDOT_PREFIX!+!RoadAlias_Sort.KDOT_ROUTENAME!", "PYTHON_9.3", "#") CalcField(lyr, "KDOT_ADMO", "'S'", "PYTHON_9.3", "#") CalcField(lyr, "PreCode", "0", "PYTHON_9.3", "#") removeJoin(lyr) CalcField( lyr, "RID", "str(!KDOT_COUNTY_R!)+str(!KDOT_COUNTY_L!)+str(!KDOT_CITY_R!)+str(!KDOT_CITY_L!)+str(!PreCode!) + !Soundex! + str(!SuffCode!)+str(!UniqueNo!)+str(!TDirCode!)", "PYTHON_9.3", "#") CalcField(lyr, "LRSKEY", "str(!RID!)", "PYTHON_9.3", "#")
def UniqueIDgen(): for i in range(87, 88): c = str(i).zfill(3) #print "filling in unique Route IDs for county %s" %c expression = "LRS_ROUTE_PREFIX = 'L' AND LRS_COUNTY_PRE = '%s'" % c layer = "County" + c MakeFeatureLayer_management(target, layer, expression) #this part of the script performs a couple types of dissolves to create a unique set of numbers in 4 characters for every route in a county #first, do an unsplit dissolve for each local road in the county based on the RD, STS, and POD fields #this creates nice segments from which to build the destination routes Dissolve_management(layer, "in_memory/" + layer + "d1", "RD;STS;POD;LRS_COUNTY_PRE", "GCID COUNT", "SINGLE_PART", "UNSPLIT_LINES") #add, calculate, and index a field for a join operation #cant add index to in memory database so skip that part AddField_management(layer + "d1", "ConCatRtName", "TEXT", "", "", "50", "", "NULLABLE", "NON_REQUIRED", "") AddField_management(layer + "d1", "RouteNum1", "TEXT", "", "", "6", "", "NULLABLE", "NON_REQUIRED", "") AddField_management(layer + "d1", "UniqueNum1", "TEXT", "", "", "3", "", "NULLABLE", "NON_REQUIRED", "") CalculateField_management( layer + "d1", "ConCatRtName", """[LRS_COUNTY_PRE]&[RD] & [STS] & [POD] """, "VB", "") #dissolve the unsplit dissolve layer to a multipart, full dissolve to get unique road names Dissolve_management(layer + "d1", "in_memory/" + layer + "d2", "ConCatRtName;RouteNum1", "", "MULTI_PART", "DISSOLVE_LINES") #A spatial sort here might be nice #Calculate a unique 4 digit number for each road name #I'm just using the Object ID to calculate the unique number string, with a spatial sort another incrementation method would be needed #the road names should mostly be unique, so a spatial sort at this level would only be beneficial of there is POD field is the only road name distinction #otherwise an attribute sort would be sufficient, if necessary CalculateField_management("in_memory/" + layer + "d2", "RouteNum1", "str(!OBJECTID!).zfill(4)", "PYTHON_9.3", "") # add the unique id field and increment each duplicate road name part # calculate that unique number back to the split dissolve AddJoin_management(layer + "d1", "ConCatRtName", "County087d2", "ConCatRtName", "KEEP_ALL") CalculateField_management(layer + "d1", layer + "d1.RouteNum1", "[" + layer + "d2.RouteNum1]", "VB", "") #AddField_management("in_memory/"+layer+"d2", "UniqueNum1", "TEXT", "", "", "3", "", "NULLABLE", "NON_REQUIRED", "") RemoveJoin_management(layer + "d1") #try this spatial sort thing here Sort_management("in_memory/" + layer + "d1", "in_memory/" + layer + "d1_Sort", "Shape ASCENDING;RouteNum1 ASCENDING", "LL") #now we run the incrementer to calcualte the unique ID's #the incrementer isnt working here, but it is calculating a unique ID on for the segments, and it is going it better and much faster than the join method #it might be better to use the incrementer to do this calculation on the sorted table, then figure out the unique ID Incrementer("in_memory/" + layer + "d1") Delete_management("in_memory/" + layer + "d1") Delete_management("in_memory/" + layer + "d2") Delete_management("in_memory/" + layer + "d2_Sort")
env.workspace = "C:\\demographicMaps\\setores" # Demographic data demographicData = open("C:\\demographicMaps\\tabelas\\Pessoa03_SP1.csv") demographicCsv = csv.reader(demographicData, delimiter=";") demographicCsv.next() demographicSorted = sorted(demographicCsv, key=operator.itemgetter(0)) # Geographic Data geographicData = "sampa" geographicDataOrder = geographicData + "Order" geographicDataTable = geographicDataOrder + ".dbf" # Create a order geographic data Sort_management(env.workspace + "\\" + geographicData + ".shp", geographicDataOrder, [["ID", "ASCENDING"]]) # Id census tract idField = "CD_GEOCODI" # Position variable of demographic data valueEt = 2 # New fields to gographich data newFields = ["white", "black", "yellow", "parda", "indig"] for field in newFields: # Add a new field in table of order geographic data AddField_management(geographicDataTable, field, "SHORT", 5) valueEt += 1
from arcpy import env, AddField_management, Sort_management from arcpy.da import UpdateCursor demographicData = open("C:\\demographicMaps\\tabelas\\Pessoa03_SP1.csv") demographicCsv = csv.reader(demographicData, delimiter=";") demographicCsv.next() demographicSorted = sorted(demographicCsv, key=operator.itemgetter(0)) env.workspace = "C:\\demographicMaps\\setores" geographicData = "sampa" geographicDataOrder = "sampaOrder" geographicDataTable = "sampaOrder.dbf" Sort_management("C:\\demographicMaps\\setores\\sampa.shp", geographicDataOrder, [["ID", "ASCENDING"]]) idField = "CD_GEOCODI" newFields = ["white", "black", "yellow", "parda", "indigenous"] valueEt = 2 for field in newFields: AddField_management(geographicDataTable, field, "TEXT", 100) valueEt += 1 with UpdateCursor(geographicDataOrder + ".shp", (idField, field)) as geographicRows: for geographicRow in geographicRows:
def CreateUniqueRoadNameTable(optionsObject): """ From: Kansas_Unique_Road_Name_List_By_County.py This is an search cursor that creates an in_memory table listing all of the unique road names in a county. The primary purpose of the table that this function returns is to be used by the preAccidentGeocodingUpdate function to inflate the road names from 2010 and prior accident data that has been truncated to 6 characters or less. Note: The 6 character maximum was removed partway through 2010, so a significant portion of the 2010 data does not have that limitation. """ inputCenterlines = optionsObject.roadsFeaturesLocation try: # Set the variables # These should be provided by the optionsObject and # modified by input parameters # (if available how the script was called). GDBPath = "in_memory" env.workspace = "in_memory" tableName = "UniqueRoadNames" sortedTableName = "UniqueRoadNames_Sorted" fieldName2 = "RD" fieldLength2 = 99 roadList = list() newRoadList = list() cursorFields = ["RD"] ### If the table already exists, delete it. if Exists(tableName): Delete_management(tableName) else: pass ### Write a new table to the geodatabase. CreateTable_management(GDBPath, tableName) ### Add a field to the new table. AddField_management(tableName, fieldName2, "TEXT", "", "", fieldLength2) # Create a new search cursor to get road names. # Repoint this to the passed in parameter for the roads centerline. sCursor = SearchCursor(inputCenterlines, cursorFields) for rowItem in sCursor: roadListPart = [rowItem[0]] # Append the current road name to the road name list. roadList.append(roadListPart) # Delete cursor to remove locks on the data try: del sCursor except: pass # Print the first and last item in the roadList. AddMessage("roadList first item:") AddMessage(str(roadList[0])) AddMessage("roadList last item:") AddMessage(str(roadList[-1])) print "" # Assign only the unique values in roadList to a new list. for i in roadList: if (i not in newRoadList): newRoadList.append(i) # Write the newRoadList to the new table using an insert cursor. iCursor = InsertCursor(tableName, cursorFields) # Rewrite this so that it properly uses the information that it iterates over. for roadPlaceHolder in newRoadList: ##AddMessage(str(roadPlaceHolder[0])) iCursor.insertRow(roadPlaceHolder) # Delete cursor to remove locks on the data try: del iCursor except: pass Sort_management(tableName, sortedTableName, [["RD", "ASCENDING"]]) except Exception as Exception1: print str(Exception1[0]) try: del Exception1 except: pass # Even if the program has an exception, it should still delete the cursor objects. finally: try: del sCursor except: pass try: del iCursor except: pass