//Downloads job results to files names Success.txt (successfully geocoded results) and // Failed.txt (info about spatial data that was not geocoded successfully). //Parameters: // statusDetails: Inclues job status and the URLs to use to download all geocoded results. // key: The Bing Maps Key for this job. The same key is used to create the job and get job status. static void DownloadResults(DownloadDetails statusDetails, string key) { //Write the results for data that was geocoded successfully to a file named Success.xml if (statusDetails.suceededlink != null && !statusDetails.suceededlink.Equals(String.Empty)) { //Create a request to download successfully geocoded data. You must add the Bing Maps Key to the // download location URL provided in the response to the job status request. UriBuilder successUriBuilder = new UriBuilder(statusDetails.suceededlink + @"?key=" + key); HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(successUriBuilder.Uri); request1.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse)request1.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception("An HTTP error status code was encountered when downloading results."); } using (Stream receiveStream = response.GetResponseStream()) { StreamWriter successfile = new StreamWriter("Success.txt"); using (StreamReader r = new StreamReader(receiveStream)) { string line; while ((line = r.ReadLine()) != null) { successfile.WriteLine(line); } } successfile.Close(); } } } //If some spatial data could not be geocoded, write the error information to a file called Failed.xml if (statusDetails.failedlink != null && !statusDetails.failedlink.Equals(String.Empty)) { //Create an HTTP request to download error information. You must add the Bing Maps Key to the // download location URL provided in the response to the job status request. UriBuilder failedUriBuilder = new UriBuilder(statusDetails.failedlink + @"?key=" + key); HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(failedUriBuilder.Uri); request2.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse)request2.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception("An HTTP error status code was encountered when downloading results."); } using (Stream receiveStream = response.GetResponseStream()) { StreamWriter failedfile = new StreamWriter("Failed.txt"); using (StreamReader r = new StreamReader(receiveStream)) { string line; while ((line = r.ReadLine()) != null) { failedfile.WriteLine(line); } } failedfile.Close(); } } } }
// This example mostly copied from Microsoft Bing Sample pages with some changes // https://docs.microsoft.com/en-us/bingmaps/spatial-data-services/geocode-dataflow-api/geocode-dataflow-sample-code // // remade by Serge Klokov // // Sample command-line: // GeocodeDataFlowExample.exe <dataFilePath> <dataFormat> <key> [<description>] // // Where: // <dataFilePath> is a path to the data file containing entities to geocode. // <dataFormat> is one of these types: xml, csv, tab, pipe. // <key> is a Bing Maps Key from https://www.bingmapsportal.com. // <description> is an optional description for the dataflow job. // static void Main(string[] args) { string bingKey = ConfigurationManager.AppSettings["BingKey"]; string bingURI = ConfigurationManager.AppSettings["BingURI"]; string dataFilePath; string dataFormat; string key; if (args.Length == 0) { dataFilePath = "TextFileAddresses.txt"; // local folder dataFormat = "pipe"; // | in a file key = bingKey; } else { dataFilePath = args[0]; dataFormat = args[1]; key = args[2]; } string description = null; try { if (args.Length > 3) { description = args[3]; } string dataflowJobLocation = CreateJob(dataFilePath, dataFormat, key, description); Console.WriteLine("Dataflow Job Location: {0}", dataflowJobLocation); //Continue to check the dataflow job status until the job has completed DownloadDetails statusDetails = new DownloadDetails(); do { statusDetails = CheckStatus(dataflowJobLocation, key); Console.WriteLine("Dataflow Job Status: {0}", statusDetails.jobStatus); if (statusDetails.jobStatus == "Aborted") { throw new Exception("Job was aborted due to an error."); } Thread.Sleep(30000); //Get status every 30 seconds }while (statusDetails.jobStatus.Equals("Pending")); //When the job is completed, get the results //Two files are created to record the results: // Success.xml contains the data that was successfully geocoded // Failed.mxl contains the data that could not be geocoded DownloadResults(statusDetails, key); } catch (Exception e) { Console.WriteLine("Exception :" + e.Message); } Console.ReadKey(); }
//Checks the status of a dataflow job and defines the URLs to use to download results when the job is completed. //Parameters: // dataflowJobLocation: The URL to use to check status for a job. // key: The Bing Maps Key for this job. The same key is used to create the job and download results. //Return value: A DownloadDetails object that contains the status of the geocode dataflow job (Completed, Pending, Aborted). // When the status is set to Completed, DownloadDetails also contains the links to download the results static DownloadDetails CheckStatus(string dataflowJobLocation, string key) { DownloadDetails statusDetails = new DownloadDetails(); statusDetails.jobStatus = "Pending"; //Build the HTTP Request to get job status UriBuilder uriBuilder = new UriBuilder(dataflowJobLocation + @"?key=" + key + "&output=xml"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri); request.Method = "GET"; //Submit the request and read the response to get job status and to retrieve the links for // downloading the job results //Note: The following conditional statements make use of the fact that the 'Status' field will // always appear after the 'Link' fields in the HTTP response. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception("An HTTP error status code was encountered when checking job status."); } using (Stream receiveStream = response.GetResponseStream()) { XmlTextReader reader = new XmlTextReader(receiveStream); while (reader.Read()) { if (reader.IsStartElement()) { if (reader.Name.Equals("Status")) { //return job status statusDetails.jobStatus = reader.ReadString(); return(statusDetails); } else if (reader.Name.Equals("Link")) { //Set the URL location values for retrieving // successful and failed job results reader.MoveToFirstAttribute(); if (reader.Value.Equals("output")) { reader.MoveToNextAttribute(); if (reader.Value.Equals("succeeded")) { statusDetails.suceededlink = reader.ReadString(); } else if (reader.Value.Equals("failed")) { statusDetails.failedlink = reader.ReadString(); } } } } } } } return(statusDetails); }