I am currently attempting to create a script to download shapefiles from ArcGIS Online to my local drive before adding it to an aprx project and thereafter uploading it to a ArcGIS portal/enterprise.
All the above I have managed by downloading them as shapefiles -> convert .shp to feature class (in gdb) and then uploading it to the aprx.
However, when I run the script the symbology do not tag along. I have tried finding methods of downloading lyrx files from AGOL without any luck.
Do any of you know of a way to download the symbology from AGOL and merge them with the shapefile or feature class?
Here's the most relevant functions in the code I've used:
def agol_login(AGOL_url,AGOL_user,AGOL_pw):
# gis_q = AGOL_url + ', ' + AGOL_user + ', ' + AGOL_pw
print('AGOL login')
gis = GIS(AGOL_url,AGOL_user,AGOL_pw)
return gis
def createGDB():
if folder_exist(path to gdb):
gdb = 'testpath'
else:
gdb = arcpy.CreateFileGDB_management(path to folder, gdb)
def folder_exist(path):
if arcpy.Exists(path):
print(path + " Exist")
return True
else:
print(path + " does not exist")
return False
def downloadUserItem2(item, export_location, downloadFormat):
# type: (object, object) -> object
try:
#Download
name = item.title.replace(" ", "_")
result = item.export(name.format(item.type), downloadFormat)
result.download(export_location)
result.delete()
#Unzip
zip_path = export_location + "\\" + name + ".zip"
zip_file = ZipFile(zip_path)
zip_file.extractall(export_location)
except Exception as e:
print(e)
def SHPtoGDB():
# Set the workspace for ListFeatureClasses
arcpy.env.workspace = "path to folder"
# Use the ListFeatureClasses function to return a list of shapefiles.
featureclasses = arcpy.ListFeatureClasses()
# Copy shapefiles to a file geodatabase
for fc in featureclasses:
arcpy.CopyFeatures_management(
fc, os.path.join("path to gdb",
os.path.splitext(fc)[0]))
def AddFCtoAPRX():
arcpy.env.workspace = "path to gdb"
datasets = arcpy.ListDatasets(feature_type='feature')
datasets = [''] + datasets if datasets is not None else []
rootPath_aprx = 'path to aprx folder'
aprx = arcpy.mp.ArcGISProject(rootPath_aprx + 'TestWMS.aprx')
aprxMap = aprx.listMaps("Map")[0]
for ds in datasets:
for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
path = os.path.join(arcpy.env.workspace, ds, fc)
print ("List of feature classes:")
print(path)
try:
test = aprxMap.addDataFromPath(path)
aprxMap.addLayer(test)
print("Feature Classes added to Arcgis Pro..")
except:
print("gah.")
aprx.save()
print("APRX path is:")
print(aprx.filePath)
def UploadtoPortal():
rootPath_aprx = 'C:\\Users\\ESSE\\Documents\\ArcGIS\\Projects\\TestWMS\\'
aprx = arcpy.mp.ArcGISProject(rootPath_aprx + 'TestWMS.aprx')
m = aprx.listMaps('Map')[0]
arcpy.mp.CreateWebLayerSDDraft(m, rootPath_aprx + 'TestWMS.sddraft', 'Map', 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS')
arcpy.StageService_server(rootPath_aprx + 'TestWMS.sddraft', rootPath_aprx + 'TestWMS.sd')
print("Uploading Service Definition to Portal...")
arcpy.UploadServiceDefinition_server(rootPath_aprx + 'TestWMS.sd', 'My Hosted Services')
downloadFormat
being used in this chunk:result = item.export(name.format(item.type), downloadFormat)