HOW TO
A KML or KMZ layer can be added as an item to the ArcGIS Online portal through the 'Add Layer from Web' function, or by referencing the URL of the files. Refer to Portal for ArcGIS: Add layers to maps for more information.
To use the URL, .kml or .kmz files can be added to the ArcGIS Portal Directory. The process can be automated using ArcGIS API for Python to add multiple files to the same folder.
The following steps describe how to add KML or KMZ files to the ArcGIS Portal Directory.
Note: This procedure can be executed in Jupyter Notebook.
import os from arcgis.gis import GIS
path = r'C:\Users\Test\Desktop\KML_Files'
KMLs = [] for root, dirs, files in os.walk(path): for file in files: if '.kml' in file or '.kmz' in file: KMLs.append(os.path.join(root, file)) for KML in KMLs: print(KML)
gis = GIS("https://machine.domain.com/portal", "username", "password")
Note: If the HTTP Error 500 displays, add the following parameter, 'verify_cert=False' to bypass certificate verification.
Items = [] for KML in KMLs: kml_properties = { 'type': 'KML', 'typeKeywords': os.path.split(KML)[1] } data_file_location = KML # Add item to portal add_KML = gis.content.add(item_properties = kml_properties, data = data_file_location) # Share item to everyone add_KML.share(everyone = True) Items.append(add_KML) Items
for item in Items: print(item.title + ": " + "https://machine.domain.com/portal/sharing/rest/content/items/" + item.id + "/data")
The following shows the full script:
import os from arcgis.gis import GIS path = r'C:\Users\Test\Desktop\KML_Files' # the folder where the .kml and .kmz files are stored # List all .kml and .kmz files in the folder KMLs = [] for root, dirs, files in os.walk(path): for file in files: if '.kml' in file or '.kmz' in file: KMLs.append(os.path.join(root, file)) for KML in KMLs: print(KML) gis = GIS("https://machine.domain.com/portal", "admin", "password", verify_cert=False) # Add the .kml and .kmz files Items = [] for KML in KMLs: kml_properties = { 'type': 'KML', 'typeKeywords': os.path.split(KML)[1] } data_file_location = KML # Add item to portal add_KML = gis.content.add(item_properties = kml_properties, data = data_file_location) # Share item to everyone add_KML.share(everyone = True) Items.append(add_KML) Items for item in Items: print(item.title + ": " + "https://machine.domain.com/portal/sharing/rest/content/items/" + item.id + "/data")
Article ID: 000022388
Get help from ArcGIS experts
Download the Esri Support App