Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

in_memory workspace bug

$
0
0
I posted this comment here but wanted to start a new thread, since this seems like a clear bug & I'd be interested if anyone else has come across it or found a solution.

I have a script that ranks the values in a field and writes the ranks (e.g largest = 1, smallest = n or vice versa) to another field. When I do this on a FC stored in a fileGDB I get accurate results. However, if I switch to the in-memory workspace, the rank values are sequential with the OBJECTIDs. Can anyone replicate this, have similar problems, or know a workaround?

Code:

import arcpy
import sys

#Function to rank the values of a field write the rank to an existing field.
#Arguments include workspace path (workspace), FeatureClass name (FeatureClass), field with values to rank (valueField),
#and field to write values to (RankField).
#Order can be either ascending "A" (smallest value rank=1) or descending "D" (largest value rank =1)
#
class Rank:
        def __init__(self, workspace, FeatureClass, ValueField, RankField, Order):
                self.workspace = workspace
                self.fc = FeatureClass
                self.valueField = ValueField
                self.rankField = RankField
                self.Order = Order

        def rank(self):
                try:
                        arcpy.env.workspace = self.workspace
                        currRank = 0
                        lastRank = 0
                        lastVal = None

                        #Since arcpy.da takes "ASC" and "DESC" instead of "A" and "D", reassign values if needed
                        if self.Order == "A" or self.Order == "ASC":
                                self.Order = "ASC"
                        elif self.Order =="D" or self.Order == "DESC":
                                self.Order = "DESC"
                        else:
                                arcpy.AddError('Sort order was not recognized.  Use "A" for ascending or "D" for descending.')
                                print('Sort order was not recognized.  Use "A" for ascending or "D" for descending.')
                                sys.exit()

                        #****************************************************
                        #arcpy.da version of rank script
                        #****************************************************
                        fields = (self.valueField, self.rankField)

                        #sql_clause is a named parameter (don't need to add None values for other optional params.  ORDER BY sorts the ranking
                        with arcpy.da.UpdateCursor(self.fc, fields, sql_clause=(None, 'ORDER BY {} {}'.format(self.valueField, self.Order))) as rows:
                                for row in rows:
                                        currVal = row[0]
                                        if currVal != lastVal:                  #if the current value does not equal the previous value
                                                currRank += 1
                                                row[1] = currRank
                                        elif currVal == lastVal:
                                                row[1] = currRank
                                        else:
                                                print "Unexpected occurance"
                                        lastVal = currVal
                                        rows.updateRow(row)
                                print "Finished rank script"

                except Exception as e:
                        arcpy.AddError("The was a problem with the Rank Module... " + e.message)
                        print("The was a problem with the Rank Module... " + e.message)
                        sys.exit()


Viewing all articles
Browse latest Browse all 2485

Trending Articles