[GIS] Publish file geodatabase to ArcGIS Online using ArcGIS API for Python

arcgis-onlinearcgis-proarcgis-python-apifile-geodatabasehosted-feature-layer

I am reading in the documentation it should be possible to publish a local file geodatabase as a hosted feature layer to ArcGIS Online. I have been trying different things, but that job keeps failing.

I am using the code below:

# Publish zipped fgdb to AGOL

fgdb = r"C:\Users\path\to\zipped\geodatabase.gdb.zip"
serviceProp = {}
serviceProp['type'] = 'Feature Service'
serviceProp['url'] = "https://organisation.url.com"

fgdb1 = gis.content.add(item_properties=serviceProp, data = fgdb, folder = "New folder")
fgdb2 = fgdb1.publish(publish_parameters = {'itemID': fgdb1.id}, file_type = 'fileGeodatabase', build_initial_cache=True, overwrite=True)

Has anyone been successful doing something similar? The best I can get is a an empty hosted feature layer. The feature service seems to be created but no feature layers are inside, while the local file geodatabase contains 20 layers.


error message related to comment above: using 'File Geodatabase' as a file type.

error message related to comment above: using 'File Geodatabase' as a file type.

Best Answer

Your code is close, but there are a few things you're doing wrong with the properties you're passing into each call. Try the below code (specifically updating what you're passing for the properties and not passing in the itemId to the publish... the item itself knows which item it is, thus you don't need to give it this).

import arcgis
from arcgis.gis import GIS

g = arcgis.gis.GIS("https://www.arcgis.com", "USER", "PASSWORD")

fgdb = r"C:\temp\foo.zip"
serviceProp = {}
serviceProp['type'] = 'File Geodatabase'
serviceProp['itemType'] = "file"
serviceProp['tags'] = "sometag"

pubProps = {}
pubProps["hasStaticData"] = 'true'
pubProps["name"] ="foo"
pubProps["maxRecordCount"] = 2000
pubProps["layerInfo"] = {"capabilities":"Query"}


fgdb1 = g.content.add(item_properties=serviceProp, data = fgdb)
print(fgdb1)
>><Item title:"foo" type:File Geodatabase owner:USER>

fgdb2 = fgdb1.publish(publish_parameters = pubProps, file_type = 'filegeodatabase', overwrite=True)
print(fgdb2)
>><Item title:"foo" type:Feature Layer Collection owner:USER>
Related Question