Cannot add Umlaut and other special characters into FeatureService via Python

906
3
05-20-2021 07:44 AM
StefanFlury
New Contributor III

Cannot add Umlaut and other special characters into FeatureService via Python, via Browser and REST API works fine

1)Share a Feature Service. Use a referenced enterprise geodatabase.
2)Share FeatureService and MapService with everyone
3)a)Go to the service directory and add data:
My URL: https://xxxxxxx/rest/services/test/PyTest/FeatureServer/0/addFeatures
Featrues: [{"geometry":{"x" : 7.15, "y" : 47.15,"z":99.0},"attributes":{"UMLAUT":"ABCäüöèàéèëß"}}]
b)Alternativ: Do an insert in ArcGIS Pro directly into the FeatureClass
The result for both inserts is: ABCäüöèàéèëß
4)Execute the same on Python
url = 'https://xxxxxxxxxx/rest/services/test/PyTest/FeatureServer/0/addFeatures';
features = '[{"geometry":{"x" : 7.15, "y" : 47.15,"z":99.0},"attributes":{"UMLAUT":"ABCäüöèàéèëß"}}]'
gdbVersion = 'sde.default'
format = 'json'
The result is: ABCäüöèà éèëÃ

def get_jsonfromurl_post(url, parameter={}):
""" Gets a json from a url via a get with parameterlist

Args:
url: the url to call
paramter: paramterd ictionary

Returns:
Json

Raises:
ConnectionError if there is no connction to the rest end point
"""
if "f" not in parameter:
parameter.update({"f": "json"})
requests.packages.urllib3.disable_warnings(r_ex.InsecureRequestWarning)
session = requests.Session()
session.trust_env = False
_params = parameter
try:
req = session.post(
url=url,
params=_params,
auth=requests.auth.HTTPDigestAuth(os.environ["USERNAME"], False),
verify=False,
headers={"Content-type": "text/plain; charset=utf-8"},
)
except ConnectionError:
_errmsg = "Could not get connection to {0} with parameterset {1}".format(
url, str(parameter)
)
raise
except Exception as _e:
_errmsg = "Exception while query featureservice {0}".format(_e)
raise _e
if req.status_code == 200:
_json = req.json()
if "error" not in _json:
return json.loads(json.dumps(_json))
else:
_errcode = _json["error"]["code"]
_errmessage = _json["error"]["message"]
_errdetails = _json["error"]["details"]
raise ConnectionError(
"Error while post featureservice with code {0}, {1}, {2}".
format(_errcode, _errmessage, _errdetails[0])
)

else:
_errmsg = "Exception while query featureservice with status {0}".format(
req.status_code
)
raise ConnectionError(_errmsg)


parameter = {}
parameter.update({"features": features})
parameter.update({"gdbVersion": gdbVersion})
parameter.update({"f": format})
print(get_jsonfromurl_post(url, parameter))

0 Kudos
3 Replies
JoeBorgione
MVP Emeritus

Not sure if this will help or not: https://stackoverflow.com/questions/46818040/how-to-work-with-german-umlaut-characters-in-python

 

That should just about do it....
0 Kudos
StefanFlury
New Contributor III

Hoi Joe
I work with Python 3, the example is with Python 2.
I still tested with the following Conten-type headers={"Content-type": "application/json; charset=utf-8"} unfortunately also without success

0 Kudos
StefanFlury
New Contributor III

I have found a workaround in which I develop a GPTool via Python API

gis = GIS(_portal_url_)

search_results = gis.content.search(_keyword_,"Feature Layer")

lyrs = search_results[0]

lyr = lyrs.layers[1]

umlaute = "ABCäüö"

add = {"attributes":{"PUNKTBESCHREIBUNG": umlaute},"geometry": {"x":2784373.041,"y":1187263.299,"z":9999.0}}

add_result = lyr.edit_features(adds=[add],gdb_version=_myVersion_)

 

0 Kudos