/// <summary> /// Downloads job results to files names Success.txt (successfully geocoded results) and Failed.txt (info about spatial data that was not geocoded successfully). /// </summary> /// <param name="statusDetails">Inclues job status and the URLs to use to download all geocoded results.</param> /// <param name="bingMapsKey">The Bing Maps Key for this job. The same key is used to create the job and get job status. </param> private async Task <BatchGeocoderResults> DownloadResults(DownloadDetails statusDetails, string bingMapsKey) { var results = new BatchGeocoderResults(); //Write the results for data that was geocoded successfully to a file named Success.xml if (statusDetails.SucceededUrl != null && !statusDetails.SucceededUrl.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. var successUriBuilder = new UriBuilder(statusDetails.SucceededUrl + "?clientApi=SDSToolkit&key=" + bingMapsKey); var successfulRequest = (HttpWebRequest)WebRequest.Create(successUriBuilder.Uri); successfulRequest.Method = "GET"; using (var response = (HttpWebResponse)await successfulRequest.GetResponseAsync()) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception("An HTTP error status code was encountered when downloading results."); } using (var receiveStream = response.GetResponseStream()) { results.Succeeded = await GeocodeFeed.ReadAsync(receiveStream); } } } //If some spatial data could not be geocoded, write the error information to a file called Failed.xml if (statusDetails.FailedUrl != null && !statusDetails.FailedUrl.Equals(String.Empty)) { var failedRequest = (HttpWebRequest)WebRequest.Create(new Uri(statusDetails.FailedUrl + "?clientApi=SDSToolkit&key=" + bingMapsKey)); failedRequest.Method = "GET"; using (var response = (HttpWebResponse)await failedRequest.GetResponseAsync()) { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception("An HTTP error status code was encountered when downloading results."); } using (Stream receiveStream = response.GetResponseStream()) { results.Failed = await GeocodeFeed.ReadAsync(receiveStream); } } } return(results); }
/// <summary> /// Method to geocode a set of data. /// </summary> /// <param name="dataFeed">GeocodeFeed which contains the data to batch geocode/reverse geocode.</param> /// <param name="bingMapsKey">Bing Maps key to use for accessing service.</param> /// <returns>The results of the batch geocoding process.</returns> public async Task <BatchGeocoderResults> Geocode(GeocodeFeed dataFeed, string bingMapsKey) { BatchGeocoderResults results; try { ReportStatus("Creating batch geocode job."); string dataflowJobLocation = await CreateJob(dataFeed, bingMapsKey); ReportStatus("Job created and being processed."); //Continue to check the dataflow job status until the job has completed var statusDetails = new DownloadDetails(); do { statusDetails = await CheckStatus(dataflowJobLocation, bingMapsKey); if (statusDetails.JobStatus == "Aborted") { ReportStatus("Batch geocode job aborted."); return(new BatchGeocoderResults() { Error = "Batch geocode job was aborted due to an error." }); } if (statusDetails.JobStatus.Equals("Pending")) { await Task.Delay(_statusUpdateInterval); } }while (statusDetails.JobStatus.Equals("Pending")); ReportStatus("Batch geocode job completed. Downloading results."); results = await DownloadResults(statusDetails, bingMapsKey); ReportStatus("Batch geocode results downloaded."); } catch (Exception ex) { results = new BatchGeocoderResults() { Error = ex.Message }; } return(results); }