} // end Worker_FetchFiles // Worker routine private void Worker_AGOL_KMZ(object sender, DoWorkEventArgs e) { String latMin, latMax, lonMin, lonMax; String filename; PortalItem item; String uploadMIME = "application/octet-stream"; String downloadMIME = "application/vnd.google-earth.kmz"; String baseUri = "https://wvs.earthdata.nasa.gov/api/v1/snapshot?REQUEST=GetSnapshot"; String timeUri = "&TIME="; String bboxUri = "&BBOX="; String crsUri = "&CRS=EPSG:4326"; String formatUri = "&FORMAT=" + downloadMIME; String widthUri = "&WIDTH="; String heightUri = "&HEIGHT="; String layerUri = "&LAYERS="; foreach (WMTS.DownloadLayerInfo info in mainWin.wmts.downloadInfo) { // Create URI used to fetch the imagery info.uriStr = baseUri + layerUri + info.name + bboxUri + info.bbox[0] + "," + info.bbox[1] + "," + info.bbox[2] + "," + info.bbox[3] + crsUri + timeUri + info.date + formatUri + widthUri + info.pixelWidth.ToString() + heightUri + info.pixelHeight.ToString(); Debug.WriteLine(info.uriStr); Uri uri = new Uri(info.uriStr); // Get the data using web calls WebRequest request = WebRequest.Create(uri); WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); if (dataStream != null) { #region GetFileName // Get the filename and add it to the list of downloaded files if (info.bbox[0] < 0) { latMin = info.bbox[0].ToString("F2") + "S"; } else { latMin = info.bbox[0].ToString("F2") + "N"; } if (info.bbox[2] < 0) { latMax = info.bbox[2].ToString("F2") + "S"; } else { latMax = info.bbox[2].ToString("F2") + "N"; } if (info.bbox[1] < 0) { lonMin = info.bbox[1].ToString("F2") + "W"; } else { lonMin = info.bbox[1].ToString("F2") + "E"; } if (info.bbox[3] < 0) { lonMax = info.bbox[3].ToString("F2") + "W"; } else { lonMax = info.bbox[3].ToString("F2") + "E"; } filename = info.name + "_" + info.date + "_" + info.latency + "_Zoom-" + info.zoomLvl + "_" + latMin + "_" + lonMin + "_x_" + latMax + "_" + lonMax + ".kmz"; #endregion downloadedFiles.Add(filename); // Generate portal item and content to upload try { Debug.WriteLine("Starting agol upload"); if (agolUser.portal == null) { MessageBox.Show("null portal\n + file: " + filename); } else { item = new PortalItem(agolUser.portal, PortalItemType.KML, filename) { // Add a brief summary for the item Snippet = "NASA Snapshot image created by Q Basket", // Add a description of the item in more detail Description = "NASA snapshot KMZ file uploaded to ArcGIS Online user account by Q Basket" }; if (item != null) { // provide relevant tags to the item item.Tags.Add("Snapshot"); item.Tags.Add("NASA"); item.Tags.Add("Q Basket"); item.Tags.Add("Quartic Solutions"); item.AccessInformation = "NASA GIBS SNAPSHOT SERVER"; item.SpatialReferenceName = "WGS-84"; item.Name = filename; item.Type = PortalItemType.KML; // add the new item (without content) to the portal MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue(uploadMIME); PortalItemContentParameters itemContent = new PortalItemContentParameters(dataStream, filename, mediaType); // Launch the upload on a separate thread var t = Task.Run(() => agolUser.portal.User.AddPortalItemAsync(item, itemContent)); t.Wait(); } } } catch (Exception ex) { MessageBox.Show("ERROR: " + ex.ToString(), "Create portal item error"); } // Close these up otherwise bad things will happen response.Close(); dataStream.Close(); } // end if data stream Debug.WriteLine("File written: " + downloadedFiles[downloadedFiles.Count - 1]); } // end foreach download item } // end Worker_AGOL
} // end Worker_FetchFiles // Worker routine private void Worker_AGOL_KMZ(object sender, DoWorkEventArgs e) { String latMin, latMax, lonMin, lonMax; String filename; PortalItem item; String uploadMIME = "application/octet-stream"; String downloadMIME = "application/vnd.google-earth.kmz"; String baseUri = "https://wvs.earthdata.nasa.gov/api/v1/snapshot?REQUEST=GetSnapshot"; String timeUri = "&TIME="; String bboxUri = "&BBOX="; String crsUri = "&CRS=EPSG:4326"; String formatUri = "&FORMAT=" + downloadMIME; String widthUri = "&WIDTH="; String heightUri = "&HEIGHT="; String layerUri = "&LAYERS="; foreach (WMTS.DownloadLayerInfo info in mainWin.wmts.downloadInfo) { // Create URI used to fetch the imagery info.uriStr = baseUri + layerUri + info.name + bboxUri + info.bbox[0] + "," + info.bbox[1] + "," + info.bbox[2] + "," + info.bbox[3] + crsUri + timeUri + info.date + formatUri + widthUri + info.pixelWidth.ToString() + heightUri + info.pixelHeight.ToString(); Debug.WriteLine("Snapshot URI:" + info.uriStr); Uri uri = new Uri(info.uriStr); // Get the data using web calls WebRequest request = WebRequest.Create(uri); WebResponse response = request.GetResponse(); Stream dataStream = response.GetResponseStream(); if (dataStream != null) { #region GetFileName // Create the filename for this image if (info.bbox[0] < 0) { latMin = info.bbox[0].ToString("F2") + "S"; } else { latMin = info.bbox[0].ToString("F2") + "N"; } if (info.bbox[2] < 0) { latMax = info.bbox[2].ToString("F2") + "S"; } else { latMax = info.bbox[2].ToString("F2") + "N"; } if (info.bbox[1] < 0) { lonMin = info.bbox[1].ToString("F2") + "W"; } else { lonMin = info.bbox[1].ToString("F2") + "E"; } if (info.bbox[3] < 0) { lonMax = info.bbox[3].ToString("F2") + "W"; } else { lonMax = info.bbox[3].ToString("F2") + "E"; } filename = info.name + "_" + info.date + "_" + info.latency + "_Zoom-" + info.zoomLvl + "_" + latMin + "_" + lonMin + "_x_" + latMax + "_" + lonMax + ".kmz"; #endregion Debug.WriteLine("Uploading " + filename); // Check for duplicate item filenames string msg = String.Empty; Boolean noDuplicates = true; // Check for a duplicate files in the files just uploaded foreach (String fileDownloaded in downloadedFiles) { if (String.Equals(fileDownloaded, filename)) { noDuplicates = false; msg = "\n The file\n" + filename + "\nalready exists in download list"; msg += "\n Item will not be created\n"; MessageBox.Show(msg, "DUPLICATE DOWNLOADED FILE"); break; } } foreach (PortalItem pItem in agolUser.userContent.Items) { if ((String.Equals(filename, pItem.Name)) || (String.Equals(filename, pItem.Title))) { noDuplicates = false; msg = "\n" + filename; msg += "\n This item already exists in User AGOL Content\n"; msg += "\n"; msg += "\n Portal Item Details"; msg += "\n Name: " + pItem.Name; msg += "\n Title: " + pItem.Title; msg += "\n Item ID: " + pItem.ItemId; msg += "\n"; MessageBox.Show(msg, "DUPLICATE PORTAL ITEM NAME"); break; } } // Generate portal item and content to upload if (noDuplicates) { Debug.WriteLine(filename + " is not a duplicate portal item"); try { if (agolUser.portal == null) { MessageBox.Show("Null ArcGIS User portal\n + Writing file: " + filename); } else { item = new PortalItem(agolUser.portal, PortalItemType.KML, filename) { // Add a brief summary for the item Snippet = "NASA Snapshot image created by Q Basket", // Add a description of the item in more detail Description = "NASA snapshot KMZ file uploaded to ArcGIS Online user account by Q Basket" }; if (item != null) { // provide relevant tags to the item item.Tags.Add("Snapshot"); item.Tags.Add("NASA"); item.Tags.Add("Q Basket"); item.Tags.Add("Quartic Solutions"); item.AccessInformation = "NASA GIBS SNAPSHOT SERVER"; item.SpatialReferenceName = "WGS-84"; item.Name = filename; item.Type = PortalItemType.KML; // add the new item (without content) to the portal MediaTypeHeaderValue mediaType = new MediaTypeHeaderValue(uploadMIME); PortalItemContentParameters itemContent = new PortalItemContentParameters(dataStream, filename, mediaType); // Launch the upload on a separate thread var t = Task.Run(() => agolUser.portal.User.AddPortalItemAsync(item, itemContent)); t.Wait(); downloadedFiles.Add(filename); } } } catch (Exception ex) { MessageBox.Show("ERROR: " + ex.ToString(), "CREATE PORTAL ITEM ERROR"); } } // end if noDuplicates // Close these up otherwise bad things will happen response.Close(); dataStream.Close(); } // end if data stream } // end foreach download item } // end Worker_AGOL