Passing parameters to the featureLayer.calculate

719
10
Jump to solution
08-12-2021 11:41 AM
ChuckBenton
Occasional Contributor

I'm sure this is a simple issue, but it's stumped me.

The following line executes without issue:

fs_layer.calculate(where="FIPS=18039", calc_expression={"field":"VERSION", "value": "07/04/2021"})

This fails:

where_str = '"FIPS=18039"'
calc_str = '"field":"VERSION", "value":"07/10/2021"'
fs_layer.calculate(where=where_str, calc_expression={calc_str})

Ultimately I want to iterate the fs_layer.calculate line in a loop, passing different FIPS and VERSION values each time.

I've tried multiple ways to build the parameters to be passed, building the entire argument, varying quote schemes, and so on. What am I missing?

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

I don't know your data types. I've gone over what you previously have said has worked, and although I don't entirely understand the leading 0 and it not being a string, this will probably work:

"FIPS={0}".format(fips)

View solution in original post

10 Replies
DavidPike
MVP Frequent Contributor

just as you might have a string going to a string within a list, the quotes aren't removed. e.g.

my_string = 'a'
my_list = [my_string]

# so my_list looks like ['a'], not [a]

my_string = "I've made a string"
my_list = [my_string]

#... 

 containing the string in curly braces acts in the same way as the list forcing, calc_expression just becomes a set containing the string.

0 Kudos
ChuckBenton
Occasional Contributor

This doesn't seem to be the issue. I've converted to lists and run the following:

where_str = '"FIPS=18039"'
where_list = [where_str]
calc_str = '"field":"VERSION", "value":"07/10/2021"'
calc_list = [calc_str]
print("where="+where_list[0]+", calc_expression={"+calc_list[0]+"}")
fs_layer.calculate(where=where_list[0], calc_expression={calc_list[0]})

I added the print line to confirm formatting, its output:

where="FIPS=18039", calc_expression={"field":"VERSION", "value":"07/10/2021"}

 I get the same error as when I passed the strings:

Exception: 
Invalid parameters
(Error Code: 400)

Keep in mind this line works:

fs_layer.calculate(where="FIPS=18039", calc_expression={"field":"VERSION", "value": "07/04/2021"})

 I'm still stumped....

0 Kudos
DavidPike
MVP Frequent Contributor

Forget about the list, it's just an example of what's going on.  When printing calc_expression, it looks like a dictionary but it's not - it's still a string.  Check this by using the type() method - e.g. print(type(calc_expression))

you'll see the one which works is a dict, the other is either a set or a string.

What's the purpose of trying to use the first method to create the dictionary?

0 Kudos
ChuckBenton
Occasional Contributor

What I need is a generic function I can call, passing it a FIPS and date (both as strings). I have about 150M records, spread among about 2000 distinct FIPS values, for which I want to update the VERSION fields. Something like

def updateRecords(fips, version_date)

         fs_layer.calculate(where=fips, , calc_expression={"field":"ATTDATE", "value": version_date})

Of course, passing the parameters to work within .calculate is the issue. As you've probably guessed, I'm not a Python expert! 

Thanks in advance for your advice!

0 Kudos
DavidPike
MVP Frequent Contributor

ok so you just need to construct a dictionary with the function.

def updateRecords(fips, version_date):
    calc_dict = {}
    calc_dict['field'] = 'ATTDATE'
    calc_dict['value'] = version_date
    fs_layer.calculate(where=fips, , calc_expression=calc_dict)

 

ChuckBenton
Occasional Contributor

Thanks so much! Still struggling with one last detail.

def updateRecords(fips, version_date):
    fips = '"FIPS=' +fips+'"'
   print(fips)
   calc_dict = {}
   calc_dict['field'] = 'VERSION'
   calc_dict['value'] = '"' + version_date + '"'
   fs_layer.calculate(where=fips, calc_expression=calc_dict)

The print statement yields 

"FIPS=06007"

but I get 

Exception: 
'where' parameter is invalid
(Error Code: 400)

 Change the last line to 

fs_layer.calculate(where="FIPS=06007", calc_expression=calc_dict)

and all works.

I'm still missing something that's not jumping out to me.

Thanks again for helping a newbie!

 

0 Kudos
DavidPike
MVP Frequent Contributor

not sure if FIPS is a string or Int, I'll assume its a string from your code

 

 

fips = '"FIPS" = "{0}"'.format(fips)

 

 

0 Kudos
ChuckBenton
Occasional Contributor

Still didn't do the trick.

fips = '"FIPS={0}"'.format(fips) yields 

"FIPS" = "06007"

from the print, and

Exception: 
'Invalid field: 06007' parameter is invalid
(Error Code: 400)

for the error.

 

Suggestions?

0 Kudos
DavidPike
MVP Frequent Contributor

I don't know your data types. I've gone over what you previously have said has worked, and although I don't entirely understand the leading 0 and it not being a string, this will probably work:

"FIPS={0}".format(fips)