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?
Solved! Go to Solution.
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)
Hello Anthony, Data Interoperability extension has a transformer called ArcGISOnlineConnector that would let you batch this upload.
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
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)
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?
Do you have any ideas on my issue above with service url's not being generated if I upload my kml files through python?