Needing to automate the task of uploading hunderds of KMZ files to my ArcGIS Online account.

603
5
Jump to solution
08-27-2020 09:27 AM
vonmoosa
MVP

I recently converted a bunch of registered tif images to kmz format and now need to push these to AGOL as seperate files. Any ideas on how to automate this process?

0 Kudos
1 Solution

Accepted Solutions
vonmoosa
MVP

Below is the python code I ended up using to accomplish this. Thanks for the help!

import os
from arcgis.gis import GIS

path = r'PATH' # 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://arcgis.com", "USERNAME", "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://OrganizationName.maps.arcgis.com/home/item.html?id=" + item.id)

View solution in original post

0 Kudos
5 Replies
BruceHarold
Esri Regular Contributor

Hello Anthony, Data Interoperability extension has a transformer called ArcGISOnlineConnector that would let you batch this upload.

0 Kudos
CalvinLietz
Esri Contributor

Hi Anthony,

If you're looking for something more on the Python side, then the easiest way I can think of would be to use the ArcGIS API for Python. We can iterate through all the KMZ files and add them as items to your ArcGIS Online content. The specific method would be the add operation in the arcgis.gis.ContentManager class arcgis.gis module — arcgis 1.8.2 documentation . KMZ files are a supported file format for ArcGIS Online (a full list can be found here), so you should be able to do this directly.

Here's a sample:

import arcgis

from arcgis.gis import GIS
gis=GIS("https://arcgis.com", "username" , "password")

properties={'title':'myKMZ', 'type':'KMZ','description':'myDescription','tags':'myTags'}

item = gis.content.add(item_properties=properties, data=r"\path\to\myKMZ.kmz")

To iterate through a folder, there is a sample script that can provide a solid starting point: Publishing SDs, Shapefiles and CSVs | ArcGIS for Developers 

We can just substitute KMZ files wherever we see SD files in the above sample, and we should be good to go.

Hope this helps!

Best,

Calvin

vonmoosa
MVP

Below is the python code I ended up using to accomplish this. Thanks for the help!

import os
from arcgis.gis import GIS

path = r'PATH' # 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://arcgis.com", "USERNAME", "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://OrganizationName.maps.arcgis.com/home/item.html?id=" + item.id)
0 Kudos
vonmoosa
MVP

I noticed publishing KML files using the script I provided doesn't give a service URL. If I upload a kml manually through the add items tool in arcgis online then it does give it a service url. Any ideas? 

0 Kudos
vonmoosa
MVP

Jake Skinner‌ 

Do you have any ideas on my issue above with service url's not being generated if I upload my kml files through python?

0 Kudos