replace file path in table for AGOL hosted feature layer

735
11
Jump to solution
12-19-2018 08:17 AM
JaredPilbeam2
MVP Regular Contributor

I'm looking for a way to change the file path of 100+ fields in a table of a feature layer. I have Jupyter Notebook installed. I'm guessing it can be done with Python? I'm pretty familiar with arcpy, but I've never used python inside AGOL before. 

We recently set up a new server. This 'Attractions' layer (below) has pop-ups and in the pop-ups are links to images. But the file path to these images is to the old server. I want to use python to replace this old path with the updated one.

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Jared Pilbeam , 

In that case I think it might be easier to just perform two field calculations on the hosted feature layer. You could use Arcade for this:

 

View solution in original post

11 Replies
FFSL_-_JustinJohnson
New Contributor III

Have you done much with Python's string methods?  The code below should get you most of the way there.  It's based on the steps outlined here:  Editing Features | ArcGIS for Developers 

You will need the URL of your organization account and your username/password, and the URL of the Feature Layer you want to edit.  After that, you'll need the name of the field containing the URL strings.  Then, you'll need to write the expression to change the current value in the field to the desired value.

Let me know if you have any issues.

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

# create the GIS object from the arcgis.gis module
gis = GIS("http://[your organization name].maps.arcgis.com", username="[your username]", password="[your password]")

# get the Feature Layer from the Feature Layer URL
feat_layer_url = r'https://services.arcgis.com/[...]/arcgis/rest/services/[ ...]/FeatureServer/0'
feat_layer = FeatureLayer(feat_layer_url)

# query/get a list of features from the feature layer:
# an empty query string will return all the features or the first 1000 which ever is smaller
existing_feats = feat_layer.query()

# loop through the features and udpate an attribute value in each
for feat in existing_feats:
    # create a copy of the feature
    # this will be used to update the current feature in the Feature Layer after editing
    edit_feat = feat

    # here is where the changes are made to the attribute value
    # use any Python string methods you need, i.e. replace() or slice, to update the field value
    ptname = feat.attributes['URL_FIELD']
    newptname = ptname.replace('oldurl', 'newurl')
    edit_feat.attributes['URL_FIELD'] = newptname

    # use the changed copy of the feature to update the attributes of the current feature
    feat_layer.edit_features(updates=[edit_feat])‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JaredPilbeam2
MVP Regular Contributor

Justin,

Thanks. I've been attempting your code. It seems as though it almost ran. But there was an error of sorts, something about 'no permission'. 

{'addResults': [],
 'deleteResults': [],
 'updateResults': [{'error': {'code': 1009,
    'description': 'No permission to edit the specified feature.'},
   'globalId': None,
   'objectId': 52,
   'success': False,
   'uniqueId': 52}]}
0 Kudos
FFSL_-_JustinJohnson
New Contributor III

I can't seem to reproduce your error, but from this other GeoNet post, it looks like it may be a permissions issue with the Feature Service:  https://community.esri.com/thread/194183-unable-to-edit-submitted-surveys-send-error-code-1009 

Are you able to make edits to the Feature Service when you log in to ArcGIS Online with the username and password you're using in the script?

JaredPilbeam2
MVP Regular Contributor

My mistake, I wasn't the originator of that Feature Layer. I couldn't edit it in AGOL either. So, I added a copy and ran the script on it. 

Now I'm getting a NameError.

NameError: name 'FeatureLayer' is not defined

But, here's how I have it:

feat_layer = FeatureLayer(feat_layer_url)

And the feat_layer_url is the new URL, which is valid. 

FFSL_-_JustinJohnson
New Contributor III

That NameError is odd.  Does your version of the script include the line that imports the FeatureLayer module?

from arcgis.features import FeatureLayer
0 Kudos
JaredPilbeam2
MVP Regular Contributor

Here's how the script looks; minus the correct URL.

#get the feature layer from the feature layer URL
feat_layer_url = r'https://services.arcgis.com...'
feat_layer = FeatureLayer(feat_layer_url)

# query/get a list of features from the feature layer:
# an empty query string will return all the features or the first 1000 which ever is smaller
existing_feats = feat_layer.query()

#loop through the features and update an attribute value in each
for feat in existing_feats:
    #create a copy of the feature
    #this will be used to update the current feature in the Feature Layer after editing
    edit_feat = feat
    
    #here is where the changes are made to the attribute value
    #use any Python string mehtods you need, i.e.. replace() or slice, to update the field value
    ptname = feat.attributes['Photo']
    newptname = ptname.replace('http://www.willcogis.org/website2014/gis/images/harrahs.png', 'https://webapp.willcountyillinois.com/gisweb/images/cretelibrary.png')
    edit_feat.attributes['Photo'] = newptname
    
    # use the changed copy of the feature to update the attributes of the current feature
    feat_layer.edit_features(updates=[edit_feat])
    print('finished')
0 Kudos
FFSL_-_JustinJohnson
New Contributor III

Is there more to your version of the script than that?  It looks like you're missing the first 5 lines (unless you didn't copy/paste that section).  

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

# create the GIS object from the arcgis.gis module
gis = GIS("http://[your organization name].maps.arcgis.com", username="[your username]", password="[your password]")
‍‍‍‍‍

If you're trying to run it exactly as it appears in your previous message, it would be the cause of the error:

 'FeatureLayer' is not defined 

0 Kudos
JaredPilbeam2
MVP Regular Contributor

Justin,

Yes, those five lines are not pasted. I did run the entire script. 

Now I'm getting an HTTPError.

RuntimeError: HTTPError: this service url encountered an HTTP Error:

The HTTPError happens when I include the token at the end of the feat_layer_url. And there doesn't seem to be a problem with the URL. It works fine when I put it in the browser window(s). If I remove the token I get a RuntimeError on line 22.

RuntimeError: 
This operation is not supported.
Unable to update the features.
This operation is not supported.
(Error Code: 400)‍‍‍‍‍
0 Kudos
XanderBakker
Esri Esteemed Contributor

Just to clarify: you have 100+ fields or do you have a single hosted feature layer with 2 fields and 100+ records (as shown in the screenshot). If it is the latter a simple field calculation should do the job. However, if you need to walk through many fields, the Python API is the way to go.