English

How To: Add KML and KMZ files to Portal for ArcGIS using ArcGIS API for Python

Summary

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.

Procedure

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.
  1. Import the necessary modules.
import os
from arcgis.gis import GIS
  1. Direct the path to the folder in which the files reside.
path = r'C:\Users\Test\Desktop\KML_Files'
  1. 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)
  1. Connect to the Portal for ArcGIS URL.
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.
  1. Add the 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
  1. Print the URL for all the KML and KMZ files in the ArcGIS Portal Directory.
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")
The image of the Jupyter Notebook result.

Related Information