Solved! Go to Solution.
Thanks! I think that is my problem - I would like to use the REST API, however can't figure out how to make the GET request (passing in the filter geometry to the map service and setting the export to KMZ) work for KMZ prgramatically. The reason I can't figure it out is because, as far as I know, GET requests can only be handled as text (json, text, html), but not as binary (zip, kmz). So I was wondering is there was a way around this (or if I have it entirely wrong). It here any ways browsers can handle binary responses from http get requests? The api says to use esri.request but I can't get it to deliver a KMZ.
Thanks for your assistance!
Jay
private static geGeometry JsonToKmlGeometry(Dictionary<string, object> geometry) { geGeometry placemarkGeometry; if (geometry.Keys.Contains("x")) { // Create point geometry // x and y are decimals; var x = Convert.ToDouble(geometry["x"]); var y = Convert.ToDouble(geometry["y"]); placemarkGeometry = new gePoint(new geCoordinates(new geAngle90(y), new geAngle180(x))); } else if (geometry.Keys.Contains("rings")) { // Create polygon geometry var rings = (ArrayList)geometry["rings"]; var linearRings = from ArrayList ring in rings select new geLinearRing((from ArrayList point in ring select new geCoordinates( new geAngle90(Convert.ToDouble(point[1])), new geAngle180(Convert.ToDouble(point[0])) )).ToList()); if (linearRings.Count() > 1) { var polygon = new gePolygon(new geOuterBoundaryIs(linearRings.ElementAt(0))); polygon.InnerBoundaries.AddRange(from ring in linearRings.Skip(1) select new geInnerBoundaryIs(ring)); placemarkGeometry = polygon; } else { placemarkGeometry = linearRings.First(); } } else if (geometry.Keys.Contains("paths")) { // Create line geometry var paths = (ArrayList)geometry["paths"]; var lineStrings = from ArrayList ring in paths select new geLineString((from ArrayList point in ring select new geCoordinates( new geAngle90(Convert.ToDouble(point[1])), new geAngle180(Convert.ToDouble(point[0])) )).ToList()); if (lineStrings.Count() > 1) { var multiGeo = new geMultiGeometry(); multiGeo.Geometries.AddRange(lineStrings); placemarkGeometry = multiGeo; } else { placemarkGeometry = lineStrings.First(); } } else { placemarkGeometry = null; } return placemarkGeometry; }
if (Regex.IsMatch(format, "(?i)km[lz]")) { // Generate a KML document. var kmlDocument = new geDocument(); var jsSerializer = new JavaScriptSerializer(); // Loop through the layers. var layers = jsSerializer.Deserialize<Dictionary<string, object>>(json); foreach (var kvp in layers) { var folder = new geFolder { Name = kvp.Key }; kmlDocument.Features.Add(folder); var graphics = kvp.Value as ArrayList; foreach (Dictionary<string, object> graphic in graphics) { var placemark = new gePlacemark(); folder.Features.Add(placemark); placemark.Geometry = JsonToKmlGeometry(graphic["geometry"] as Dictionary<string, object>); var attributesJson = (Dictionary<string, object>)graphic["attributes"]; attributesJson.Remove("RouteGeometry"); ////if (attributesJson.Keys.Contains("BufferedGeometry") && attributesJson.Keys.Contains("BufferSize") && Convert.ToDouble(attributesJson["BufferSize"]) > 0) ////{ //// // TODO: Set the placemark geometry to a multi-geometry containing both the main and buffered geometries. ////} string desc = ToHtmlDL(attributesJson); placemark.Description = desc; } } var kml = new geKML(kmlDocument); // Export the geKML into either KML or KMZ, depending on the specified format. byte[] bytes; if (string.Compare(format, "kmz", true) == 0) { bytes = kml.ToKMZ(); context.Response.ContentType = "application/vnd.google-earth.kmz"; context.Response.AddHeader("Content-Disposition", "filename=ExportedGraphics.kmz"); } else { bytes = kml.ToKML(); context.Response.ContentType = "application/vnd.google-earth.kml+xml"; context.Response.AddHeader("Content-Disposition", "filename=ExportedGraphics.kml"); } context.Response.BinaryWrite(bytes); }
Thanks! I think that is my problem - I would like to use the REST API, however can't figure out how to make the GET request (passing in the filter geometry to the map service and setting the export to KMZ) work for KMZ prgramatically. The reason I can't figure it out is because, as far as I know, GET requests can only be handled as text (json, text, html), but not as binary (zip, kmz). So I was wondering is there was a way around this (or if I have it entirely wrong). It here any ways browsers can handle binary responses from http get requests? The api says to use esri.request but I can't get it to deliver a KMZ.
Thanks for your assistance!
Jay
Jay
How did you resolve this issue without using ArcGIS Geoprocessing Service? The example Jeff Jacobson provided is no longer available.
Many Thanks