603
5
08-27-2020 09:27 AM
by
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?

1 Solution

Accepted Solutions
by
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)

# 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
# Share item to everyone

Items

for item in Items:

print(item.title + ": " + "https://OrganizationName.maps.arcgis.com/home/item.html?id=" + item.id)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
5 Replies
Esri Regular Contributor

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

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

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

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

by
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)

# 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
# Share item to everyone

Items

for item in Items:

print(item.title + ": " + "https://OrganizationName.maps.arcgis.com/home/item.html?id=" + item.id)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
by
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?

by
MVP

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