#Process: Add the specified ORIG_OID field name tempOidFieldName = originalOidFieldName if originalOidFieldName not in ["","#"," "]: originalOidFieldName = gp.ValidateFieldName(originalOidFieldName, os.path.dirname(outputLayer)) if outShpDbfFlag == True: gp.AddField_management(outputLayer, originalOidFieldName[0:10], "LONG") else: gp.AddField_management(outputLayer, originalOidFieldName, "LONG") #...... In Warnings section. if not tempOidFieldName == originalOidFieldName: message = "WARNING: The Original OID field was changed from " + tempOidFieldName + " to " + originalOidFieldName + "!"; showPyWarning()
import arcgisscripting gp = arcgisscripting.create(9.3) fc = r"D:\csny490\oesf_nso_models_20100623\landscape_model\results\results_bu.gdb\summary_results" fieldList = gp.listfields(fc) fieldIndex = 0 fieldNameDict = {} for field in fieldList: fieldNameDict[field.name] = fieldIndex fieldIndex = fieldIndex + 1 fcDictionary = {} oidFieldName = gp.describe(fc).oidfieldname searchRows = gp.searchcursor(fc) searchRow = searchRows.next() while searchRow: recordReadCount = 0 oidFieldValue = searchRow.getvalue(oidFieldName) fcDictionary[oidFieldValue] = [] for field in fieldList: fcDictionary[oidFieldValue].append(searchRow.getvalue(field.name)) searchRow = searchRows.next() del searchRow del searchRows #Now that it's in the memory, fetch the habitat density field value of OID = 31 and its width... fcDictionary[31][fieldNameDict["HAB_DENSITY"]] + " - " + str(fcDictionary[31][fieldNameDict["Shape"]].extent.width) + " feet wide!"
However, for truly embedded cursor routines that have to look up records against each other or walk through a sorted pair of cursors (using multi-field keys especially), the primary hit is from maintaining the secondary query synchronization on unsorted records or records that are only sorted in memory.
Jinx posting. Look at my post right above yours concerning dictionaries. This is all great stuff - I'm a hack self-taught programmer, so it's great to get someone else's suggestions, tips, tricks, etc.
However, I don't think there is a way to retrieve a field value in a cursor using it's index value. Maybe I'm wrong though... Seems that would be a major performance enhancement like you said. I have mostly shied away from embedded cursors (current script being an exception) since they tend to be slow.
Rich,You are totally correct! Great observation... I thought it seemed like when I was testing this stuff yesterday it was running a bit slow when I used the ORIG_OID option... Another idea to structure it would be to append the ORIG_OID field (if the user provides it) to the inputLayerFieldList, and then have a condition in the loop that says if inputLayerField == originalOidFieldName, then attempt to write it.
lookupDict = {} searchRows = gp.searchcursor(lookupTbl) searchRow = searchRows.next() while searchRow: lookupDict[searchRow.LOOKUPITEM] = [searchRow.FIELDVALUE1,searchRow.FIELDVALUE2] searchRow = searchRows.next() del searchRow del searchRows updateRows = gp.updatecursor(tableToUpdateTbl) updateRow = updateRows.next() while updateRow: updateRow.MEAN = (lookupDict[updateRow.LOOKUPITEM][0] + lookupDict[updateRow.LOOKUPITEM][1]) / 2 updateRows.UpdateRow(updateRow) updateRow = updateRows.next() del updateRow del updateRows del lookupDict
inputLayerOidFieldName = dsc.oidfieldname #Only attempt to write to the users OID field if the a field name is provided and the input has an OID field. if originalOidFieldName not in ["","#"," "] and inputLayerOidFieldName > " ": tryOidFieldWrite = True origOidPopulateSuccessFlag = True else: tryOidFieldWrite = False origOidPopulateSuccessFlag = False while searchRow: insertRow = insertRows.newrow() for fieldName in inputLayerFieldList: try: #you can't write the OID, length, area, etc. fields! if outShpDbfFlag == False: # Only attempt to write to the user OID field when the input OID field is being read if tryOidFieldWrite == True and inputLayerOidFieldName == fieldName: try: insertRow.setvalue(originalOidFieldName, searchRow.getvalue(inputLayerOidFieldName)) except: origOidPopulateSuccessFlag = False pass # All fields other than the input OID field are passed to this code. The input OID will also be passed if the user did not provide an output OID field name, but being non-writable it will fail. else: insertRow.setvalue(fieldName, searchRow.getvalue(fieldName)) else: # Only attempt to write to the user OID field when the input OID field is being read if tryOidFieldWrite == True and inputLayerOidFieldName == fieldName: try: insertRow.setvalue(originalOidFieldName[0:10], searchRow.getvalue(inputLayerOidFieldName)) except: origOidPopulateSuccessFlag = False pass # All fields other than the input OID field are passed to this code. The input OID will also be passed if the user did not provide an output OID field name, but being non-writable it will fail. else: insertRow.setvalue(fieldName[0:10], searchRow.getvalue(fieldName)) except: pass recordCount = recordCount + 1 insertRows.insertrow(insertRow) searchRow = searchRows.next() message = "Successfully wrote " + str(recordCount) + " records!"; showPyMessage() # Only publish a warning if the OID field write attempt should have been made and failed or if the user provided a field but the input had no OID field. if originalOidFieldName not in ["","#"," "] and tryOidFieldWrite == True and origOidPopulateSuccessFlag == False: message = "WARNING: For some reason, the original OIDs could not be preserved in the output!"; showPyWarning() if originalOidFieldName not in ["","#"," "] and tryOidFieldWrite == False: message = "WARNING: The input had no OID field and could not be preserved in the output!"; showPyWarning()
Didn't do much extensivee bug testing - Let me know if you find any issues...
#make sure the same field isn't trying to be sorted twice if (sortFieldName4 not in ["","#"," "] and (sortFieldName4 in [sortFieldName1,sortFieldName2,sortFieldName3])): message = "ERROR: Duplicate field name in Sort Field #4. Can't sort the same field twice! Exiting script..."; showPyError(); sys.exit() if (sortFieldName3 not in ["","#"," "] and (sortFieldName3 in [sortFieldName1,sortFieldName2])): message = "ERROR: Duplicate field name in Sort Field #3. Can't sort the same field twice! Exiting script..."; showPyError(); sys.exit() if (sortFieldName1 == sortFieldName2): message = "ERROR: Duplicate field name in Sort Field #2. Can't sort the same field twice! Exiting script..."; showPyError(); sys.exit()
I guess the old ArcScripts site is now uneditable - Damn! Until I get a CodeGallery thing going, here's the new script/toolbox. Didn't do much extensivee bug testing - Let me know if you find any issues...
Rich,I will post an update to acrscripts in the next few days. So as I understand, the requirements are:1. The user can select up to four fields to sort.2. The user can specify a new field name that will contain the original OID prior to sorting.Sound Good?
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.