import arcpy, os, timefrom zipfile import ZipFilefrom arcgis.gis import GISarcpy.env.overwriteOutput = TruestartTime = time.clock() # Variables # AGOL User username = "nick.shannon" #AGOL Password namepassword = "Geography2017" #Path to Feature Class fc = r "C:\Projects\Data.gdb\CustomerData" #Feature Service Item ID to update fsItemId = "c0e41f90407043a2b4ae6b2761c496db" # Create GIS object gis = GIS("https://www.arcgis.com", 'nick.shannon', 'Geography2017') # Function to Zip FGD def zipDir(dirPath, zipPath): zipf = ZipFile(zipPath , mode='w') gdb = os.path.basename(dirPath) for root, _ , files in os.walk(dirPath): for file in files: filePath = os.path.join(root, file) zipf.write(filePath , os.path.join(gdb, file)) zipf.close() print("Creating temporary File Geodatabase") arcpy.CreateFileGDB_management(arcpy.env.scratchFolder, "TempGDB") #Export feature classes to temporary File Geodatabase fcName = os.path.basename(fc) fcName = fcName.split('.')[-1] print("Exporting {0} to temp FGD".format(fcName)) arcpy.conversion.FeatureClassToFeatureClass(fc, os.path.join(arcpy.env.scratchFolder, "TempGDB.gdb"), fcName) # Zip temp FGD print("Zipping temp FGD") zipDir(os.path.join(arcpy.env.scratchFolder,"TempGDB.gdb"), os.path.join(arcpy.env.scratchFolder, "TempGDB.gdb.zip")) #Check if FGD exists, if True, delete itemsearchResults = gis.content.search('title:tempFGD AND owner:{0}'.format(username), item_type = 'File Geodatabase') if len(searchResults) > 0: item = searchResults[0] item.delete() #Upload zipped File Geodatabase print("Uploading File Geodatabase") fgd_properties = { 'title': 'tempFGD', 'tags': 'temp file geodatabase', 'type': 'File Geodatabase'} fgd_item = gis.content.add(item_properties = fgd_properties, data = os.path.join(arcpy.env.scratchFolder, "TempGDB.gdb.zip")) # Truncate Feature Service print("Truncating Feature Service") premiseLayer = gis.content.get(fsItemId) fLyr = premiseLayer.layers[0] fLyr.manager.truncate() #Append features from feature class print("Appending features") fLyr.append(item_id = fgd_item.id, upload_format = "filegdb", upsert = False, field_mappings = []) # Delete Uploaded File Geodatabase print("Deleting uploaded File Geodatabase") fgd_item.delete() # Delete temporary File Geodatabase and zip file print("Deleting temporary FGD and zip file") arcpy.Delete_management(os.path.join(arcpy.env.scratchFolder, "TempGDB.gdb")) os.remove(os.path.join(arcpy.env.scratchFolder, "TempGDB.gdb.zip")) endTime = time.clock() elapsedTime = round((endTime - startTime) / 60, 2) print("Script finished in {0} minutes".format(elapsedTime)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question