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?
Below is the python code I ended up using to accomplish this. Thanks for the help!
<SPAN class="keyword token">import</SPAN> os <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>gis <SPAN class="keyword token">import</SPAN> GIS path <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'PATH'</SPAN> <SPAN class="comment token"># the folder where the .kml and .kmz files are stored</SPAN> <SPAN class="comment token"># List all .kml and .kmz files in the folder</SPAN> KMLs <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> root<SPAN class="punctuation token">,</SPAN> dirs<SPAN class="punctuation token">,</SPAN> files <SPAN class="keyword token">in</SPAN> os<SPAN class="punctuation token">.</SPAN>walk<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> file <SPAN class="keyword token">in</SPAN> files<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="string token">'.kml'</SPAN> <SPAN class="keyword token">in</SPAN> file <SPAN class="operator token">or</SPAN> <SPAN class="string token">'.kmz'</SPAN> <SPAN class="keyword token">in</SPAN> file<SPAN class="punctuation token">:</SPAN> KMLs<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>root<SPAN class="punctuation token">,</SPAN> file<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> KML <SPAN class="keyword token">in</SPAN> KMLs<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>KML<SPAN class="punctuation token">)</SPAN> gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"https://arcgis.com"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"USERNAME"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"PASSWORD"</SPAN><SPAN class="punctuation token">,</SPAN> verify_cert<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Add the .kml and .kmz files</SPAN> Items <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> KML <SPAN class="keyword token">in</SPAN> KMLs<SPAN class="punctuation token">:</SPAN> kml_properties <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">'type'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'KML'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'typeKeywords'</SPAN><SPAN class="punctuation token">:</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN>KML<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="punctuation token">}</SPAN> data_file_location <SPAN class="operator token">=</SPAN> KML <SPAN class="comment token"># Add item to portal</SPAN> add_KML <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>item_properties <SPAN class="operator token">=</SPAN> kml_properties<SPAN class="punctuation token">,</SPAN> data <SPAN class="operator token">=</SPAN> data_file_location<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Share item to everyone</SPAN> add_KML<SPAN class="punctuation token">.</SPAN>share<SPAN class="punctuation token">(</SPAN>everyone <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN> Items<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>add_KML<SPAN class="punctuation token">)</SPAN> Items <SPAN class="keyword token">for</SPAN> item <SPAN class="keyword token">in</SPAN> Items<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>item<SPAN class="punctuation token">.</SPAN>title <SPAN class="operator token">+</SPAN> <SPAN class="string token">": "</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"https://OrganizationName.maps.arcgis.com/home/item.html?id="</SPAN> <SPAN class="operator token">+</SPAN> item<SPAN class="punctuation token">.</SPAN>id<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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 GISgis=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
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?
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?
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.