Skip to main content
Solved

Edit coded value domains for a feature layer in AGOL using PythonCaller?


ileung_cnw
Contributor
Forum|alt.badge.img+2

I am receiving this error below when I try to retrieve the getJSON variable:

Python Exception <Exception>: Unable to update feature service layer definition.
Object reference not set to an instance of an object.
(Error Code: 400)
Error encountered while calling function `processFeature'
PythonCaller_3 (PythonFactory)PythonFactory failed to process feature
PythonCaller_3 (PythonFactory)A fatal error has occurredCheck the logfile above for details
A fatal error has occurredCheck the logfile above for details
JSONFormatter (JSONFormatterFactory):

This is the code in PythonCaller (simplified for readability):

import fme
import fmeobjects
from arcgis.gis import GIS
 
def processFeature(feature):
   getJSON = feature.getAttribute('JSON')
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

I know getJSON is giving me issues because if I replace lyr.manager.update_definition(getJSON) with lyr.manager.update_definition({ "fields" : [ { "name" : "FIELDNAME", "domain" : { "type" : "codedValue", "name" : "DOMAINNAME" : [ { "name" : "TEST", "code" : "TEST" }, { "name" : "TEST1", "code" : "TEST1" }, { "name" : "TEST2", "code" : "TEST2" } ] } } ] } then it works. 

 

Essentially, I like to create a JSON object in an attribute using JSONTemplater and put it into PythonCaller so I don't have to hardcode the JSON object. 

 

Thanks in advance!

Best answer by david_r

getAttribute() returns a string, where as update_definition() seems to expect a dict.

Possible workaround:

import fmeobjects
from arcgis.gis import GIS
import json
 
def processFeature(feature):
   # parse str to dict
   getJSON = json.loads(feature.getAttribute('JSON'))
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

 

View original
Did this help you find an answer to your question?

9 replies

ileung_cnw
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • April 29, 2021

Alternatively, I was able to edit coded value domains using HTTPCaller. This now allows me to update new coded value domains to an existing feature class from data collected in Survey123.

 

Below are some of the tips that got me through this issue:

 

Screenshot of my HTTPCaller parameters for reference:

httpcaller


david_r
Celebrity
  • Best Answer
  • April 29, 2021

getAttribute() returns a string, where as update_definition() seems to expect a dict.

Possible workaround:

import fmeobjects
from arcgis.gis import GIS
import json
 
def processFeature(feature):
   # parse str to dict
   getJSON = json.loads(feature.getAttribute('JSON'))
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

 


ileung_cnw
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • April 29, 2021
david_r wrote:

getAttribute() returns a string, where as update_definition() seems to expect a dict.

Possible workaround:

import fmeobjects
from arcgis.gis import GIS
import json
 
def processFeature(feature):
   # parse str to dict
   getJSON = json.loads(feature.getAttribute('JSON'))
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

 

Thanks for the quick reply! Parsing JSON str to dictionary resolved the issue. The API reference documented this but now I understand how to parse it. Good to know, thank you! 👍 


ileung_cnw
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • April 29, 2021
ileung_cnw wrote:

Alternatively, I was able to edit coded value domains using HTTPCaller. This now allows me to update new coded value domains to an existing feature class from data collected in Survey123.

 

Below are some of the tips that got me through this issue:

 

Screenshot of my HTTPCaller parameters for reference:

httpcaller

I compared using PythonCaller and HTTPCaller and realized HTTPCaller finishes ~3x faster.


Forum|alt.badge.img
david_r wrote:

getAttribute() returns a string, where as update_definition() seems to expect a dict.

Possible workaround:

import fmeobjects
from arcgis.gis import GIS
import json
 
def processFeature(feature):
   # parse str to dict
   getJSON = json.loads(feature.getAttribute('JSON'))
   gis = GIS(profile='username') 
   lyr = gis.content.get('item_id').layers
   for lyr in gis.content.get('item_id').layers:
      lyr.manager.update_definition(getJSON)

 

This worked like a charm, but somehow I get an error when trying to import json. 

Python Exception <ModuleNotFoundError>: No module named 'ujson'

Does anyone have a clue what is happening here? 


david_r
Celebrity
  • June 30, 2021
tsmit_gis wrote:

This worked like a charm, but somehow I get an error when trying to import json.

Python Exception <ModuleNotFoundError>: No module named 'ujson'

Does anyone have a clue what is happening here?

Sounds like a problem with a third-party module.

Have you tried setting different Python interpreters in your workspace?


Forum|alt.badge.img
tsmit_gis wrote:

This worked like a charm, but somehow I get an error when trying to import json.

Python Exception <ModuleNotFoundError>: No module named 'ujson'

Does anyone have a clue what is happening here?

Thanks David, it looks like that indeed; Have to look into the extra module; case closed for now.


vhruska
Contributor
Forum|alt.badge.img+6
  • Contributor
  • October 13, 2022
ileung_cnw wrote:

Alternatively, I was able to edit coded value domains using HTTPCaller. This now allows me to update new coded value domains to an existing feature class from data collected in Survey123.

 

Below are some of the tips that got me through this issue:

 

Screenshot of my HTTPCaller parameters for reference:

httpcaller

Hello! I'm trying to accomplish the same task in Survey 123 but I'm getting hung up pulling down the initial domain values. Do you have an example workspace you may be able to share?


evieatsafe
Safer
  • Safer
  • October 14, 2022
vhruska wrote:

Hello! I'm trying to accomplish the same task in Survey 123 but I'm getting hung up pulling down the initial domain values. Do you have an example workspace you may be able to share?

Hi @vhruska​ I would post a new question on the community and refer to this thread as this question is quite old. Hope this helps.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings