Select to view content in your preferred language

Updating rows in RowSet using UpdateCursor

1410
3
Jump to solution
08-29-2014 06:42 AM
larsanundskås
Deactivated User

Hello,

I'm new to python, so bear with me

I am fetching features from a featureclass from the rest endpoint

        query = "?where={}&outFields=*&returnGeometry=false&f=json&token={}".format(where, self.token)

        fsURL = self.protocol + self.serverName + ':' + self.serverPort + url + query

        print(fsURL)

        fs = arcpy.RecordSet()

        fs.load(fsURL)

        cursor = arcpy.UpdateCursor(fs)

Then I can iterate over these rows like this:

        for row in cursor:

                print('Updating ' + key + ' to : ' + json.dumps(attributeDict[key], ensure_ascii=False))

            pass

However, I've not been able to see how I can update data on the returned rows.

Since I am fetching data through http I probably just have a local in-memory copy of these features.

Is there any "object oriented" way using arcpy, to update features through the rest endpoint?

Please advise.

Lars

0 Kudos
1 Solution

Accepted Solutions
FilipKrál
Frequent Contributor

Hi,

I have never done this before but I am guessing you could use the applyEdits end point of a Feature Service.

Apply Edits (Operation)

May I bring this python module to your attention:

arcapi/arrest.py at feature-arrest · NERC-CEH/arcapi · GitHub

It is part of arcapi package. although editing has not been implemented, the module makes it easier to interact with the REST end points.

Look at some example at the bottom. Do you have any live feature service we could try this update on?

Cheers,

Filip.

View solution in original post

0 Kudos
3 Replies
FilipKrál
Frequent Contributor

Hi,

I have never done this before but I am guessing you could use the applyEdits end point of a Feature Service.

Apply Edits (Operation)

May I bring this python module to your attention:

arcapi/arrest.py at feature-arrest · NERC-CEH/arcapi · GitHub

It is part of arcapi package. although editing has not been implemented, the module makes it easier to interact with the REST end points.

Look at some example at the bottom. Do you have any live feature service we could try this update on?

Cheers,

Filip.

0 Kudos
larsanundskås
Deactivated User

Thanks Filip, It's not a public service.I will test the apply edits endpoint and post back my approach and results!

have a nice weekend,

Lars

0 Kudos
larsanundskås
Deactivated User

Something like this did the trick using applyEdits

    def writeFeatures(self, url, where, attributeDict):

        query = "?where={}&outFields=*&returnGeometry=false&f=json&token={}".format(where, self.token)

        fsURL = self.protocol + self.serverName + ':' + self.serverPort + url + query

        fs = arcpy.RecordSet()

        fs.load(fsURL)

        cursor = arcpy.SearchCursor(fs)

        for row in cursor:

            print('updating ' + row.getValue('AREAID'))

            features = { 'attributes': {} }

            for key in attributeDict.iterkeys():

                if attributeDict[key] != None:

                    features['attributes'][key] = attributeDict[key]

                else:

                    features['attributes'][key] = None

                pass

            features['attributes']['OBJECTID'] = row.getValue('OBJECTID')

            params = urllib.urlencode({'updates': json.dumps([features]), 'f': 'pjson', 'token': self.token})

            headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

            httpConn = httplib.HTTPConnection(self.serverName, self.serverPort)

            httpConn.request("POST", self.applyEditsUrl, params, headers)

            response = httpConn.getresponse()

Thanks a lot  Filip