Using Python to Overwrite a Feature Layer

4786
9
12-07-2016 04:17 PM
CynthiaKozma
Occasional Contributor

We are trying to automate data updates on our AGOL site.  I have a script that zips up 4 shapefiles and uploads this to a specific folder in our Content.  There is also a Feature Layer sitting in the folder created from the Shapefile data.  Manually, you can open the Feature Layer and hit the 'Overwrite' button to update it.  How do you program Python to achieve this same thing?  I also played around with having python publish a new Feature Layer after it uploads the shapefile, but haven't found any examples where the shapefile sitting in the directly has multiple shapefiles in it.  Hopefully, this all makes sense.  Any help would be appreciated. 

0 Kudos
9 Replies
AdamZiegler1
Esri Contributor

Hi Cynthia - Have you read these two blog posts? They might contain what you're looking for.

Updating ArcGIS.com Hosted Feature Services with Python | ArcGIS Blog 

Updating your hosted feature service for 10.2 | ArcGIS Blog 

-Adam Z

0 Kudos
CynthiaKozma
Occasional Contributor

Thank you for your reply.  Both of these examples involve creating an .MXD and thus a service definition.  I was kind of hoping to avoid this.  I already have the shapefile up there from the script that runs addItem().  I don't know if it is possible to do without creating an SD or if that is the route that I need to take.

0 Kudos
Lake_Worth_BeachAdmin
Regular Contributor

can this script be used utilizing enterprise logins rather than AGOL logins?

0 Kudos
AdamZiegler1
Esri Contributor

Hi Joe - That is a great question. Give this article a read to see if it has your answer.

Working with different authentication schemes | ArcGIS for Developers 

Also, here is a more recent article on overwriting Hosted Services.

overwriting_feature_layers | ArcGIS for Developers 

-Adam Z

JonathanQuinn
Esri Frequent Contributor

What I would suggest is using Fiddler or your browsers Dev tools to capture the requests that are made when you manually go through the workflow.  Then, recreate the requests using Python.  You can probably update some of the syntax in the sample scripts.

CynthiaKozma
Occasional Contributor

Well, I have found a work around to this by looping through the shapefiles, zipping them up and publishing them individually instead of all together.  The only problem I am having now is that when the script is rerun, it gets to the arcrest.manageorg.PublishShapefileParameter and throws the error that the service name already exists and thus can not run the user.publishItem.  Does anyone know of a way to delete the hosted feature service first before publishing?  I've tried looking through other scripts but only find ways to delete records within in the feature service and not the entire thing.  Any help would be appreciated.

0 Kudos
CynthiaKozma
Occasional Contributor

Figured it out.  I thought I had read that overwrite = True couldn't be used with publishItem and didn't see it in the documentation, but this line seems to work....

 result = user.publishItem(fileType="Shapefile", publishParameters=publishParams, itemId=itemId, overwrite = True)

0 Kudos
DavidAskov1
Occasional Contributor

Do you mind posting the whole script to update the resources and publish it? I have the same issue with a GeoJSON file I published as a hosted feature service. thanks!

0 Kudos
CynthiaKozma
Occasional Contributor

Is this what you need?  This is just the portion that uploads the shapefiles.  Hopefully, it will help you.  

print 'Starting Upload of Zip Files to AGOL'
 
if __name__=="__main__":
 
        proxy_port = None
        proxy_url = None
 
        securityinfo = {}
        securityinfo['security_type'] = 'Portal'
        securityinfo['username'] = "username"
        username = "username"
        securityinfo['password'] = "password"
        password = "password"
        securityinfo['org_url'] = "http://www.yourorganization.com"
        securityinfo['proxy_url'] = proxy_url
        securityinfo['proxy_port'] = proxy_port
        securityinfo['referer_url'] = None
        securityinfo['token_url'] = None
        securityinfo['certificatefile'] = None
        securityinfo['keyfile'] = None
        securityinfo['client_id'] = None
        securityinfo['secret_id'] = None
 
        #Zipped Files
        url = "http://www.yourorganization.com"
        serviceName = "ROZAData"
        folderTitle = "Irrigation"
        tags = "Data"
        folderId = 'foldId'
        baseURL = "http://www.yourorganization.com/sharing/rest"
#        summary = "ROZA Data File"
        maxRecords = 1000
 
        shAGOL = arcrest.AGOLTokenSecurityHandler(username, password)
        if shAGOL.valid == False:
                print shAGOL.message
        else:
                sh = arcrest.AGOLTokenSecurityHandler(username, password)
                admin = arcrest.manageorg.Administration(securityHandler=sh)
                content = admin.content
                users = content.users
                user = users.user()
                user.currentFolder = "Irrigation"
 
                zippedFlList = {zipDir + "dist_bndy.zip", zipDir + "lat_pt.zip", zipDir + "laterals.zip", zipDir + "delivery.zip",
                                zipDir + "drain_pt.zip", zipDir + "drains.zip", zipDir + "canalrow.zip", zipDir + "canals.zip"}
               
                for zFile in zippedFlList:
                        if (zFile == zipDir + "dist_bndy.zip"):
                                title = "DistrictBoundary"
                        if (zFile == zipDir + "lat_pt.zip"):
                                title = "LateralPoints"
                        if (zFile == zipDir + "laterals.zip"):
                                title = "Laterals"
                        if (zFile == zipDir + "delivery.zip"):
                                title = "Deliveries"
                        if (zFile ==  zipDir + "drain_pt.zip"):
                                title = "DrainPoints"
                        if (zFile ==  zipDir + "drains.zip"):
                                title = "Drains"
                        if (zFile ==  zipDir + "canalrow.zip"):
                                title = "MainCanalRightofWay"
                        if (zFile == zipDir + "canals.zip"):
                                title = "MainCanals"
                        itemParams = arcrest.manageorg.ItemParameter()
                        itemParams.title = title
                        itemParams.type = "Shapefile"
                        itemParams.overwrite = True
                        itemParams.description = "ROZA Irrigation Data Layers"
                        itemParams.tags = "tags"
                        itemParams.snippet = "ROZA Irrigation Data Layers"
                        itemParams.typeKeywords = "Data"
                        item = user.addItem(
                                itemParameters=itemParams,
                                filePath = zFile,
                                overwrite = True,
                                folder = folderId,
                                relationshipType=None,
                                originItemId=None,
                                destinationItemId=None,
                                serviceProxyParams=None,
                                metadata=None)
                        print item.title + " Created"
                        itemId = item.id
                        print itemId
 
                        publishParams = arcrest.manageorg.PublishShapefileParameter(name=title, layerInfo={"type":"Shapefile"})
                        result = user.publishItem(fileType="Shapefile", publishParameters=publishParams, itemId=itemId, overwrite = True)
                        print 'Finished'